]> Sergey Matveev's repositories - vim-lsp.git/commitdiff
Add "runIfSearch" to as make "rootSearch" only have one concern
authorAndreas Louv <andreas@louv.dk>
Sat, 15 Apr 2023 13:47:17 +0000 (15:47 +0200)
committerAndreas Louv <andreas@louv.dk>
Sat, 15 Apr 2023 17:44:11 +0000 (19:44 +0200)
autoload/lsp/lsp.vim
autoload/lsp/lspserver.vim
doc/lsp.txt

index 303e30ce51074be9e7f209413150404ac2fea6dc..84534a0202afc9b76b504e3324afd28982364ab7 100644 (file)
@@ -51,35 +51,18 @@ def LspGetServers(bnr: number, ftype: string): list<dict<any>>
     return []
   endif
 
-  var lspservers = ftypeServerMap[ftype]
-
-  # All the langauge servers with the same score are being used
-  var bestMatches: list<dict<any>> = []
-  var bestScore: number = -2
-
   var bufDir = bnr->bufname()->fnamemodify(':p:h')
 
-  for lspserver in lspservers
-    var score: number = 0
-
-    if !lspserver.rootSearchFiles->empty()
-      # The score is calculated by how deep the workspace root dir is, the
-      # deeper the better.
-      var path = util.FindNearestRootDir(bufDir, lspserver.rootSearchFiles)
-      # subtract one to make servers with a non matching 'rootSearch' rank below
-      # servers without a 'rootSearch'.
-      score = path->strcharlen() - 1
+  return ftypeServerMap[ftype]->filter((key, lspserver) => {
+    if lspserver.runIfSearchFiles->empty()
+      return true
     endif
 
-    if score > bestScore
-      bestMatches = [lspserver]
-      bestScore = score
-    elseif score == bestScore
-      bestMatches->add(lspserver)
-    endif
-  endfor
+    var path = util.FindNearestRootDir(bufDir, lspserver.runIfSearchFiles)
 
-  return bestMatches
+    # Run the server if the path is found
+    return !path->empty()
+  })
 enddef
 
 # Add a LSP server for a filetype
@@ -635,11 +618,16 @@ export def AddServer(serverList: list<dict<any>>)
       server.rootSearch = []
     endif
 
+    if !server->has_key('runIfSearch') || server.runIfSearch->type() != v:t_list
+      server.runIfSearch = []
+    endif
+
     var lspserver: dict<any> = lserver.NewLspServer(server.name, server.path,
                                                    args, server.syncInit,
                                                    initializationOptions,
                                                    server.workspaceConfig,
                                                    server.rootSearch,
+                                                   server.runIfSearch,
                                                    customNotificationHandlers,
                                                    features, server.debug)
 
index 69baa9c2029534842e1f664c50a18a7d0b1cabf4..02e8d9ba1c7f971c06519097c6edd9b23ad33da8 100644 (file)
@@ -1475,6 +1475,7 @@ export def NewLspServer(name_arg: string, path_arg: string, args: list<string>,
                        isSync: bool, initializationOptions: any,
                        workspaceConfig: dict<any>,
                        rootSearchFiles: list<any>,
+                       runIfSearchFiles: list<any>,
                        customNotificationHandlers: dict<func>,
                        features: dict<bool>, debug_arg: bool): dict<any>
   var lspserver: dict<any> = {
@@ -1494,6 +1495,7 @@ export def NewLspServer(name_arg: string, path_arg: string, args: list<string>,
     caps: {},
     requests: {},
     rootSearchFiles: rootSearchFiles,
+    runIfSearchFiles: runIfSearchFiles,
     omniCompletePending: false,
     completionTriggerChars: [],
     signaturePopup: -1,
index 828bfd468c8a1fe6ffe34d63deb1c1df7c12395e..05c4d27f506b91e3c540f6dd1e4e5a1e0484a540 100644 (file)
@@ -265,18 +265,26 @@ To add a language server, the following information is needed:
                        closest to the directory of the current buffer is used
                        as the workspace root.
 
-                       This option is also used to decide which server to run
-                       when multiple servers are defined for a filetype. The
-                       servers that share the longest found workspace root will
-                       be selected as the relevant language servers for a given
-                       file.
-
                        If this parameter is not specified or the files are not
                        found, then the current working directory is used as the
                        workspace root for decendent files, for any other files
                        the parent directory of the file is used.
 
-Aditionally the following configurations can be made:
+                                               *lsp-cfg-runIfSearch*
+       runIfSearch     (Optional) a List of file and directory names used to
+                       determinate if a server should run or not. The directory
+                       names in "runIfSearch" must end in "/" or "\".  Each
+                       file and directory name in "runIfSearch" is searched
+                       upwards in all the parent directories.  Exactly like
+                       |lsp-cfg-rootSearch|.
+
+                       If a file or directory is found then the server will be
+                       started, otherwise it will not.
+
+                       If this parameter is not specified or is an empty list,
+                       then the server will always be started.
+
+Additionally the following configurations can be made:
 
                                        *lsp-cfg-customNotificationHandlers*
        customNotificationHandlers