firefly-compiler 0.4.40 → 0.4.46
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.hintrc +4 -4
- package/.vscode/settings.json +4 -4
- package/bin/Release.ff +99 -71
- package/bin/firefly.mjs +1 -1
- package/compiler/Builder.ff +257 -257
- package/compiler/Compiler.ff +227 -227
- package/compiler/Dependencies.ff +186 -186
- package/compiler/DependencyLock.ff +17 -17
- package/compiler/JsEmitter.ff +946 -946
- package/compiler/LspHook.ff +202 -202
- package/compiler/ModuleCache.ff +178 -178
- package/compiler/Workspace.ff +88 -88
- package/core/.firefly/include/package-lock.json +394 -394
- package/core/.firefly/include/package.json +5 -5
- package/core/.firefly/include/prepare.sh +1 -1
- package/core/.firefly/package.ff +2 -2
- package/core/Array.ff +265 -265
- package/core/Atomic.ff +64 -64
- package/core/Box.ff +7 -7
- package/core/BrowserSystem.ff +40 -40
- package/core/BuildSystem.ff +148 -148
- package/core/Crypto.ff +96 -102
- package/core/Equal.ff +36 -36
- package/core/HttpClient.ff +87 -87
- package/core/JsSystem.ff +69 -69
- package/core/Json.ff +434 -434
- package/core/List.ff +415 -415
- package/core/Lock.ff +144 -144
- package/core/NodeSystem.ff +189 -189
- package/core/Ordering.ff +161 -161
- package/core/Path.ff +401 -401
- package/core/Random.ff +134 -134
- package/core/RbMap.ff +216 -216
- package/core/Show.ff +43 -43
- package/core/SourceLocation.ff +68 -68
- package/core/Task.ff +141 -141
- package/experimental/benchmarks/ListGrab.ff +23 -23
- package/experimental/benchmarks/ListGrab.java +55 -55
- package/experimental/benchmarks/Pyrotek45.ff +30 -30
- package/experimental/benchmarks/Pyrotek45.java +64 -64
- package/experimental/bidirectional/Bidi.ff +88 -88
- package/experimental/random/Index.ff +53 -53
- package/experimental/random/Process.ff +120 -120
- package/experimental/random/Scrape.ff +51 -51
- package/experimental/random/Symbols.ff +73 -73
- package/experimental/random/Tensor.ff +52 -52
- package/experimental/random/Units.ff +36 -36
- package/experimental/s3/S3TestAuthorizationHeader.ff +38 -38
- package/experimental/s3/S3TestPut.ff +15 -15
- package/experimental/tests/TestJson.ff +26 -26
- package/firefly.sh +0 -0
- package/fireflysite/Main.ff +13 -13
- package/lsp/.firefly/package.ff +1 -1
- package/lsp/CompletionHandler.ff +811 -811
- package/lsp/Handler.ff +714 -714
- package/lsp/HoverHandler.ff +79 -79
- package/lsp/LanguageServer.ff +272 -272
- package/lsp/SignatureHelpHandler.ff +55 -55
- package/lsp/SymbolHandler.ff +181 -181
- package/lsp/TestReferences.ff +16 -16
- package/lsp/TestReferencesCase.ff +7 -7
- package/lsp/stderr.txt +1 -1
- package/lsp/stdout.txt +34 -34
- package/lux/.firefly/package.ff +1 -1
- package/lux/Css.ff +648 -648
- package/lux/CssTest.ff +48 -48
- package/lux/Lux.ff +487 -487
- package/lux/LuxEvent.ff +116 -116
- package/lux/Main.ff +128 -128
- package/lux/Main2.ff +144 -144
- package/output/js/ff/compiler/Builder.mjs +43 -43
- package/output/js/ff/compiler/Dependencies.mjs +3 -3
- package/output/js/ff/core/Array.mjs +59 -59
- package/output/js/ff/core/Atomic.mjs +36 -36
- package/output/js/ff/core/BrowserSystem.mjs +11 -11
- package/output/js/ff/core/BuildSystem.mjs +30 -30
- package/output/js/ff/core/Crypto.mjs +58 -62
- package/output/js/ff/core/HttpClient.mjs +24 -24
- package/output/js/ff/core/Json.mjs +147 -147
- package/output/js/ff/core/List.mjs +50 -50
- package/output/js/ff/core/Lock.mjs +97 -97
- package/output/js/ff/core/NodeSystem.mjs +77 -77
- package/output/js/ff/core/Ordering.mjs +8 -8
- package/output/js/ff/core/Path.mjs +231 -231
- package/output/js/ff/core/Random.mjs +56 -56
- package/output/js/ff/core/Task.mjs +31 -31
- package/package.json +29 -29
- package/rpc/.firefly/package.ff +1 -1
- package/rpc/Rpc.ff +69 -69
- package/s3/.firefly/package.ff +1 -1
- package/s3/S3.ff +92 -92
- package/unsafejs/UnsafeJs.ff +19 -19
- package/vscode/LICENSE.txt +21 -21
- package/vscode/Prepublish.ff +15 -15
- package/vscode/README.md +16 -16
- package/vscode/client/package.json +22 -22
- package/vscode/client/src/extension.ts +104 -104
- package/vscode/icons/firefly-icon.svg +10 -10
- package/vscode/language-configuration.json +61 -61
- package/vscode/package-lock.json +3623 -3623
- package/vscode/package.json +160 -160
- package/vscode/snippets.json +241 -241
- package/webserver/.firefly/include/package-lock.json +16 -16
- package/webserver/.firefly/include/package.json +5 -5
- package/webserver/.firefly/package.ff +2 -2
- package/webserver/WebServer.ff +685 -685
- package/websocket/.firefly/package.ff +1 -1
- package/websocket/WebSocket.ff +131 -131
package/lsp/LanguageServer.ff
CHANGED
|
@@ -1,272 +1,272 @@
|
|
|
1
|
-
import ModuleCache from ff:compiler
|
|
2
|
-
import DependencyLock from ff:compiler
|
|
3
|
-
import Handler
|
|
4
|
-
|
|
5
|
-
class LanguageServer(
|
|
6
|
-
mutable openFiles: Map[String, String]
|
|
7
|
-
)
|
|
8
|
-
|
|
9
|
-
data BadRequestException(reason: String)
|
|
10
|
-
|
|
11
|
-
data Request(
|
|
12
|
-
headers: Map[String, String]
|
|
13
|
-
object: Json
|
|
14
|
-
)
|
|
15
|
-
|
|
16
|
-
data RequestMessage(
|
|
17
|
-
id: Option[MessageId] // None when the request is a Notification Message
|
|
18
|
-
method: String
|
|
19
|
-
parameters: Map[String, Json]
|
|
20
|
-
)
|
|
21
|
-
|
|
22
|
-
logMessages = False
|
|
23
|
-
|
|
24
|
-
main(system: NodeSystem) {
|
|
25
|
-
let logDirectory = if(logMessages) {
|
|
26
|
-
let directory = system.path(".").slash("messages")
|
|
27
|
-
if(directory.isDirectory()) {directory.delete()}
|
|
28
|
-
directory.createDirectory()
|
|
29
|
-
directory
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
let fireflyPath = system.path(".")
|
|
33
|
-
let cache = ModuleCache.new(0)
|
|
34
|
-
let handler = Handler(
|
|
35
|
-
fireflyPath = fireflyPath
|
|
36
|
-
rootPath = None
|
|
37
|
-
virtualFiles = Map.new()
|
|
38
|
-
cancelledRequests = [].toSet()
|
|
39
|
-
responseCache = Map.new()
|
|
40
|
-
fileSymbolsCache = Map.new()
|
|
41
|
-
moduleCache = cache
|
|
42
|
-
dependencyLock = DependencyLock.new(system.mainTask())
|
|
43
|
-
)
|
|
44
|
-
try {
|
|
45
|
-
mutable input = system.readStream()
|
|
46
|
-
let responseChannel: Channel[Pair[Json, String]] = system.mainTask().channel()
|
|
47
|
-
system.mainTask().spawn {_ =>
|
|
48
|
-
while {True} {
|
|
49
|
-
let body = responseChannel.read()
|
|
50
|
-
let json = body.first.write(Some(" ")).toBuffer()
|
|
51
|
-
logDirectory.each {logMessage(_, "response", body.first, body.second)}
|
|
52
|
-
system.writeText("Content-Length: " + json.size() + "\r\n\r\n")
|
|
53
|
-
system.writeBuffer(json)
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
taskGroup(system.mainTask()) {spawn, abort =>
|
|
57
|
-
mutable version = 1
|
|
58
|
-
while {True} {
|
|
59
|
-
version += 1
|
|
60
|
-
let requestPair = parseRequest(system, input)
|
|
61
|
-
input = requestPair.second
|
|
62
|
-
let request = requestPair.first
|
|
63
|
-
let message = parseRequestMessage(request.object)
|
|
64
|
-
logDirectory.each {logMessage(_, "request", request.object, message.method)}
|
|
65
|
-
spawn(message.id) {_ =>
|
|
66
|
-
if(message.method == "$/cancelRequest") {
|
|
67
|
-
abort(Some(parseMessageId(message.parameters.grab("id"))))
|
|
68
|
-
}
|
|
69
|
-
try {
|
|
70
|
-
handleRequestMessage(system, handler, message, version).each {body =>
|
|
71
|
-
responseChannel.write(Pair(body, message.method))
|
|
72
|
-
}
|
|
73
|
-
} catchAny {error =>
|
|
74
|
-
let problem = if(error.name() == "AbortError") {
|
|
75
|
-
Error(-32800, "Request cancelled")
|
|
76
|
-
} else {
|
|
77
|
-
Log.trace("ERROR: " + error.message())
|
|
78
|
-
Log.trace(error.stack())
|
|
79
|
-
Error(-32603, "Internal error")
|
|
80
|
-
}
|
|
81
|
-
message.id.each {id =>
|
|
82
|
-
let body = makeResponseMessage(id, problem)
|
|
83
|
-
responseChannel.write(Pair(body, message.method))
|
|
84
|
-
}
|
|
85
|
-
} grab()
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
} catch {| BadRequestException(reason), error =>
|
|
90
|
-
Log.trace(reason)
|
|
91
|
-
} grab()
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
taskGroup[K: Order, R](
|
|
95
|
-
parentTask: Task,
|
|
96
|
-
groupBody: ((K, Task => Unit) => Task, K => Unit) => R
|
|
97
|
-
): R {
|
|
98
|
-
let lock = parentTask.lock()
|
|
99
|
-
mutable tasks = [].toMap()
|
|
100
|
-
function spawn(key: K, body: Task => Unit): Task {
|
|
101
|
-
let body2 = {task =>
|
|
102
|
-
lock.do(reentrant = True) {tasks = tasks.add(key, task)}
|
|
103
|
-
try {
|
|
104
|
-
body(task)
|
|
105
|
-
} finally {
|
|
106
|
-
lock.do(reentrant = True) {tasks = tasks.remove(key)}
|
|
107
|
-
} grab()
|
|
108
|
-
}
|
|
109
|
-
parentTask.spawn(body2)
|
|
110
|
-
}
|
|
111
|
-
function abort(key: K): Unit {
|
|
112
|
-
lock.do(reentrant = True) {tasks.get(key).each {_.abort()}}
|
|
113
|
-
}
|
|
114
|
-
groupBody(spawn, abort)
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
parseRequest(system: NodeSystem, input: Stream[Buffer]): Pair[Request, Stream[Buffer]] {
|
|
118
|
-
let headersPair = parseRequestHeaders(input)
|
|
119
|
-
let headers = headersPair.first
|
|
120
|
-
let contentLength = headers
|
|
121
|
-
.get("content-length").else {throw(BadRequestException("'content-length' header is missing"))}
|
|
122
|
-
.trim().getInt().else {throw(BadRequestException("Value for 'content-length' is not an integer"))}
|
|
123
|
-
let bodyPair = parseRequestBody(contentLength, headersPair.second)
|
|
124
|
-
bodyPair.mapFirst {body => Request(headers, body)}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
parseRequestHeaders(input: Stream[Buffer]): Pair[Map[String, String], Stream[Buffer]] {
|
|
128
|
-
let buffers = Array.new()
|
|
129
|
-
mutable buffer = input.next().else {
|
|
130
|
-
throw(BadRequestException("End of input while parsing request headers"))
|
|
131
|
-
}
|
|
132
|
-
mutable offset = 0
|
|
133
|
-
mutable droppingInitialNewlines = True
|
|
134
|
-
mutable lastWasNewline = False
|
|
135
|
-
mutable done = False
|
|
136
|
-
while {!done} {
|
|
137
|
-
if(offset == buffer.size()) {
|
|
138
|
-
offset = 0
|
|
139
|
-
buffers.push(buffer)
|
|
140
|
-
buffer = input.next().else {
|
|
141
|
-
throw(BadRequestException("End of input while parsing request headers"))
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
let byte = buffer.grabUint8(offset)
|
|
145
|
-
if(droppingInitialNewlines && (byte == '\n'.codeUnit || byte == '\r'.codeUnit)) {
|
|
146
|
-
// Skip
|
|
147
|
-
} elseIf {byte == '\n'.codeUnit} {
|
|
148
|
-
if(lastWasNewline) {
|
|
149
|
-
done = True
|
|
150
|
-
} else {
|
|
151
|
-
lastWasNewline = True
|
|
152
|
-
}
|
|
153
|
-
} elseIf {byte != '\r'.codeUnit && lastWasNewline} {
|
|
154
|
-
lastWasNewline = False
|
|
155
|
-
} else {
|
|
156
|
-
droppingInitialNewlines = False
|
|
157
|
-
}
|
|
158
|
-
offset += 1
|
|
159
|
-
}
|
|
160
|
-
buffers.push(buffer.view(0, offset))
|
|
161
|
-
let headers = Buffer.fromBufferList(buffers.drain()).toString()
|
|
162
|
-
let map = headers.lines().pairs().filter {_.second.size() != 0}.map {| Pair(i, line) =>
|
|
163
|
-
line.splitFirst(':').else {
|
|
164
|
-
throw(BadRequestException("Invalid header at line " + i + " '" + line + "'"))
|
|
165
|
-
}.mapFirst {_.lower()} //.mapSecond {_.trim()}
|
|
166
|
-
}.toMap()
|
|
167
|
-
Pair(map, [buffer.view(offset, buffer.size())].toStream().addAll(input))
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
parseRequestBody(contentLength: Int, input: Stream[Buffer]): Pair[Json, Stream[Buffer]] {
|
|
171
|
-
let bodyPair = try {
|
|
172
|
-
input.readBytes(contentLength) // Should Stream.readBytes return an option?
|
|
173
|
-
} catchAny {error =>
|
|
174
|
-
throw(BadRequestException("End of input while parsing request body"))
|
|
175
|
-
} grab()
|
|
176
|
-
let body = Buffer.fromBufferList(bodyPair.first).toString()
|
|
177
|
-
let json = Json.read(body).else {
|
|
178
|
-
throw(BadRequestException("Invalid JSON in request body: " + body))
|
|
179
|
-
}
|
|
180
|
-
Pair(json, bodyPair.second)
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
parseRequestMessage(object: Json): RequestMessage {
|
|
184
|
-
let id = object.getField("id").map {parseMessageId(_)}
|
|
185
|
-
|
|
186
|
-
let method = object.getField("method").{
|
|
187
|
-
| None => throw(BadRequestException("Bad JSON-RPC, missing method"))
|
|
188
|
-
| Some(m) {!m.isString()} => throw(BadRequestException("Bad JSON-RPC method, string expected"))
|
|
189
|
-
| Some(m) => m.grabString()
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
let parameters = object.getField("params").{
|
|
193
|
-
| None => [].toMap()
|
|
194
|
-
| Some(o) {!o.isObject()} => throw(BadRequestException("Bad JSON-RPC params, object expected"))
|
|
195
|
-
| Some(o) => o.grabMap()
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
RequestMessage(id, method, parameters)
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
parseMessageId(id: Json): MessageId {
|
|
202
|
-
if(id.isInt()) {
|
|
203
|
-
MessageIdInt(id.grabInt())
|
|
204
|
-
} elseIf {id.isString()} {
|
|
205
|
-
MessageIdString(id.grabString())
|
|
206
|
-
} else {
|
|
207
|
-
throw(BadRequestException("Bad JSON-RPC id, int or string expected"))
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
handleRequestMessage(system: NodeSystem, handler: Handler, message: RequestMessage, version: Int): List[Json] {
|
|
212
|
-
try {
|
|
213
|
-
message.id.{
|
|
214
|
-
| None {message.method == "$/cancelRequest"} => // TODO: Needs to read ahead to cancel anything
|
|
215
|
-
handler.cancelledRequests = handler.cancelledRequests.add(
|
|
216
|
-
parseMessageId(message.parameters.grab("id"))
|
|
217
|
-
)
|
|
218
|
-
[]
|
|
219
|
-
| None =>
|
|
220
|
-
let notification = handler.handleNotification(system, message.method, message.parameters, version)
|
|
221
|
-
notification.map {| Pair(method, params) => makeNotificationMessage(method, params)}
|
|
222
|
-
| Some(id) {handler.cancelledRequests.contains(id)} =>
|
|
223
|
-
handler.cancelledRequests = handler.cancelledRequests.remove(id)
|
|
224
|
-
[makeResponseMessage(id, Error(-32800, "Request cancelled"))]
|
|
225
|
-
| Some(id) =>
|
|
226
|
-
let result = handler.handleRequest(system, message.method, message.parameters, version)
|
|
227
|
-
[makeResponseMessage(id, result)]
|
|
228
|
-
}
|
|
229
|
-
} catchAny {error =>
|
|
230
|
-
//system.files().writeText("lsp-failure.txt", error.name() + ": " + error.message())
|
|
231
|
-
error.rethrow()
|
|
232
|
-
} grab()
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
makeResponseMessage(id: MessageId, result: ResultOrError): Json {
|
|
236
|
-
mutable o = Json.object().with("jsonrpc", "2.0")
|
|
237
|
-
id.{
|
|
238
|
-
| MessageIdInt(id) => o = o.with("id", id)
|
|
239
|
-
| MessageIdString(id) => o = o.with("id", id)
|
|
240
|
-
}
|
|
241
|
-
result.{
|
|
242
|
-
| Result(result) =>
|
|
243
|
-
o = o.with("result", Json.read(result).grab())
|
|
244
|
-
| Error(code, message) =>
|
|
245
|
-
let e = Json.object()
|
|
246
|
-
.with("code", code)
|
|
247
|
-
.with("message", message)
|
|
248
|
-
o = o.with("error", e)
|
|
249
|
-
}
|
|
250
|
-
o
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
makeNotificationMessage(method: String, params: Json): Json {
|
|
254
|
-
Json.object()
|
|
255
|
-
.with("jsonrpc", "2.0")
|
|
256
|
-
.with("method", method)
|
|
257
|
-
.with("params", params)
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
logMessage(directory: Path, messageType: String, body: Json, method: String) {
|
|
261
|
-
let id = if(method == "$/cancelRequest") {
|
|
262
|
-
Some(body.field("params").field("id").grabInt())
|
|
263
|
-
} else {
|
|
264
|
-
body.field("id").getInt()
|
|
265
|
-
}
|
|
266
|
-
let number = directory.entries().toList().size()
|
|
267
|
-
let idName = id.map {_ + ""}.else {"_"}
|
|
268
|
-
let methodName = method.replace("$", "notification").replace("/", "_")
|
|
269
|
-
let fileName = number + "-id-" + idName + "-" + messageType + "-" + methodName + ".json"
|
|
270
|
-
let path = directory.slash(fileName)
|
|
271
|
-
path.writeText(body.write(Some(" ")))
|
|
272
|
-
}
|
|
1
|
+
import ModuleCache from ff:compiler
|
|
2
|
+
import DependencyLock from ff:compiler
|
|
3
|
+
import Handler
|
|
4
|
+
|
|
5
|
+
class LanguageServer(
|
|
6
|
+
mutable openFiles: Map[String, String]
|
|
7
|
+
)
|
|
8
|
+
|
|
9
|
+
data BadRequestException(reason: String)
|
|
10
|
+
|
|
11
|
+
data Request(
|
|
12
|
+
headers: Map[String, String]
|
|
13
|
+
object: Json
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
data RequestMessage(
|
|
17
|
+
id: Option[MessageId] // None when the request is a Notification Message
|
|
18
|
+
method: String
|
|
19
|
+
parameters: Map[String, Json]
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
logMessages = False
|
|
23
|
+
|
|
24
|
+
main(system: NodeSystem) {
|
|
25
|
+
let logDirectory = if(logMessages) {
|
|
26
|
+
let directory = system.path(".").slash("messages")
|
|
27
|
+
if(directory.isDirectory()) {directory.delete()}
|
|
28
|
+
directory.createDirectory()
|
|
29
|
+
directory
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
let fireflyPath = system.path(".")
|
|
33
|
+
let cache = ModuleCache.new(0)
|
|
34
|
+
let handler = Handler(
|
|
35
|
+
fireflyPath = fireflyPath
|
|
36
|
+
rootPath = None
|
|
37
|
+
virtualFiles = Map.new()
|
|
38
|
+
cancelledRequests = [].toSet()
|
|
39
|
+
responseCache = Map.new()
|
|
40
|
+
fileSymbolsCache = Map.new()
|
|
41
|
+
moduleCache = cache
|
|
42
|
+
dependencyLock = DependencyLock.new(system.mainTask())
|
|
43
|
+
)
|
|
44
|
+
try {
|
|
45
|
+
mutable input = system.readStream()
|
|
46
|
+
let responseChannel: Channel[Pair[Json, String]] = system.mainTask().channel()
|
|
47
|
+
system.mainTask().spawn {_ =>
|
|
48
|
+
while {True} {
|
|
49
|
+
let body = responseChannel.read()
|
|
50
|
+
let json = body.first.write(Some(" ")).toBuffer()
|
|
51
|
+
logDirectory.each {logMessage(_, "response", body.first, body.second)}
|
|
52
|
+
system.writeText("Content-Length: " + json.size() + "\r\n\r\n")
|
|
53
|
+
system.writeBuffer(json)
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
taskGroup(system.mainTask()) {spawn, abort =>
|
|
57
|
+
mutable version = 1
|
|
58
|
+
while {True} {
|
|
59
|
+
version += 1
|
|
60
|
+
let requestPair = parseRequest(system, input)
|
|
61
|
+
input = requestPair.second
|
|
62
|
+
let request = requestPair.first
|
|
63
|
+
let message = parseRequestMessage(request.object)
|
|
64
|
+
logDirectory.each {logMessage(_, "request", request.object, message.method)}
|
|
65
|
+
spawn(message.id) {_ =>
|
|
66
|
+
if(message.method == "$/cancelRequest") {
|
|
67
|
+
abort(Some(parseMessageId(message.parameters.grab("id"))))
|
|
68
|
+
}
|
|
69
|
+
try {
|
|
70
|
+
handleRequestMessage(system, handler, message, version).each {body =>
|
|
71
|
+
responseChannel.write(Pair(body, message.method))
|
|
72
|
+
}
|
|
73
|
+
} catchAny {error =>
|
|
74
|
+
let problem = if(error.name() == "AbortError") {
|
|
75
|
+
Error(-32800, "Request cancelled")
|
|
76
|
+
} else {
|
|
77
|
+
Log.trace("ERROR: " + error.message())
|
|
78
|
+
Log.trace(error.stack())
|
|
79
|
+
Error(-32603, "Internal error")
|
|
80
|
+
}
|
|
81
|
+
message.id.each {id =>
|
|
82
|
+
let body = makeResponseMessage(id, problem)
|
|
83
|
+
responseChannel.write(Pair(body, message.method))
|
|
84
|
+
}
|
|
85
|
+
} grab()
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
} catch {| BadRequestException(reason), error =>
|
|
90
|
+
Log.trace(reason)
|
|
91
|
+
} grab()
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
taskGroup[K: Order, R](
|
|
95
|
+
parentTask: Task,
|
|
96
|
+
groupBody: ((K, Task => Unit) => Task, K => Unit) => R
|
|
97
|
+
): R {
|
|
98
|
+
let lock = parentTask.lock()
|
|
99
|
+
mutable tasks = [].toMap()
|
|
100
|
+
function spawn(key: K, body: Task => Unit): Task {
|
|
101
|
+
let body2 = {task =>
|
|
102
|
+
lock.do(reentrant = True) {tasks = tasks.add(key, task)}
|
|
103
|
+
try {
|
|
104
|
+
body(task)
|
|
105
|
+
} finally {
|
|
106
|
+
lock.do(reentrant = True) {tasks = tasks.remove(key)}
|
|
107
|
+
} grab()
|
|
108
|
+
}
|
|
109
|
+
parentTask.spawn(body2)
|
|
110
|
+
}
|
|
111
|
+
function abort(key: K): Unit {
|
|
112
|
+
lock.do(reentrant = True) {tasks.get(key).each {_.abort()}}
|
|
113
|
+
}
|
|
114
|
+
groupBody(spawn, abort)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
parseRequest(system: NodeSystem, input: Stream[Buffer]): Pair[Request, Stream[Buffer]] {
|
|
118
|
+
let headersPair = parseRequestHeaders(input)
|
|
119
|
+
let headers = headersPair.first
|
|
120
|
+
let contentLength = headers
|
|
121
|
+
.get("content-length").else {throw(BadRequestException("'content-length' header is missing"))}
|
|
122
|
+
.trim().getInt().else {throw(BadRequestException("Value for 'content-length' is not an integer"))}
|
|
123
|
+
let bodyPair = parseRequestBody(contentLength, headersPair.second)
|
|
124
|
+
bodyPair.mapFirst {body => Request(headers, body)}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
parseRequestHeaders(input: Stream[Buffer]): Pair[Map[String, String], Stream[Buffer]] {
|
|
128
|
+
let buffers = Array.new()
|
|
129
|
+
mutable buffer = input.next().else {
|
|
130
|
+
throw(BadRequestException("End of input while parsing request headers"))
|
|
131
|
+
}
|
|
132
|
+
mutable offset = 0
|
|
133
|
+
mutable droppingInitialNewlines = True
|
|
134
|
+
mutable lastWasNewline = False
|
|
135
|
+
mutable done = False
|
|
136
|
+
while {!done} {
|
|
137
|
+
if(offset == buffer.size()) {
|
|
138
|
+
offset = 0
|
|
139
|
+
buffers.push(buffer)
|
|
140
|
+
buffer = input.next().else {
|
|
141
|
+
throw(BadRequestException("End of input while parsing request headers"))
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
let byte = buffer.grabUint8(offset)
|
|
145
|
+
if(droppingInitialNewlines && (byte == '\n'.codeUnit || byte == '\r'.codeUnit)) {
|
|
146
|
+
// Skip
|
|
147
|
+
} elseIf {byte == '\n'.codeUnit} {
|
|
148
|
+
if(lastWasNewline) {
|
|
149
|
+
done = True
|
|
150
|
+
} else {
|
|
151
|
+
lastWasNewline = True
|
|
152
|
+
}
|
|
153
|
+
} elseIf {byte != '\r'.codeUnit && lastWasNewline} {
|
|
154
|
+
lastWasNewline = False
|
|
155
|
+
} else {
|
|
156
|
+
droppingInitialNewlines = False
|
|
157
|
+
}
|
|
158
|
+
offset += 1
|
|
159
|
+
}
|
|
160
|
+
buffers.push(buffer.view(0, offset))
|
|
161
|
+
let headers = Buffer.fromBufferList(buffers.drain()).toString()
|
|
162
|
+
let map = headers.lines().pairs().filter {_.second.size() != 0}.map {| Pair(i, line) =>
|
|
163
|
+
line.splitFirst(':').else {
|
|
164
|
+
throw(BadRequestException("Invalid header at line " + i + " '" + line + "'"))
|
|
165
|
+
}.mapFirst {_.lower()} //.mapSecond {_.trim()}
|
|
166
|
+
}.toMap()
|
|
167
|
+
Pair(map, [buffer.view(offset, buffer.size())].toStream().addAll(input))
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
parseRequestBody(contentLength: Int, input: Stream[Buffer]): Pair[Json, Stream[Buffer]] {
|
|
171
|
+
let bodyPair = try {
|
|
172
|
+
input.readBytes(contentLength) // Should Stream.readBytes return an option?
|
|
173
|
+
} catchAny {error =>
|
|
174
|
+
throw(BadRequestException("End of input while parsing request body"))
|
|
175
|
+
} grab()
|
|
176
|
+
let body = Buffer.fromBufferList(bodyPair.first).toString()
|
|
177
|
+
let json = Json.read(body).else {
|
|
178
|
+
throw(BadRequestException("Invalid JSON in request body: " + body))
|
|
179
|
+
}
|
|
180
|
+
Pair(json, bodyPair.second)
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
parseRequestMessage(object: Json): RequestMessage {
|
|
184
|
+
let id = object.getField("id").map {parseMessageId(_)}
|
|
185
|
+
|
|
186
|
+
let method = object.getField("method").{
|
|
187
|
+
| None => throw(BadRequestException("Bad JSON-RPC, missing method"))
|
|
188
|
+
| Some(m) {!m.isString()} => throw(BadRequestException("Bad JSON-RPC method, string expected"))
|
|
189
|
+
| Some(m) => m.grabString()
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
let parameters = object.getField("params").{
|
|
193
|
+
| None => [].toMap()
|
|
194
|
+
| Some(o) {!o.isObject()} => throw(BadRequestException("Bad JSON-RPC params, object expected"))
|
|
195
|
+
| Some(o) => o.grabMap()
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
RequestMessage(id, method, parameters)
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
parseMessageId(id: Json): MessageId {
|
|
202
|
+
if(id.isInt()) {
|
|
203
|
+
MessageIdInt(id.grabInt())
|
|
204
|
+
} elseIf {id.isString()} {
|
|
205
|
+
MessageIdString(id.grabString())
|
|
206
|
+
} else {
|
|
207
|
+
throw(BadRequestException("Bad JSON-RPC id, int or string expected"))
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
handleRequestMessage(system: NodeSystem, handler: Handler, message: RequestMessage, version: Int): List[Json] {
|
|
212
|
+
try {
|
|
213
|
+
message.id.{
|
|
214
|
+
| None {message.method == "$/cancelRequest"} => // TODO: Needs to read ahead to cancel anything
|
|
215
|
+
handler.cancelledRequests = handler.cancelledRequests.add(
|
|
216
|
+
parseMessageId(message.parameters.grab("id"))
|
|
217
|
+
)
|
|
218
|
+
[]
|
|
219
|
+
| None =>
|
|
220
|
+
let notification = handler.handleNotification(system, message.method, message.parameters, version)
|
|
221
|
+
notification.map {| Pair(method, params) => makeNotificationMessage(method, params)}
|
|
222
|
+
| Some(id) {handler.cancelledRequests.contains(id)} =>
|
|
223
|
+
handler.cancelledRequests = handler.cancelledRequests.remove(id)
|
|
224
|
+
[makeResponseMessage(id, Error(-32800, "Request cancelled"))]
|
|
225
|
+
| Some(id) =>
|
|
226
|
+
let result = handler.handleRequest(system, message.method, message.parameters, version)
|
|
227
|
+
[makeResponseMessage(id, result)]
|
|
228
|
+
}
|
|
229
|
+
} catchAny {error =>
|
|
230
|
+
//system.files().writeText("lsp-failure.txt", error.name() + ": " + error.message())
|
|
231
|
+
error.rethrow()
|
|
232
|
+
} grab()
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
makeResponseMessage(id: MessageId, result: ResultOrError): Json {
|
|
236
|
+
mutable o = Json.object().with("jsonrpc", "2.0")
|
|
237
|
+
id.{
|
|
238
|
+
| MessageIdInt(id) => o = o.with("id", id)
|
|
239
|
+
| MessageIdString(id) => o = o.with("id", id)
|
|
240
|
+
}
|
|
241
|
+
result.{
|
|
242
|
+
| Result(result) =>
|
|
243
|
+
o = o.with("result", Json.read(result).grab())
|
|
244
|
+
| Error(code, message) =>
|
|
245
|
+
let e = Json.object()
|
|
246
|
+
.with("code", code)
|
|
247
|
+
.with("message", message)
|
|
248
|
+
o = o.with("error", e)
|
|
249
|
+
}
|
|
250
|
+
o
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
makeNotificationMessage(method: String, params: Json): Json {
|
|
254
|
+
Json.object()
|
|
255
|
+
.with("jsonrpc", "2.0")
|
|
256
|
+
.with("method", method)
|
|
257
|
+
.with("params", params)
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
logMessage(directory: Path, messageType: String, body: Json, method: String) {
|
|
261
|
+
let id = if(method == "$/cancelRequest") {
|
|
262
|
+
Some(body.field("params").field("id").grabInt())
|
|
263
|
+
} else {
|
|
264
|
+
body.field("id").getInt()
|
|
265
|
+
}
|
|
266
|
+
let number = directory.entries().toList().size()
|
|
267
|
+
let idName = id.map {_ + ""}.else {"_"}
|
|
268
|
+
let methodName = method.replace("$", "notification").replace("/", "_")
|
|
269
|
+
let fileName = number + "-id-" + idName + "-" + messageType + "-" + methodName + ".json"
|
|
270
|
+
let path = directory.slash(fileName)
|
|
271
|
+
path.writeText(body.write(Some(" ")))
|
|
272
|
+
}
|
|
@@ -1,55 +1,55 @@
|
|
|
1
|
-
import LspHook from ff:compiler
|
|
2
|
-
import Unification from ff:compiler
|
|
3
|
-
import Environment from ff:compiler
|
|
4
|
-
import Syntax from ff:compiler
|
|
5
|
-
import Handler
|
|
6
|
-
import CompletionHandler
|
|
7
|
-
|
|
8
|
-
handleSignatureHelp(system: NodeSystem, lspHook: LspHook): Json {
|
|
9
|
-
let signatureAndUnification = lspHook.results().collectFirst {
|
|
10
|
-
| InferLookupHook h =>
|
|
11
|
-
let isMember = h.symbol.value.qualifiedName.contains("_")
|
|
12
|
-
h.instantiated.value.map {i =>
|
|
13
|
-
let signature = if(!isMember) {i.scheme.signature} else {
|
|
14
|
-
i.scheme.signature.Signature(parameters = i.scheme.signature.parameters.dropFirst())
|
|
15
|
-
}
|
|
16
|
-
Pair(signature, h.unification)
|
|
17
|
-
}
|
|
18
|
-
| _ => None
|
|
19
|
-
}
|
|
20
|
-
signatureAndUnification.map {| Pair(s, unification) =>
|
|
21
|
-
let shownTypes = [...s.parameters.map {_.valueType}, s.returnType].map {unification.substitute(_)}
|
|
22
|
-
let realGenerics = s.generics.filter {_ != "Q$"}
|
|
23
|
-
let generics = if(realGenerics.isEmpty()) {""} else {"[" + realGenerics.join(", ") + "]"}
|
|
24
|
-
// TODO only instantiate acording to self. Eg. Some("foo").map {...}
|
|
25
|
-
let parameters = s.parameters.map {p =>
|
|
26
|
-
CompletionHandler.showCompletionParameter("", p.Parameter(valueType = unification.substitute(p.valueType)))
|
|
27
|
-
}
|
|
28
|
-
let label =
|
|
29
|
-
s.name + generics + "(" + parameters.join(", ") + "): " + unification.substitute(s.returnType).show(shownTypes)
|
|
30
|
-
Json.object()
|
|
31
|
-
.with("signatures", [Json.object()
|
|
32
|
-
.with("label", label)
|
|
33
|
-
.with("parameters", [
|
|
34
|
-
...parameters.map {p =>
|
|
35
|
-
Json.object().with("label", p)
|
|
36
|
-
}
|
|
37
|
-
// Fake parameter to trick the client into remembering the call position
|
|
38
|
-
Json.object().with("label", "/* Call " + lspHook.at.show() + " */")
|
|
39
|
-
])
|
|
40
|
-
])
|
|
41
|
-
.with("activeSignature", 0)
|
|
42
|
-
}.else {Json.null()}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
pickActiveParameter(help: Json, argumentIndex: Int, parameterName: Option[String]): Json {
|
|
46
|
-
parameterName.flatMap {name =>
|
|
47
|
-
help.field("signatures").index(0).field("parameters").getArray().flatMap {parameters =>
|
|
48
|
-
parameters.pairs().collectFirst {| Pair(i, p) =>
|
|
49
|
-
if(name == p.field("label").grabString().takeWhile {_.isAsciiLetterOrDigit()}) {
|
|
50
|
-
help.with("activeParameter", i)
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
}.else {help.with("activeParameter", argumentIndex)}
|
|
55
|
-
}
|
|
1
|
+
import LspHook from ff:compiler
|
|
2
|
+
import Unification from ff:compiler
|
|
3
|
+
import Environment from ff:compiler
|
|
4
|
+
import Syntax from ff:compiler
|
|
5
|
+
import Handler
|
|
6
|
+
import CompletionHandler
|
|
7
|
+
|
|
8
|
+
handleSignatureHelp(system: NodeSystem, lspHook: LspHook): Json {
|
|
9
|
+
let signatureAndUnification = lspHook.results().collectFirst {
|
|
10
|
+
| InferLookupHook h =>
|
|
11
|
+
let isMember = h.symbol.value.qualifiedName.contains("_")
|
|
12
|
+
h.instantiated.value.map {i =>
|
|
13
|
+
let signature = if(!isMember) {i.scheme.signature} else {
|
|
14
|
+
i.scheme.signature.Signature(parameters = i.scheme.signature.parameters.dropFirst())
|
|
15
|
+
}
|
|
16
|
+
Pair(signature, h.unification)
|
|
17
|
+
}
|
|
18
|
+
| _ => None
|
|
19
|
+
}
|
|
20
|
+
signatureAndUnification.map {| Pair(s, unification) =>
|
|
21
|
+
let shownTypes = [...s.parameters.map {_.valueType}, s.returnType].map {unification.substitute(_)}
|
|
22
|
+
let realGenerics = s.generics.filter {_ != "Q$"}
|
|
23
|
+
let generics = if(realGenerics.isEmpty()) {""} else {"[" + realGenerics.join(", ") + "]"}
|
|
24
|
+
// TODO only instantiate acording to self. Eg. Some("foo").map {...}
|
|
25
|
+
let parameters = s.parameters.map {p =>
|
|
26
|
+
CompletionHandler.showCompletionParameter("", p.Parameter(valueType = unification.substitute(p.valueType)))
|
|
27
|
+
}
|
|
28
|
+
let label =
|
|
29
|
+
s.name + generics + "(" + parameters.join(", ") + "): " + unification.substitute(s.returnType).show(shownTypes)
|
|
30
|
+
Json.object()
|
|
31
|
+
.with("signatures", [Json.object()
|
|
32
|
+
.with("label", label)
|
|
33
|
+
.with("parameters", [
|
|
34
|
+
...parameters.map {p =>
|
|
35
|
+
Json.object().with("label", p)
|
|
36
|
+
}
|
|
37
|
+
// Fake parameter to trick the client into remembering the call position
|
|
38
|
+
Json.object().with("label", "/* Call " + lspHook.at.show() + " */")
|
|
39
|
+
])
|
|
40
|
+
])
|
|
41
|
+
.with("activeSignature", 0)
|
|
42
|
+
}.else {Json.null()}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
pickActiveParameter(help: Json, argumentIndex: Int, parameterName: Option[String]): Json {
|
|
46
|
+
parameterName.flatMap {name =>
|
|
47
|
+
help.field("signatures").index(0).field("parameters").getArray().flatMap {parameters =>
|
|
48
|
+
parameters.pairs().collectFirst {| Pair(i, p) =>
|
|
49
|
+
if(name == p.field("label").grabString().takeWhile {_.isAsciiLetterOrDigit()}) {
|
|
50
|
+
help.with("activeParameter", i)
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}.else {help.with("activeParameter", argumentIndex)}
|
|
55
|
+
}
|