-
Notifications
You must be signed in to change notification settings - Fork 11
/
LSPCompletionHandler.sc
162 lines (137 loc) · 5.44 KB
/
LSPCompletionHandler.sc
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
// Base class for handling code completion activities.
// Handlers are registered using `addHandler` - built-in handlers
// get registered automatically in `initClass`, but additional handlers
// can be added e.g. by Quarks.
LSPCompletionHandler {
classvar <>completionHandlers;
classvar <completionLimit = 30;
var <>name, <>trigger, <>prefixHandler, <>action;
// 'trigger' is one or more characters that can START this completion handler
// 'prefixHandler' is a function that returns the parsed prefix (e.g. a class name) before the trigger.
// If there is no prefix handler, the handler is always processed. If the prefix handler returns nil,
// it is skipped.
// 'action' is a function to process the prefix, where the arguments are: |doc, provideCompletions|
*addHandler {
|name, trigger, prefixHandler, action|
completionHandlers = completionHandlers.add(this.prNew(name, trigger, prefixHandler, action))
}
*prNew {
|name, trigger, prefixHandler, action|
^super.newCopyArgs(name, trigger, prefixHandler, action)
}
*initClass {
completionHandlers = completionHandlers.addAll([
LSPCompletionHandler.classMethodHandler,
LSPCompletionHandler.methodHandler,
LSPCompletionHandler.environmentVariableHandler,
LSPCompletionHandler.singletonHandler,
LSPCompletionHandler.classNameHandler
// LSPCompletionHandler.anonymousMethodHandler,
]);
}
// Given a lineString like:
// prefix.completion
// and a list of triggerCharacters:
// "."
// return a list of [prefix, completionTrigger, completion] e.g.:
// [ "prefix", ".", "completion" ]
*prGetCompletionString {
|lineString, triggerCharacters|
var prefix, completion, completionTrigger;
var char = lineString.size - 1;
var classnameRegex = "^.*\\b([A-Z][A-Za-z0-9_]*)$";
var classnameMatch;
classnameMatch = lineString.findRegexp(classnameRegex);
if (classnameMatch.size > 0) {
^["", "", classnameMatch[1][1]]
};
while {
(char >= 0) and: { triggerCharacters.includes(lineString[char]).not }} {
char = char - 1;
};
if (char >= 0) {
completion = lineString[char+1..].stripWhiteSpace;
completionTrigger = lineString[char];
prefix = lineString[..char-1].findRegexp("([~\\w]*\\(?)$".format(
triggerCharacters.escapeChar($().escapeChar($\\)
));
if (prefix.notEmpty) {
prefix = prefix[1][1]
} {
prefix = nil
};
^[prefix, completionTrigger.asString, completion]
} {
^[nil, nil, nil]
}
}
// For a given handler, return true if trigger matches the handler's trigger
// AND the handler.validatePrefix returns a non-nil result (meaning: it
// recognised the prefix as something it can do a completion on).
*prValidateHandler {
|handler, prefix, trigger, completion|
var validatedPrefix;
^(
(handler.trigger == trigger)
and: {
(validatedPrefix = handler.validatePrefix(prefix)).notNil
}
)
}
*handleCompletion {
|lineString, triggerCharacters|
var prefix, trigger, completion;
var validatedPrefix, handler;
triggerCharacters = triggerCharacters ?? { "(." };
// prefix: all text before the trigger
// trigger: one character that triggered the completion
// completion: the remaining characters to do completion on
#prefix, trigger, completion = this.prGetCompletionString(
lineString,
triggerCharacters
);
Log('LanguageServer.quark').info("Doing completion on: % / % / %", prefix, trigger, completion);
// Find the first handler for which prValidateHandler returns non-nil.
handler = completionHandlers.detect({
|handler|
(validatedPrefix = handler.validatePrefix(prefix, trigger)).notNil
});
if (handler.notNil) {
Log('LanguageServer.quark').info("Using handler: %", handler.name);
^handler.handle(validatedPrefix, trigger, completion);
} {
Log('LanguageServer.quark').info("No handler for completion: % / % / %", prefix, trigger, completion);
^nil
}
}
validatePrefix {
|prefix, validateTrigger|
var reResult;
if (trigger != validateTrigger) { ^nil };
^if (prefixHandler.isString) {
reResult = prefix.findRegexp(prefixHandler);
if (reResult.notEmpty) {
reResult[1][1]
} {
nil
}
} {
prefixHandler.value(prefix)
}
}
handle {
|prefix, trigger, completion|
var deferredResult = Deferred();
action.value(
prefix, trigger, completion,
{
|completions, isIncomplete=true|
deferredResult.value = (
isIncomplete: isIncomplete,
items: completions
)
}
);
^deferredResult;
}
}