fix: address shortcomings of vue setup
This commit is contained in:
+61
-56
@@ -165,63 +165,8 @@ return {
|
|||||||
-- yet expose a stable API (expected in 7.1). Once Vue supports
|
-- yet expose a stable API (expected in 7.1). Once Vue supports
|
||||||
-- TS7, this branching can be simplified.
|
-- TS7, this branching can be simplified.
|
||||||
-- See: https://github.com/vuejs/language-tools/issues/5381
|
-- See: https://github.com/vuejs/language-tools/issues/5381
|
||||||
local function detect_local_ts_version()
|
|
||||||
local pkg_path = vim.fn.getcwd() .. "/node_modules/typescript/package.json"
|
|
||||||
|
|
||||||
local ok, content = pcall(vim.fn.readfile, pkg_path)
|
-- Define tsgo config unconditionally (simple, no detection needed)
|
||||||
|
|
||||||
if ok and content and #content > 0 then
|
|
||||||
local decoded = vim.json.decode(table.concat(content, "\n"))
|
|
||||||
|
|
||||||
if decoded and decoded.version then
|
|
||||||
return tonumber(decoded.version:match("^(%d+)"))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return nil
|
|
||||||
end
|
|
||||||
|
|
||||||
local function has_vue_project()
|
|
||||||
local cwd = vim.fn.getcwd()
|
|
||||||
local ok, content = pcall(vim.fn.readfile, cwd .. "/package.json")
|
|
||||||
|
|
||||||
if ok and content and #content > 0 then
|
|
||||||
local decoded = vim.json.decode(table.concat(content, "\n"))
|
|
||||||
|
|
||||||
if decoded then
|
|
||||||
local deps = vim.tbl_extend("force", decoded.dependencies or {}, decoded.devDependencies or {})
|
|
||||||
|
|
||||||
if deps["vue"] or deps["nuxt"] or deps["@vue/compiler-sfc"] then
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if vim.fn.glob(cwd .. "/vue.config.*") ~= "" then
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
local ts_version = detect_local_ts_version()
|
|
||||||
local is_vue_project = has_vue_project()
|
|
||||||
|
|
||||||
if is_vue_project then
|
|
||||||
local tsserver_filetypes = { "typescript", "javascript", "javascriptreact", "typescriptreact", "vue" }
|
|
||||||
|
|
||||||
vim.lsp.config("vue_ls", {})
|
|
||||||
vim.lsp.config("ts_ls", {
|
|
||||||
init_options = {
|
|
||||||
plugins = {
|
|
||||||
vue_plugin,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
filetypes = tsserver_filetypes,
|
|
||||||
})
|
|
||||||
|
|
||||||
vim.lsp.enable("vue_ls")
|
|
||||||
vim.lsp.enable("ts_ls")
|
|
||||||
elseif ts_version and ts_version >= 7 then
|
|
||||||
vim.lsp.config("tsgo", {
|
vim.lsp.config("tsgo", {
|
||||||
cmd = function(dispatchers, config)
|
cmd = function(dispatchers, config)
|
||||||
local cmd = "tsc"
|
local cmd = "tsc"
|
||||||
@@ -239,11 +184,71 @@ return {
|
|||||||
filetypes = { "typescript", "javascript", "javascriptreact", "typescriptreact" },
|
filetypes = { "typescript", "javascript", "javascriptreact", "typescriptreact" },
|
||||||
})
|
})
|
||||||
|
|
||||||
|
-- Detect project type from the first loaded buffer (so vim.fs.root works)
|
||||||
|
local function detect_and_enable_lsp()
|
||||||
|
local root = vim.fs.root(0, "package.json") or vim.fn.getcwd()
|
||||||
|
|
||||||
|
-- Check if this is a Vue project
|
||||||
|
local ok, content = pcall(vim.fn.readfile, root .. "/package.json")
|
||||||
|
local is_vue = false
|
||||||
|
|
||||||
|
if ok and content and #content > 0 then
|
||||||
|
local decoded = vim.json.decode(table.concat(content, "\n"))
|
||||||
|
|
||||||
|
if decoded then
|
||||||
|
local deps = vim.tbl_extend("force", decoded.dependencies or {}, decoded.devDependencies or {})
|
||||||
|
|
||||||
|
if deps["vue"] or deps["nuxt"] or deps["@vue/compiler-sfc"] then
|
||||||
|
is_vue = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if not is_vue and vim.fn.glob(root .. "/vue.config.*") ~= "" then
|
||||||
|
is_vue = true
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Check TypeScript version (for tsgo vs ts_ls decision)
|
||||||
|
local ts_version
|
||||||
|
local ts_ok, ts_content = pcall(vim.fn.readfile, root .. "/node_modules/typescript/package.json")
|
||||||
|
|
||||||
|
if ts_ok and ts_content and #ts_content > 0 then
|
||||||
|
local decoded = vim.json.decode(table.concat(ts_content, "\n"))
|
||||||
|
|
||||||
|
if decoded and decoded.version then
|
||||||
|
ts_version = tonumber(decoded.version:match("^(%d+)"))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if is_vue then
|
||||||
|
vim.lsp.config("vue_ls", {})
|
||||||
|
vim.lsp.config("ts_ls", {
|
||||||
|
init_options = {
|
||||||
|
plugins = {
|
||||||
|
vue_plugin,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
filetypes = { "typescript", "javascript", "javascriptreact", "typescriptreact", "vue" },
|
||||||
|
})
|
||||||
|
|
||||||
|
vim.lsp.enable("vue_ls")
|
||||||
|
vim.lsp.enable("ts_ls")
|
||||||
|
elseif ts_version and ts_version >= 7 then
|
||||||
vim.lsp.enable("tsgo")
|
vim.lsp.enable("tsgo")
|
||||||
else
|
else
|
||||||
vim.lsp.config("ts_ls", {})
|
vim.lsp.config("ts_ls", {})
|
||||||
vim.lsp.enable("ts_ls")
|
vim.lsp.enable("ts_ls")
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Defer detection until a real buffer is loaded (so buffer 0 has a path)
|
||||||
|
vim.api.nvim_create_autocmd("BufReadPost", {
|
||||||
|
group = vim.api.nvim_create_augroup("TsVueLspSetup", { clear = true }),
|
||||||
|
once = true,
|
||||||
|
callback = function()
|
||||||
|
pcall(detect_and_enable_lsp)
|
||||||
|
end,
|
||||||
|
})
|
||||||
|
|
||||||
vim.lsp.config("jsonls", {
|
vim.lsp.config("jsonls", {
|
||||||
capabilities = capabilities,
|
capabilities = capabilities,
|
||||||
|
|||||||
@@ -15,6 +15,12 @@ return {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
vim.keymap.set("n", "<leader>gf", vim.lsp.buf.format, { desc = "Format" })
|
vim.keymap.set("n", "<leader>gf", function()
|
||||||
|
vim.lsp.buf.format({
|
||||||
|
filter = function(client)
|
||||||
|
return client.name ~= "vue_ls"
|
||||||
|
end,
|
||||||
|
})
|
||||||
|
end, { desc = "Format" })
|
||||||
end,
|
end,
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user