diff options
-rw-r--r-- | configs/nvim/init.lua | 51 | ||||
-rw-r--r-- | configs/nvim/lua/crupest-util.lua | 63 |
2 files changed, 112 insertions, 2 deletions
diff --git a/configs/nvim/init.lua b/configs/nvim/init.lua index 6f87738..9c4b99b 100644 --- a/configs/nvim/init.lua +++ b/configs/nvim/init.lua @@ -74,6 +74,15 @@ require("nvim-autopairs").setup {} -- setup lint local lint = require("lint") + +local linter_eslint = require("lint.linters.eslint") +linter_eslint.cmd = function () + local current_buffer = vim.api.nvim_buf_get_name(0) + local local_eslint = require("crupest-util").find_npm_exe(current_buffer, "eslint") + if local_eslint then return local_eslint end + return "eslint" +end + lint.linters_by_ft = { javascript = { "eslint", "cspell" }, javascriptreact = { "eslint", "cspell" }, @@ -81,8 +90,34 @@ lint.linters_by_ft = { typescriptreact = { "eslint", "cspell" }, } +vim.api.nvim_create_autocmd({ "BufWritePost" }, { + callback = function() + lint.try_lint() + end, +}) + -- setup formatter require("formatter").setup { + filetype = { + html = { + require("formatter.filetypes.html").prettier + }, + css = { + require("formatter.filetypes.css").prettier + }, + javascript = { + require("formatter.filetypes.javascript").prettier + }, + javascriptreact = { + require("formatter.filetypes.javascriptreact").prettier + }, + typescript = { + require("formatter.filetypes.typescript").prettier + }, + typescriptreact = { + require("formatter.filetypes.typescriptreact").prettier + } + } } -- setup nvim-cmp @@ -121,7 +156,7 @@ lspconfig.clangd.setup { } -- setup lsp lua -require("lspconfig").lua_ls.setup { +lspconfig.lua_ls.setup { capabilities = capabilities, settings = { Lua = { @@ -142,6 +177,18 @@ require("lspconfig").lua_ls.setup { }, } +-- setup lsp frontend +lspconfig.cssls.setup {} +lspconfig.html.setup {} +lspconfig.tsserver.setup{ + on_new_config = function (new_config, new_root_dir) + local local_tsserver = require("crupest-util").find_npm_exe(new_root_dir, "typescript-language-server"); + if local_tsserver then + new_config.cmd = { local_tsserver, "--stdio" } + end + end, +} + -- setup trouble require("trouble").setup() @@ -152,7 +199,7 @@ vim.keymap.set('n', '<leader>fg', builtin.live_grep, {}) vim.keymap.set('n', '<leader>fb', builtin.buffers, {}) vim.keymap.set('n', '<leader>fh', builtin.help_tags, {}) --- setup keymap for lsp +-- setup keymap fnamemodifyfor lsp -- Global mappings. -- See `:help vim.diagnostic.*` for documentation on any of the below functions diff --git a/configs/nvim/lua/crupest-util.lua b/configs/nvim/lua/crupest-util.lua new file mode 100644 index 0000000..dcfc706 --- /dev/null +++ b/configs/nvim/lua/crupest-util.lua @@ -0,0 +1,63 @@ +local M = {} + +M.clean_path = function (path) + return string.gsub(path, "[/\\]+", "/") +end + +M.get_exe = function (path) + if vim.fn.has("win32") then + local suffixes = { ".exe", ".CMD", ".cmd", ".ps1" } + for _, v in ipairs(suffixes) do + if string.find(path, v.."$") and vim.uv.fs_stat(path) then + return path + end + end + for _, v in ipairs(suffixes) do + local p = path..v + if vim.uv.fs_stat(p) then return p end + end + return nil + end + + if vim.fn.executable(path) then + return path + end + return nil +end + +M.walk_up = function (path, func) + local current_path = vim.fn.fnamemodify(path, ":p") + while true do + print(current_path) + local result = func(current_path) + if result then + return result + end + local new_path = vim.fn.fnamemodify(current_path, ":p:h") + print(new_path) + if new_path == current_path then + break + end + end + return nil +end + +M.find_node_modules = function (path) + return M.walk_up(path, function (current_path) + local node_modules_path = current_path.."/node_modules" + if vim.fn.isdirectory(node_modules_path) then + return node_modules_path + end + return nil + end) +end + +M.find_npm_exe = function (path, exe) + local node_modules_path = M.find_node_modules(path) + if not node_modules_path then return nil end + local exe_path = M.get_exe(node_modules_path.."/.bin/"..exe) + if exe_path then return M.clean_path(exe_path) end + return nil +end + +return M |