From 055648ea19897d38a9ee8a753acff149e98d53b6 Mon Sep 17 00:00:00 2001 From: voylin Date: Fri, 19 Dec 2025 16:05:38 +0900 Subject: [PATCH] Add highlight support for language server Update modules/gdscript/language_server/gdscript_text_document.cpp Co-authored-by: A Thousand Ships <96648715+AThousandShips@users.noreply.github.com> --- .../gdscript_language_protocol.cpp | 1 + .../gdscript_text_document.cpp | 19 +++++++++++++++ .../language_server/gdscript_text_document.h | 1 + modules/gdscript/language_server/godot_lsp.h | 24 ++++++++++++++++++- 4 files changed, 44 insertions(+), 1 deletion(-) diff --git a/modules/gdscript/language_server/gdscript_language_protocol.cpp b/modules/gdscript/language_server/gdscript_language_protocol.cpp index 7662252c620..9e5080a8325 100644 --- a/modules/gdscript/language_server/gdscript_language_protocol.cpp +++ b/modules/gdscript/language_server/gdscript_language_protocol.cpp @@ -557,6 +557,7 @@ GDScriptLanguageProtocol::GDScriptLanguageProtocol() { SET_DOCUMENT_METHOD(didSave); SET_DOCUMENT_METHOD(documentSymbol); + SET_DOCUMENT_METHOD(documentHighlight); SET_DOCUMENT_METHOD(completion); SET_DOCUMENT_METHOD(rename); SET_DOCUMENT_METHOD(prepareRename); diff --git a/modules/gdscript/language_server/gdscript_text_document.cpp b/modules/gdscript/language_server/gdscript_text_document.cpp index ad0ccb338f0..46b15fbaefa 100644 --- a/modules/gdscript/language_server/gdscript_text_document.cpp +++ b/modules/gdscript/language_server/gdscript_text_document.cpp @@ -165,6 +165,25 @@ Array GDScriptTextDocument::documentSymbol(const Dictionary &p_params) { return arr; } +Array GDScriptTextDocument::documentHighlight(const Dictionary &p_params) { + Array arr; + LSP::TextDocumentPositionParams params; + params.load(p_params); + + const LSP::DocumentSymbol *symbol = GDScriptLanguageProtocol::get_singleton()->get_workspace()->resolve_symbol(params); + if (symbol) { + String path = GDScriptLanguageProtocol::get_singleton()->get_workspace()->get_file_path(params.textDocument.uri); + Vector usages = GDScriptLanguageProtocol::get_singleton()->get_workspace()->find_usages_in_file(*symbol, path); + + for (const LSP::Location &usage : usages) { + LSP::DocumentHighlight highlight; + highlight.range = usage.range; + arr.push_back(highlight.to_json()); + } + } + return arr; +} + Array GDScriptTextDocument::completion(const Dictionary &p_params) { Array arr; diff --git a/modules/gdscript/language_server/gdscript_text_document.h b/modules/gdscript/language_server/gdscript_text_document.h index 86cd675bacd..54eecca64bb 100644 --- a/modules/gdscript/language_server/gdscript_text_document.h +++ b/modules/gdscript/language_server/gdscript_text_document.h @@ -62,6 +62,7 @@ public: Variant nativeSymbol(const Dictionary &p_params); Array documentSymbol(const Dictionary &p_params); + Array documentHighlight(const Dictionary &p_params); Array completion(const Dictionary &p_params); Dictionary resolve(const Dictionary &p_params); Dictionary rename(const Dictionary &p_params); diff --git a/modules/gdscript/language_server/godot_lsp.h b/modules/gdscript/language_server/godot_lsp.h index 75470bfe782..31a5bdc1432 100644 --- a/modules/gdscript/language_server/godot_lsp.h +++ b/modules/gdscript/language_server/godot_lsp.h @@ -1761,7 +1761,7 @@ struct ServerCapabilities { /** * The server provides document highlight support. */ - bool documentHighlightProvider = false; + bool documentHighlightProvider = true; /** * The server provides document symbol support. @@ -2118,4 +2118,26 @@ static String marked_documentation(const String &p_bbcode) { } return markdown; } + +/** + * A document highlight is a range inside a text document which deserves + * special attention. Usually a document highlight is visualized by changing + * the background color of its range. + */ +struct DocumentHighlight { + /** + * The range this highlight applies to. + */ + Range range; + + _FORCE_INLINE_ Dictionary to_json() const { + Dictionary dict; + dict["range"] = range.to_json(); + return dict; + } + + _FORCE_INLINE_ void load(const Dictionary &p_params) { + range.load(p_params["range"]); + } +}; } // namespace LSP