Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

LSP: Don't respond to non-GDScript files #103186

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

Na-r
Copy link

@Na-r Na-r commented Feb 22, 2025

Background

When an LSP client saves, Godot's LSP server receives a textDocument/didSave notification. Depending on how the LSP client is implemented, it may send this notification for any file, not necessarily only GDScript files. Godot's LSP server wouldn't do any checking on the file type received from this notification, so it would send back syntax errors for whatever file was saved.

According to the LSP spec, unless the server has previously sent TextDocumentSaveRegistrationOptions, the LSP client should send the textDocument/didSave notification for all files saved. See: the LSP spec and zed-industries/zed/pull/19183. (Can someone more familiar with LSP spec confirm this?)

Godot's LSP mostly uses static registrations during initialization for its LSP server capabilities, but TextDocumentSaveRegistrationOptions is only available as a dynamic registration. This makes implementing the fix that way a bit uglier than just checking file types when receiving notifications.

Fix

We will now check for a file being a valid GDScript file, via the language ID or file extension, before processing it any further. While this fix was specifically for the textDocument/didSave notification, I added checks for other related notifications as well.

@Na-r Na-r requested a review from a team as a code owner February 22, 2025 19:25
@@ -133,6 +147,11 @@ lsp::TextDocumentItem GDScriptTextDocument::load_document_item(const Variant &p_
return doc;
}

bool GDScriptTextDocument::is_valid_gd_file(const lsp::TextDocumentItem &p_doc) {
// LanguageId isn't always set, but check it for good practice
return p_doc.languageId == "gdscript" || p_doc.uri.ends_with(".gd");
Copy link
Contributor

Choose a reason for hiding this comment

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

this could need a change after #97657 is merged, unles p_doc.languageId == "gdscript"is good enough

Copy link
Author

Choose a reason for hiding this comment

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

This would definitely need a change. Some of the notifications (including didSave) don't return anything besides the URI and text content. Should a check against .gdt be put in now to get ahead of it? There's also a similar update that would be needed at here.

Copy link
Contributor

Choose a reason for hiding this comment

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

well, for now both prs are open, i just wanted to reference this PR in the other one 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

When GDScript LSP is active, it reports issues on other filetypes (e.g. .rs files)
3 participants