commit | author | age
|
5cb5f7
|
1 |
;;; company-eclim.el --- company-mode completion backend for Eclim |
C |
2 |
|
|
3 |
;; Copyright (C) 2009, 2011, 2013, 2015 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 backend is |
|
25 |
;; recommended, as it allows you to use other Eclim features. |
|
26 |
;; |
|
27 |
;; The alternative backend 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 backend 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 (bound-and-true-p eclim-executable) |
|
52 |
(executable-find "eclim") |
|
53 |
(company-eclim-executable-find)) |
|
54 |
"Location of eclim executable." |
|
55 |
:type 'file) |
|
56 |
|
|
57 |
(defcustom company-eclim-auto-save t |
|
58 |
"Determines whether to save the buffer when retrieving completions. |
|
59 |
eclim can only complete correctly when the buffer has been saved." |
|
60 |
:type '(choice (const :tag "Off" nil) |
|
61 |
(const :tag "On" t))) |
|
62 |
|
|
63 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
|
64 |
|
|
65 |
(defvar-local company-eclim--project-dir 'unknown) |
|
66 |
|
|
67 |
(defvar-local company-eclim--project-name nil) |
|
68 |
|
|
69 |
(declare-function json-read "json") |
|
70 |
(defvar json-array-type) |
|
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 |
(let ((dir (locate-dominating-file buffer-file-name ".project"))) |
|
93 |
(when dir |
|
94 |
(setq company-eclim--project-dir |
|
95 |
(directory-file-name |
|
96 |
(expand-file-name dir))))) |
|
97 |
company-eclim--project-dir)) |
|
98 |
|
|
99 |
(defun company-eclim--project-name () |
|
100 |
(or company-eclim--project-name |
|
101 |
(let ((dir (company-eclim--project-dir))) |
|
102 |
(when dir |
|
103 |
(setq company-eclim--project-name |
|
104 |
(cl-loop for project in (company-eclim--project-list) |
|
105 |
when (equal (cdr (assoc 'path project)) dir) |
|
106 |
return (cdr (assoc 'name project)))))))) |
|
107 |
|
|
108 |
(defun company-eclim--candidates (prefix) |
|
109 |
(interactive "d") |
|
110 |
(let ((project-file (file-relative-name buffer-file-name |
|
111 |
(company-eclim--project-dir))) |
|
112 |
completions) |
|
113 |
(when company-eclim-auto-save |
|
114 |
(when (buffer-modified-p) |
|
115 |
(basic-save-buffer)) |
|
116 |
;; FIXME: Sometimes this isn't finished when we complete. |
|
117 |
(company-eclim--call-process "java_src_update" |
|
118 |
"-p" (company-eclim--project-name) |
|
119 |
"-f" project-file)) |
|
120 |
(dolist (item (cdr (assoc 'completions |
|
121 |
(company-eclim--call-process |
|
122 |
"java_complete" "-p" (company-eclim--project-name) |
|
123 |
"-f" project-file |
|
124 |
"-o" (number-to-string |
|
125 |
(company-eclim--search-point prefix)) |
|
126 |
"-e" "utf-8" |
|
127 |
"-l" "standard")))) |
|
128 |
(let* ((meta (cdr (assoc 'info item))) |
|
129 |
(completion meta)) |
|
130 |
(when (string-match " ?[(:-]" completion) |
|
131 |
(setq completion (substring completion 0 (match-beginning 0)))) |
|
132 |
(put-text-property 0 1 'meta meta completion) |
|
133 |
(push completion completions))) |
|
134 |
(let ((completion-ignore-case nil)) |
|
135 |
(all-completions prefix completions)))) |
|
136 |
|
|
137 |
(defun company-eclim--search-point (prefix) |
|
138 |
(if (or (cl-plusp (length prefix)) (eq (char-before) ?.)) |
|
139 |
(1- (point)) |
|
140 |
(point))) |
|
141 |
|
|
142 |
(defun company-eclim--meta (candidate) |
|
143 |
(get-text-property 0 'meta candidate)) |
|
144 |
|
|
145 |
(defun company-eclim--annotation (candidate) |
|
146 |
(let ((meta (company-eclim--meta candidate))) |
|
147 |
(when (string-match "\\(([^-]*\\) -" meta) |
|
148 |
(substring meta (match-beginning 1) (match-end 1))))) |
|
149 |
|
|
150 |
(defun company-eclim--prefix () |
|
151 |
(let ((prefix (company-grab-symbol))) |
|
152 |
(when prefix |
|
153 |
;; Completion candidates for annotations don't include '@'. |
|
154 |
(when (eq ?@ (string-to-char prefix)) |
|
155 |
(setq prefix (substring prefix 1))) |
|
156 |
prefix))) |
|
157 |
|
|
158 |
(defun company-eclim (command &optional arg &rest ignored) |
|
159 |
"`company-mode' completion backend for Eclim. |
|
160 |
Eclim provides access to Eclipse Java IDE features for other editors. |
|
161 |
|
|
162 |
Eclim version 1.7.13 or newer (?) is required. |
|
163 |
|
|
164 |
Completions only work correctly when the buffer has been saved. |
|
165 |
`company-eclim-auto-save' determines whether to do this automatically." |
|
166 |
(interactive (list 'interactive)) |
|
167 |
(cl-case command |
|
168 |
(interactive (company-begin-backend 'company-eclim)) |
|
169 |
(prefix (and (derived-mode-p 'java-mode 'jde-mode) |
|
170 |
buffer-file-name |
|
171 |
company-eclim-executable |
|
172 |
(company-eclim--project-name) |
|
173 |
(not (company-in-string-or-comment)) |
|
174 |
(or (company-eclim--prefix) 'stop))) |
|
175 |
(candidates (company-eclim--candidates arg)) |
|
176 |
(meta (company-eclim--meta arg)) |
|
177 |
;; because "" doesn't return everything |
|
178 |
(no-cache (equal arg "")) |
|
179 |
(annotation (company-eclim--annotation arg)) |
|
180 |
(post-completion (let ((anno (company-eclim--annotation arg))) |
|
181 |
(when anno |
|
182 |
(insert anno) |
|
183 |
(company-template-c-like-templatify anno)))))) |
|
184 |
|
|
185 |
(provide 'company-eclim) |
|
186 |
;;; company-eclim.el ends here |