aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Monnier2022-12-05 23:50:55 -0500
committerStefan Monnier2022-12-05 23:50:55 -0500
commita03bc3e98c649226cffc6aeafc2d41ece297aa62 (patch)
treef9545cf7e0702aa44c763111a745b3377e4a6fe2
parent18e790944e231a6aa49b09a56f149dced796e939 (diff)
downloademacs-scratch/backend-completion.tar.gz
emacs-scratch/backend-completion.zip
external-completion.el: Minor changesscratch/backend-completion
* lisp/external-completion.el (external-completion-table): Tweak docstring. Explicitly handle `lambda`. * lisp/progmodes/eglot.el (xref-backend-identifier-completion-table): Rename category to `eglot-xref`.
-rw-r--r--lisp/external-completion.el79
-rw-r--r--lisp/progmodes/eglot.el2
2 files changed, 43 insertions, 38 deletions
diff --git a/lisp/external-completion.el b/lisp/external-completion.el
index 4f5595e74bc..d305d3d923c 100644
--- a/lisp/external-completion.el
+++ b/lisp/external-completion.el
@@ -30,9 +30,9 @@
30;; such as a shell utility, an inferior process, an http server. 30;; such as a shell utility, an inferior process, an http server.
31 31
32;; The table and external tool are fully in control of the matching of 32;; The table and external tool are fully in control of the matching of
33;; the pattern string to the potential candidates of completion. When 33;; the pattern string to the potential candidates of completion.
34;; `external' is in use, the usual styles configured by the user or 34;; When `external' is in use, the usual styles configured by the user
35;; other in `completion-styles' are ignored. 35;; or other in `completion-styles' are ignored.
36;; 36;;
37;; This compromise is for speed: all other styles need the full data 37;; This compromise is for speed: all other styles need the full data
38;; set to be available in Emacs' addressing space, which is often slow 38;; set to be available in Emacs' addressing space, which is often slow
@@ -50,13 +50,12 @@
50 external-completion--all-completions 50 external-completion--all-completions
51 "Ad-hoc completion style provided by the completion table.")) 51 "Ad-hoc completion style provided by the completion table."))
52 52
53(defun external-completion-table (category lookup 53(defun external-completion-table ( category lookup
54 &optional metadata 54 &optional metadata try-completion-function)
55 try-completion-function)
56 "Make completion table using the `external' completion style. 55 "Make completion table using the `external' completion style.
57 56
58The completion table produced foregoes any styles normally set in 57The completion table produced foregoes any styles normally set in
59`completion-styles' and will sets up an entry for the symbol 58`completion-styles' and will set up an entry for the symbol
60CATEGORY in `completion-category-defaults', linking it to the 59CATEGORY in `completion-category-defaults', linking it to the
61special completion style `external'. 60special completion style `external'.
62 61
@@ -65,43 +64,45 @@ tool that provides completions. This may be a shell utility, an
65inferior process, an http server, etc. The external tool does 64inferior process, an http server, etc. The external tool does
66the matching, and the full set of candidates doesn't need to be 65the matching, and the full set of candidates doesn't need to be
67transferred to Emacs's address space. This often results in much 66transferred to Emacs's address space. This often results in much
68faster overall completion at the expense of the convenience of 67faster overall completion at the expense of the choice of
69offered by the rich variety of usual completion styles usually 68completion styles.
70available.
71 69
72LOOKUP is a function taking a string PATTERN and a number 70LOOKUP is a function taking a string PATTERN and a position POINT.
73POINT. The function should contact the backend and return a list 71The function should contact the backend and return a list
74of strings representing the candidates matching PATTERN given 72of strings representing the candidates matching PATTERN given
75that POINT is the location of point within it. LOOKUP decides if 73that POINT is the location of point within it. LOOKUP decides if
76PATTERN is interpreted as a substring, a regular expression, or 74PATTERN is interpreted as a substring, a regular expression, or
77any other type of matching key. Strings returned by LOOKUP may 75any other type of matching key. Strings returned by LOOKUP may
78be propertized with `completions-common-part' to illustrate the 76be propertized with `completions-common-part' to illustrate the
79specific interpretationq. To maintain responsiveness in the face 77specific interpretation. To maintain responsiveness in the face
80of all but the spiffiest external tools, LOOKUP should detect 78of all but the spiffiest external tools, LOOKUP should detect
81timeouts and pending user input with `while-no-input' or 79timeouts and pending user input with `while-no-input' or
82`sit-for' (which see), cancel the request (if that is possible) 80`sit-for' (which see), cancel the request (if that is possible)
83and immediately return any non-list. 81and immediately return any non-list.
84 82
85CATEGORY is a symbol identifying the external tool. METADATA is 83Calls to LOOKUP are cached. There is no option to flush the cache,
86an alist of additional properties such as `cycle-sort-function' 84so if you need to do it, create a new completion table by calling
85`external-completion-table' again.
86
87
88CATEGORY is a symbol that should identify the kind of things
89being completed, so users can refer to it in `completion-category-override'.
90METADATA is an alist of additional properties such as `cycle-sort-function'
87to associate with CATEGORY. This means that the caller may still 91to associate with CATEGORY. This means that the caller may still
88retain control the sorting of the candidates while the tool 92retain control the sorting of the candidates while the tool
89controls the matching. 93controls the matching.
90 94
91TRY-COMPLETION-FUNCTION is if you want to get fancy. It's a 95TRY-COMPLETION-FUNCTION is a function taking arguments
92function taking a (PATTERN POINT ALL-COMPLETIONS), where PATTERN 96\(PATTERN POINT ALL-COMPLETIONS), where PATTERN
93and POINT are as described above and ALL-COMPLETIONS are all 97and POINT are as described above and ALL-COMPLETIONS are all
94candidates previously gathered by LOOKUP (don't worry, we do some 98candidates previously gathered by LOOKUP. It should return
95caching so it doesn't get called more than needed). If you know 99a cons cell (NEWPATTERN . NEWPOINT) such that NEWPATTERN is
96how the external tool is interpreting PATTERN, 100as long as possible within the constraints that LOOKUP still returns
97TRY-COMPLETION-FUNCTION may return a cons cell (NEW-PATTERN 101the same set of candidates as it does for PATTERN and POINT.
98. NEW-POINT) to partially (or fully) complete the user's 102This can only be provided if you happen to know what kind of
99completion input. For example, if the tool is completing by 103completion style is used by the external tool and is only worthwhile
100prefix, you could use `try-completion' to find the largest prefix 104to provide if that completion style is fairly simple:
101in ALL-COMPLETIONS and then return that as NEW-PATTERN. If the 105if the tool is using something like \"flex\", it's probably useless."
102tool is using something else, like \"flex\", it's probably
103useless. Anyway, the default is to return simply a (PATTERN
104. POINT) unaltered."
105 (unless (assq category completion-category-defaults) 106 (unless (assq category completion-category-defaults)
106 (push `(,category (styles external)) 107 (push `(,category (styles external))
107 completion-category-defaults)) 108 completion-category-defaults))
@@ -130,17 +131,19 @@ useless. Anyway, the default is to return simply a (PATTERN
130 `(external-completion--allc 131 `(external-completion--allc
131 . ,(if pred (cl-remove-if-not pred all) all)))) 132 . ,(if pred (cl-remove-if-not pred all) all))))
132 (`(boundaries . ,_) nil) 133 (`(boundaries . ,_) nil)
134 ('lambda t) ;; `test-completion' should assume all results are valid.
133 (_method 135 (_method
136 ;; FIXME: I suspect many external backends can't make much use of
137 ;; POINT, in which case the call below may actually be equivalent
138 ;; to one that's already in cache but we will fail to reuse it
139 ;; just because POINT is different :-(
134 (let ((all (lookup-internal string (length string)))) 140 (let ((all (lookup-internal string (length string))))
135 ;; This is here for two reasons: 141 ;; This is here for two reasons:
136 ;; 142 ;;
137 ;; * for when users try to work around 143 ;; * for when users customize `completion-category-overrides'
138 ;; `completion-category-defaults' and access this table a 144 ;; to access this table with a non-`external' completion style.
139 ;; non-`external' completion style. It won't work very 145 ;; It won't work very well for styles more complex than PCM,
140 ;; well, as this `all' here very often doesn't equate 146 ;; but it should work fine for prefix completion.
141 ;; "the full set" (many tools cap to sth like 100-1000
142 ;; results). FIXME: I think it would be better to just
143 ;; error here.
144 ;; 147 ;;
145 ;; * for when `_method' above can also be `nil' or `lambda' 148 ;; * for when `_method' above can also be `nil' or `lambda'
146 ;; which has some semantics and use I don't fully 149 ;; which has some semantics and use I don't fully
@@ -160,10 +163,12 @@ useless. Anyway, the default is to return simply a (PATTERN
160 (cdr res))))) 163 (cdr res)))))
161 164
162(defun external-completion--try-completion (string table pred point) 165(defun external-completion--try-completion (string table pred point)
163 (external-completion--call 'external-completion--tryc string table pred point)) 166 (external-completion--call 'external-completion--tryc
167 string table pred point))
164 168
165(defun external-completion--all-completions (string table pred point) 169(defun external-completion--all-completions (string table pred point)
166 (external-completion--call 'external-completion--allc string table pred point)) 170 (external-completion--call 'external-completion--allc
171 string table pred point))
167 172
168(provide 'external-completion) 173(provide 'external-completion)
169;;; external-completion.el ends here 174;;; external-completion.el ends here
diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el
index 5cc769d4b03..0dc7357a436 100644
--- a/lisp/progmodes/eglot.el
+++ b/lisp/progmodes/eglot.el
@@ -2575,7 +2575,7 @@ If BUFFER, switch to it before."
2575 0 'eglot--lsp-workspaceSymbol c) 2575 0 'eglot--lsp-workspaceSymbol c)
2576 :score 0))) 2576 :score 0)))
2577 (external-completion-table 2577 (external-completion-table
2578 'eglot-indirection-joy 2578 'eglot-xref
2579 #'lookup 2579 #'lookup
2580 `((cycle-sort-function 2580 `((cycle-sort-function
2581 . ,(lambda (completions) 2581 . ,(lambda (completions)