[Bugfix] Fix tsserver lsp setup. Fix ts-fmt-lint setup (#1013)

This commit is contained in:
Pasi Bergman 2021-07-18 21:38:41 +03:00 committed by GitHub
parent d9936b0d84
commit dd4dbdd184
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 8 deletions

View file

@ -1,30 +1,49 @@
-- Example configuations here: https://github.com/mattn/efm-langserver -- Example configuations here: https://github.com/mattn/efm-langserver
-- You can look for project scope Prettier and Eslint with e.g. vim.fn.glob("node_modules/.bin/prettier") etc. If it is not found revert to global Prettier where needed.
local M = {} local M = {}
M.setup = function() M.setup = function()
vim.cmd "let proj = FindRootDirectory()"
local root_dir = vim.api.nvim_get_var "proj"
local get_linter_instance = function()
-- prioritize local instance over global
local local_instance = root_dir .. "/node_modules/.bin/" .. O.lang.tsserver.linter
if vim.fn.executable(local_instance) == 1 then
return local_instance
end
return O.lang.tsserver.linter
end
local tsserver_args = {} local tsserver_args = {}
local formattingSupported = false
if O.lang.tsserver.linter == "eslint" or O.lang.tsserver.linter == "eslint_d" then if O.lang.tsserver.linter == "eslint" or O.lang.tsserver.linter == "eslint_d" then
local eslint = { local eslint = {
lintCommand = O.lang.tsserver.linter .. " -f visualstudio --stdin --stdin-filename ${INPUT}", lintCommand = get_linter_instance() .. " -f visualstudio --stdin --stdin-filename ${INPUT}",
lintStdin = true, lintStdin = true,
lintFormats = { lintFormats = {
"%f(%l,%c): %tarning %m", "%f(%l,%c): %tarning %m",
"%f(%l,%c): %rror %m", "%f(%l,%c): %trror %m",
}, },
lintSource = O.lang.tsserver.linter, lintSource = O.lang.tsserver.linter,
lintIgnoreExitCode = true, lintIgnoreExitCode = true,
formatCommand = O.lang.tsserver.linter .. " --fix-to-stdout --stdin --stdin-filename=${INPUT}",
formatStdin = true,
} }
table.insert(tsserver_args, eslint) table.insert(tsserver_args, eslint)
-- Only eslint_d supports --fix-to-stdout
if string.find(get_linter_instance(), "eslint_d") then
formattingSupported = true
local eslint_fix = {
formatCommand = get_linter_instance() .. " --fix-to-stdout --stdin --stdin-filename ${INPUT}",
formatStdin = true,
}
table.insert(tsserver_args, eslint_fix)
end
end end
require("lspconfig").efm.setup { require("lspconfig").efm.setup {
-- init_options = {initializationOptions}, -- init_options = {initializationOptions},
cmd = { DATA_PATH .. "/lspinstall/efm/efm-langserver" }, cmd = { DATA_PATH .. "/lspinstall/efm/efm-langserver" },
init_options = { documentFormatting = true, codeAction = false }, init_options = { documentFormatting = formattingSupported, codeAction = false },
root_dir = require("lspconfig").util.root_pattern(".git/", "package.json"), root_dir = require("lspconfig").util.root_pattern(".git/", "package.json"),
filetypes = { filetypes = {
"vue", "vue",

View file

@ -61,9 +61,8 @@ require("lspconfig").tsserver.setup {
"typescript.tsx", "typescript.tsx",
}, },
on_attach = require("lsp").tsserver_on_attach, on_attach = require("lsp").tsserver_on_attach,
-- This makes sure tsserver is not used for formatting (I prefer prettier)
-- on_attach = require'lsp'.common_on_attach, -- on_attach = require'lsp'.common_on_attach,
root_dir = require("lspconfig/util").root_pattern("package.json", "tsconfig.json", "jsconfig.json", ".git"), -- This makes sure tsserver is not used for formatting (I prefer prettier)
settings = { documentFormatting = false }, settings = { documentFormatting = false },
handlers = { handlers = {
-- ["textDocument/publishDiagnostics"] = vim.lsp.with(vim.lsp.diagnostic.on_publish_diagnostics, { -- ["textDocument/publishDiagnostics"] = vim.lsp.with(vim.lsp.diagnostic.on_publish_diagnostics, {
@ -74,4 +73,5 @@ require("lspconfig").tsserver.setup {
-- }), -- }),
}, },
} }
require("lsp.ts-fmt-lint").setup() require("lsp.ts-fmt-lint").setup()