Skip to content

Commit 7020f4e

Browse files
committed
Nicer formatting error boxes
1 parent ea27c81 commit 7020f4e

File tree

1 file changed

+45
-39
lines changed

1 file changed

+45
-39
lines changed

server/src/server.ts

+45-39
Original file line numberDiff line numberDiff line change
@@ -244,42 +244,61 @@ process.on('message', (a: (m.RequestMessage | m.NotificationMessage)) => {
244244
process.send!(response);
245245
}
246246
} else if (aa.method === p.DocumentFormattingRequest.method) {
247+
// technically, a formatting failure should reply with the error. Sadly
248+
// the LSP alert box for these error replies suck (e.g. don't actually
249+
// display the message). In order to signal the client to display a proper
250+
// alert box (sometime with actionable buttons), we need to first send
251+
// back a fake success message (because reach request mandates a
252+
// response), then right away send a server notification to display a
253+
// nicer alert. Ugh.
254+
let fakeSuccessResponse: m.ResponseMessage = {
255+
jsonrpc: c.jsonrpcVersion,
256+
id: aa.id,
257+
result: [],
258+
}
259+
247260
let params = (aa.params as p.DocumentFormattingParams)
248261
let filePath = fileURLToPath(params.textDocument.uri)
249262
let extension = path.extname(params.textDocument.uri);
250263
if (extension !== c.resExt && extension !== c.resiExt) {
251-
let response: m.ResponseMessage = {
264+
let params: p.ShowMessageParams = {
265+
type: p.MessageType.Error,
266+
message: `Not a ${c.resExt} or ${c.resiExt} file. Cannot format it.`
267+
}
268+
let response: m.NotificationMessage = {
252269
jsonrpc: c.jsonrpcVersion,
253-
id: aa.id,
254-
error: {
255-
code: m.ErrorCodes.InvalidRequest,
256-
message: `Not a ${c.resExt} or ${c.resiExt} file.`
257-
}
258-
};
270+
method: 'window/showMessage',
271+
params: params,
272+
}
273+
process.send!(fakeSuccessResponse);
259274
process.send!(response);
260275
} else {
261276
let projectRootPath = utils.findProjectRootOfFile(filePath)
262277
if (projectRootPath == null) {
263-
let response: m.ResponseMessage = {
278+
let params: p.ShowMessageParams = {
279+
type: p.MessageType.Error,
280+
message: `Cannot find a nearby ${c.bsconfigPartialPath}. It's needed for determining the project's root.`,
281+
}
282+
let response: m.NotificationMessage = {
264283
jsonrpc: c.jsonrpcVersion,
265-
id: aa.id,
266-
error: {
267-
code: m.ErrorCodes.InvalidRequest,
268-
message: `Cannot find a nearby ${c.bsconfigPartialPath}. It's needed for determining the project's root.`,
269-
}
270-
};
284+
method: 'window/showMessage',
285+
params: params,
286+
}
287+
process.send!(fakeSuccessResponse);
271288
process.send!(response);
272289
} else {
273290
let bscPath = path.join(projectRootPath, c.bscPartialPath)
274291
if (!fs.existsSync(bscPath)) {
275-
let response: m.ResponseMessage = {
292+
let params: p.ShowMessageParams = {
293+
type: p.MessageType.Error,
294+
message: `Cannot find a nearby ${c.bscPartialPath}. It's needed for formatting.`,
295+
}
296+
let response: m.NotificationMessage = {
276297
jsonrpc: c.jsonrpcVersion,
277-
id: aa.id,
278-
error: {
279-
code: m.ErrorCodes.InvalidRequest,
280-
message: `Cannot find a nearby ${c.bscPartialPath}. It's needed for formatting.`,
281-
}
282-
};
298+
method: 'window/showMessage',
299+
params: params,
300+
}
301+
process.send!(fakeSuccessResponse);
283302
process.send!(response);
284303
} else {
285304
// code will always be defined here, even though technically it can be undefined
@@ -304,24 +323,11 @@ process.on('message', (a: (m.RequestMessage | m.NotificationMessage)) => {
304323
};
305324
process.send!(response);
306325
} else {
307-
let response: m.ResponseMessage = {
308-
jsonrpc: c.jsonrpcVersion,
309-
id: aa.id,
310-
result: [],
311-
// technically a formatting failure should return the error but
312-
// since this is LSP... the idiom seems to be to silently return
313-
// nothing (to avoid an alert window each time on bad formatting)
314-
// while sending a diagnostic about the error afterward. We won't
315-
// send an extra diagnostic because the .compiler.log watcher
316-
// should have reported th won't send an extra diagnostic because
317-
// theiler.log watcher should have reported them.
318-
319-
// error: {
320-
// code: m.ErrorCodes.ParseError,
321-
// message: formattedResult.error,
322-
// }
323-
};
324-
process.send!(response);
326+
// let the diagnostics logic display the updated syntax errors,
327+
// from the build.
328+
// Again, not sending the actual errors. See fakeSuccessResponse
329+
// above for explanation
330+
process.send!(fakeSuccessResponse);
325331
}
326332
}
327333
}

0 commit comments

Comments
 (0)