diff options
author | crupest <crupest@outlook.com> | 2023-09-08 17:57:37 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2023-09-11 15:57:28 +0800 |
commit | ae3dfb2c4fccfde1c69a23af1d734c795247e1d7 (patch) | |
tree | 092a32730882904699c4041d8836289cd9fa80d0 | |
parent | c2ead01d0d15ff7f6945fdd92d295d2ec9712963 (diff) | |
download | crupest-ae3dfb2c4fccfde1c69a23af1d734c795247e1d7.tar.gz crupest-ae3dfb2c4fccfde1c69a23af1d734c795247e1d7.tar.bz2 crupest-ae3dfb2c4fccfde1c69a23af1d734c795247e1d7.zip |
Update nvim config.
-rw-r--r-- | configs/nvim/init.lua | 80 | ||||
-rw-r--r-- | configs/nvim/lua/crupest-util-test.lua | 6 | ||||
-rw-r--r-- | configs/nvim/lua/crupest-util.lua | 17 |
3 files changed, 68 insertions, 35 deletions
diff --git a/configs/nvim/init.lua b/configs/nvim/init.lua index 9c4b99b..7634d2d 100644 --- a/configs/nvim/init.lua +++ b/configs/nvim/init.lua @@ -72,6 +72,52 @@ require("toggleterm").setup() -- setup autopairs require("nvim-autopairs").setup {} +-- setup formatter +local prettier_formatter = function () + local current_buffer = vim.api.nvim_buf_get_name(0) + local prettier_exe = require("crupest-util").find_npm_exe(current_buffer, "prettier") or "prettier" + + if vim.fn.has("win32") ~= 0 then + local escape = function (str) + return ({ string.gsub(str, " ", "\\ " )})[1] + end + + current_buffer = escape(current_buffer) +prettier_exe = escape(prettier_exe) +end + return { + exe = prettier_exe, + args = { + "--stdin-filepath", + current_buffer + }, + stdin = true, + } +end + +require("formatter").setup { + filetype = { + html = { + prettier_formatter + }, + css = { + prettier_formatter + }, + javascript = { + prettier_formatter + }, + javascriptreact = { + prettier_formatter + }, + typescript = { + prettier_formatter + }, + typescriptreact = { + prettier_formatter + } + } +} + -- setup lint local lint = require("lint") @@ -82,6 +128,10 @@ linter_eslint.cmd = function () if local_eslint then return local_eslint end return "eslint" end +-- lint library use 'cmd /C' to run exe, but we don't need this, so explicitly +-- set args to empty. +linter_eslint.args = {} +linter_eslint.append_fname = true lint.linters_by_ft = { javascript = { "eslint", "cspell" }, @@ -91,35 +141,11 @@ lint.linters_by_ft = { } vim.api.nvim_create_autocmd({ "BufWritePost" }, { - callback = function() - lint.try_lint() - end, + 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 local cmp = require("cmp") diff --git a/configs/nvim/lua/crupest-util-test.lua b/configs/nvim/lua/crupest-util-test.lua new file mode 100644 index 0000000..1f12aeb --- /dev/null +++ b/configs/nvim/lua/crupest-util-test.lua @@ -0,0 +1,6 @@ +local test_tsx_path = "~/codes/Timeline/FrontEnd/src/index.tsx" + +local util = loadfile("./lua/crupest-util.lua")() + +print(util.find_npm_exe(test_tsx_path, "eslint")) + diff --git a/configs/nvim/lua/crupest-util.lua b/configs/nvim/lua/crupest-util.lua index dcfc706..c2bb727 100644 --- a/configs/nvim/lua/crupest-util.lua +++ b/configs/nvim/lua/crupest-util.lua @@ -1,11 +1,11 @@ local M = {} M.clean_path = function (path) - return string.gsub(path, "[/\\]+", "/") + return path and ({string.gsub(path, "[/\\]+", "/")})[1] end M.get_exe = function (path) - if vim.fn.has("win32") then + if vim.fn.has("win32") ~= 0 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 @@ -19,25 +19,25 @@ M.get_exe = function (path) return nil end - if vim.fn.executable(path) then + if vim.fn.executable(path) ~= 0 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) + local new_path = vim.fn.fnamemodify(current_path, ":h") if new_path == current_path then break end + current_path = new_path end return nil end @@ -45,7 +45,7 @@ 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 + if vim.fn.isdirectory(node_modules_path) ~= 0 then return node_modules_path end return nil @@ -55,7 +55,8 @@ 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) + local try_exe_path = node_modules_path.."/.bin/"..exe + local exe_path = M.get_exe(try_exe_path) if exe_path then return M.clean_path(exe_path) end return nil end |