Klimi's new dotfiles with stow.
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

186 linhas
7.0 KiB

há 4 anos
  1. ;;; company-eclim.el --- company-mode completion backend for Eclim
  2. ;; Copyright (C) 2009, 2011, 2013, 2015 Free Software Foundation, Inc.
  3. ;; Author: Nikolaj Schumacher
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;
  17. ;; Using `emacs-eclim' together with (or instead of) this backend is
  18. ;; recommended, as it allows you to use other Eclim features.
  19. ;;
  20. ;; The alternative backend provided by `emacs-eclim' uses `yasnippet'
  21. ;; instead of `company-template' to expand function calls, and it supports
  22. ;; some languages other than Java.
  23. ;;; Code:
  24. (require 'company)
  25. (require 'company-template)
  26. (require 'cl-lib)
  27. (defgroup company-eclim nil
  28. "Completion backend for Eclim."
  29. :group 'company)
  30. (defun company-eclim-executable-find ()
  31. (let (file)
  32. (cl-dolist (eclipse-root '("/Applications/eclipse" "/usr/lib/eclipse"
  33. "/usr/local/lib/eclipse"))
  34. (and (file-exists-p (setq file (expand-file-name "plugins" eclipse-root)))
  35. (setq file (car (last (directory-files file t "^org.eclim_"))))
  36. (file-exists-p (setq file (expand-file-name "bin/eclim" file)))
  37. (cl-return file)))))
  38. (defcustom company-eclim-executable
  39. (or (bound-and-true-p eclim-executable)
  40. (executable-find "eclim")
  41. (company-eclim-executable-find))
  42. "Location of eclim executable."
  43. :type 'file)
  44. (defcustom company-eclim-auto-save t
  45. "Determines whether to save the buffer when retrieving completions.
  46. eclim can only complete correctly when the buffer has been saved."
  47. :type '(choice (const :tag "Off" nil)
  48. (const :tag "On" t)))
  49. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  50. (defvar-local company-eclim--project-dir 'unknown)
  51. (defvar-local company-eclim--project-name nil)
  52. (declare-function json-read "json")
  53. (defvar json-array-type)
  54. (defun company-eclim--call-process (&rest args)
  55. (let ((coding-system-for-read 'utf-8)
  56. res)
  57. (require 'json)
  58. (with-temp-buffer
  59. (if (= 0 (setq res (apply 'call-process company-eclim-executable nil t nil
  60. "-command" args)))
  61. (let ((json-array-type 'list))
  62. (goto-char (point-min))
  63. (unless (eobp)
  64. (json-read)))
  65. (message "Company-eclim command failed with error %d:\n%s" res
  66. (buffer-substring (point-min) (point-max)))
  67. nil))))
  68. (defun company-eclim--project-list ()
  69. (company-eclim--call-process "project_list"))
  70. (defun company-eclim--project-dir ()
  71. (if (eq company-eclim--project-dir 'unknown)
  72. (let ((dir (locate-dominating-file buffer-file-name ".project")))
  73. (when dir
  74. (setq company-eclim--project-dir
  75. (directory-file-name
  76. (expand-file-name dir)))))
  77. company-eclim--project-dir))
  78. (defun company-eclim--project-name ()
  79. (or company-eclim--project-name
  80. (let ((dir (company-eclim--project-dir)))
  81. (when dir
  82. (setq company-eclim--project-name
  83. (cl-loop for project in (company-eclim--project-list)
  84. when (equal (cdr (assoc 'path project)) dir)
  85. return (cdr (assoc 'name project))))))))
  86. (defun company-eclim--candidates (prefix)
  87. (interactive "d")
  88. (let ((project-file (file-relative-name buffer-file-name
  89. (company-eclim--project-dir)))
  90. completions)
  91. (when company-eclim-auto-save
  92. (when (buffer-modified-p)
  93. (basic-save-buffer))
  94. ;; FIXME: Sometimes this isn't finished when we complete.
  95. (company-eclim--call-process "java_src_update"
  96. "-p" (company-eclim--project-name)
  97. "-f" project-file))
  98. (dolist (item (cdr (assoc 'completions
  99. (company-eclim--call-process
  100. "java_complete" "-p" (company-eclim--project-name)
  101. "-f" project-file
  102. "-o" (number-to-string
  103. (company-eclim--search-point prefix))
  104. "-e" "utf-8"
  105. "-l" "standard"))))
  106. (let* ((meta (cdr (assoc 'info item)))
  107. (completion meta))
  108. (when (string-match " ?[(:-]" completion)
  109. (setq completion (substring completion 0 (match-beginning 0))))
  110. (put-text-property 0 1 'meta meta completion)
  111. (push completion completions)))
  112. (let ((completion-ignore-case nil))
  113. (all-completions prefix completions))))
  114. (defun company-eclim--search-point (prefix)
  115. (if (or (cl-plusp (length prefix)) (eq (char-before) ?.))
  116. (1- (point))
  117. (point)))
  118. (defun company-eclim--meta (candidate)
  119. (get-text-property 0 'meta candidate))
  120. (defun company-eclim--annotation (candidate)
  121. (let ((meta (company-eclim--meta candidate)))
  122. (when (string-match "\\(([^-]*\\) -" meta)
  123. (substring meta (match-beginning 1) (match-end 1)))))
  124. (defun company-eclim--prefix ()
  125. (let ((prefix (company-grab-symbol)))
  126. (when prefix
  127. ;; Completion candidates for annotations don't include '@'.
  128. (when (eq ?@ (string-to-char prefix))
  129. (setq prefix (substring prefix 1)))
  130. prefix)))
  131. (defun company-eclim (command &optional arg &rest ignored)
  132. "`company-mode' completion backend for Eclim.
  133. Eclim provides access to Eclipse Java IDE features for other editors.
  134. Eclim version 1.7.13 or newer (?) is required.
  135. Completions only work correctly when the buffer has been saved.
  136. `company-eclim-auto-save' determines whether to do this automatically."
  137. (interactive (list 'interactive))
  138. (cl-case command
  139. (interactive (company-begin-backend 'company-eclim))
  140. (prefix (and (derived-mode-p 'java-mode 'jde-mode)
  141. buffer-file-name
  142. company-eclim-executable
  143. (company-eclim--project-name)
  144. (not (company-in-string-or-comment))
  145. (or (company-eclim--prefix) 'stop)))
  146. (candidates (company-eclim--candidates arg))
  147. (meta (company-eclim--meta arg))
  148. ;; because "" doesn't return everything
  149. (no-cache (equal arg ""))
  150. (annotation (company-eclim--annotation arg))
  151. (post-completion (let ((anno (company-eclim--annotation arg)))
  152. (when anno
  153. (insert anno)
  154. (company-template-c-like-templatify anno))))))
  155. (provide 'company-eclim)
  156. ;;; company-eclim.el ends here