From 641a71e381e23f1d1d6a0722a8b796fda896b55b Mon Sep 17 00:00:00 2001 From: Wuelner Martínez Date: Sat, 11 Feb 2023 19:43:43 -0600 Subject: fix(stylelint_lsp): update root directory pattern (#2447) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(stylelint_lsp): update root directory pattern Signed-off-by: Wuelner Martínez * feat(util): add `add_package_json_to_config_files_if_field_exists` function Signed-off-by: Wuelner Martínez * fix(stylelint_lsp): update update root directory pattern implementation Signed-off-by: Wuelner Martínez * fix(eslint): update update root directory pattern implementation Signed-off-by: Wuelner Martínez * fix(util): update `is_windows` detection Co-authored-by: Raphael * fix(util): update new function name Co-authored-by: Raphael * fix(server_configurations): update new function name implementation Signed-off-by: Wuelner Martínez * fix(stylelint_lsp): fix duplicate equals Signed-off-by: Wuelner Martínez --------- Signed-off-by: Wuelner Martínez Co-authored-by: Raphael --- lua/lspconfig/server_configurations/eslint.lua | 16 ++-------------- lua/lspconfig/server_configurations/stylelint_lsp.lua | 18 ++++++++++++++---- lua/lspconfig/util.lua | 19 +++++++++++++++++-- 3 files changed, 33 insertions(+), 20 deletions(-) (limited to 'lua') diff --git a/lua/lspconfig/server_configurations/eslint.lua b/lua/lspconfig/server_configurations/eslint.lua index dc71e94f..af0cdd14 100644 --- a/lua/lspconfig/server_configurations/eslint.lua +++ b/lua/lspconfig/server_configurations/eslint.lua @@ -1,6 +1,5 @@ local util = require 'lspconfig.util' local lsp = vim.lsp -local is_windows = vim.fn.has 'win32' == 1 local function fix_all(opts) opts = opts or {} @@ -36,7 +35,7 @@ end local bin_name = 'vscode-eslint-language-server' local cmd = { bin_name, '--stdio' } -if is_windows then +if vim.fn.has 'win32' == 1 then cmd = { 'cmd.exe', '/C', bin_name, '--stdio' } end @@ -50,18 +49,7 @@ local root_file = { 'eslint.config.js', } -local root_with_package = util.find_package_json_ancestor(vim.fn.expand '%:p:h') - -if root_with_package then - -- only add package.json if it contains eslintConfig field - local path_sep = is_windows and '\\' or '/' - for line in io.lines(root_with_package .. path_sep .. 'package.json') do - if line:find 'eslintConfig' then - table.insert(root_file, 'package.json') - break - end - end -end +root_file = util.insert_package_json(root_file, 'eslintConfig') return { default_config = { diff --git a/lua/lspconfig/server_configurations/stylelint_lsp.lua b/lua/lspconfig/server_configurations/stylelint_lsp.lua index d471d267..314bb89f 100644 --- a/lua/lspconfig/server_configurations/stylelint_lsp.lua +++ b/lua/lspconfig/server_configurations/stylelint_lsp.lua @@ -7,6 +7,19 @@ if vim.fn.has 'win32' == 1 then cmd = { 'cmd.exe', '/C', bin_name, '--stdio' } end +local root_file = { + '.stylelintrc', + '.stylelintrc.cjs', + '.stylelintrc.js', + '.stylelintrc.json', + '.stylelintrc.yaml', + '.stylelintrc.yml', + 'stylelint.config.cjs', + 'stylelint.config.js', +} + +root_file = util.insert_package_json(root_file, 'stylelint') + return { default_config = { cmd = cmd, @@ -22,7 +35,7 @@ return { 'typescript', 'typescriptreact', }, - root_dir = util.root_pattern('.stylelintrc', 'package.json'), + root_dir = util.root_pattern(unpack(root_file)), settings = {}, }, docs = { @@ -47,8 +60,5 @@ require'lspconfig'.stylelint_lsp.setup{ } ``` ]], - default_config = { - root_dir = [[ root_pattern('.stylelintrc', 'package.json') ]], - }, }, } diff --git a/lua/lspconfig/util.lua b/lua/lspconfig/util.lua index 698cd2d9..ae2b1ada 100644 --- a/lua/lspconfig/util.lua +++ b/lua/lspconfig/util.lua @@ -4,6 +4,8 @@ local api = vim.api local lsp = vim.lsp local uv = vim.loop +local is_windows = uv.os_uname().sysname == 'Windows_NT' + local M = {} M.default_config = { @@ -94,8 +96,6 @@ end -- Some path utilities M.path = (function() - local is_windows = uv.os_uname().version:match 'Windows' - local function escape_wildcards(path) return path:gsub('([%[%]%?%*])', '\\%1') end @@ -487,6 +487,21 @@ function M.find_package_json_ancestor(startpath) end) end +function M.insert_package_json(config_files, field) + local root_with_package = M.find_package_json_ancestor(vim.fn.expand '%:p:h') + + if root_with_package then + -- only add package.json if it contains field parameter + local path_sep = is_windows and '\\' or '/' + for line in io.lines(root_with_package .. path_sep .. 'package.json') do + if line:find(field) then + return table.insert(config_files, 'package.json') + end + end + end + return config_files +end + function M.get_active_clients_list_by_ft(filetype) local clients = vim.lsp.get_active_clients() local clients_list = {} -- cgit v1.3.1