]> code.delx.au - gnu-emacs-elpa/blob - company-semantic.el
Add company-semantic-begin-after-member-access
[gnu-emacs-elpa] / company-semantic.el
1 ;;; company-semantic.el --- company-mode completion backend using Semantic
2
3 ;; Copyright (C) 2009-2011, 2013-2016 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 (require 'cl-lib)
30
31 (defvar semantic-idle-summary-function)
32 (declare-function semantic-documentation-for-tag "semantic/doc" )
33 (declare-function semantic-analyze-current-context "semantic/analyze")
34 (declare-function semantic-analyze-possible-completions "semantic/complete")
35 (declare-function semantic-analyze-find-tags-by-prefix "semantic/analyze/fcn")
36 (declare-function semantic-tag-class "semantic/tag")
37 (declare-function semantic-tag-name "semantic/tag")
38 (declare-function semantic-tag-start "semantic/tag")
39 (declare-function semantic-tag-buffer "semantic/tag")
40 (declare-function semantic-active-p "semantic")
41 (declare-function semantic-format-tag-prototype "semantic/format")
42
43 (defgroup company-semantic nil
44 "Completion backend using Semantic."
45 :group 'company)
46
47 (defcustom company-semantic-metadata-function 'company-semantic-summary-and-doc
48 "The function turning a semantic tag into doc information."
49 :type 'function)
50
51 (defcustom company-semantic-begin-after-member-access t
52 "When non-nil, automatic completion will start whenever the current
53 symbol is preceded by \".\", \"->\" or \"::\", ignoring
54 `company-minimum-prefix-length'.
55
56 If `company-begin-commands' is a list, it should include `c-electric-lt-gt'
57 and `c-electric-colon', for automatic completion right after \">\" and
58 \":\".")
59
60 (defvar company-semantic-modes '(c-mode c++-mode jde-mode java-mode))
61
62 (defvar-local company-semantic--current-tags nil
63 "Tags for the current context.")
64
65 (defun company-semantic-documentation-for-tag (tag)
66 (when (semantic-tag-buffer tag)
67 ;; When TAG's buffer is unknown, the function below raises an error.
68 (semantic-documentation-for-tag tag)))
69
70 (defun company-semantic-doc-or-summary (tag)
71 (or (company-semantic-documentation-for-tag tag)
72 (and (require 'semantic-idle nil t)
73 (require 'semantic/idle nil t)
74 (funcall semantic-idle-summary-function tag nil t))))
75
76 (defun company-semantic-summary-and-doc (tag)
77 (let ((doc (company-semantic-documentation-for-tag tag))
78 (summary (funcall semantic-idle-summary-function tag nil t)))
79 (and (stringp doc)
80 (string-match "\n*\\(.*\\)$" doc)
81 (setq doc (match-string 1 doc)))
82 (concat summary
83 (when doc
84 (if (< (+ (length doc) (length summary) 4) (window-width))
85 " -- "
86 "\n"))
87 doc)))
88
89 (defun company-semantic-doc-buffer (tag)
90 (let ((doc (company-semantic-documentation-for-tag tag)))
91 (when doc
92 (company-doc-buffer
93 (concat (funcall semantic-idle-summary-function tag nil t)
94 "\n"
95 doc)))))
96
97 (defsubst company-semantic-completions (prefix)
98 (ignore-errors
99 (let ((completion-ignore-case nil)
100 (context (semantic-analyze-current-context)))
101 (setq company-semantic--current-tags
102 (semantic-analyze-possible-completions context 'no-unique))
103 (all-completions prefix company-semantic--current-tags))))
104
105 (defun company-semantic-completions-raw (prefix)
106 (setq company-semantic--current-tags nil)
107 (dolist (tag (semantic-analyze-find-tags-by-prefix prefix))
108 (unless (eq (semantic-tag-class tag) 'include)
109 (push tag company-semantic--current-tags)))
110 (delete "" (mapcar 'semantic-tag-name company-semantic--current-tags)))
111
112 (defun company-semantic-annotation (argument tags)
113 (let* ((tag (assq argument tags))
114 (kind (when tag (elt tag 1))))
115 (cl-case kind
116 (function (let* ((prototype (semantic-format-tag-prototype tag nil nil))
117 (par-pos (string-match "(" prototype)))
118 (when par-pos (substring prototype par-pos)))))))
119
120 (defun company-semantic--prefix ()
121 (if company-semantic-begin-after-member-access
122 (company-grab-symbol-cons "\\.\\|->\\|::" 2)
123 (company-grab-symbol)))
124
125 ;;;###autoload
126 (defun company-semantic (command &optional arg &rest ignored)
127 "`company-mode' completion backend using CEDET Semantic."
128 (interactive (list 'interactive))
129 (cl-case command
130 (interactive (company-begin-backend 'company-semantic))
131 (prefix (and (featurep 'semantic)
132 (semantic-active-p)
133 (memq major-mode company-semantic-modes)
134 (not (company-in-string-or-comment))
135 (or (company-semantic--prefix) 'stop)))
136 (candidates (if (and (equal arg "")
137 (not (looking-back "->\\|\\." (- (point) 2))))
138 (company-semantic-completions-raw arg)
139 (company-semantic-completions arg)))
140 (meta (funcall company-semantic-metadata-function
141 (assoc arg company-semantic--current-tags)))
142 (annotation (company-semantic-annotation arg
143 company-semantic--current-tags))
144 (doc-buffer (company-semantic-doc-buffer
145 (assoc arg company-semantic--current-tags)))
146 ;; Because "" is an empty context and doesn't return local variables.
147 (no-cache (equal arg ""))
148 (duplicates t)
149 (location (let ((tag (assoc arg company-semantic--current-tags)))
150 (when (buffer-live-p (semantic-tag-buffer tag))
151 (cons (semantic-tag-buffer tag)
152 (semantic-tag-start tag)))))))
153
154 (provide 'company-semantic)
155 ;;; company-semantic.el ends here