From e143ce3d0cd6feffd07c50248d9dbe4646d6ccfa Mon Sep 17 00:00:00 2001 From: Eran Ifrah Date: Mon, 15 Jul 2024 10:45:31 +0300 Subject: [PATCH] LSP: don't send the `tokenTypes` in the `InitializeRequest` unless this is the gopls LSP --- CodeLite/LSP/InitializeRequest.cpp | 73 ++++++++++++++------------- CodeLite/LSP/InitializeRequest.h | 5 +- LanguageServer/LSPDetectorManager.cpp | 3 -- Plugin/LSP/LanguageServerProtocol.cpp | 10 +++- docs/docs/plugins/lsp.md | 25 ++------- 5 files changed, 54 insertions(+), 62 deletions(-) diff --git a/CodeLite/LSP/InitializeRequest.cpp b/CodeLite/LSP/InitializeRequest.cpp index ae8f8e8ee6..7ae56ffef8 100644 --- a/CodeLite/LSP/InitializeRequest.cpp +++ b/CodeLite/LSP/InitializeRequest.cpp @@ -2,7 +2,8 @@ #include -LSP::InitializeRequest::InitializeRequest(const wxString& rootUri) +LSP::InitializeRequest::InitializeRequest(bool withTokenTypes, const wxString& rootUri) + : m_withTokenTypes(withTokenTypes) { SetMethod("initialize"); m_processId = ::wxGetProcessId(); @@ -45,41 +46,43 @@ JSONItem LSP::InitializeRequest::ToJSON(const wxString& name) const hoverFormat.arrayAppend("markdown"); hoverFormat.arrayAppend("plaintext"); - auto sematicTokens = textDocumentCapabilities.AddObject("semanticTokens"); - auto tokenTypes = sematicTokens.AddArray("tokenTypes"); - tokenTypes.arrayAppend("type"); - tokenTypes.arrayAppend("class"); - tokenTypes.arrayAppend("enum"); - tokenTypes.arrayAppend("interface"); - tokenTypes.arrayAppend("struct"); - tokenTypes.arrayAppend("typeParameter"); - tokenTypes.arrayAppend("parameter"); - tokenTypes.arrayAppend("variable"); - tokenTypes.arrayAppend("property"); - tokenTypes.arrayAppend("enumMember"); - tokenTypes.arrayAppend("event"); - tokenTypes.arrayAppend("function"); - tokenTypes.arrayAppend("method"); - tokenTypes.arrayAppend("macro"); - tokenTypes.arrayAppend("keyword"); - tokenTypes.arrayAppend("modifier"); - tokenTypes.arrayAppend("comment"); - tokenTypes.arrayAppend("string"); - tokenTypes.arrayAppend("number"); - tokenTypes.arrayAppend("regexp"); - tokenTypes.arrayAppend("operator"); + if (m_withTokenTypes) { + auto sematicTokens = textDocumentCapabilities.AddObject("semanticTokens"); + auto tokenTypes = sematicTokens.AddArray("tokenTypes"); + tokenTypes.arrayAppend("type"); + tokenTypes.arrayAppend("class"); + tokenTypes.arrayAppend("enum"); + tokenTypes.arrayAppend("interface"); + tokenTypes.arrayAppend("struct"); + tokenTypes.arrayAppend("typeParameter"); + tokenTypes.arrayAppend("parameter"); + tokenTypes.arrayAppend("variable"); + tokenTypes.arrayAppend("property"); + tokenTypes.arrayAppend("enumMember"); + tokenTypes.arrayAppend("event"); + tokenTypes.arrayAppend("function"); + tokenTypes.arrayAppend("method"); + tokenTypes.arrayAppend("macro"); + tokenTypes.arrayAppend("keyword"); + tokenTypes.arrayAppend("modifier"); + tokenTypes.arrayAppend("comment"); + tokenTypes.arrayAppend("string"); + tokenTypes.arrayAppend("number"); + tokenTypes.arrayAppend("regexp"); + tokenTypes.arrayAppend("operator"); - auto tokenModifiers = sematicTokens.AddArray("tokenModifiers"); - tokenModifiers.arrayAppend("declaration"); - tokenModifiers.arrayAppend("definition"); - tokenModifiers.arrayAppend("readonly"); - tokenModifiers.arrayAppend("static"); - tokenModifiers.arrayAppend("deprecated"); - tokenModifiers.arrayAppend("abstract"); - tokenModifiers.arrayAppend("async"); - tokenModifiers.arrayAppend("modification"); - tokenModifiers.arrayAppend("documentation"); - tokenModifiers.arrayAppend("defaultLibrary"); + auto tokenModifiers = sematicTokens.AddArray("tokenModifiers"); + tokenModifiers.arrayAppend("declaration"); + tokenModifiers.arrayAppend("definition"); + tokenModifiers.arrayAppend("readonly"); + tokenModifiers.arrayAppend("static"); + tokenModifiers.arrayAppend("deprecated"); + tokenModifiers.arrayAppend("abstract"); + tokenModifiers.arrayAppend("async"); + tokenModifiers.arrayAppend("modification"); + tokenModifiers.arrayAppend("documentation"); + tokenModifiers.arrayAppend("defaultLibrary"); + } return json; } diff --git a/CodeLite/LSP/InitializeRequest.h b/CodeLite/LSP/InitializeRequest.h index 5f9ddd0d5d..0a4d8f4a04 100644 --- a/CodeLite/LSP/InitializeRequest.h +++ b/CodeLite/LSP/InitializeRequest.h @@ -10,9 +10,10 @@ class WXDLLIMPEXP_CL InitializeRequest : public LSP::Request int m_processId = wxNOT_FOUND; wxString m_rootUri; wxString m_initOptions; + bool m_withTokenTypes = false; public: - InitializeRequest(const wxString& rootUri = ""); + InitializeRequest(bool withTokenTypes, const wxString& rootUri = ""); virtual ~InitializeRequest(); InitializeRequest& SetProcessId(int processId) { @@ -32,5 +33,5 @@ class WXDLLIMPEXP_CL InitializeRequest : public LSP::Request void SetInitOptions(const wxString& initOptions) { this->m_initOptions = initOptions; } const wxString& GetInitOptions() const { return m_initOptions; } }; -}; // namespace LSP +}; // namespace LSP #endif // INITIALIZEREQUEST_H diff --git a/LanguageServer/LSPDetectorManager.cpp b/LanguageServer/LSPDetectorManager.cpp index 506f4594fe..ee6f55cf88 100644 --- a/LanguageServer/LSPDetectorManager.cpp +++ b/LanguageServer/LSPDetectorManager.cpp @@ -1,7 +1,5 @@ #include "LSPDetectorManager.hpp" -#include "LanguageServerConfig.h" -#include "LanguageServerEntry.h" #include "detectors/LSPCMakeDetector.hpp" #include "detectors/LSPCTagsdDetector.hpp" #include "detectors/LSPClangdDetector.hpp" @@ -11,7 +9,6 @@ #include "detectors/LSPRustAnalyzerDetector.hpp" #include "detectors/LSPTypeScriptDetector.hpp" #include "environmentconfig.h" -#include "file_logger.h" LSPDetectorManager::LSPDetectorManager() { diff --git a/Plugin/LSP/LanguageServerProtocol.cpp b/Plugin/LSP/LanguageServerProtocol.cpp index b44f89a8bf..85ab338e7b 100644 --- a/Plugin/LSP/LanguageServerProtocol.cpp +++ b/Plugin/LSP/LanguageServerProtocol.cpp @@ -264,7 +264,7 @@ void LanguageServerProtocol::DoClear() m_initializeRequestID = wxNOT_FOUND; m_Queue.Clear(); m_lastCompletionRequestId = wxNOT_FOUND; - // Destory the current connection + // Destroy the current connection m_network->Close(); } @@ -674,7 +674,13 @@ void LanguageServerProtocol::OnNetConnected(clCommandEvent& event) // The process started successfully // Send the 'initialize' request - LSP::InitializeRequest::Ptr_t req = LSP::MessageWithParams::MakeRequest(new LSP::InitializeRequest()); + + // Go lsp requires the client to send the list of tokenTypes (for semanticTokens support) in the + // initialise request + bool withTokenTypesHack = CanHandle(FileExtManager::TypeGo); + LSP::InitializeRequest::Ptr_t req = + LSP::MessageWithParams::MakeRequest(new LSP::InitializeRequest(withTokenTypesHack)); + // some LSPs will crash if we path an empty root folder wxString root_uri = m_rootFolder; if (root_uri.empty()) { diff --git a/docs/docs/plugins/lsp.md b/docs/docs/plugins/lsp.md index f836f423e7..f068ab35db 100644 --- a/docs/docs/plugins/lsp.md +++ b/docs/docs/plugins/lsp.md @@ -109,28 +109,13 @@ select the ones you are interested in and then [configure them in CodeLite][14] TARGET=$(rustup target list|grep installed|cut -d" " -f1) $HOME/.rustup/toolchains/nightly-$TARGET/bin/rust-analyzer``` -=== "rls" - `rls` is yet another rust language server, but less popular than `rust-analzyer` +=== "gopls" + `gopls` (pronounced "Go Please") is the official LSP for `golang` To install it: - - - [Install rust][13] - - Open a terminal and type: - - On `macOS` and `Linux`: - ```bash - rustup update - rustup component add rls rust-analysis rust-src``` - - - On `Windows`: - ```batch - %USERPROFILE%\.cargo\bin\rustup update - %USERPROFILE%\.cargo\bin\rustup component add rls rust-analysis rust-src``` - - Visit the [project home page][8] - - !!! Important - The `rls` language server must be started from the cargo project directory - i.e. the location of the `Cargo.toml` file + ```bash + go install golang.org/x/tools/gopls@latest + ``` ---