2 (eval-when-compile (require 'cl))
4 (defun company-eclim-executable-find ()
6 (dolist (eclipse-root '("/Applications/eclipse" "/usr/lib/eclipse"
7 "/usr/local/lib/eclipse"))
8 (and (file-exists-p (setq file (expand-file-name "plugins" eclipse-root)))
9 (setq file (car (last (directory-files file t "^org.eclim_"))))
10 (file-exists-p (setq file (expand-file-name "bin/eclim" file)))
13 (defcustom company-eclim-executable
14 (or (executable-find "eclim") (company-eclim-executable-find))
15 "*Location of eclim executable"
19 (defcustom company-eclim-auto-save nil
20 "*Determines whether to save the buffer when retrieving completions.
21 eclim can only complete correctly when the buffer has been saved."
23 :type '(choice (const :tag "Off" nil)
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 (defvar company-eclim--project-dir 'unknown)
29 (make-variable-buffer-local 'company-eclim--project-dir)
31 (defvar company-eclim--project-name 'unknown)
32 (make-variable-buffer-local 'company-eclim--project-name)
34 (defvar company-eclim--doc nil)
35 (make-variable-buffer-local 'company-eclim--doc)
37 (defun company-eclim--buffer-lines ()
38 (goto-char (point-max))
40 (while (= 0 (forward-line -1))
41 (push (buffer-substring-no-properties (point-at-bol) (point-at-eol))
45 (defun company-eclim--call-process (&rest args)
46 (let ((coding-system-for-read 'utf-8))
48 (if (= 0 (apply 'call-process company-eclim-executable nil t nil
50 (company-eclim--buffer-lines)
51 (message "Company-eclim command failed")
54 (defun company-eclim--project-list ()
55 (mapcar (lambda (line) (nreverse (split-string line " *- *" nil)))
56 (company-eclim--call-process "project_list")))
58 (defun company-eclim--locate-dominating-file (file name)
60 (let ((dir (file-name-directory buffer-file-name)))
61 (while (not (equal dir "/"))
62 (when (file-exists-p (expand-file-name name dir))
64 (setq dir (file-name-directory (directory-file-name dir)))))))
66 (defun company-eclim--project-dir ()
67 (if (eq company-eclim--project-dir 'unknown)
68 (setq company-eclim--project-dir
70 (if (fboundp 'locate-dominating-file)
71 (expand-file-name (locate-dominating-file buffer-file-name
73 (company-eclim--locate-dominating-file buffer-file-name
75 company-eclim--project-dir))
77 (defun company-eclim--project-name ()
78 (if (eq company-eclim--project-name 'unknown)
79 (setq company-eclim--project-name
80 (car (cddr (assoc (company-eclim--project-dir)
81 (company-eclim--project-list)))))
82 company-eclim--project-name))
84 (defun company-eclim--candidates (prefix)
86 (let ((project-file (file-relative-name buffer-file-name
87 (company-eclim--project-dir)))
88 (project-name (company-eclim--project-name)))
89 (when company-eclim-auto-save
91 ;; FIXME: Sometimes this isn't finished when we complete.
92 (company-eclim--call-process "java_src_update"
93 "-p" (company-eclim--project-name)
95 (setq company-eclim--doc
96 (mapcar (lambda (line)
97 (cdr (split-string line "|" nil)))
98 (company-eclim--call-process
99 "java_complete" "-p" (company-eclim--project-name)
101 "-o" (number-to-string (1- (point)))
104 (let ((completion-ignore-case nil))
105 (all-completions prefix (mapcar 'car company-eclim--doc))))
107 (defun company-eclim (command &optional arg &rest ignored)
108 "A `company-mode' completion back-end for eclim.
109 eclim provides access to Eclipse Java IDE features for other editors.
111 Completions only work correctly when the buffer has been saved.
112 `company-eclim-auto-save' determines whether to do this automatically."
113 (interactive (list 'interactive))
115 ('interactive (company-begin-backend 'company-eclim))
116 ('prefix (and (derived-mode-p 'java-mode 'jde-mode)
118 company-eclim-executable
119 (company-eclim--project-name)
120 (not (company-in-string-or-comment))
121 (or (company-grab-symbol) 'stop)))
122 ('candidates (company-eclim--candidates arg))
123 ('meta (cadr (assoc arg company-eclim--doc)))
124 ;; because "" doesn't return everything
125 ('no-cache (equal arg ""))))
127 (provide 'company-eclim)
128 ;;; company-eclim.el ends here