]> code.delx.au - gnu-emacs-elpa/blob - company-eclim.el
Mention save requirement in for eclim back-end.
[gnu-emacs-elpa] / company-eclim.el
1 (require 'company)
2 (eval-when-compile (require 'cl))
3
4 (defun company-eclim-executable-find ()
5 (let (file)
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)))
11 (return file)))))
12
13 (defcustom company-eclim-executable
14 (or (executable-find "eclim") (company-eclim-executable-find))
15 "*Location of eclim executable"
16 :group 'company
17 :type 'file)
18
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."
22 :group 'company
23 :type '(choice (const :tag "Off" nil)
24 (const :tag "On" t)))
25
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27
28 (defvar company-eclim--project-dir 'unknown)
29 (make-variable-buffer-local 'company-eclim--project-dir)
30
31 (defvar company-eclim--project-name 'unknown)
32 (make-variable-buffer-local 'company-eclim--project-name)
33
34 (defvar company-eclim--doc nil)
35 (make-variable-buffer-local 'company-eclim--doc)
36
37 (defun company-eclim--buffer-lines ()
38 (goto-char (point-max))
39 (let (lines)
40 (while (= 0 (forward-line -1))
41 (push (buffer-substring-no-properties (point-at-bol) (point-at-eol))
42 lines))
43 lines))
44
45 (defun company-eclim--call-process (&rest args)
46 (let ((coding-system-for-read 'utf-8))
47 (with-temp-buffer
48 (if (= 0 (apply 'call-process company-eclim-executable nil t nil
49 "-command" args))
50 (company-eclim--buffer-lines)
51 (message "Company-eclim command failed")
52 nil))))
53
54 (defun company-eclim--project-list ()
55 (mapcar (lambda (line) (nreverse (split-string line " *- *" nil)))
56 (company-eclim--call-process "project_list")))
57
58 (defun company-eclim--locate-dominating-file (file name)
59 (catch 'root
60 (let ((dir (file-name-directory buffer-file-name)))
61 (while (not (equal dir "/"))
62 (when (file-exists-p (expand-file-name name dir))
63 (throw 'root dir))
64 (setq dir (file-name-directory (directory-file-name dir)))))))
65
66 (defun company-eclim--project-dir ()
67 (if (eq company-eclim--project-dir 'unknown)
68 (setq company-eclim--project-dir
69 (directory-file-name
70 (if (fboundp 'locate-dominating-file)
71 (expand-file-name (locate-dominating-file buffer-file-name
72 ".project"))
73 (company-eclim--locate-dominating-file buffer-file-name
74 ".project"))))
75 company-eclim--project-dir))
76
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))
83
84 (defun company-eclim--candidates (prefix)
85 (interactive "d")
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
90 (save-buffer)
91 ;; FIXME: Sometimes this isn't finished when we complete.
92 (company-eclim--call-process "java_src_update"
93 "-p" (company-eclim--project-name)
94 "-f" project-file))
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)
100 "-f" project-file
101 "-o" (number-to-string (1- (point)))
102 "-e" "utf-8"
103 "-l" "standard"))))
104 (let ((completion-ignore-case nil))
105 (all-completions prefix (mapcar 'car company-eclim--doc))))
106
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.
110
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))
114 (case command
115 ('interactive (company-begin-backend 'company-eclim))
116 ('prefix (and (derived-mode-p 'java-mode 'jde-mode)
117 buffer-file-name
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 ""))))
126
127 (provide 'company-eclim)
128 ;;; company-eclim.el ends here