]> code.delx.au - gnu-emacs-elpa/blob - company-eclim.el
Be less verbose when writing files.
[gnu-emacs-elpa] / company-eclim.el
1 ;;; company-eclim.el --- a company-mode completion back-end for eclim.
2 ;;
3 ;; Copyright (C) 2009 Nikolaj Schumacher
4 ;;
5 ;; This file is part of company 0.4.3.
6 ;;
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.
11 ;;
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.
16 ;;
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/>.
19
20 (require 'company)
21 (eval-when-compile (require 'cl))
22
23 (defun company-eclim-executable-find ()
24 (let (file)
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)))
30 (return file)))))
31
32 (defcustom company-eclim-executable
33 (or (executable-find "eclim") (company-eclim-executable-find))
34 "*Location of eclim executable"
35 :group 'company
36 :type 'file)
37
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."
41 :group 'company
42 :type '(choice (const :tag "Off" nil)
43 (const :tag "On" t)))
44
45 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
46
47 (defvar company-eclim--project-dir 'unknown)
48 (make-variable-buffer-local 'company-eclim--project-dir)
49
50 (defvar company-eclim--project-name 'unknown)
51 (make-variable-buffer-local 'company-eclim--project-name)
52
53 (defvar company-eclim--doc nil)
54 (make-variable-buffer-local 'company-eclim--doc)
55
56 (defun company-eclim--buffer-lines ()
57 (goto-char (point-max))
58 (let (lines)
59 (while (= 0 (forward-line -1))
60 (push (buffer-substring-no-properties (point-at-bol) (point-at-eol))
61 lines))
62 lines))
63
64 (defun company-eclim--call-process (&rest args)
65 (let ((coding-system-for-read 'utf-8)
66 res)
67 (with-temp-buffer
68 (if (= 0 (setq res (apply 'call-process company-eclim-executable nil t nil
69 "-command" args)))
70 (company-eclim--buffer-lines)
71 (message "Company-eclim command failed with error %d:\n%s" res
72 (buffer-substring (point-min) (point-max)))
73 nil))))
74
75 (defun company-eclim--project-list ()
76 (mapcar (lambda (line) (nreverse (split-string line " *- *" nil)))
77 (company-eclim--call-process "project_list")))
78
79 (defun company-eclim--project-dir ()
80 (if (eq company-eclim--project-dir 'unknown)
81 (setq company-eclim--project-dir
82 (directory-file-name
83 (expand-file-name
84 (company-locate-dominating-file buffer-file-name ".project"))))
85 company-eclim--project-dir))
86
87 (defun company-eclim--project-name ()
88 (if (eq company-eclim--project-name 'unknown)
89 (setq company-eclim--project-name
90 (car (cddr (assoc (company-eclim--project-dir)
91 (company-eclim--project-list)))))
92 company-eclim--project-name))
93
94 (defun company-eclim--candidates (prefix)
95 (interactive "d")
96 (let ((project-file (file-relative-name buffer-file-name
97 (company-eclim--project-dir)))
98 (project-name (company-eclim--project-name)))
99 (when company-eclim-auto-save
100 (when (buffer-modified-p)
101 (basic-save-buffer))
102 ;; FIXME: Sometimes this isn't finished when we complete.
103 (company-eclim--call-process "java_src_update"
104 "-p" (company-eclim--project-name)
105 "-f" project-file))
106 (setq company-eclim--doc
107 (mapcar (lambda (line)
108 (cdr (split-string line "|" nil)))
109 (company-eclim--call-process
110 "java_complete" "-p" (company-eclim--project-name)
111 "-f" project-file
112 "-o" (number-to-string (1- (point)))
113 "-e" "utf-8"
114 "-l" "standard"))))
115 (let ((completion-ignore-case nil))
116 (all-completions prefix (mapcar 'car company-eclim--doc))))
117
118 (defun company-eclim (command &optional arg &rest ignored)
119 "A `company-mode' completion back-end for eclim.
120 eclim provides access to Eclipse Java IDE features for other editors.
121
122 Completions only work correctly when the buffer has been saved.
123 `company-eclim-auto-save' determines whether to do this automatically."
124 (interactive (list 'interactive))
125 (case command
126 ('interactive (company-begin-backend 'company-eclim))
127 ('prefix (and (derived-mode-p 'java-mode 'jde-mode)
128 buffer-file-name
129 company-eclim-executable
130 (company-eclim--project-name)
131 (not (company-in-string-or-comment))
132 (or (company-grab-symbol) 'stop)))
133 ('candidates (company-eclim--candidates arg))
134 ('meta (cadr (assoc arg company-eclim--doc)))
135 ;; because "" doesn't return everything
136 ('no-cache (equal arg ""))))
137
138 (provide 'company-eclim)
139 ;;; company-eclim.el ends here