From 53081e234fbf8da9790a663b2707063f0383e347 Mon Sep 17 00:00:00 2001 From: Pavol Juhas Date: Fri, 14 Oct 2022 14:44:54 -0700 Subject: [PATCH 1/3] Add per-file LSP activation rules allowpaths, blockpaths `allowpaths` and `blockpaths` are lists of regular expressions matched with the full path of the active file. LSP is enabled if it matches any pattern in the `allowpaths` list or when `allowpaths` is not present in the server_info dictionary. LSP is disabled if any of the `blockpaths` patterns matches the current buffer. The `blockpaths` rule has priority when both `allowpaths` and `blockpaths` match. The `allowpaths` and `blockpaths` are evaluated only if the LSP is enabled by the `allowlist`, `blocklist` rules for buffer filetypes. --- autoload/lsp.vim | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/autoload/lsp.vim b/autoload/lsp.vim index bef5c702e..a14dd1d5a 100644 --- a/autoload/lsp.vim +++ b/autoload/lsp.vim @@ -984,6 +984,24 @@ function! lsp#get_allowed_servers(...) abort endfor endif + if !l:blocked && !empty(@%) + let l:curpath = fnamemodify(@%, ':p') + for l:pattern in get(l:server_info, 'blockpaths', []) + if match(l:curpath, l:pattern) != -1 + let l:blocked = 1 + break + endif + endfor + let l:allowed = !has_key(l:server_info, 'allowpaths') + for l:pattern in get(l:server_info, 'allowpaths', []) + if match(l:curpath, l:pattern) != -1 + let l:allowed = 1 + break + endif + endfor + let l:blocked = l:blocked || !l:allowed + endif + if l:blocked continue endif From e9dcdd59204e3d4da06fe47c1335908f3afcc97a Mon Sep 17 00:00:00 2001 From: Pavol Juhas Date: Mon, 17 Oct 2022 10:35:32 -0700 Subject: [PATCH 2/3] Allow all-match `*` in allowpaths, blockpaths A single `*` pattern in `allowpaths` or `blockpaths` matches everything. All other patterns are evaluated and matched as regular expressions. --- autoload/lsp.vim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/autoload/lsp.vim b/autoload/lsp.vim index a14dd1d5a..cff06d4a9 100644 --- a/autoload/lsp.vim +++ b/autoload/lsp.vim @@ -987,14 +987,14 @@ function! lsp#get_allowed_servers(...) abort if !l:blocked && !empty(@%) let l:curpath = fnamemodify(@%, ':p') for l:pattern in get(l:server_info, 'blockpaths', []) - if match(l:curpath, l:pattern) != -1 + if l:pattern == '*' || match(l:curpath, l:pattern) != -1 let l:blocked = 1 break endif endfor let l:allowed = !has_key(l:server_info, 'allowpaths') for l:pattern in get(l:server_info, 'allowpaths', []) - if match(l:curpath, l:pattern) != -1 + if l:pattern == '*' || match(l:curpath, l:pattern) != -1 let l:allowed = 1 break endif From d2ed13b558d2f5ccf6d7db4c3a34064df28bca63 Mon Sep 17 00:00:00 2001 From: Pavol Juhas Date: Tue, 18 Oct 2022 10:34:01 -0700 Subject: [PATCH 3/3] Document LSP activation rules allowpaths, blockpaths --- doc/vim-lsp.txt | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/doc/vim-lsp.txt b/doc/vim-lsp.txt index 3bff63741..0305e4a9f 100644 --- a/doc/vim-lsp.txt +++ b/doc/vim-lsp.txt @@ -1091,6 +1091,8 @@ one parameter which is a vim |dict| and is referred to as |vim-lsp-server_info| \ 'cmd': {server_info->['server-exectuable']}, \ 'allowlist': ['filetype to allowlist'], \ 'blocklist': ['filetype to blocklist'], + \ 'allowpaths': ['regexp for allowed files'], + \ 'blockpaths': ['regexp for blocked files'], \ 'config': {}, \ 'workspace_config': {'param': {'enabled': v:true}}, \ 'languageId': {server_info->'python'}, @@ -1112,6 +1114,8 @@ The vim |dict| containing information about the server. 'cmd': {server_info->['server_executable']}, 'allowlist': ['filetype'], 'blocklist': ['filetype'], + 'allowpaths': ['regexp'], + 'blockpaths': ['regexp'], 'config': {}, 'workspace_config': {}, 'languageId': {server_info->'filetype'}, @@ -1184,6 +1188,28 @@ The vim |dict| containing information about the server. 'allowlist': ['*'] 'blocklist': ['javascript', 'typescript'] < + * allowpaths: + optional + String array of |regexp| patterns for absolute file paths that should have + the language server enabled. allowpaths are consulted only when file + is enabled by the allowlist and blocklist rules for its filetype. + + Example: > + 'allowpaths': ['^/home/user/Code/'] +< + '*' is treated as matching every file. + + * blockpaths: + optional + String array of |regexp| patterns for absolute file paths that should not + run the language server. The blockpaths rule has priority when both + allowpaths and blockpaths match active file. + + Example: > + 'blockpaths': ['^/home/user/Code/project1/'] +< + '*' is treated as matching every file. + * workspace_config: optional vim |dict| Used to pass workspace configuration to the server after