1 ;;; company-eclim.el --- a company-mode completion back-end for eclim.
3 ;; Copyright (C) 2009 Nikolaj Schumacher
5 ;; This file is part of company 0.4.1.
7 ;; This program is free software; you can redistribute it and/or
8 ;; modify it under the terms of the GNU General Public License
9 ;; as published by the Free Software Foundation; either version 2
10 ;; of the License, or (at your option) any later version.
12 ;; This program is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
21 (eval-when-compile (require 'cl))
23 (defun company-eclim-executable-find ()
25 (dolist (eclipse-root '("/Applications/eclipse" "/usr/lib/eclipse"
26 "/usr/local/lib/eclipse"))
27 (and (file-exists-p (setq file (expand-file-name "plugins" eclipse-root)))
28 (setq file (car (last (directory-files file t "^org.eclim_"))))
29 (file-exists-p (setq file (expand-file-name "bin/eclim" file)))
32 (defcustom company-eclim-executable
33 (or (executable-find "eclim") (company-eclim-executable-find))
34 "*Location of eclim executable"
38 (defcustom company-eclim-auto-save nil
39 "*Determines whether to save the buffer when retrieving completions.
40 eclim can only complete correctly when the buffer has been saved."
42 :type '(choice (const :tag "Off" nil)
45 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
47 (defvar company-eclim--project-dir 'unknown)
48 (make-variable-buffer-local 'company-eclim--project-dir)
50 (defvar company-eclim--project-name 'unknown)
51 (make-variable-buffer-local 'company-eclim--project-name)
53 (defvar company-eclim--doc nil)
54 (make-variable-buffer-local 'company-eclim--doc)
56 (defun company-eclim--buffer-lines ()
57 (goto-char (point-max))
59 (while (= 0 (forward-line -1))
60 (push (buffer-substring-no-properties (point-at-bol) (point-at-eol))
64 (defun company-eclim--call-process (&rest args)
65 (let ((coding-system-for-read 'utf-8))
67 (if (= 0 (apply 'call-process company-eclim-executable nil t nil
69 (company-eclim--buffer-lines)
70 (message "Company-eclim command failed")
73 (defun company-eclim--project-list ()
74 (mapcar (lambda (line) (nreverse (split-string line " *- *" nil)))
75 (company-eclim--call-process "project_list")))
77 (defun company-eclim--locate-dominating-file (file name)
79 (let ((dir (file-name-directory buffer-file-name)))
80 (while (not (equal dir "/"))
81 (when (file-exists-p (expand-file-name name dir))
83 (setq dir (file-name-directory (directory-file-name dir)))))))
85 (defun company-eclim--project-dir ()
86 (if (eq company-eclim--project-dir 'unknown)
87 (setq company-eclim--project-dir
89 (if (fboundp 'locate-dominating-file)
90 (expand-file-name (locate-dominating-file buffer-file-name
92 (company-eclim--locate-dominating-file buffer-file-name
94 company-eclim--project-dir))
96 (defun company-eclim--project-name ()
97 (if (eq company-eclim--project-name 'unknown)
98 (setq company-eclim--project-name
99 (car (cddr (assoc (company-eclim--project-dir)
100 (company-eclim--project-list)))))
101 company-eclim--project-name))
103 (defun company-eclim--candidates (prefix)
105 (let ((project-file (file-relative-name buffer-file-name
106 (company-eclim--project-dir)))
107 (project-name (company-eclim--project-name)))
108 (when company-eclim-auto-save
110 ;; FIXME: Sometimes this isn't finished when we complete.
111 (company-eclim--call-process "java_src_update"
112 "-p" (company-eclim--project-name)
114 (setq company-eclim--doc
115 (mapcar (lambda (line)
116 (cdr (split-string line "|" nil)))
117 (company-eclim--call-process
118 "java_complete" "-p" (company-eclim--project-name)
120 "-o" (number-to-string (1- (point)))
123 (let ((completion-ignore-case nil))
124 (all-completions prefix (mapcar 'car company-eclim--doc))))
126 (defun company-eclim (command &optional arg &rest ignored)
127 "A `company-mode' completion back-end for eclim.
128 eclim provides access to Eclipse Java IDE features for other editors.
130 Completions only work correctly when the buffer has been saved.
131 `company-eclim-auto-save' determines whether to do this automatically."
132 (interactive (list 'interactive))
134 ('interactive (company-begin-backend 'company-eclim))
135 ('prefix (and (derived-mode-p 'java-mode 'jde-mode)
137 company-eclim-executable
138 (company-eclim--project-name)
139 (not (company-in-string-or-comment))
140 (or (company-grab-symbol) 'stop)))
141 ('candidates (company-eclim--candidates arg))
142 ('meta (cadr (assoc arg company-eclim--doc)))
143 ;; because "" doesn't return everything
144 ('no-cache (equal arg ""))))
146 (provide 'company-eclim)
147 ;;; company-eclim.el ends here