]> code.delx.au - gnu-emacs-elpa/blob - company-eclim.el
company-eclim: reorder commentary text
[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 (eval-when-compile (require 'cl))
35
36 (defun company-eclim-executable-find ()
37 (let (file)
38 (dolist (eclipse-root '("/Applications/eclipse" "/usr/lib/eclipse"
39 "/usr/local/lib/eclipse"))
40 (and (file-exists-p (setq file (expand-file-name "plugins" eclipse-root)))
41 (setq file (car (last (directory-files file t "^org.eclim_"))))
42 (file-exists-p (setq file (expand-file-name "bin/eclim" file)))
43 (return file)))))
44
45 (defcustom company-eclim-executable
46 (or (executable-find "eclim") (company-eclim-executable-find))
47 "Location of eclim executable."
48 :group 'company
49 :type 'file)
50
51 (defcustom company-eclim-auto-save t
52 "Determines whether to save the buffer when retrieving completions.
53 eclim can only complete correctly when the buffer has been saved."
54 :group 'company
55 :type '(choice (const :tag "Off" nil)
56 (const :tag "On" t)))
57
58 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
59
60 (defvar company-eclim--project-dir 'unknown)
61 (make-variable-buffer-local 'company-eclim--project-dir)
62
63 (defvar company-eclim--project-name 'unknown)
64 (make-variable-buffer-local 'company-eclim--project-name)
65
66 (defvar company-eclim--doc nil)
67 (make-variable-buffer-local 'company-eclim--doc)
68
69 (defun company-eclim--call-process (&rest args)
70 (let ((coding-system-for-read 'utf-8)
71 res)
72 (require 'json)
73 (with-temp-buffer
74 (if (= 0 (setq res (apply 'call-process company-eclim-executable nil t nil
75 "-command" args)))
76 (let ((json-array-type 'list))
77 (goto-char (point-min))
78 (unless (eobp)
79 (json-read)))
80 (message "Company-eclim command failed with error %d:\n%s" res
81 (buffer-substring (point-min) (point-max)))
82 nil))))
83
84 (defun company-eclim--project-list ()
85 (company-eclim--call-process "project_list"))
86
87 (defun company-eclim--project-dir ()
88 (if (eq company-eclim--project-dir 'unknown)
89 (setq company-eclim--project-dir
90 (directory-file-name
91 (expand-file-name
92 (company-locate-dominating-file buffer-file-name ".project"))))
93 company-eclim--project-dir))
94
95 (defun company-eclim--project-name ()
96 (if (eq company-eclim--project-name 'unknown)
97 (setq company-eclim--project-name
98 (let ((project (find-if (lambda (project)
99 (equal (cdr (assoc 'path project))
100 (company-eclim--project-dir)))
101 (company-eclim--project-list))))
102 (when project
103 (cdr (assoc 'name project)))))
104 company-eclim--project-name))
105
106 (defun company-eclim--candidates (prefix)
107 (interactive "d")
108 (let ((project-file (file-relative-name buffer-file-name
109 (company-eclim--project-dir)))
110 (project-name (company-eclim--project-name)))
111 (when company-eclim-auto-save
112 (when (buffer-modified-p)
113 (basic-save-buffer))
114 ;; FIXME: Sometimes this isn't finished when we complete.
115 (company-eclim--call-process "java_src_update"
116 "-p" (company-eclim--project-name)
117 "-f" project-file))
118 (setq company-eclim--doc
119 (cdr (assoc 'completions
120 (company-eclim--call-process
121 "java_complete" "-p" (company-eclim--project-name)
122 "-f" project-file
123 "-o" (number-to-string (1- (point)))
124 "-e" "utf-8"
125 "-l" "standard")))))
126 (let ((completion-ignore-case nil))
127 ;; TODO: Handle overloaded methods somehow. Show one candidate per overload?
128 ;; That would look nice, but kinda useless: a bunch of candidates for the
129 ;; same completion. Maybe do expansion like `company-clang-objc-templatify'.
130 (all-completions prefix (mapcar (lambda (item) (cdr (assoc 'completion item)))
131 company-eclim--doc))))
132
133 (defun company-eclim--meta (candidate)
134 (cdr (assoc 'info (find-if
135 (lambda (item) (equal (cdr (assoc 'completion item))
136 arg))
137 company-eclim--doc))))
138
139 (defun company-eclim (command &optional arg &rest ignored)
140 "A `company-mode' completion back-end for eclim.
141 eclim provides access to Eclipse Java IDE features for other editors.
142
143 Completions only work correctly when the buffer has been saved.
144 `company-eclim-auto-save' determines whether to do this automatically."
145 (interactive (list 'interactive))
146 (case command
147 (interactive (company-begin-backend 'company-eclim))
148 (prefix (and (derived-mode-p 'java-mode 'jde-mode)
149 buffer-file-name
150 company-eclim-executable
151 (company-eclim--project-name)
152 (not (company-in-string-or-comment))
153 (or (company-grab-symbol) 'stop)))
154 (candidates (company-eclim--candidates arg))
155 (meta (company-eclim--meta arg))
156 (duplicates t)
157 ;; because "" doesn't return everything
158 (no-cache (equal arg ""))))
159
160 (provide 'company-eclim)
161 ;;; company-eclim.el ends here