Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add support for publishDiagnostics #4912

Merged
merged 2 commits into from
Feb 8, 2025
Merged

Conversation

jonmeow
Copy link
Contributor

@jonmeow jonmeow commented Feb 6, 2025

Screenshot 2025-02-06 at 3 02 18 PM

Requires the caching work in #4897

@@ -42,6 +42,10 @@ constexpr DiagnosticKind UntestedDiagnosticKinds[] = {

// This is a little long but is tested in lex/numeric_literal_test.cpp.
DiagnosticKind::TooManyDigits,

// TODO: The LSP doesn't support checking multiple files, figure something
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't understand exactly what this comment meant by checking multiple files -- is it saying that the LSP doesn't support producing diagnostics in files other than the current file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, unsupported. If you look at LSP's Diagnostic, range is a text range, not including a filename. There is no filename on that field; it's implicitly the given file.

In theory, compiling a file could publish diagnostics for the other files it finds diagnostics for. However, that would create complexity trying to merge diagnostics if you had the other file open at the same time -- we wouldn't want to accidentally replace a file's diagnostics with some misc diagnostic produced by a different file.

I think there's a possibility we should just require that messages[0].loc.filename is always the same for a given file when we call CheckUnit, which would eliminate the need for this. In theory, "in import" messages might essentially get there, but I believe we have no enforcement. Any thoughts on making it a strict requirement?

In the meantime, rephrasing to try to make it clearer.

Comment on lines +44 to +49
context_->file_emitter().Emit(
params_.uri.file(), LanguageServerDiagnosticInWrongFile,
message.loc.filename.str(), stream.TakeStr());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this recursing back into this same function to produce the error, or is this diagnostic going somewhere else? If this recurses back here, would it make sense to directly push a diagnostic instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The emitters on context are hooked up to stderr, for diagnosing issues with the language server itself. I think you're mixing this up with the consumer declared inside SetText, which isn't added to context -- it's ephemeral.

Comment on lines +39 to +48
// CHECK:STDOUT: Content-Length: 144{{\r}}
// CHECK:STDOUT: {{\r}}
// CHECK:STDOUT: {
// CHECK:STDOUT: "jsonrpc": "2.0",
// CHECK:STDOUT: "method": "textDocument/publishDiagnostics",
// CHECK:STDOUT: "params": {
// CHECK:STDOUT: "diagnostics": [],
// CHECK:STDOUT: "uri": "file:///test.carbon"
// CHECK:STDOUT: }
// CHECK:STDOUT: }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to skip the publishDiagnostics step if there are none? Is it important that we do publish after a /didChange in the case where we used to have diagnostics and don't any more?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, need to publish to clear. Added a TODO to consider caching diagnostics and only publishing on change.

@jonmeow jonmeow added this pull request to the merge queue Feb 7, 2025
Merged via the queue into carbon-language:trunk with commit f89985d Feb 8, 2025
8 checks passed
@jonmeow jonmeow deleted the lsp-diag branch February 8, 2025 00:31
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants