[clang-tools-extra] 6c5f17e - [clangd] Delay binding LSP methods until initialize. NFC

Sam McCall via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 15 10:33:47 PST 2021


Author: Sam McCall
Date: 2021-02-15T19:33:40+01:00
New Revision: 6c5f17e701ff586d69a43b3cfc1e25314b84892d

URL: https://github.com/llvm/llvm-project/commit/6c5f17e701ff586d69a43b3cfc1e25314b84892d
DIFF: https://github.com/llvm/llvm-project/commit/6c5f17e701ff586d69a43b3cfc1e25314b84892d.diff

LOG: [clangd] Delay binding LSP methods until initialize. NFC

This is NFC because the MessageHandler refused to dispatch to them until the
server is initialized anyway.

This is a more natural time to bind them - it's when they become callable, and
it's when client capabalities are available and server ones can be set.

One module-lifecycle function will be responsible for all three.

Differential Revision: https://reviews.llvm.org/D96608

Added: 
    

Modified: 
    clang-tools-extra/clangd/ClangdLSPServer.cpp
    clang-tools-extra/clangd/ClangdLSPServer.h

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/ClangdLSPServer.cpp b/clang-tools-extra/clangd/ClangdLSPServer.cpp
index 0f5fe7c3528c..c30890f14787 100644
--- a/clang-tools-extra/clangd/ClangdLSPServer.cpp
+++ b/clang-tools-extra/clangd/ClangdLSPServer.cpp
@@ -165,19 +165,17 @@ class ClangdLSPServer::MessageHandler : public Transport::MessageHandler {
     log("<-- {0}", Method);
     if (Method == "exit")
       return false;
-    if (!Server.Server) {
+    auto Handler = Server.Handlers.NotificationHandlers.find(Method);
+    if (Handler != Server.Handlers.NotificationHandlers.end()) {
+      Handler->second(std::move(Params));
+      Server.maybeExportMemoryProfile();
+      Server.maybeCleanupMemory();
+    } else if (!Server.Server) {
       elog("Notification {0} before initialization", Method);
     } else if (Method == "$/cancelRequest") {
       onCancel(std::move(Params));
     } else {
-      auto Handler = Server.Handlers.NotificationHandlers.find(Method);
-      if (Handler != Server.Handlers.NotificationHandlers.end()) {
-        Handler->second(std::move(Params));
-        Server.maybeExportMemoryProfile();
-        Server.maybeCleanupMemory();
-      } else {
-        log("unhandled notification {0}", Method);
-      }
+      log("unhandled notification {0}", Method);
     }
     return true;
   }
@@ -191,18 +189,16 @@ class ClangdLSPServer::MessageHandler : public Transport::MessageHandler {
     SPAN_ATTACH(Tracer, "Params", Params);
     ReplyOnce Reply(ID, Method, &Server, Tracer.Args);
     log("<-- {0}({1})", Method, ID);
-    if (!Server.Server && Method != "initialize") {
+    auto Handler = Server.Handlers.MethodHandlers.find(Method);
+    if (Handler != Server.Handlers.MethodHandlers.end()) {
+      Handler->second(std::move(Params), std::move(Reply));
+    } else if (!Server.Server) {
       elog("Call {0} before initialization.", Method);
       Reply(llvm::make_error<LSPError>("server not initialized",
                                        ErrorCode::ServerNotInitialized));
     } else {
-      auto Handler = Server.Handlers.MethodHandlers.find(Method);
-      if (Handler != Server.Handlers.MethodHandlers.end()) {
-        Handler->second(std::move(Params), std::move(Reply));
-      } else {
-        Reply(llvm::make_error<LSPError>("method not found",
-                                         ErrorCode::MethodNotFound));
-      }
+      Reply(llvm::make_error<LSPError>("method not found",
+                                       ErrorCode::MethodNotFound));
     }
     return true;
   }
@@ -583,6 +579,7 @@ void ClangdLSPServer::onInitialize(const InitializeParams &Params,
 
   {
     LSPBinder Binder(Handlers);
+    bindMethods(Binder);
     if (Opts.Modules)
       for (auto &Mod : *Opts.Modules)
         Mod.initializeLSP(Binder, Params.capabilities, ServerCaps);
@@ -1457,10 +1454,12 @@ ClangdLSPServer::ClangdLSPServer(class Transport &Transp,
     this->Opts.ContextProvider = ClangdServer::createConfiguredContextProvider(
         Opts.ConfigProvider, this);
   }
-
-  // clang-format off
   LSPBinder Bind(this->Handlers);
   Bind.method("initialize", this, &ClangdLSPServer::onInitialize);
+}
+
+void ClangdLSPServer::bindMethods(LSPBinder &Bind) {
+  // clang-format off
   Bind.notification("initialized", this, &ClangdLSPServer::onInitialized);
   Bind.method("shutdown", this, &ClangdLSPServer::onShutdown);
   Bind.method("sync", this, &ClangdLSPServer::onSync);

diff  --git a/clang-tools-extra/clangd/ClangdLSPServer.h b/clang-tools-extra/clangd/ClangdLSPServer.h
index 9207fe7e034f..659d7668591b 100644
--- a/clang-tools-extra/clangd/ClangdLSPServer.h
+++ b/clang-tools-extra/clangd/ClangdLSPServer.h
@@ -168,6 +168,7 @@ class ClangdLSPServer : private ClangdServer::Callbacks {
   void applyEdit(WorkspaceEdit WE, llvm::json::Value Success,
                  Callback<llvm::json::Value> Reply);
 
+  void bindMethods(LSPBinder &);
   std::vector<Fix> getFixes(StringRef File, const clangd::Diagnostic &D);
 
   /// Checks if completion request should be ignored. We need this due to the


        


More information about the cfe-commits mailing list