From 009829d2e854a5f894f66b306b4f280363784fb1 Mon Sep 17 00:00:00 2001 From: Tovi Jaeschke-Rogers Date: Sat, 7 Dec 2024 11:20:36 +1030 Subject: [PATCH] fix: breaking changes to ts server --- .config/nvim/lua/plugins/linting.lua | 206 ++++++++++++++++--------- .config/nvim/lua/plugins/lspconfig.lua | 61 ++++---- 2 files changed, 160 insertions(+), 107 deletions(-) diff --git a/.config/nvim/lua/plugins/linting.lua b/.config/nvim/lua/plugins/linting.lua index 2907c39..3eeb616 100644 --- a/.config/nvim/lua/plugins/linting.lua +++ b/.config/nvim/lua/plugins/linting.lua @@ -1,86 +1,144 @@ return { - "mfussenegger/nvim-lint", - event = { "BufReadPre", "BufNewFile" }, - config = function() - local lint = require("lint") + "mfussenegger/nvim-lint", + event = { "BufReadPre", "BufNewFile" }, + config = function() + local lint = require("lint") - local severities = { - ERROR = vim.diagnostic.severity.ERROR, - WARNING = vim.diagnostic.severity.WARN, - } + local severities = { + ERROR = vim.diagnostic.severity.ERROR, + WARNING = vim.diagnostic.severity.WARN, + } - lint.linters.phpcs = { - name = "phpcs", - cmd = "phpcs", - stdin = true, - args = { - "-q", - "--report=json", - "--standard=~/.config/phpcs.xml", - "-", -- need `-` at the end for stdin support - }, - ignore_exitcode = true, - parser = function(output, _) - if vim.trim(output) == "" or output == nil then - return {} - end + lint.linters.phpcs = { + name = "phpcs", + cmd = "phpcs", + stdin = true, + args = { + "-q", + "--report=json", + "--standard=~/.config/phpcs.xml", + "-", -- need `-` at the end for stdin support + }, + ignore_exitcode = true, + parser = function(output, _) + if vim.trim(output) == "" or output == nil then + return {} + end - if not vim.startswith(output, "{") then - vim.notify(output) - return {} - end + if not vim.startswith(output, "{") then + vim.notify(output) + return {} + end - local decoded = vim.json.decode(output) - local diagnostics = {} - local messages = decoded["files"]["STDIN"]["messages"] + local decoded = vim.json.decode(output) + local diagnostics = {} + local messages = decoded["files"]["STDIN"]["messages"] - for _, msg in ipairs(messages or {}) do - table.insert(diagnostics, { - lnum = msg.line - 1, - end_lnum = msg.line - 1, - col = msg.column - 1, - end_col = msg.column - 1, - message = msg.message, - code = msg.source, - source = "phpcs", - severity = assert(severities[msg.type], "missing mapping for severity " .. msg.type), - }) - end + for _, msg in ipairs(messages or {}) do + table.insert(diagnostics, { + lnum = msg.line - 1, + end_lnum = msg.line - 1, + col = msg.column - 1, + end_col = msg.column - 1, + message = msg.message, + code = msg.source, + source = "phpcs", + severity = assert(severities[msg.type], "missing mapping for severity " .. msg.type), + }) + end - return diagnostics - end, - } + return diagnostics + end, + } - lint.linters_by_ft = { - javascript = { "eslint" }, - typescript = { "eslint" }, - vue = { "eslint" }, - json = { "jsonlint" }, - markdown = { "markdownlint" }, - php = { "phpcs" }, - golang = { "gospell", "golangci-lint" }, - python = { "pylint" }, - dockerfile = { "hadolint" }, - blade = { "phpcs" }, - } + -- lint.linters.eslint_d = { + -- name = "eslint_d", + -- cmd = "eslint_d", + -- args = { + -- '--config=./frontend/.eslintrc.js', + -- '--no-warn-ignored', + -- '--format', + -- 'json', + -- '--stdin', + -- '--stdin-filename', + -- function() + -- return vim.api.nvim_buf_get_name(0) + -- end, + -- }, + -- parser = function(output, _) + -- local diagnostics = {} + -- local decoded = vim.json.decode(output) + -- for _, message in ipairs(decoded[1].messages or {}) do + -- table.insert(diagnostics, { + -- lnum = message.line - 1, + -- end_lnum = message.endLine and message.endLine - 1 or nil, + -- col = message.column - 1, + -- end_col = message.endColumn and message.endColumn - 1 or nil, + -- message = message.message, + -- code = message.ruleId, + -- source = "eslint_d", + -- severity = severities[message.severity], + -- }) + -- end + -- return diagnostics + -- end, + -- } - local lint_augroup = vim.api.nvim_create_augroup("lint", { - clear = true, - }) + lint.linters_by_ft = { + javascript = { "eslint" }, + typescript = { "eslint" }, + vue = { "eslint" }, + json = { "jsonlint" }, + markdown = { "markdownlint" }, + php = { "phpcs" }, + golang = { "gospell", "golangci-lint" }, + python = { "pylint" }, + dockerfile = { "hadolint" }, + blade = { "phpcs" }, + } - vim.api.nvim_create_autocmd({ - "BufEnter", - "BufWritePost", - "InsertLeave", - }, { - group = lint_augroup, - callback = function() - lint.try_lint() - end, - }) + local lint_augroup = vim.api.nvim_create_augroup("lint", { + clear = true, + }) - -- vim.keymap.set("n", "ll", function() - -- lint.try_lint() - -- end) - end, + + local function find_nearest_node_modules_dir() + -- current buffer dir + local current_dir = vim.fn.expand('%:p:h') .. "./frontend" + while current_dir ~= "/" do + if vim.fn.isdirectory(current_dir .. "/node_modules") == 1 then + return current_dir + end + current_dir = vim.fn.fnamemodify(current_dir, ":h") + end + return nil + end + + vim.api.nvim_create_autocmd({ + "BufEnter", + "BufWritePost", + "InsertLeave", + }, { + group = lint_augroup, + callback = function() + local ft = vim.bo.filetype + local js_types = { "javascript", "typescript", "javascriptreact", "typescriptreact", "vue" } + if not vim.tbl_contains(js_types, ft) then + lint.try_lint() + return + end + local original_cwd = vim.fn.getcwd() + local node_modules_dir = find_nearest_node_modules_dir() + if node_modules_dir then + vim.cmd("cd " .. node_modules_dir) + end + lint.try_lint() + vim.cmd("cd " .. original_cwd) + end, + }) + + -- vim.keymap.set("n", "ll", function() + -- lint.try_lint() + -- end) + end, } diff --git a/.config/nvim/lua/plugins/lspconfig.lua b/.config/nvim/lua/plugins/lspconfig.lua index f1ad61c..d808fc3 100644 --- a/.config/nvim/lua/plugins/lspconfig.lua +++ b/.config/nvim/lua/plugins/lspconfig.lua @@ -114,37 +114,24 @@ return { local found_ts = "" local function check_dir(path) found_ts = util.path.join(path, "node_modules", "typescript", "lib") - if util.path.exists(found_ts) then + if vim.loop.fs_stat(found_ts) then return path end end - if util.search_ancestors(root_dir, check_dir) then - return found_ts - else - return global_ts - end - end - - local path = util.path - local function get_python_path(workspace) - -- Use activated virtualenv. - if vim.env.VIRTUAL_ENV then - return path.join(vim.env.VIRTUAL_ENV, 'bin', 'python') + if util.search_ancestors(root_dir .. '/frontend/node_modules', check_dir) then + return found_ts end - -- Find and use virtualenv in workspace directory. - for _, pattern in ipairs({'*', '.*'}) do - local match = vim.fn.glob(path.join(workspace, pattern, 'pyvenv.cfg')) - if match ~= '' then - return path.join(path.dirname(match), 'bin', 'python') - end + if util.search_ancestors(root_dir .. '/node_modules', check_dir) then + return found_ts end - -- Fallback to system Python. - return exepath('python3') or exepath('python') or 'python' + return global_ts end + local path = util.path + local function organize_imports() local params = { command = "_typescript.organizeImports", @@ -176,20 +163,28 @@ return { organize_imports, description = "Organize Imports" } - } - }, - - volar = { - filetypes = { - "javascript", - "typescript", - "vue", }, on_new_config = function(new_config, new_root_dir) - new_config.init_options.typescript.tsdk = get_typescript_server_path(new_root_dir) + if get_typescript_server_path then + new_config.init_options.typescript = new_config.init_options.typescript or {} + new_config.init_options.typescript.tsdk = get_typescript_server_path(new_root_dir) + else + vim.notify("get_typescript_server_path is not defined", vim.log.levels.ERROR) + end end, }, + -- volar = { + -- filetypes = { + -- "javascript", + -- "typescript", + -- "vue", + -- }, + -- on_new_config = function(new_config, new_root_dir) + -- new_config.init_options.typescript.tsdk = get_typescript_server_path(new_root_dir) + -- end, + -- }, + cssls = {}, intelephense = { @@ -258,9 +253,9 @@ return { }, pyright = { - before_init = function(_, config) - config.settings.python.pythonpath = get_python_path(config.root_dir) - end + -- before_init = function(_, config) + -- config.settings.python.pythonpath = get_python_path(config.root_dir) + -- end }, yamlls = {