]> rtime.felk.cvut.cz Git - sojka/company-mode.git/blob - company-eclim.el
Merge changes from the GNU ELPA repository
[sojka/company-mode.git] / company-eclim.el
1 ;;; company-eclim.el --- A company-mode completion back-end for eclim.
2
3 ;; Copyright (C) 2009, 2011  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
23 ;;; Commentary:
24 ;;
25
26 ;;; Code:
27
28 (require 'company)
29 (eval-when-compile (require 'cl))
30
31 (defun company-eclim-executable-find ()
32   (let (file)
33     (dolist (eclipse-root '("/Applications/eclipse" "/usr/lib/eclipse"
34                             "/usr/local/lib/eclipse"))
35       (and (file-exists-p (setq file (expand-file-name "plugins" eclipse-root)))
36            (setq file (car (last (directory-files file t "^org.eclim_"))))
37            (file-exists-p (setq file (expand-file-name "bin/eclim" file)))
38            (return file)))))
39
40 (defcustom company-eclim-executable
41   (or (executable-find "eclim") (company-eclim-executable-find))
42   "*Location of eclim executable."
43   :group 'company
44   :type 'file)
45
46 (defcustom company-eclim-auto-save t
47   "*Determines whether to save the buffer when retrieving completions.
48 eclim can only complete correctly when the buffer has been saved."
49   :group 'company
50   :type '(choice (const :tag "Off" nil)
51                  (const :tag "On" t)))
52
53 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
54
55 (defvar company-eclim--project-dir 'unknown)
56 (make-variable-buffer-local 'company-eclim--project-dir)
57
58 (defvar company-eclim--project-name 'unknown)
59 (make-variable-buffer-local 'company-eclim--project-name)
60
61 (defvar company-eclim--doc nil)
62 (make-variable-buffer-local 'company-eclim--doc)
63
64 (defun company-eclim--buffer-lines ()
65   (goto-char (point-max))
66   (let (lines)
67     (while (= 0 (forward-line -1))
68       (push (buffer-substring-no-properties (point-at-bol) (point-at-eol))
69             lines))
70     lines))
71
72 (defun company-eclim--call-process (&rest args)
73   (let ((coding-system-for-read 'utf-8)
74         res)
75     (with-temp-buffer
76       (if (= 0 (setq res (apply 'call-process company-eclim-executable nil t nil
77                                 "-command" args)))
78           (company-eclim--buffer-lines)
79         (message "Company-eclim command failed with error %d:\n%s" res
80                  (buffer-substring (point-min) (point-max)))
81         nil))))
82
83 (defun company-eclim--project-list ()
84   (mapcar (lambda (line) (nreverse (split-string line " *- *" nil)))
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             (car (cddr (assoc (company-eclim--project-dir)
99                               (company-eclim--project-list)))))
100     company-eclim--project-name))
101
102 (defun company-eclim--candidates (prefix)
103   (interactive "d")
104   (let ((project-file (file-relative-name buffer-file-name
105                                           (company-eclim--project-dir)))
106         (project-name (company-eclim--project-name)))
107     (when company-eclim-auto-save
108       (when (buffer-modified-p)
109         (basic-save-buffer))
110       ;; FIXME: Sometimes this isn't finished when we complete.
111       (company-eclim--call-process "java_src_update"
112                                   "-p" (company-eclim--project-name)
113                                   "-f" project-file))
114     (setq company-eclim--doc
115           (mapcar (lambda (line)
116                     (cdr (split-string line "|" nil)))
117                   (company-eclim--call-process
118                    "java_complete" "-p" (company-eclim--project-name)
119                    "-f" project-file
120                    "-o" (number-to-string (1- (point)))
121                    "-e" "utf-8"
122                    "-l" "standard"))))
123   (let ((completion-ignore-case nil))
124     (all-completions prefix (mapcar 'car company-eclim--doc))))
125
126 (defun company-eclim (command &optional arg &rest ignored)
127   "A `company-mode' completion back-end for eclim.
128 eclim provides access to Eclipse Java IDE features for other editors.
129
130 Completions only work correctly when the buffer has been saved.
131 `company-eclim-auto-save' determines whether to do this automatically."
132   (interactive (list 'interactive))
133   (case command
134     (interactive (company-begin-backend 'company-eclim))
135     (prefix (and (derived-mode-p 'java-mode 'jde-mode)
136                  buffer-file-name
137                  company-eclim-executable
138                  (company-eclim--project-name)
139                  (not (company-in-string-or-comment))
140                  (or (company-grab-symbol) 'stop)))
141     (candidates (company-eclim--candidates arg))
142     (meta (cadr (assoc arg company-eclim--doc)))
143     ;; because "" doesn't return everything
144     (no-cache (equal arg ""))))
145
146 (provide 'company-eclim)
147 ;;; company-eclim.el ends here