diff options
| author | crupest <crupest@outlook.com> | 2023-11-19 00:39:11 +0800 | 
|---|---|---|
| committer | crupest <crupest@outlook.com> | 2023-11-19 00:39:11 +0800 | 
| commit | cc0e89edcf0f1e11864efb26df992cc1d6609414 (patch) | |
| tree | 0fa6e76f7f6ef734d4fb1b866cb531dd53c3b59f | |
| parent | 3673d39bb9788a68183940507f24e86d994f2aca (diff) | |
| download | crupest-cc0e89edcf0f1e11864efb26df992cc1d6609414.tar.gz crupest-cc0e89edcf0f1e11864efb26df992cc1d6609414.tar.bz2 crupest-cc0e89edcf0f1e11864efb26df992cc1d6609414.zip  | |
config(nvim): refactor lint, make all lint optional.
| -rw-r--r-- | configs/nvim/init.lua | 8 | ||||
| -rw-r--r-- | configs/nvim/lazy-lock.json | 20 | ||||
| -rw-r--r-- | configs/nvim/lua/crupest/nvim/plugins/format.lua | 114 | ||||
| -rw-r--r-- | configs/nvim/lua/crupest/nvim/plugins/lint.lua | 90 | ||||
| -rw-r--r-- | configs/nvim/lua/crupest/nvim/plugins/snip.lua | 2 | ||||
| -rw-r--r-- | configs/nvim/lua/crupest/system/find.lua | 100 | ||||
| -rw-r--r-- | configs/nvim/lua/crupest/table.lua | 14 | 
7 files changed, 285 insertions, 63 deletions
diff --git a/configs/nvim/init.lua b/configs/nvim/init.lua index 9e4c742..b57c0bf 100644 --- a/configs/nvim/init.lua +++ b/configs/nvim/init.lua @@ -93,9 +93,9 @@ require("nvim-autopairs").setup {}  -- setup gitsigns  require('gitsigns').setup() --- setup formatter -local formatter = require("crupest.nvim.plugins.formatter") -formatter.setup_formatter() +-- setup format +local format = require("crupest.nvim.plugins.format") +format.setup_formatter()  -- setup lint  local lint = require("crupest.nvim.plugins.lint") @@ -169,7 +169,7 @@ vim.api.nvim_create_autocmd('LspAttach', {          vim.keymap.set({ 'n', 'v' }, '<space>ca', vim.lsp.buf.code_action, opts)          vim.keymap.set('n', 'gr', vim.lsp.buf.references, opts) -        vim.keymap.set('n', '<space>f', formatter.run_formatter, opts) +        vim.keymap.set('n', '<space>f', format.run_formatter, opts)      end,  }) diff --git a/configs/nvim/lazy-lock.json b/configs/nvim/lazy-lock.json index 2deb237..2f0730a 100644 --- a/configs/nvim/lazy-lock.json +++ b/configs/nvim/lazy-lock.json @@ -1,24 +1,24 @@  { -  "LuaSnip": { "branch": "master", "commit": "80a8528f084a97b624ae443a6f50ff8074ba486b" }, +  "LuaSnip": { "branch": "master", "commit": "1f4ad8bb72bdeb60975e98652636b991a9b7475d" },    "cmp-buffer": { "branch": "main", "commit": "3022dbc9166796b644a841a02de8dd1cc1d311fa" },    "cmp-cmdline": { "branch": "main", "commit": "8ee981b4a91f536f52add291594e89fb6645e451" },    "cmp-nvim-lsp": { "branch": "main", "commit": "44b16d11215dce86f253ce0c30949813c0a90765" },    "cmp-path": { "branch": "main", "commit": "91ff86cd9c29299a64f968ebb45846c485725f23" },    "cmp_luasnip": { "branch": "master", "commit": "05a9ab28b53f71d1aece421ef32fee2cb857a843" },    "everforest": { "branch": "master", "commit": "72f101bd63228a1a45012325e39c5280ec22c828" }, -  "formatter.nvim": { "branch": "master", "commit": "34dcdfa0c75df667743b2a50dd99c84a557376f0" }, -  "gitsigns.nvim": { "branch": "main", "commit": "af0f583cd35286dd6f0e3ed52622728703237e50" }, -  "lazy.nvim": { "branch": "main", "commit": "16603c6917435d8446f7357cb61095138a417085" }, +  "formatter.nvim": { "branch": "master", "commit": "91651e6afaf6f73b0ffb8b433c06cd4e06f90403" }, +  "gitsigns.nvim": { "branch": "main", "commit": "0ccd5fb2316b3f8d8b2f775bc31cae7bc6a77a55" }, +  "lazy.nvim": { "branch": "main", "commit": "96584866b9c5e998cbae300594d0ccfd0c464627" },    "lualine.nvim": { "branch": "master", "commit": "2248ef254d0a1488a72041cfb45ca9caada6d994" }, -  "neo-tree.nvim": { "branch": "main", "commit": "936b029e514adc1fa7ab51c6c1a8a2c872a58797" }, +  "neo-tree.nvim": { "branch": "main", "commit": "f86e871584bd3c5a00b4ff8344305889eb52ebff" },    "nui.nvim": { "branch": "main", "commit": "c0c8e347ceac53030f5c1ece1c5a5b6a17a25b32" },    "nvim-autopairs": { "branch": "master", "commit": "0f04d78619cce9a5af4f355968040f7d675854a1" }, -  "nvim-cmp": { "branch": "main", "commit": "51260c02a8ffded8e16162dcf41a23ec90cfba62" }, -  "nvim-lint": { "branch": "master", "commit": "962a76877a4479a535b935bd7ef35ad41ba308b2" }, -  "nvim-lspconfig": { "branch": "master", "commit": "d0467b9574b48429debf83f8248d8cee79562586" }, -  "nvim-web-devicons": { "branch": "master", "commit": "5de460ca7595806044eced31e3c36c159a493857" }, +  "nvim-cmp": { "branch": "main", "commit": "0b751f6beef40fd47375eaf53d3057e0bfa317e4" }, +  "nvim-lint": { "branch": "master", "commit": "15fcab91e6a4e2a3e41dc55c5b3471f66c11ce39" }, +  "nvim-lspconfig": { "branch": "master", "commit": "7384e7149e79365c96dc623d9fa3444737776982" }, +  "nvim-web-devicons": { "branch": "master", "commit": "3523d6e6d40ab11fd66c1b2732b3d6b60affa951" },    "omnisharp-extended-lsp.nvim": { "branch": "main", "commit": "53edfb413a54c9e55dcddc9e9fa4977a897e4425" },    "plenary.nvim": { "branch": "master", "commit": "50012918b2fc8357b87cff2a7f7f0446e47da174" }, -  "telescope.nvim": { "branch": "master", "commit": "4522d7e3ea75ffddabdc39957168a8a7060b5df0" }, +  "telescope.nvim": { "branch": "master", "commit": "18774ec7929c8a8003a91e9e1f69f6c32258bbfe" },    "toggleterm.nvim": { "branch": "main", "commit": "c80844fd52ba76f48fabf83e2b9f9b93273f418d" }  }
\ No newline at end of file diff --git a/configs/nvim/lua/crupest/nvim/plugins/format.lua b/configs/nvim/lua/crupest/nvim/plugins/format.lua new file mode 100644 index 0000000..9f6138f --- /dev/null +++ b/configs/nvim/lua/crupest/nvim/plugins/format.lua @@ -0,0 +1,114 @@ +local fs = require("crupest.system.fs") +local find_npm_exe = require("crupest.system.find").find_npm_exe; + +local prettier_formatter = function() +    local current_buffer = vim.api.nvim_buf_get_name(0) +    local prettier_exe = find_npm_exe(current_buffer, "prettier") or "prettier" + +    if vim.fn.has("win32") ~= 0 then +        local escape = fs.escape_space +        current_buffer = escape(current_buffer) +        prettier_exe = escape(prettier_exe) +    end + +    return { +        exe = prettier_exe, +        args = { +            "--stdin-filepath", +            current_buffer +        }, +        stdin = true, +    } +end + +local formatters_for_filetype = { +    html = { +        prettier_formatter +    }, +    css = { +        prettier_formatter +    }, +    javascript = { +        prettier_formatter +    }, +    javascriptreact = { +        prettier_formatter +    }, +    typescript = { +        prettier_formatter +    }, +    typescriptreact = { +        prettier_formatter +    } +} + +local function get_formatter_name(formatter) +    if formatter == prettier_formatter then return "prettier" end +    return nil +end + +local function get_formatter_name_list(formatters) +    local result = {} +    for _, formatter in ipairs(formatters) do +        table.insert(result, get_formatter_name(formatter)) +    end +    return result +end + +local function setup_formatter() +    require("formatter").setup { +        filetype = formatters_for_filetype +    } +end + + +local function get_custom_formatters(bufnr) +    local filetype = vim.api.nvim_buf_get_option(bufnr, "filetype") +    for ft, formatters in pairs(formatters_for_filetype) do +        if filetype == ft then +            return true, get_formatter_name_list(formatters) +        end +    end +    return false, {} +end + +local function run_formatter(opt) +    if not opt then +        opt = {} +    end + +    if not opt.buf then +        opt.buf = 0 +    end + +    local has_custom_formatter, formatter_names = get_custom_formatters(opt.buf) + +    local formatter_name_str = "" +    for i, name in ipairs(formatter_names) do +        if i == 1 then +            formatter_name_str = name +        else +            formatter_name_str = formatter_name_str .. " " .. name +        end +    end + +    if has_custom_formatter then +        print("Use custom formatters: " .. formatter_name_str .. ".") +        vim.cmd("Format") +        return +    end + +    local has_lsp = vim.lsp.get_active_clients({ bufnr = 0 }) +    if has_lsp then +        print("Use lsp formatter.") +        vim.lsp.buf.format { async = true } +        return +    end + +    vim.notify("No formatters found.", vim.log.levels.ERROR); +end + +return { +    setup_formatter = setup_formatter, +    run_formatter = run_formatter +} diff --git a/configs/nvim/lua/crupest/nvim/plugins/lint.lua b/configs/nvim/lua/crupest/nvim/plugins/lint.lua index 46832bd..491c0d1 100644 --- a/configs/nvim/lua/crupest/nvim/plugins/lint.lua +++ b/configs/nvim/lua/crupest/nvim/plugins/lint.lua @@ -1,5 +1,5 @@ -local fs = require("crupest.system.fs")  local lint = require("lint") +local find = require('crupest.system.find')  local cspell_config_filenames = {      ".cspell.json", @@ -15,70 +15,72 @@ local cspell_config_filenames = {      "cspell.yml",  } -local cspell_enable_dirs = {} +local frontend_file_type = { 'javascript', 'javascriptreact', 'typescript', 'typescriptreact' } -local function detect_cspell(file) -    for _, dir in ipairs(cspell_enable_dirs) do -        if string.find(fs.full_path(file), dir, 0, true) == 1 then -            return true -        end -    end -    return fs.walk_up(file, function(current_path) -        for _, name in ipairs(cspell_config_filenames) do -            local cspell_config_file = current_path .. "/" .. name -            if fs.isfile(cspell_config_file) then -                table.insert(cspell_enable_dirs, current_path) -                return true -            end -        end -        return nil -    end) or false -end +local my_linters = { +    { +        name = "cspell", +        exe_places = { "npm", "global" }, +        config_files = cspell_config_filenames, +    }, +    { +        name = "eslint", +        exe_places = { "npm" }, +        filetypes = frontend_file_type, +        config_files = { "package.json" } +    }, +    { +        name = "deno", +        exe_places = { "global" }, +        filetypes = frontend_file_type, +        config_files = { "deno.json", "deno.jsonc" } +    } +}  local function run_lint(opt)      if not opt then          opt = {}      end -    if not opt.file then -        opt.file = vim.api.nvim_buf_get_name(0) +    if not opt.buf then +        opt.buf = 0      end -    if opt.run_cspell == nil then -        opt.run_cspell = detect_cspell(opt.file) +    local linters = {} + +    for _, l in ipairs(my_linters) do +        local linter = find.find_exe_for_buf(opt.buf, l) +        if linter then table.insert(linters, linter) end      end -    lint.try_lint() -    if opt.run_cspell then -        lint.try_lint("cspell") +    local linter_names = {} + +    for _, linter in ipairs(linters) do +        table.insert(linter_names, linter.name) +        require('lint.linters.' .. linter.name).cmd = linter.exe_path      end + +    lint.try_lint(linter_names)  end  local function setup_lint() -    local linter_eslint = require("lint.linters.eslint") - -    linter_eslint.cmd = function() -        local current_buffer = vim.api.nvim_buf_get_name(0) -        return require("crupest.system.find").find_npm_exe(current_buffer, "eslint") +    if require('crupest.system').is_win then +        for _, l in ipairs(my_linters) do +            local name = l.name +            local linter = require('lint.linters.' .. name) +            if linter.cmd == 'cmd.exe' then +                linter.cmd = linter.args[2] +            end +            table.remove(linter.args, 1) +            table.remove(linter.args, 1) +        end      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" }, -        javascriptreact = { "eslint" }, -        typescript = { "eslint" }, -        typescriptreact = { "eslint" }, -    } -      vim.api.nvim_create_autocmd({ "BufWritePost" }, {          callback = function(opt)              run_lint({ -                file = opt.file +                buf = opt.buffer              })          end,      }) diff --git a/configs/nvim/lua/crupest/nvim/plugins/snip.lua b/configs/nvim/lua/crupest/nvim/plugins/snip.lua index 1dde7d3..1cf1b7c 100644 --- a/configs/nvim/lua/crupest/nvim/plugins/snip.lua +++ b/configs/nvim/lua/crupest/nvim/plugins/snip.lua @@ -59,7 +59,7 @@ local function setup_snip()              f(copy, 1),              t("("),              i(0), -            t({") { }", "", ""}), +            t({ ") { }", "", "" }),              f(copy, 1),              t("::~"),              f(copy, 1), diff --git a/configs/nvim/lua/crupest/system/find.lua b/configs/nvim/lua/crupest/system/find.lua index a2631a6..0876622 100644 --- a/configs/nvim/lua/crupest/system/find.lua +++ b/configs/nvim/lua/crupest/system/find.lua @@ -1,15 +1,17 @@  local system = require("crupest.system")  local fs = require("crupest.system.fs"); +local win_exe_exts = { "exe", "CMD", "cmd", "ps1" } + +  local function get_exe(path)      if system.is_win then -        local exts = { "exe", "CMD", "cmd", "ps1" } -        for _, ext in ipairs(exts) do +        for _, ext in ipairs(win_exe_exts) do              if string.find(path, "%." .. ext .. "$") and fs.isfile(path) then                  return path              end          end -        for _, ext in ipairs(exts) do +        for _, ext in ipairs(win_exe_exts) do              local p = path .. "." .. ext              if fs.isfile(p) then return p end          end @@ -23,6 +25,14 @@ local function get_exe(path)      return nil  end +local function find_global_exe(name) +    if vim.fn.executable(name) ~= 0 then +        return name +    end + +    return nil +end +  local function first_exe(paths)      for _, v in ipairs(paths) do          local exe = get_exe(v) @@ -32,6 +42,44 @@ local function first_exe(paths)      return nil  end +local function find_file_or_directory(path, name) +    return fs.walk_up(path, function(current_path) +        local p = current_path .. "/" .. name +        if fs.isdir(p) then +            return p, "directory" +        elseif fs.isfile(p) then +            return p, "file" +        end +        return nil +    end) +end + +local function find_file(path, name) +    return fs.walk_up(path, function(current_path) +        local p = current_path .. "/" .. name +        if fs.isfile(p) then +            return p +        end +        return nil +    end) +end + +local function find_files_or_directories(path, names) +    for _, name in ipairs(names) do +        local p, type = find_file_or_directory(path, name) +        if p then return p, type end +    end +    return nil +end + +local function find_files(path, names) +    for _, name in ipairs(names) do +        local p = find_file(path, name) +        if p then return p end +    end +    return nil +end +  local function find_node_modules(path)      return fs.walk_up(path, function(current_path)          local node_modules_path = current_path .. "/node_modules" @@ -51,9 +99,55 @@ local function find_npm_exe(path, exe)      return nil  end +local function find_exe(path, exe, places) +    for _, place in ipairs(places) do +        if place == "npm" then +            local r = find_npm_exe(path, exe) +            if r then return r end +        end +        if place == "global" then +            local r = find_global_exe(exe) +            if r then return r end +        end +    end +    return nil +end + +local function find_exe_for_buf(buf, opts) +    local r = {} +    r.name = opts.name +    r.file = vim.api.nvim_buf_get_name(buf) +    r.filetype = vim.api.nvim_buf_get_option(buf, "filetype") +    r.exe_name = opts.name +    r.exe_places = opts.exe_places or { "global" } + +    if opts.config_files then +        r.config_file = find_files(r.file, opts.config_files) +        if r.config_file == nil then return nil end +    end + +    if opts.filetypes then +        if not require("crupest.table").includes(opts.filetypes, r.filetype) then +            return nil +        end +    end + +    r.exe_path = find_exe(r.file, r.exe_name, r.exe_places) +    if r.exe_path == nil then return nil end + +    return r +end +  return {      get_exe = get_exe, +    find_global_exe = find_global_exe,      first_exe = first_exe, +    find_file_or_directory = find_file_or_directory, +    find_files_or_directories = find_files_or_directories, +    find_file = find_file, +    find_files = find_files,      find_node_modules = find_node_modules,      find_npm_exe = find_npm_exe, +    find_exe = find_exe, +    find_exe_for_buf = find_exe_for_buf,  } diff --git a/configs/nvim/lua/crupest/table.lua b/configs/nvim/lua/crupest/table.lua index ab069ce..22419b0 100644 --- a/configs/nvim/lua/crupest/table.lua +++ b/configs/nvim/lua/crupest/table.lua @@ -23,8 +23,20 @@ local function element_at(tbl, element)      return at  end +local function includes(tbl, element) +    for _, v in ipairs(tbl) do +        if v == element then return true end +    end +    return false +end + +local function string_start_with(str, prefix) +    return string.find(str, prefix, 0, true) == 1 +end +  return {      remove_element = remove_element,      element_at = element_at, +    includes = includes, +    string_start_with = string_start_with,  } -  | 
