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

Sam McCall via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 15 10:32:40 PST 2021


sammccall updated this revision to Diff 323793.
sammccall marked an inline comment as done.
sammccall added a comment.

address comments & rebase


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D96608/new/

https://reviews.llvm.org/D96608

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


Index: clang-tools-extra/clangd/ClangdLSPServer.h
===================================================================
--- clang-tools-extra/clangd/ClangdLSPServer.h
+++ clang-tools-extra/clangd/ClangdLSPServer.h
@@ -168,6 +168,7 @@
   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
Index: clang-tools-extra/clangd/ClangdLSPServer.cpp
===================================================================
--- clang-tools-extra/clangd/ClangdLSPServer.cpp
+++ clang-tools-extra/clangd/ClangdLSPServer.cpp
@@ -165,19 +165,17 @@
     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 @@
     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 @@
 
   {
     LSPBinder Binder(Handlers);
+    bindMethods(Binder);
     if (Opts.Modules)
       for (auto &Mod : *Opts.Modules)
         Mod.initializeLSP(Binder, Params.capabilities, ServerCaps);
@@ -1457,10 +1454,12 @@
     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);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D96608.323793.patch
Type: text/x-patch
Size: 3685 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210215/7349fbcf/attachment.bin>


More information about the cfe-commits mailing list