This repository has been archived by the owner on Aug 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcompany-lean.el
211 lines (186 loc) · 8.63 KB
/
company-lean.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
;;; company-lean.el --- A company backend for lean-mode -*- lexical-binding: t -*-
;; Copyright (c) 2014 Microsoft Corporation. All rights reserved.
;; Author: Leonardo de Moura <[email protected]>
;; Soonho Kong <[email protected]>
;; Gabriel Ebner <[email protected]>
;; Sebastian Ullrich <[email protected]>
;; Maintainer: Sebastian Ullrich <[email protected]>
;; Created: Jan 09, 2014
;; Keywords: languages
;; Package-Requires: ((emacs "24.3") (dash "2.18.0") (s "1.10.0") (f "0.19.0") (company "0.9.3") (lean-mode "3.3.0"))
;; URL: https://github.com/leanprover/lean-mode
;; Released under Apache 2.0 license as described in the file LICENSE.
;;; Commentary:
;; Provides context-sensitive auto completion for lean-mode.
;;; Code:
(require 'company)
(require 'company-etags)
(require 'dash)
(require 'f)
(require 's)
(require 'cl-lib)
(require 'lean-util)
(require 'lean-server)
(defcustom company-lean-type-foreground (face-foreground 'font-lock-keyword-face)
"Color of type parameter in auto-complete candidates"
:group 'lean
:type 'color)
;;;###autoload
(defun company-lean-hook ()
(set (make-local-variable 'company-backends) '(company-lean))
(setq-local company-tooltip-limit 20) ; bigger popup window
(setq-local company-minimum-prefix-length 5)
(setq-local company-idle-delay nil) ; decrease delay before autocompletion popup shows
;(setq-local company-echo-delay 0) ; remove annoying blinking
(setq-local company-begin-commands '(self-insert-command)) ; start autocompletion only after typing
(company-mode t))
(cl-defun company-lean--make-candidate (prefix &key text type (tactic_params 'empty) doc source &allow-other-keys)
(cl-destructuring-bind (&key file line _column) source
(let ((source (cond
(file (cons file line))
(line (cons (current-buffer) (lean-pos-at-line-col line 0))))))
(propertize text
'type type
'tactic_params tactic_params
'doc doc
'source source
'prefix prefix))))
(defun company-lean--handle-singleton-candidate (prefix candidates)
"Handle singleton candidate. If the candidate does not start
with prefix, we add prefix itself as a candidate to prevent
from auto-completion."
(let ((candidate (car candidates)))
(cond ((s-prefix? prefix candidate) candidates)
(t `(,candidate ,prefix)))))
(cl-defun company-lean--exec (&key skip-completions)
"Synchronously queries completions for the current point from server and returns a plist with keys :prefix and :candidates., or nil if no completion should be triggered."
(lean-server-sync)
(let* ((col (lean-line-offset))
(response (lean-server-send-synchronous-command
'complete (list :file_name (buffer-file-name)
:line (line-number-at-pos)
:column col
:skip_completions (or skip-completions :json-false))))
(candidates (plist-get response :completions))
(prefix (plist-get response :prefix)))
(when candidates
(setq candidates
(--map (apply 'company-lean--make-candidate prefix it)
candidates))
(when (= (length candidates) 1)
(setq candidates
(company-lean--handle-singleton-candidate prefix candidates))))
(when (plist-member response :prefix)
(list :prefix prefix :candidates candidates))))
(defun company-lean--annotation (candidate)
(let ((type (get-text-property 0 'type candidate))
(tactic_params (get-text-property 0 'tactic_params candidate)))
(when type
(let* ((annotation-str (if (not (eq tactic_params 'empty))
(format " %s" (mapconcat 'identity tactic_params " "))
(format " : %s" type)))
(annotation-len (length annotation-str))
(candidate-len (length candidate))
(entry-width (+ candidate-len
annotation-len))
(allowed-width (truncate (* 0.90 (window-body-width)))))
(when (> entry-width allowed-width)
(setq annotation-str
(concat
(substring-no-properties annotation-str
0
(- allowed-width candidate-len 3))
"...")))
annotation-str))))
(defun company-lean--location (arg)
(get-text-property 0 'source arg))
(defun company-lean--match (arg)
"Return the end of matched region"
(let ((prefix (get-text-property 0 'prefix arg)))
(when (and prefix (eq (s-index-of prefix arg) 0))
(length prefix))))
(defun company-lean--meta (arg)
(get-text-property 0 'doc arg))
(defun company-lean (command &optional arg &rest ignored)
(cl-case command
(prefix (plist-get (company-lean--exec :skip-completions t) :prefix))
(candidates (plist-get (company-lean--exec) :candidates))
(annotation (company-lean--annotation arg))
(location (company-lean--location arg))
(match (company-lean--match arg))
(meta (company-lean--meta arg))
(no-cache t)
(require-match 'never)
(sorted t)))
;; ADVICES
;; =======
(defadvice company--window-width
(after company-lean--window-width activate)
(when (eq major-mode 'lean-mode)
(setq ad-return-value (truncate (* 0.95 (window-body-width))))))
(defun company-lean--replace-regex-return-position (regex rep string &optional start)
"Find regex and replace with rep on string.
Return replaced string and start and end positions of replacement."
(let* ((start (or start 0))
(m-start (string-match regex string start))
(m-end (match-end 0))
pre-string post-string matched-string replaced-string result)
(cond (m-start
(setq pre-string (substring string 0 m-start))
(setq matched-string (substring string m-start m-end))
(setq post-string (substring string m-end))
(string-match regex matched-string)
(setq replaced-string
(replace-match rep nil nil matched-string))
(setq result (concat pre-string
replaced-string
post-string))
`(,result ,m-start ,(+ m-start (length replaced-string)))
))))
(defun company-lean--replace-regex-add-properties-all (regex rep string properties)
"Find all occurrences of regex in string, and replace them with
rep. Then, add text-properties on the replaced region."
(let ((replace-result-items (company-lean--replace-regex-return-position regex rep string))
(result string))
(while replace-result-items
(pcase replace-result-items
(`(,replaced-string ,m-start ,m-end)
(setq result replaced-string)
(add-text-properties m-start m-end properties result)
(setq replace-result-items
(company-lean--replace-regex-return-position regex rep result m-end)))))
result))
(eval-after-load 'company
'(defadvice company-fill-propertize
(after company-lean-fill-propertize activate)
(when (eq major-mode 'lean-mode)
(let* ((selected (ad-get-arg 3))
(foreground-color company-lean-type-foreground)
(background-color (if selected (face-background 'company-tooltip-selection)
(face-background 'company-tooltip)))
(face-attrs
(cond (background-color `(:foreground ,foreground-color
:background ,background-color))
(t `(:foreground ,foreground-color))))
(properties `(face ,face-attrs
mouse-face company-tooltip))
(old-return ad-return-value)
(old-len (length old-return))
new-return new-len)
(setq new-return
(company-lean--replace-regex-add-properties-all
(rx "?" word-start (group (+ (not white))) word-end)
"\\1"
ad-return-value
properties))
(setq new-len (length new-return))
(while (< (length new-return) old-len)
(setq new-return
(concat new-return " ")))
(when background-color
(add-text-properties new-len old-len properties new-return))
(setq ad-return-value new-return)))))
;;;###autoload
(add-hook 'lean-mode-hook #'company-lean-hook)
(provide 'company-lean)
;;; company-lean.el ends here