This repository has been archived by the owner on Sep 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 132
/
ensime-completion-util.el
157 lines (145 loc) · 5.63 KB
/
ensime-completion-util.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
;;; ensime-completion-util.el
;;
;;;; License
;;
;; Copyright (C) 2015 Aemon Cannon
;;
;; 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 2 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
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; 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, write to the Free
;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
;; MA 02111-1307, USA.
(eval-when-compile
(require 'cl)
(require 'ensime-macros))
(require 'scala-mode2-syntax)
;; In order to efficiently and accurately match completion prefixes, we construct
;; a regular expression which matches scala identifiers in reverse.
;;
;; For referece, Scala identifier syntax (SLS 2.11)
;; -------------------------------
;; op ::= opchar {opchar}
;; varid ::= lower idrest
;; plainid ::= upper idrest | varid | op
;; id ::= plainid | ‘`’ stringLiteral ‘`’
;; idrest ::= {letter | digit} [‘_’ op]
;;
(defconst ensime--rev-idrest-re
(concat
"\\(" scala-syntax:op-re "_+" "\\|" "_" "\\)?"
"\\(" "[" scala-syntax:letter-group scala-syntax:digit-group "]+" "_?" "\\)*"
))
(defconst ensime--rev-alphaid-re
;; '$' intentionally omitted, as user identifiers should not include it, and
;; it breaks completion of interpolated vars in strings.
(concat "\\(" ensime--rev-idrest-re
"[" "[:lower:]" "[:upper:]" "_" "]" "\\)"))
(defconst ensime--rev-plainid-re
(concat "\\(" ensime--rev-alphaid-re "\\|" scala-syntax:op-re "\\)"))
(defconst ensime--rev-quotedid-re "`[^`\n\r]+`")
(defconst ensime--rev-id-re
(concat "^" "\\(" ensime--rev-quotedid-re "\\|" ensime--rev-plainid-re "\\)"))
(defconst ensime--prefix-char-class
(concat "["
"`"
scala-syntax:letterOrDigit-group
scala-syntax:opchar-group
"]"))
(defun ensime--annotate-completions (completions)
"Maps plist structures to propertized strings that will survive
being passed through the innards of auto-complete or company."
(mapcar
(lambda (m)
(let* ((type-sig (plist-get m :type-sig))
(type-id (plist-get m :type-id))
(is-callable (plist-get m :is-callable))
(to-insert (plist-get m :to-insert))
(name (plist-get m :name))
(candidate name))
(propertize candidate
'symbol-name name
'type-sig type-sig
'type-id type-id
'is-callable is-callable
'to-insert to-insert
))) completions))
(defun ensime-completion-prefix-at-point ()
"Returns the prefix to complete."
;; A bit of a hack: Piggyback on font-lock's tokenization to
;; avoid requesting completions inside comments.
(when (not (ensime-in-comment-p (point)))
;; As an optimization, first get an upper bound on the length of prefix using
;; ensime--prefix-char-class.
(let ((i (point))
(s ""))
(while (and (> i 1) (string-match ensime--prefix-char-class (char-to-string (char-before i))))
(setq s (concat s (char-to-string (char-before i))))
(decf i))
;; Then use a proper scala identifier regex to extract the prefix.
(if (string-match ensime--rev-id-re s)
(s-reverse (match-string 1 s)) ""))))
(defun ensime-get-completions-async
(max-results case-sense callback)
(ensime-rpc-async-completions-at-point max-results case-sense
(lexical-let ((continuation callback))
(lambda (info)
(let* ((candidates (ensime--annotate-completions
(plist-get info :completions))))
(funcall continuation candidates))))))
(defun ensime-get-completions (max-results case-sense)
(let* ((info
(ensime-rpc-completions-at-point
max-results case-sense))
(result (list :prefix (plist-get info :prefix)
:candidates (ensime--annotate-completions
(plist-get info :completions)))))
result))
(defun ensime-unique-candidate-at-point ()
"If the identifier preceding point is already complete, returns it as a fully
annotated candidate. Otherwise returns nil."
(let ((prefix (ensime-completion-prefix-at-point)))
(when (> (length prefix) 0)
(let* ((info (ensime-rpc-completions-at-point
2 ensime-company-case-sensitive))
(candidates (ensime--annotate-completions (plist-get info :completions))))
(when (and (= (length candidates) 1)
(string= prefix (car candidates)))
(car candidates))))))
(defun ensime-completion-at-point-function ()
"Standard Emacs 24+ completion function, handles completion-at-point requests.
See: https://www.gnu.org/software/emacs/manual/html_node/elisp/Completion-in-Buffers.html"
(when (ensime-connected-p)
(let* ((prefix (ensime-completion-prefix-at-point))
(start (- (point) (length prefix)))
(end (point))
(props '(:annotation-function
(lambda (m)
(when (get-text-property 0 'is-callable m)
(ensime-brief-type-sig
(get-text-property 0 'type-sig m))))
:exit-function
(lambda (m status)
(when (eq status 'finished)
(ensime-ac-complete-action m)))))
(completion-func
(lambda (prefix pred action)
(cond
((eq action 'metadata)
'(metadata . ((display-sort-function . identity))))
(t
(complete-with-action
action (plist-get (ensime-get-completions 1000000 nil)
:candidates) prefix pred))))))
`(,start ,end ,completion-func . ,props))))
(provide 'ensime-completion-util)
;; Local Variables:
;; End: