Browse Source

fix: breaking changes to ts server

master
Tovi Jaeschke-Rogers 3 weeks ago
parent
commit
009829d2e8
2 changed files with 160 additions and 107 deletions
  1. +132
    -74
      .config/nvim/lua/plugins/linting.lua
  2. +28
    -33
      .config/nvim/lua/plugins/lspconfig.lua

+ 132
- 74
.config/nvim/lua/plugins/linting.lua View File

@ -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", "<leader>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", "<leader>ll", function()
-- lint.try_lint()
-- end)
end,
}

+ 28
- 33
.config/nvim/lua/plugins/lspconfig.lua View File

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


Loading…
Cancel
Save