1 ;;; company-eclim.el --- company-mode completion backend for Eclim
3 ;; Copyright (C) 2009, 2011, 2013, 2015 Free Software Foundation, Inc.
5 ;; Author: Nikolaj Schumacher
7 ;; This file is part of GNU Emacs.
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.
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.
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/>.
24 ;; Using `emacs-eclim' together with (or instead of) this backend is
25 ;; recommended, as it allows you to use other Eclim features.
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.
34 (require 'company-template)
37 (defgroup company-eclim nil
38 "Completion backend for Eclim."
41 (defun company-eclim-executable-find ()
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)))
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."
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)
63 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
65 (defvar-local company-eclim--project-dir 'unknown)
67 (defvar-local company-eclim--project-name nil)
69 (declare-function json-read "json")
70 (defvar json-array-type)
72 (defun company-eclim--call-process (&rest args)
73 (let ((coding-system-for-read 'utf-8)
77 (if (= 0 (setq res (apply 'call-process company-eclim-executable nil t nil
79 (let ((json-array-type 'list))
80 (goto-char (point-min))
83 (message "Company-eclim command failed with error %d:\n%s" res
84 (buffer-substring (point-min) (point-max)))
87 (defun company-eclim--project-list ()
88 (company-eclim--call-process "project_list"))
90 (defun company-eclim--project-dir ()
91 (if (eq company-eclim--project-dir 'unknown)
92 (setq company-eclim--project-dir
95 (locate-dominating-file buffer-file-name ".project"))))
96 company-eclim--project-dir))
98 (defun company-eclim--project-name ()
99 (or company-eclim--project-name
100 (let ((dir (company-eclim--project-dir)))
102 (setq company-eclim--project-name
103 (cl-loop for project in (company-eclim--project-list)
104 when (equal (cdr (assoc 'path project)) dir)
105 return (cdr (assoc 'name project))))))))
107 (defun company-eclim--candidates (prefix)
109 (let ((project-file (file-relative-name buffer-file-name
110 (company-eclim--project-dir)))
112 (when company-eclim-auto-save
113 (when (buffer-modified-p)
115 ;; FIXME: Sometimes this isn't finished when we complete.
116 (company-eclim--call-process "java_src_update"
117 "-p" (company-eclim--project-name)
119 (dolist (item (cdr (assoc 'completions
120 (company-eclim--call-process
121 "java_complete" "-p" (company-eclim--project-name)
123 "-o" (number-to-string
124 (company-eclim--search-point prefix))
127 (let* ((meta (cdr (assoc 'info item)))
129 (when (string-match " ?[(:-]" completion)
130 (setq completion (substring completion 0 (match-beginning 0))))
131 (put-text-property 0 1 'meta meta completion)
132 (push completion completions)))
133 (let ((completion-ignore-case nil))
134 (all-completions prefix completions))))
136 (defun company-eclim--search-point (prefix)
137 (if (or (cl-plusp (length prefix)) (eq (char-before) ?.))
141 (defun company-eclim--meta (candidate)
142 (get-text-property 0 'meta candidate))
144 (defun company-eclim--annotation (candidate)
145 (let ((meta (company-eclim--meta candidate)))
146 (when (string-match "\\(([^-]*\\) -" meta)
147 (substring meta (match-beginning 1) (match-end 1)))))
149 (defun company-eclim--prefix ()
150 (let ((prefix (company-grab-symbol)))
152 ;; Completion candidates for annotations don't include '@'.
153 (when (eq ?@ (string-to-char prefix))
154 (setq prefix (substring prefix 1)))
157 (defun company-eclim (command &optional arg &rest ignored)
158 "`company-mode' completion backend for Eclim.
159 Eclim provides access to Eclipse Java IDE features for other editors.
161 Eclim version 1.7.13 or newer (?) is required.
163 Completions only work correctly when the buffer has been saved.
164 `company-eclim-auto-save' determines whether to do this automatically."
165 (interactive (list 'interactive))
167 (interactive (company-begin-backend 'company-eclim))
168 (prefix (and (derived-mode-p 'java-mode 'jde-mode)
170 company-eclim-executable
171 (company-eclim--project-name)
172 (not (company-in-string-or-comment))
173 (or (company-eclim--prefix) 'stop)))
174 (candidates (company-eclim--candidates arg))
175 (meta (company-eclim--meta arg))
176 ;; because "" doesn't return everything
177 (no-cache (equal arg ""))
178 (annotation (company-eclim--annotation arg))
179 (post-completion (let ((anno (company-eclim--annotation arg)))
182 (company-template-c-like-templatify anno))))))
184 (provide 'company-eclim)
185 ;;; company-eclim.el ends here