]> code.delx.au - gnu-emacs-elpa/blob - company-eclim.el
Backport defvar-local from Emacs 24.2
[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 (require 'cl-lib)
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 (cl-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 (cl-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-local company-eclim--project-dir 'unknown)
64
65 (defvar-local company-eclim--project-name nil)
66
67 (declare-function json-read "json")
68 (defvar json-array-type)
69
70 (defun company-eclim--call-process (&rest args)
71 (let ((coding-system-for-read 'utf-8)
72 res)
73 (require 'json)
74 (with-temp-buffer
75 (if (= 0 (setq res (apply 'call-process company-eclim-executable nil t nil
76 "-command" args)))
77 (let ((json-array-type 'list))
78 (goto-char (point-min))
79 (unless (eobp)
80 (json-read)))
81 (message "Company-eclim command failed with error %d:\n%s" res
82 (buffer-substring (point-min) (point-max)))
83 nil))))
84
85 (defun company-eclim--project-list ()
86 (company-eclim--call-process "project_list"))
87
88 (defun company-eclim--project-dir ()
89 (if (eq company-eclim--project-dir 'unknown)
90 (setq company-eclim--project-dir
91 (directory-file-name
92 (expand-file-name
93 (locate-dominating-file buffer-file-name ".project"))))
94 company-eclim--project-dir))
95
96 (defun company-eclim--project-name ()
97 (or company-eclim--project-name
98 (let ((dir (company-eclim--project-dir)))
99 (when dir
100 (setq company-eclim--project-name
101 (cl-loop for project in (company-eclim--project-list)
102 when (equal (cdr (assoc 'path project)) dir)
103 return (cdr (assoc 'name project))))))))
104
105 (defun company-eclim--candidates (prefix)
106 (interactive "d")
107 (let ((project-file (file-relative-name buffer-file-name
108 (company-eclim--project-dir)))
109 completions)
110 (when company-eclim-auto-save
111 (when (buffer-modified-p)
112 (basic-save-buffer))
113 ;; FIXME: Sometimes this isn't finished when we complete.
114 (company-eclim--call-process "java_src_update"
115 "-p" (company-eclim--project-name)
116 "-f" project-file))
117 (dolist (item (cdr (assoc 'completions
118 (company-eclim--call-process
119 "java_complete" "-p" (company-eclim--project-name)
120 "-f" project-file
121 "-o" (number-to-string
122 (company-eclim--search-point prefix))
123 "-e" "utf-8"
124 "-l" "standard"))))
125 (let* ((meta (cdr (assoc 'info item)))
126 (completion meta))
127 (when (string-match " ?[(:-]" completion)
128 (setq completion (substring completion 0 (match-beginning 0))))
129 (put-text-property 0 1 'meta meta completion)
130 (push completion completions)))
131 (let ((completion-ignore-case nil))
132 (all-completions prefix completions))))
133
134 (defun company-eclim--search-point (prefix)
135 (if (or (cl-plusp (length prefix)) (eq (char-before) ?.))
136 (1- (point))
137 (point)))
138
139 (defun company-eclim--meta (candidate)
140 (get-text-property 0 'meta candidate))
141
142 (defun company-eclim--annotation (candidate)
143 (let ((meta (company-eclim--meta candidate)))
144 (when (string-match "\\(([^-]*\\) -" meta)
145 (substring meta (match-beginning 1) (match-end 1)))))
146
147 (defun company-eclim--prefix ()
148 (let ((prefix (company-grab-symbol)))
149 (when prefix
150 ;; Completion candidates for annotations don't include '@'.
151 (when (eq ?@ (string-to-char prefix))
152 (setq prefix (substring prefix 1)))
153 prefix)))
154
155 (defun company-eclim (command &optional arg &rest ignored)
156 "`company-mode' completion back-end for Eclim.
157 Eclim provides access to Eclipse Java IDE features for other editors.
158
159 Eclim version 1.7.13 or newer (?) is required.
160
161 Completions only work correctly when the buffer has been saved.
162 `company-eclim-auto-save' determines whether to do this automatically."
163 (interactive (list 'interactive))
164 (cl-case command
165 (interactive (company-begin-backend 'company-eclim))
166 (prefix (and (derived-mode-p 'java-mode 'jde-mode)
167 buffer-file-name
168 company-eclim-executable
169 (company-eclim--project-name)
170 (not (company-in-string-or-comment))
171 (or (company-eclim--prefix) 'stop)))
172 (candidates (company-eclim--candidates arg))
173 (meta (company-eclim--meta arg))
174 ;; because "" doesn't return everything
175 (no-cache (equal arg ""))
176 (annotation (company-eclim--annotation arg))
177 (post-completion (let ((anno (company-eclim--annotation arg)))
178 (when anno
179 (insert anno)
180 (company-template-c-like-templatify anno))))))
181
182 (provide 'company-eclim)
183 ;;; company-eclim.el ends here