]> code.delx.au - gnu-emacs-elpa/blob - company-eclim.el
Support c++-mode in company-clang; templatify calls in C and C++
[gnu-emacs-elpa] / company-eclim.el
1 ;;; company-eclim.el --- company-mode completion back-end for Eclim
2
3 ;; Copyright (C) 2009, 2011, 2013 Free Software Foundation, Inc.
4
5 ;; Author: Nikolaj Schumacher
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
21
22 ;;; Commentary:
23 ;;
24 ;; Using `emacs-eclim' together with (or instead of) this back-end is
25 ;; recommended, as it allows you to use other Eclim features.
26 ;;
27 ;; The alternative back-end provided by `emacs-eclim' uses `yasnippet'
28 ;; instead of `company-template' to expand function calls, and it supports
29 ;; some languages other than Java.
30
31 ;;; Code:
32
33 (require 'company)
34 (require 'company-template)
35 (eval-when-compile (require 'cl))
36
37 (defgroup company-eclim nil
38 "Completion back-end for Eclim."
39 :group 'company)
40
41 (defun company-eclim-executable-find ()
42 (let (file)
43 (dolist (eclipse-root '("/Applications/eclipse" "/usr/lib/eclipse"
44 "/usr/local/lib/eclipse"))
45 (and (file-exists-p (setq file (expand-file-name "plugins" eclipse-root)))
46 (setq file (car (last (directory-files file t "^org.eclim_"))))
47 (file-exists-p (setq file (expand-file-name "bin/eclim" file)))
48 (return file)))))
49
50 (defcustom company-eclim-executable
51 (or (executable-find "eclim") (company-eclim-executable-find))
52 "Location of eclim executable."
53 :type 'file)
54
55 (defcustom company-eclim-auto-save t
56 "Determines whether to save the buffer when retrieving completions.
57 eclim can only complete correctly when the buffer has been saved."
58 :type '(choice (const :tag "Off" nil)
59 (const :tag "On" t)))
60
61 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
62
63 (defvar company-eclim--project-dir 'unknown)
64 (make-variable-buffer-local 'company-eclim--project-dir)
65
66 (defvar company-eclim--project-name nil)
67 (make-variable-buffer-local 'company-eclim--project-name)
68
69 (defvar company-eclim--doc nil)
70 (make-variable-buffer-local 'company-eclim--doc)
71
72 (defun company-eclim--call-process (&rest args)
73 (let ((coding-system-for-read 'utf-8)
74 res)
75 (require 'json)
76 (with-temp-buffer
77 (if (= 0 (setq res (apply 'call-process company-eclim-executable nil t nil
78 "-command" args)))
79 (let ((json-array-type 'list))
80 (goto-char (point-min))
81 (unless (eobp)
82 (json-read)))
83 (message "Company-eclim command failed with error %d:\n%s" res
84 (buffer-substring (point-min) (point-max)))
85 nil))))
86
87 (defun company-eclim--project-list ()
88 (company-eclim--call-process "project_list"))
89
90 (defun company-eclim--project-dir ()
91 (if (eq company-eclim--project-dir 'unknown)
92 (setq company-eclim--project-dir
93 (directory-file-name
94 (expand-file-name
95 (company-locate-dominating-file buffer-file-name ".project"))))
96 company-eclim--project-dir))
97
98 (defun company-eclim--project-name ()
99 (or company-eclim--project-name
100 (let ((dir (company-eclim--project-dir)))
101 (when dir
102 (setq company-eclim--project-name
103 (loop for project in (company-eclim--project-list)
104 when (equal (cdr (assoc 'path project)) dir)
105 return (cdr (assoc 'name project))))))))
106
107 (defun company-eclim--candidates (prefix)
108 (interactive "d")
109 (let ((project-file (file-relative-name buffer-file-name
110 (company-eclim--project-dir)))
111 (project-name (company-eclim--project-name)))
112 (when company-eclim-auto-save
113 (when (buffer-modified-p)
114 (basic-save-buffer))
115 ;; FIXME: Sometimes this isn't finished when we complete.
116 (company-eclim--call-process "java_src_update"
117 "-p" (company-eclim--project-name)
118 "-f" project-file))
119 (setq company-eclim--doc
120 (make-hash-table :test 'equal))
121 (dolist (item (cdr (assoc 'completions
122 (company-eclim--call-process
123 "java_complete" "-p" (company-eclim--project-name)
124 "-f" project-file
125 "-o" (number-to-string
126 (company-eclim--search-point prefix))
127 "-e" "utf-8"
128 "-l" "standard"))))
129 (let* ((meta (cdr (assoc 'info item)))
130 (completion meta))
131 (when (string-match " [:-]" completion)
132 (setq completion (substring completion 0 (match-beginning 0))))
133 (puthash completion meta company-eclim--doc))))
134 (let ((completion-ignore-case nil))
135 (all-completions prefix company-eclim--doc)))
136
137 (defun company-eclim--search-point (prefix)
138 (if (or (plusp (length prefix)) (eq (char-before) ?.))
139 (1- (point))
140 (point)))
141
142 (defun company-eclim--meta (candidate)
143 (gethash candidate company-eclim--doc))
144
145 (defun company-eclim (command &optional arg &rest ignored)
146 "`company-mode' completion back-end for Eclim.
147 Eclim provides access to Eclipse Java IDE features for other editors.
148
149 Eclim version 1.7.13 or newer (?) is required.
150
151 Completions only work correctly when the buffer has been saved.
152 `company-eclim-auto-save' determines whether to do this automatically."
153 (interactive (list 'interactive))
154 (case command
155 (interactive (company-begin-backend 'company-eclim))
156 (prefix (and (derived-mode-p 'java-mode 'jde-mode)
157 buffer-file-name
158 company-eclim-executable
159 (company-eclim--project-name)
160 (not (company-in-string-or-comment))
161 (or (company-grab-symbol) 'stop)))
162 (candidates (company-eclim--candidates arg))
163 (meta (company-eclim--meta arg))
164 ;; because "" doesn't return everything
165 (no-cache (equal arg ""))
166 (crop (when (string-match "(" arg)
167 (substring arg 0 (match-beginning 0))))
168 (post-completion (when (string-match "([^)]" arg)
169 (company-template-c-like-templatify arg)))))
170
171 (provide 'company-eclim)
172 ;;; company-eclim.el ends here