Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Tree: 5b06569735
Fetching contributors…

Cannot retrieve contributors at this time

226 lines (205 sloc) 9.35 KB
;; company-emacs-eclim.el --- an interface to the Eclipse IDE.
;; Copyright (C) 2012 Fredrik Appelberg
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <>.
;;; Contributors
;;; Conventions
;; Conventions used in this file: Name internal variables and functions
;; "eclim--<descriptive-name>", and name eclim command invocations
;; "eclim/command-name", like eclim/project-list.
;;; Description
;; company-emacs-eclim.el -- completion functions used by the company-mode
;; and auto-complete-mode backends.
(defun eclim--completion-candidate-type (candidate)
"Returns the type of a candidate."
(assoc-default 'type candidate))
(defun eclim--completion-candidate-class (candidate)
"Returns the class name of a candidate."
(assoc-default 'info candidate))
(defun eclim--completion-candidate-doc (candidate)
"Returns the documentation for a candidate."
(assoc-default 'menu candidate))
(defun eclim--completion-candidate-package (candidate)
"Returns the package name of a candidate."
(let ((doc (eclim--completion-candidate-doc candidate)))
(when (string-match "\\(.*\\)\s-\s\\(.*\\)" doc)
(match-string 2 doc))))
(defvar eclim--completion-candidates nil)
(defun eclim--complete ()
(setq eclim--is-completing t)
(setq eclim--completion-candidates
(case major-mode
(assoc-default 'completions
(eclim/execute-command "java_complete" "-p" "-f" "-e" ("-l" "standard") "-o")))
((xml-mode nxml-mode)
(eclim/execute-command "xml_complete" "-p" "-f" "-e" "-o"))
(eclim/execute-command "ruby_complete" "-p" "-f" "-e" "-o"))
(eclim/execute-command "php_complete" "-p" "-f" "-e" "-o"))
((javascript-mode js-mode)
(eclim/execute-command "javascript_complete" "-p" "-f" "-e" "-o"))))
(setq eclim--is-completing nil)))
(defun eclim--completion-candidates-filter (c)
(case major-mode
((xml-mode nxml-mode) (or (search "XML Schema" c)
(search "Namespace" c)))
(t nil)))
(defun eclim--completion-candidates ()
(remove-if #'eclim--completion-candidates-filter
(mapcar (lambda (c) (assoc-default (case major-mode
(java-mode 'info)
(t 'completion)) c))
(defun eclim--basic-complete-internal (completion-list)
"Displays a buffer of basic completions."
(let* ((window (get-buffer-window "*Completions*" 0))
(c (eclim--java-identifier-at-point))
(beg (car c))
(word (cdr c))
(compl (try-completion word
(if (and (eq last-command this-command)
window (window-live-p window) (window-buffer window)
(buffer-name (window-buffer window)))
;; If this command was repeated, and there's a fresh completion window
;; with a live buffer, and this command is repeated, scroll that
;; window.
(with-current-buffer (window-buffer window)
(if (pos-visible-in-window-p (point-max) window)
(set-window-start window (point-min))
(select-window window)
((null compl)
(message "No completions."))
((stringp compl)
(if (string= word compl)
;; Show completion buffer
(let ((list (all-completions word completion-list)))
(setq list (sort list 'string<))
(with-output-to-temp-buffer "*Completions*"
(display-completion-list list word)))
;; Complete
(delete-region (1+ beg) (point))
(insert compl)
;; close completion buffer if there's one
(let ((win (get-buffer-window "*Completions*" 0)))
(if win (quit-window nil win)))))
(t (message "That's the only possible completion."))))))
(defun eclim-complete ()
"Attempts a context sensitive completion for the current
element, then displays the possible completions in a separate
(when eclim-auto-save (save-buffer))
(defun eclim--completion-yasnippet-convert (completion)
"Convert a completion string to a yasnippet template"
(apply #' concat
(loop for c across (replace-regexp-in-string ", " "," completion)
collect (case c
(40 "(${")
(60 "<${")
(44 "}, ${")
(41 "})")
(62 "}>")
(t (char-to-string c))))))
(defvar eclim--completion-start)
(defun eclim-completion-start ()
"Work out the point where completion starts."
(setq eclim--completion-start
(case major-mode
((java-mode javascript-mode js-mode ruby-mode php-mode)
(ignore-errors (beginning-of-thing 'symbol))
((xml-mode nxml-mode)
(while (not (string-match "[<\n[:blank:]]" (char-to-string (char-before))))
(defun eclim--completion-action-java ()
(let* ((end (point))
(completion (buffer-substring-no-properties eclim--completion-start end)))
(cond ((string-match "\\(.*?\\) :.*- Override method" completion)
(let ((sig (eclim--java-parse-method-signature (match-string 1 completion))))
(delete-region eclim--completion-start end)
(eclim-java-implement (symbol-name (assoc-default :name sig)))))
((string-match "\\([^-:]+\\) .*?\\(- *\\(.*\\)\\)?" completion)
(let* ((insertion (match-string 1 completion))
(rest (match-string 3 completion))
(package (if (and rest (string-match "\\w+\\(\\.\\w+\\)*" rest)) rest nil))
(template (eclim--completion-yasnippet-convert insertion)))
(delete-region eclim--completion-start end)
(if (and eclim-use-yasnippet template (featurep 'yasnippet))
(yas/expand-snippet template)
(insert insertion))
(when package
(concat package "." (substring insertion 0 (or (string-match "[<(]" insertion)
(length insertion)))))))))))
(defun eclim--completion-action-xml ()
(when (string-match "[\n[:blank:]]" (char-to-string (char-before eclim--completion-start)))
;; we are completing an attribute; let's use yasnippet to get som nice completion going
(let* ((end (point))
(c (buffer-substring-no-properties eclim--completion-start end))
(completion (if (string-endswith-p c "\"") c (concat c "=\"\""))))
(when (string-match "\\(.*\\)=\"\\(.*\\)\"" completion)
(delete-region eclim--completion-start end)
(if (and eclim-use-yasnippet (featurep 'yasnippet))
(yas/expand-snippet (format "%s=\"${%s}\" ${}" (match-string 1 completion) (match-string 2 completion)))
(insert completion))))))
(defun eclim--completion-action-default ()
(when (and (= 40 (char-before)) (not (looking-at ")")))
;; we've inserted an open paren, so let's close it
(if (and eclim-use-yasnippet (featurep 'yasnippet))
(yas/expand-snippet "$1)$0")
(insert ")")
(defun eclim--completion-action ()
(case major-mode
('java-mode (eclim--completion-action-java))
('nxml-mode (eclim--completion-action-xml))
(t (eclim--completion-action-default))))
(defun eclim--render-doc (str)
"Performs rudimentary rendering of HTML elements in
documentation strings."
(apply #'concat
(loop for p = 0 then (match-end 0)
while (string-match "[[:blank:]]*\\(.*?\\)\\(</?.*?>\\)" str p) collect (match-string 1 str) into ret
for tag = (downcase (match-string 2 str))
when (or (string= tag "</p>") (string= tag "<p>")) collect "\n" into ret
when (string= tag "<br/>") collect " " into ret
when (string= tag "<li>") collect " * " into ret
finally return (append ret (list (substring str p))))))
(defun eclim--completion-documentation (symbol)
"Looks up the documentation string for the given SYMBOL in the
completion candidates list."
(let ((doc (assoc-default 'info (find symbol eclim--completion-candidates :test #'string= :key (lambda (c) (assoc-default 'completion c))))))
(when doc
(eclim--render-doc doc))))
(provide 'eclim-completion)
Jump to Line
Something went wrong with that request. Please try again.