diff options
| author | Justin M. Keyes <justinkz@gmail.com> | 2025-04-21 18:27:20 +0200 |
|---|---|---|
| committer | Justin M. Keyes <justinkz@gmail.com> | 2025-04-21 18:37:46 +0200 |
| commit | 0e29bf5ed68f9a67222c834ec44e70c8f884bde7 (patch) | |
| tree | 270a1c25e93b8d14ded593526d518dec64fe080f | |
| parent | docs: update configs.md (diff) | |
| download | nvim-lspconfig-0e29bf5ed68f9a67222c834ec44e70c8f884bde7.tar nvim-lspconfig-0e29bf5ed68f9a67222c834ec44e70c8f884bde7.tar.gz nvim-lspconfig-0e29bf5ed68f9a67222c834ec44e70c8f884bde7.tar.bz2 nvim-lspconfig-0e29bf5ed68f9a67222c834ec44e70c8f884bde7.tar.lz nvim-lspconfig-0e29bf5ed68f9a67222c834ec44e70c8f884bde7.tar.xz nvim-lspconfig-0e29bf5ed68f9a67222c834ec44e70c8f884bde7.tar.zst nvim-lspconfig-0e29bf5ed68f9a67222c834ec44e70c8f884bde7.zip | |
ci(lint): enforce "Lsp" command name prefix
| -rw-r--r-- | .github/ci/run_sanitizer.sh | 59 | ||||
| -rw-r--r-- | lsp/eslint.lua | 4 | ||||
| -rw-r--r-- | lsp/tinymist.lua | 13 |
3 files changed, 50 insertions, 26 deletions
diff --git a/.github/ci/run_sanitizer.sh b/.github/ci/run_sanitizer.sh index 9b560d84..6f1577da 100644 --- a/.github/ci/run_sanitizer.sh +++ b/.github/ci/run_sanitizer.sh @@ -1,24 +1,53 @@ #!/usr/bin/env bash + +# USAGE: To run locally: +# bash .github/ci/run_sanitizer.sh origin/master HEAD + set -e REF_BRANCH="$1" PR_BRANCH="$2" -# checks for added lines that contain search pattern and prints them -SEARCH_PATTERN='(path\.dirname|fn\.cwd)' +# Enforce buffer-local commands. +_check_cmd_buflocal() { + if git grep -P 'nvim_create_user_command' -- 'lsp/*.lua' ; then + echo + echo 'Define commands with nvim_buf_create_user_command (buffer-local), not nvim_create_user_command' + exit 1 + fi +} + +# Enforce "Lsp" prefix on all user commands. +_check_lsp_cmd_prefix() { + local exclude='tinymist' + if git grep -P 'nvim_buf_create_user_command' -- 'lsp/*.lua' | grep -v "$exclude" | grep --color -v Lsp ; then + echo + echo 'Command names must start with "Lsp" prefix' + exit 1 + fi +} + +_check_deprecated_utils() { + # checks for added lines that contain search pattern and prints them + SEARCH_PATTERN='(path\.dirname|fn\.cwd)' + + if git diff --pickaxe-all -U0 -G "${SEARCH_PATTERN}" "${REF_BRANCH}" "${PR_BRANCH}" -- '*.lua' | grep -Ev '(configs|utils)\.lua$' | grep -E "^\+.*${SEARCH_PATTERN}" ; then + echo + echo 'String "dirname" found. There is a high risk that this might contradict the directive:' + echo '"Do not use vim.fn.cwd or util.path.dirname in root_dir".' + echo "see: https://github.com/neovim/nvim-lspconfig/blob/master/CONTRIBUTING.md#adding-a-server-to-lspconfig." + exit 1 + fi -if git diff --pickaxe-all -U0 -G "${SEARCH_PATTERN}" "${REF_BRANCH}" "${PR_BRANCH}" -- '*.lua' | grep -Ev '(configs|utils)\.lua$' | grep -E "^\+.*${SEARCH_PATTERN}" ; then - echo - echo 'String "dirname" found. There is a high risk that this might contradict the directive:' - echo '"Do not use vim.fn.cwd or util.path.dirname in root_dir".' - echo "see: https://github.com/neovim/nvim-lspconfig/blob/master/CONTRIBUTING.md#adding-a-server-to-lspconfig." - exit 1 -fi + SEARCH_PATTERN='(util\.path\.dirname|util\.path\.sanitize|util\.path\.exists|util\.path\.is_file|util\.path\.is_dir|util\.path\.join|util\.path\.iterate_parents|util\.path\.traverse_parents|util\.find_mercurial_ancestor|util\.find_node_modules_ancestor|util\.find_package_json_ancestor|util\.find_git_ancestor|util\.get_lsp_clients|util\.get_active_client_by_name)' -SEARCH_PATTERN='(util\.path\.dirname|util\.path\.sanitize|util\.path\.exists|util\.path\.is_file|util\.path\.is_dir|util\.path\.join|util\.path\.iterate_parents|util\.path\.traverse_parents|util\.find_mercurial_ancestor|util\.find_node_modules_ancestor|util\.find_package_json_ancestor|util\.find_git_ancestor|util\.get_lsp_clients|util\.get_active_client_by_name)' + if git diff --pickaxe-all -U0 -G "${SEARCH_PATTERN}" "${REF_BRANCH}" "${PR_BRANCH}" -- '*.lua' | grep -Ev '\.lua$' | grep -E "^\+.*${SEARCH_PATTERN}" ; then + echo + echo 'Do not use deprecated util functions: '"${SEARCH_PATTERN}" + exit 1 + fi +} -if git diff --pickaxe-all -U0 -G "${SEARCH_PATTERN}" "${REF_BRANCH}" "${PR_BRANCH}" -- '*.lua' | grep -Ev '\.lua$' | grep -E "^\+.*${SEARCH_PATTERN}" ; then - echo - echo 'Do not use deprecated util functions: '"${SEARCH_PATTERN}" - exit 1 -fi +_check_cmd_buflocal +_check_lsp_cmd_prefix +_check_deprecated_utils diff --git a/lsp/eslint.lua b/lsp/eslint.lua index 01d324f9..4c01f3f2 100644 --- a/lsp/eslint.lua +++ b/lsp/eslint.lua @@ -44,8 +44,8 @@ return { 'svelte', 'astro', }, - on_init = function(client) - vim.api.nvim_create_user_command('LspEslintFixAll', function() + on_attach = function(client) + vim.api.nvim_buf_create_user_command(0, 'LspEslintFixAll', function() local bufnr = vim.api.nvim_get_current_buf() client:exec_cmd({ diff --git a/lsp/tinymist.lua b/lsp/tinymist.lua index a65302a8..d0a6d5c3 100644 --- a/lsp/tinymist.lua +++ b/lsp/tinymist.lua @@ -12,7 +12,7 @@ ---@param command_name string ---@return fun():nil run_tinymist_command, string cmd_name, string cmd_desc -local function create_tinymist_command(command_name) +local function create_tinymist_command(command_name, client, bufnr) local export_type = command_name:match 'tinymist%.export(%w+)' local info_type = command_name:match 'tinymist%.(%w+)' if info_type and info_type:match '^get' then @@ -22,11 +22,6 @@ local function create_tinymist_command(command_name) ---Execute the Tinymist command, supporting both 0.10 and 0.11 exec methods ---@return nil local function run_tinymist_command() - local bufnr = vim.api.nvim_get_current_buf() - local client = vim.lsp.get_clients({ name = 'tinymist', buffer = bufnr })[1] - if not client then - return vim.notify('No Tinymist client attached to the current buffer', vim.log.levels.ERROR) - end local arguments = { vim.api.nvim_buf_get_name(bufnr) } local title_str = export_type and ('Export ' .. cmd_display) or cmd_display ---@type lsp.Handler @@ -58,7 +53,7 @@ return { cmd = { 'tinymist' }, filetypes = { 'typst' }, root_markers = { '.git' }, - on_attach = function(_) + on_attach = function(client, bufnr) for _, command in ipairs { 'tinymist.exportSvg', 'tinymist.exportPng', @@ -73,8 +68,8 @@ return { 'tinymist.getWorkspaceLabels', 'tinymist.getDocumentMetrics', } do - local cmd_func, cmd_name, cmd_desc = create_tinymist_command(command) - vim.api.nvim_create_user_command(cmd_name, cmd_func, { nargs = 0, desc = cmd_desc }) + local cmd_func, cmd_name, cmd_desc = create_tinymist_command(command, client, bufnr) + vim.api.nvim_buf_create_user_command(0, cmd_name, cmd_func, { nargs = 0, desc = cmd_desc }) end end, } |
