1 ;;; company-eclim.el --- A company-mode completion back-end for eclim.
3 ;; Copyright (C) 2009, 2011, 2013 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 ;; This completion backend is pretty barebone.
26 ;; `emacs-eclim' provides an alternative backend, and it also allows you to
27 ;; actually control Eclim from Emacs. Check it out.
29 ;; Eclim version 1.7.13 or newer (?) is required.
34 (eval-when-compile (require 'cl))
36 (defun company-eclim-executable-find ()
38 (dolist (eclipse-root '("/Applications/eclipse" "/usr/lib/eclipse"
39 "/usr/local/lib/eclipse"))
40 (and (file-exists-p (setq file (expand-file-name "plugins" eclipse-root)))
41 (setq file (car (last (directory-files file t "^org.eclim_"))))
42 (file-exists-p (setq file (expand-file-name "bin/eclim" file)))
45 (defcustom company-eclim-executable
46 (or (executable-find "eclim") (company-eclim-executable-find))
47 "Location of eclim executable."
51 (defcustom company-eclim-auto-save t
52 "Determines whether to save the buffer when retrieving completions.
53 eclim can only complete correctly when the buffer has been saved."
55 :type '(choice (const :tag "Off" nil)
58 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
60 (defvar company-eclim--project-dir 'unknown)
61 (make-variable-buffer-local 'company-eclim--project-dir)
63 (defvar company-eclim--project-name 'unknown)
64 (make-variable-buffer-local 'company-eclim--project-name)
66 (defvar company-eclim--doc nil)
67 (make-variable-buffer-local 'company-eclim--doc)
69 (defun company-eclim--call-process (&rest args)
70 (let ((coding-system-for-read 'utf-8)
74 (if (= 0 (setq res (apply 'call-process company-eclim-executable nil t nil
76 (let ((json-array-type 'list))
77 (goto-char (point-min))
80 (message "Company-eclim command failed with error %d:\n%s" res
81 (buffer-substring (point-min) (point-max)))
84 (defun company-eclim--project-list ()
85 (company-eclim--call-process "project_list"))
87 (defun company-eclim--project-dir ()
88 (if (eq company-eclim--project-dir 'unknown)
89 (setq company-eclim--project-dir
92 (company-locate-dominating-file buffer-file-name ".project"))))
93 company-eclim--project-dir))
95 (defun company-eclim--project-name ()
96 (if (eq company-eclim--project-name 'unknown)
97 (setq company-eclim--project-name
98 (let ((project (find-if (lambda (project)
99 (equal (cdr (assoc 'path project))
100 (company-eclim--project-dir)))
101 (company-eclim--project-list))))
103 (cdr (assoc 'name project)))))
104 company-eclim--project-name))
106 (defun company-eclim--candidates (prefix)
108 (let ((project-file (file-relative-name buffer-file-name
109 (company-eclim--project-dir)))
110 (project-name (company-eclim--project-name)))
111 (when company-eclim-auto-save
112 (when (buffer-modified-p)
114 ;; FIXME: Sometimes this isn't finished when we complete.
115 (company-eclim--call-process "java_src_update"
116 "-p" (company-eclim--project-name)
118 (setq company-eclim--doc
119 (cdr (assoc 'completions
120 (company-eclim--call-process
121 "java_complete" "-p" (company-eclim--project-name)
123 "-o" (number-to-string (1- (point)))
126 (let ((completion-ignore-case nil))
127 ;; TODO: Handle overloaded methods somehow. Show one candidate per overload?
128 ;; That would look nice, but kinda useless: a bunch of candidates for the
129 ;; same completion. Maybe do expansion like `company-clang-objc-templatify'.
130 (all-completions prefix (mapcar (lambda (item) (cdr (assoc 'completion item)))
131 company-eclim--doc))))
133 (defun company-eclim--meta (candidate)
134 (cdr (assoc 'info (find-if
135 (lambda (item) (equal (cdr (assoc 'completion item))
137 company-eclim--doc))))
139 (defun company-eclim (command &optional arg &rest ignored)
140 "A `company-mode' completion back-end for eclim.
141 eclim provides access to Eclipse Java IDE features for other editors.
143 Completions only work correctly when the buffer has been saved.
144 `company-eclim-auto-save' determines whether to do this automatically."
145 (interactive (list 'interactive))
147 (interactive (company-begin-backend 'company-eclim))
148 (prefix (and (derived-mode-p 'java-mode 'jde-mode)
150 company-eclim-executable
151 (company-eclim--project-name)
152 (not (company-in-string-or-comment))
153 (or (company-grab-symbol) 'stop)))
154 (candidates (company-eclim--candidates arg))
155 (meta (company-eclim--meta arg))
157 ;; because "" doesn't return everything
158 (no-cache (equal arg ""))))
160 (provide 'company-eclim)
161 ;;; company-eclim.el ends here