ci(lint): enforce "Lsp" command name prefix

This commit is contained in:
Justin M. Keyes 2025-04-21 18:27:20 +02:00
parent 185cd59db1
commit 0e29bf5ed6
3 changed files with 50 additions and 26 deletions

View File

@ -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
}
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
# 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
}
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)'
_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 '\.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 '(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)'
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

View File

@ -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({

View File

@ -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,
}