Permalink
Browse files

Merge branch 'completion'

  • Loading branch information...
2 parents f34d4a7 + 26b3091 commit cdcbaa3ae91b3a5da1a6f9bc058fb3660d7b70c1 @fred-o fred-o committed Nov 14, 2012
Showing with 264 additions and 289 deletions.
  1. +5 −59 ac-emacs-eclim-source.el
  2. +13 −152 company-emacs-eclim.el
  3. +222 −0 eclim-completion.el
  4. +0 −68 eclim-java.el
  5. +1 −1 eclim-problems.el
  6. +23 −9 eclim.el
View
@@ -29,6 +29,7 @@
(require 'eclim)
(require 'eclim-java)
+(require 'eclim-completion)
(require 'auto-complete)
(defface ac-emacs-eclim-candidate-face
@@ -41,66 +42,11 @@
"Face for the emacs-eclim selected candidate."
:group 'auto-complete)
-(defun ac-emacs-eclim-candidates ()
- (with-no-warnings
- (mapcar (lambda (c) (assoc-default 'info c))
- (assoc-default 'completions (eclim/java-complete)))))
-
-(defun ac-emacs-eclim-available ()
- (eclim--accepted-p (buffer-file-name)))
-
-(defvar ac-emacs-eclim-point)
-
-(defun ac-emacs-eclim-init ()
- (setq ac-emacs-eclim-point ac-point)
- (when eclim-print-debug-messages (message "Completion started at %s, ac-point is %s" (point) ac-point)))
-
-(defun ac-emacs-eclim-yasnippet-convert (s)
- "Convert a completion string to a yasnippet template"
- (apply #'concat
- (let* ((beg (string-match "[<(]" s)))
- (cons (substring s 0 (or beg (length s)))
- (loop for start = beg then (match-end 0)
- for end = (string-match "(\\|)\\|, *" s start)
- with i = 0
- while end
- if (not (= start end)) collect (format "${%s:%s}" (incf i) (substring s start end)) into res
- collect (match-string 0 s) into res
- finally return (append res '("$0")))))))
-
-(defun ac-emacs-eclim-action ()
- (let* ((end (point))
- (completion (buffer-substring-no-properties ac-emacs-eclim-point end)))
- (if (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 (ac-emacs-eclim-yasnippet-convert insertion)))
- (delete-region ac-emacs-eclim-point end)
- (if (and eclim-use-yasnippet template (featurep 'yasnippet))
- (yas/expand-snippet template)
- (insert insertion))
- (when package
- (eclim-java-import (concat package "." (substring insertion 0 (or (string-match "[<(]" insertion)
- (length insertion))))))))))
-
(ac-define-source emacs-eclim
- '((candidates . ac-emacs-eclim-candidates)
- (available . ac-emacs-eclim-available)
- (init . ac-emacs-eclim-init)
- (action . ac-emacs-eclim-action)
- (requires . 0)
- (cache)
- (selection-face . ac-emacs-eclim-selection-face)
- (candidate-face . ac-emacs-eclim-candidate-face)
- (symbol . "f")))
-
-(ac-define-source emacs-eclim-c-dot
- '((candidates . ac-emacs-eclim-candidates)
- (available . ac-emacs-eclim-available)
- (init . ac-emacs-eclim-init)
- (action . ac-emacs-eclim-action)
- (prefix . c-dot)
+ '((candidates . eclim--completion-candidates)
+ (action . eclim--completion-action)
+ (prefix . eclim-completion-start)
+ (document . eclim--completion-documentation)
(requires . 0)
(cache)
(selection-face . ac-emacs-eclim-selection-face)
View
@@ -1,6 +1,6 @@
;; company-emacs-eclim.el --- an interface to the Eclipse IDE.
;;
-;; Copyright (C) 2009 Fredrik Appelberg
+;; Copyright (C) 2009-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
@@ -26,174 +26,35 @@
;;
;; company-emacs-eclim.el -- a company-mode backend that replaces company-eclim
;;
-;; To activate this backend, replace company-eclim with
-;; company-emacs-eclim in the eclim-backends list, or call the
+;; To activate this backend, replace company-eclim and/or company-nxml
+;; with company-emacs-eclim in the eclim-backends list, or call the
;; convenience function company-emacs-eclim-setup.
;;* Eclim Company
(require 'eclim)
+(require 'eclim-completion)
(require 'eclim-java)
(require 'company)
-(defvar cee--candidates nil)
-(make-variable-buffer-local 'cee--candidates)
-
(defun company-emacs-eclim-setup ()
"Convenience function that adds company-emacs-eclim to the list
of available company backends."
- (flet ((replace-recur (elt rpl lst)
- (cond ((null lst) nil)
- ((listp (first lst)) (cons (replace-recur elt rpl (first lst))
- (replace-recur elt rpl (rest lst))))
- (t (cons (if (equal (first lst) elt) rpl (first lst))
- (replace-recur elt rpl (rest lst)))))))
- (let ((replaced (replace-recur 'company-eclim 'company-emacs-eclim company-backends)))
- (setq company-backends
- (if (eq replaced company-backends)
- (cons 'company-emacs-eclim company-backends)
- replaced)))))
-
-(defun cee--correct-completions (candidates)
- "If we are lookup at a list of method call completions, check
- if we have already typed part of this call."
- (cond ((every (lambda (c) (string= "f" (eclim--completion-candidate-type c))) candidates)
- ;; When completing a method call that have alread been completed
- ;; up to the 'method(' point, eclim still reports the
- ;; completions as 'method(arg1, arg2, ...)', which is not what
- ;; company-mode expects.
- (let ((common (try-completion "" (mapcar 'eclim--completion-candidate-doc candidates))))
- (save-excursion
- (if (search-backward common (- (point) (length common)) t)
- (mapcar (lambda (c)
- (list
- (eclim--completion-candidate-type c)
- (eclim--completion-candidate-class c)
- (substring (eclim--completion-candidate-doc c) (length common))))
- candidates)
- candidates))))
- (t candidates)))
-
-(defun cee--candidates (prefix)
- "Calls eclim to get a list of matching completion candidates."
- (interactive "d")
- (let ((project-file (eclim--project-current-file))
- (project-name (eclim--project-name)))
- (eclim/java-src-update)
- (setq cee--candidates (cee--correct-completions (assoc-default 'completions (eclim/java-complete)))))
- (let ((completion-ignore-case nil))
- (all-completions prefix (mapcar 'eclim--completion-candidate-doc cee--candidates))))
-
-(defun cee--lookup-candidate (lookup)
- "Looks up the candidate record that matches the string inserted
-by company-mode in the list of eclim-matches."
- (find lookup cee--candidates
- :key #'eclim--completion-candidate-doc
- :test #'string=))
-
+ (setq company-backends
+ (cons 'company-emacs-eclim
+ (remove-if (lambda (b) (find b '(company-nxml company-eclim)))
+ company-backends))))
+
(defun company-emacs-eclim (command &optional arg &rest ignored)
"A `company-mode' back-end for eclim completion"
(interactive)
(case command
- ('prefix (and (derived-mode-p 'java-mode 'jde-mode)
- buffer-file-name
- eclim-executable
- (eclim--project-name)
- (not (company-in-string-or-comment))
- (or (company-grab-symbol) 'stop)))
- ('candidates (cee--candidates arg))
- ('meta (eclim--completion-candidate-doc (cee--lookup-candidate arg)))
+ ('prefix (buffer-substring-no-properties (eclim-completion-start) (point)))
+ ('candidates (eclim--completion-candidates))
+ ('meta (eclim--completion-documentation arg))
('no-cache (equal arg ""))))
-(defun cee--delete-backward (delim)
- "Delete text backwards from point up to and including the part
-of the buffer that matches DELIM. The search is bounded by
-COMPANY-POINT - 1."
- (let ((end (point))
- (start (search-backward delim (- company-point 1) t)))
- (when start
- (delete-region start end))))
-
-(defun cee--generic-args (candidate)
- "If the doc string for this CANDIDATE is a generic arg list,
- return a list of the arguments, otherwise return nil."
- (save-excursion
- (let ((doc (eclim--completion-candidate-doc candidate)))
- (if (string-match "\\(.*?<\\)\\(.*\\)>" doc)
- (let ((class (match-string 1 doc))
- (args (match-string 2 doc)))
- (if (search-backward class 0 t)
- (split-string args ",")))))))
-
-(defun cee--method-call (candidate)
- "If the doc string for this CANDIDATE is a method call argument
- list, return a list of lists representing the type and
- name of each argument."
- (let ((doc (eclim--completion-candidate-doc candidate)))
- (if (or (string-match "\\(.*\\)(\\(.*\\))" doc)
- (string-match "\\(\\)\\(.*\\))" doc))
- (mapcar (lambda (e) (split-string e " "))
- (split-string (match-string 2 doc) ", " t)))))
-
-(defun cee--join-list (lst glue)
- "Utility function; returns a list based on LST with GLUE
-inserted between each element."
- (cond ((null lst) nil)
- ((null (rest lst)) lst)
- (t
- (cons (first lst)
- (cons glue (cee--join-list (rest lst) glue))))))
-
-(defun cee--show-arg-list (start-delim args glue end-delim)
- "Displays/inserts an argument list at point, using yasnippet if
-available."
- (flet ((args-to-string (arg-list)
- (apply 'concat
- (append
- (when start-delim (list start-delim))
- (cee--join-list arg-list glue)
- (when end-delim (list end-delim))))))
- (if (and eclim-use-yasnippet (featurep 'yasnippet))
- (yas/expand-snippet (args-to-string
- (loop for arg in args
- for i from 1
- collect (concat "${" (int-to-string i) ":" arg "}"))))
- (insert (args-to-string args)))))
-
-;; TODO: handle override/implementation of methods
-;; TODO: handle constructor arguments
-(defun cee--completion-finished (arg)
- "Post-completion hook after running company-mode completions."
- (let* ((candidate (cee--lookup-candidate arg))
- (type (eclim--completion-candidate-type candidate)))
- (when candidate
- (if (string= "t" type)
- ;; If this is a type, first check if this is a completion of generic argumends
- (let ((gen-args (cee--generic-args candidate)))
- (if gen-args
- (progn
- (delete-region company-point (point))
- (cee--show-arg-list nil gen-args ", " ">"))
- (progn
- ;; otherwise, remove the doc string and insert an import statement
- (cee--delete-backward " - ")
- (eclim-java-import (concat (eclim--completion-candidate-package candidate) "."
- (eclim--completion-candidate-class candidate))))))
- ;; Otherwise, check if this is a method call
- (if (string= "f" type)
- (let ((call-args (cee--method-call candidate)))
- (push-mark (point) t)
- (goto-char (search-backward "("))
- (cee--show-arg-list "("
- (mapcar (lambda (c) (concat (first c) " " (second c))) call-args)
- ", " ")")
- (save-excursion
- (delete-region (1- (search-forward "(")) (mark t)))
- (pop-mark))
- ;; Otherwise, just delete the doc string
- (cee--delete-backward " : "))))))
-
(add-hook 'company-completion-finished-hook
- 'cee--completion-finished)
+ (lambda (arg) (eclim--completion-action)))
(provide 'company-emacs-eclim)
Oops, something went wrong.

0 comments on commit cdcbaa3

Please sign in to comment.