From: Andreas Louv Date: Sat, 15 Apr 2023 13:47:17 +0000 (+0200) Subject: Add "runIfSearch" to as make "rootSearch" only have one concern X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=876b6f53c1836808f750c16995a0e4a0d9d0fe3c;p=vim-lsp.git Add "runIfSearch" to as make "rootSearch" only have one concern --- diff --git a/autoload/lsp/lsp.vim b/autoload/lsp/lsp.vim index 303e30c..84534a0 100644 --- a/autoload/lsp/lsp.vim +++ b/autoload/lsp/lsp.vim @@ -51,35 +51,18 @@ def LspGetServers(bnr: number, ftype: string): list> return [] endif - var lspservers = ftypeServerMap[ftype] - - # All the langauge servers with the same score are being used - var bestMatches: list> = [] - 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>) server.rootSearch = [] endif + if !server->has_key('runIfSearch') || server.runIfSearch->type() != v:t_list + server.runIfSearch = [] + endif + var lspserver: dict = lserver.NewLspServer(server.name, server.path, args, server.syncInit, initializationOptions, server.workspaceConfig, server.rootSearch, + server.runIfSearch, customNotificationHandlers, features, server.debug) diff --git a/autoload/lsp/lspserver.vim b/autoload/lsp/lspserver.vim index 69baa9c..02e8d9b 100644 --- a/autoload/lsp/lspserver.vim +++ b/autoload/lsp/lspserver.vim @@ -1475,6 +1475,7 @@ export def NewLspServer(name_arg: string, path_arg: string, args: list, isSync: bool, initializationOptions: any, workspaceConfig: dict, rootSearchFiles: list, + runIfSearchFiles: list, customNotificationHandlers: dict, features: dict, debug_arg: bool): dict var lspserver: dict = { @@ -1494,6 +1495,7 @@ export def NewLspServer(name_arg: string, path_arg: string, args: list, caps: {}, requests: {}, rootSearchFiles: rootSearchFiles, + runIfSearchFiles: runIfSearchFiles, omniCompletePending: false, completionTriggerChars: [], signaturePopup: -1, diff --git a/doc/lsp.txt b/doc/lsp.txt index 828bfd4..05c4d27 100644 --- a/doc/lsp.txt +++ b/doc/lsp.txt @@ -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