forked from scztt/LanguageServer.quark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInitializeProvider.sc
166 lines (135 loc) · 5.52 KB
/
InitializeProvider.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
163
164
165
166
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#initialize
InitializeProvider : LSPProvider {
classvar <>suggestedServerPort=57110;
classvar <>initializeActions;
var <initializationOptions, initializeParams;
*methodNames {
^["initialize"]
}
*clientCapabilityName { ^nil }
*serverCapabilityName { ^nil }
*onInitialize {
|func|
initializeActions = initializeActions.add(func);
}
*prDoOnInitialize {
|options|
initializeActions.do {
|func|
protect {
func.value(options)
}
}
}
init {
}
options {
// https://microsoft.github.io/language-server-protocol/specifications/specification-3-17/#clientCapabilities
^(
// @TODO Fetch these from LSPCompletionHandler
triggerCharacters: [".", "(", "~"],
// @TODO These are overridden by commit chars for each completion - do we need?
allCommitCharacters: [],
resolveProvider: false,
completionItem: (
labelDetailsSupport: true
)
)
}
onReceived {
|method, params|
var serverCapabilities;
initializeParams = params;
initializationOptions = initializeParams["initializationOptions"] ?? {()};
initializeParams["workspaceFolders"] !? {
|folders|
folders.do {
|folder|
server.workspaceFolders.add(folder["uri"].copy.replace("file://", "").urlDecode)
};
} ?? {
initializeParams["rootUri"] ?? initializeParams["rootPath"] !? {
|root|
server.workspaceFolders.add(root.copy.replace("file://", "").urlDecode)
};
};
Log('LanguageServer.quark').error("suggestedServerPortRange: %", initializationOptions["suggestedServerPortRange"]);
initializationOptions["suggestedServerPortRange"] !? {
|range|
range = [range[0].asInteger, range[1].asInteger];
this.class.suggestedServerPort = range[0];
"Using default server port: % (allocated range: %-%)".format(range[0], range[0], range[1]-1).postln;
Server.all.do {
|s|
s.addr.port = this.class.suggestedServerPort.asInteger;
}
};
serverCapabilities = ();
this.addProviders(initializeParams["capabilities"], serverCapabilities);
Log('LanguageServer.quark').info("Server capabilities are: %", serverCapabilities);
{ this.class.prDoOnInitialize(initializationOptions) }.defer(0.0000001);
^(
"serverInfo": server.serverInfo,
"capabilities": serverCapabilities;
);
}
addProviders {
|clientCapabilities, serverCapabilities, pathRoot=([])|
var allProviders = LSPFeature.all;
Log('LanguageServer.quark').info("Found providers: %", allProviders.collect(_.methodNames).join(", "));
allProviders.do {
|providerClass|
var provider, clientCapability;
// If clientCapabilityName.isNil, assume we ALWAYS use this provider
clientCapability = providerClass.clientCapabilityName !? {
this.getClientCapability(clientCapabilities, providerClass.clientCapabilityName)
} ?? { () };
clientCapability !? {
|capability|
Log('LanguageServer.quark').info("Registering provider: %", providerClass.methodNames);
provider = providerClass.new(server, capability);
providerClass.serverCapabilityName !? {
|capabilityName|
this.addServerCapability(
serverCapabilities,
capabilityName,
provider.options
)
};
server.addProvider(provider);
}
}
}
getClientCapability {
|clientCapabilities, path|
Log('LanguageServer.quark').info("Checking for client capability at % (clientCapabilities: %)", path, clientCapabilities);
if (path.isNil) { ^() };
path.split($.).do {
|key|
if (clientCapabilities.isNil or: { clientCapabilities.isKindOf(Dictionary).not }) {
^nil
} {
clientCapabilities = clientCapabilities[key]
}
};
^clientCapabilities
}
addServerCapability {
|serverCapabilities, path, options|
Log('LanguageServer.quark').info("Adding server capability at %: %", path, options);
if (path.isNil) { ^this };
if (options.notNil) {
path = path.split($.).collect(_.asSymbol);
if (path.size > 1) {
path[0..(path.size-2)].do {
|key|
Log('LanguageServer.quark').info("looking up key %", key);
serverCapabilities[key] = serverCapabilities[key] ?? { () };
serverCapabilities = serverCapabilities[key];
};
};
Log('LanguageServer.quark').info("writing options into key %", path.last);
serverCapabilities[path.last] = options;
}
}
}