]> code.delx.au - gnu-emacs-elpa/blob - company-eclim.el
Silence some byte-compilation warnings
[gnu-emacs-elpa] / company-eclim.el
1 ;;; company-eclim.el --- A company-mode completion back-end for eclim.
2
3 ;; Copyright (C) 2009, 2011, 2013 Free Software Foundation, Inc.
4
5 ;; Author: Nikolaj Schumacher
6
7 ;; This file is part of GNU Emacs.
8
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.
13
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.
18
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/>.
21
22 ;;; Commentary:
23 ;;
24 ;; Eclim version 1.7.13 or newer (?) is required.
25 ;;
26 ;; This completion backend is pretty barebone.
27 ;;
28 ;; `emacs-eclim' provides an alternative backend, and it also allows you to
29 ;; actually control Eclim from Emacs.
30
31 ;;; Code:
32
33 (require 'company)
34 (require 'company-template)
35 (eval-when-compile (require 'cl))
36
37 (defun company-eclim-executable-find ()
38 (let (file)
39 (dolist (eclipse-root '("/Applications/eclipse" "/usr/lib/eclipse"
40 "/usr/local/lib/eclipse"))
41 (and (file-exists-p (setq file (expand-file-name "plugins" eclipse-root)))
42 (setq file (car (last (directory-files file t "^org.eclim_"))))
43 (file-exists-p (setq file (expand-file-name "bin/eclim" file)))
44 (return file)))))
45
46 (defcustom company-eclim-executable
47 (or (executable-find "eclim") (company-eclim-executable-find))
48 "Location of eclim executable."
49 :group 'company
50 :type 'file)
51
52 (defcustom company-eclim-auto-save t
53 "Determines whether to save the buffer when retrieving completions.
54 eclim can only complete correctly when the buffer has been saved."
55 :group 'company
56 :type '(choice (const :tag "Off" nil)
57 (const :tag "On" t)))
58
59 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
60
61 (defvar company-eclim--project-dir 'unknown)
62 (make-variable-buffer-local 'company-eclim--project-dir)
63
64 (defvar company-eclim--project-name nil)
65 (make-variable-buffer-local 'company-eclim--project-name)
66
67 (defvar company-eclim--doc nil)
68 (make-variable-buffer-local 'company-eclim--doc)
69
70 (defun company-eclim--call-process (&rest args)
71 (let ((coding-system-for-read 'utf-8)
72 res)
73 (require 'json)
74 (with-temp-buffer
75 (if (= 0 (setq res (apply 'call-process company-eclim-executable nil t nil
76 "-command" args)))
77 (let ((json-array-type 'list))
78 (goto-char (point-min))
79 (unless (eobp)
80 (json-read)))
81 (message "Company-eclim command failed with error %d:\n%s" res
82 (buffer-substring (point-min) (point-max)))
83 nil))))
84
85 (defun company-eclim--project-list ()
86 (company-eclim--call-process "project_list"))
87
88 (defun company-eclim--project-dir ()
89 (if (eq company-eclim--project-dir 'unknown)
90 (setq company-eclim--project-dir
91 (directory-file-name
92 (expand-file-name
93 (company-locate-dominating-file buffer-file-name ".project"))))
94 company-eclim--project-dir))
95
96 (defun company-eclim--project-name ()
97 (or company-eclim--project-name
98 (let ((dir (company-eclim--project-dir)))
99 (when dir
100 (setq company-eclim--project-name
101 (let ((project (loop for project in (company-eclim--project-list)
102 when (equal (cdr (assoc 'path project)) dir)
103 return project)))
104 (when project
105 (cdr (assoc 'name project)))))))))
106
107 (defun company-eclim--candidates (prefix)
108 (interactive "d")
109 (let ((project-file (file-relative-name buffer-file-name
110 (company-eclim--project-dir)))
111 (project-name (company-eclim--project-name)))
112 (when company-eclim-auto-save
113 (when (buffer-modified-p)
114 (basic-save-buffer))
115 ;; FIXME: Sometimes this isn't finished when we complete.
116 (company-eclim--call-process "java_src_update"
117 "-p" (company-eclim--project-name)
118 "-f" project-file))
119 (setq company-eclim--doc
120 (make-hash-table :test 'equal))
121 (dolist (item (cdr (assoc 'completions
122 (company-eclim--call-process
123 "java_complete" "-p" (company-eclim--project-name)
124 "-f" project-file
125 "-o" (number-to-string (1- (point)))
126 "-e" "utf-8"
127 "-l" "standard"))))
128 (let* ((meta (cdr (assoc 'info item)))
129 (completion meta))
130 (when (string-match " [:-]" completion)
131 (setq completion (substring completion 0 (match-beginning 0))))
132 (puthash completion meta company-eclim--doc))))
133 (let ((completion-ignore-case nil))
134 (all-completions prefix company-eclim--doc)))
135
136 (defun company-eclim--meta (candidate)
137 (gethash candidate company-eclim--doc))
138
139 (defun company-eclim--templatify (call)
140 (let* ((end (point))
141 (beg (- (point) (length call)))
142 (templ (company-template-declare-template beg end)))
143 (save-excursion
144 (goto-char beg)
145 (while (re-search-forward "\\([(,] ?\\)\\([^ ]+ \\)\\([^ ,)]*\\)" end t)
146 (let ((name (match-string 3)))
147 (replace-match "\\1" t)
148 (decf end (length (match-string 2)))
149 (company-template-add-field templ (point) name))))
150 (company-template-move-to-first templ)))
151
152 (defun company-eclim (command &optional arg &rest ignored)
153 "A `company-mode' completion back-end for eclim.
154 eclim provides access to Eclipse Java IDE features for other editors.
155
156 Completions only work correctly when the buffer has been saved.
157 `company-eclim-auto-save' determines whether to do this automatically."
158 (interactive (list 'interactive))
159 (case command
160 (interactive (company-begin-backend 'company-eclim))
161 (prefix (and (derived-mode-p 'java-mode 'jde-mode)
162 buffer-file-name
163 company-eclim-executable
164 (company-eclim--project-name)
165 (not (company-in-string-or-comment))
166 (or (company-grab-symbol) 'stop)))
167 (candidates (company-eclim--candidates arg))
168 (meta (company-eclim--meta arg))
169 ;; because "" doesn't return everything
170 (no-cache (equal arg ""))
171 (crop (when (string-match "(" arg)
172 (substring arg 0 (match-beginning 0))))
173 (post-completion (when (string-match "([^)]" arg)
174 (company-eclim--templatify arg)))))
175
176 (provide 'company-eclim)
177 ;;; company-eclim.el ends here