diff options
Diffstat (limited to 'configs/nvim/lua/crupest')
| -rw-r--r-- | configs/nvim/lua/crupest/nvim.lua | 1 | ||||
| -rw-r--r-- | configs/nvim/lua/crupest/nvim/fs.lua | 9 | ||||
| -rw-r--r-- | configs/nvim/lua/crupest/nvim/lsp/c.lua | 2 | ||||
| -rw-r--r-- | configs/nvim/lua/crupest/nvim/lsp/lua.lua | 2 | ||||
| -rw-r--r-- | configs/nvim/lua/crupest/nvim/plugins/lint.lua | 61 | ||||
| -rw-r--r-- | configs/nvim/lua/crupest/system.lua | 73 | ||||
| -rw-r--r-- | configs/nvim/lua/crupest/system/find.lua | 55 | ||||
| -rw-r--r-- | configs/nvim/lua/crupest/system/fs.lua | 50 | 
8 files changed, 162 insertions, 91 deletions
diff --git a/configs/nvim/lua/crupest/nvim.lua b/configs/nvim/lua/crupest/nvim.lua index 1c252fd..505f06c 100644 --- a/configs/nvim/lua/crupest/nvim.lua +++ b/configs/nvim/lua/crupest/nvim.lua @@ -105,6 +105,7 @@ end  return {      list_listed_bufs = list_listed_bufs, +    buf_is_normal = buf_is_normal,      get_previous_buffer = get_previous_buffer,      list_wins_editing_buf = list_wins_editing_buf,      win_close_buf = win_close_buf, diff --git a/configs/nvim/lua/crupest/nvim/fs.lua b/configs/nvim/lua/crupest/nvim/fs.lua index 94eb51e..2410535 100644 --- a/configs/nvim/lua/crupest/nvim/fs.lua +++ b/configs/nvim/lua/crupest/nvim/fs.lua @@ -1,9 +1,9 @@ -local list_listed_bufs = require("crupest.nvim").list_listed_bufs; +local crupest_nvim = require("crupest.nvim");  local fs = require("crupest.system.fs"); +local full_path = require("crupest.system").full_path; -local function full_path(name) -    return vim.fn.fnamemodify(name, ":p:gs?\\?/?") -end +local list_listed_bufs = crupest_nvim.list_listed_bufs; +local buf_is_normal = crupest_nvim.buf_is_normal;  -- There are two situations.  -- 1. the new path is not a dir, then it is used @@ -165,4 +165,3 @@ return {      rename_buf_file = rename_buf_file,      setup_filesystem_user_commands = setup_filesystem_user_commands  } - diff --git a/configs/nvim/lua/crupest/nvim/lsp/c.lua b/configs/nvim/lua/crupest/nvim/lsp/c.lua index 053f8ce..ac3cb7f 100644 --- a/configs/nvim/lua/crupest/nvim/lsp/c.lua +++ b/configs/nvim/lua/crupest/nvim/lsp/c.lua @@ -1,5 +1,5 @@ -local capabilities = require("cmp_nvim_lsp").default_capabilities()  local lspconfig = require("lspconfig") +local capabilities = require("cmp_nvim_lsp").default_capabilities()  local function setup_lsp_c()      -- setup lsp clangd diff --git a/configs/nvim/lua/crupest/nvim/lsp/lua.lua b/configs/nvim/lua/crupest/nvim/lsp/lua.lua index 97478d9..4df0f49 100644 --- a/configs/nvim/lua/crupest/nvim/lsp/lua.lua +++ b/configs/nvim/lua/crupest/nvim/lsp/lua.lua @@ -1,5 +1,5 @@ -local capabilities = require("cmp_nvim_lsp").default_capabilities()  local lspconfig = require("lspconfig") +local capabilities = require("cmp_nvim_lsp").default_capabilities()  local function setup_lsp_lua()      lspconfig.lua_ls.setup { diff --git a/configs/nvim/lua/crupest/nvim/plugins/lint.lua b/configs/nvim/lua/crupest/nvim/plugins/lint.lua index abab018..ed3140c 100644 --- a/configs/nvim/lua/crupest/nvim/plugins/lint.lua +++ b/configs/nvim/lua/crupest/nvim/plugins/lint.lua @@ -1,8 +1,58 @@ +local fs = require("crupest.system.fs")  local lint = require("lint") -local function run_lint() +local cspell_config_filenames = { +    ".cspell.json", +    "cspell.json", +    ".cSpell.json", +    "cSpell.json", +    "cspell.config.js", +    "cspell.config.cjs", +    "cspell.config.json", +    "cspell.config.yaml", +    "cspell.config.yml", +    "cspell.yaml", +    "cspell.yml", +} + +local cspell_enable_dirs = {} + +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 function run_lint(opt) +    if not opt then +        opt = {} +    end + +    if not opt.file then +        opt.file = vim.api.nvim_buf_get_name(0) +    end + +    if opt.run_cspell == nil then +        opt.run_cspell = detect_cspell(opt.file) +    end +      lint.try_lint() -    lint.try_lint("cspell") + +    if opt.run_cspell then +        lint.try_lint("cspell") +    end  end  local function setup_lint() @@ -26,7 +76,11 @@ local function setup_lint()      }      vim.api.nvim_create_autocmd({ "BufWritePost" }, { -        callback = run_lint, +        callback = function(opt) +            run_lint({ +                file = opt.file +            }) +        end,      })  end @@ -34,4 +88,3 @@ return {      setup_lint = setup_lint,      run_lint = run_lint  } - diff --git a/configs/nvim/lua/crupest/system.lua b/configs/nvim/lua/crupest/system.lua index a5a90a6..f05b230 100644 --- a/configs/nvim/lua/crupest/system.lua +++ b/configs/nvim/lua/crupest/system.lua @@ -1,74 +1,5 @@ -local function clean_path(path) -    return path and (string.gsub(path, "[/\\]+", "/")) -end - -local function get_exe(path) -    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 -                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) ~= 0 then -        return path -    end - -    return nil -end - -local function walk_up(path, func) -    local current_path = vim.fn.fnamemodify(path, ":p") -    while true do -        local result = func(current_path) -        if result then -            return result -        end -        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 - -local function find_node_modules(path) -    return walk_up(path, function (current_path) -        local node_modules_path = current_path.."/node_modules" -        if vim.fn.isdirectory(node_modules_path) ~= 0 then -            return node_modules_path -        end -        return nil -    end) -end - -local function find_npm_exe(path, exe) -    local node_modules_path = find_node_modules(path) -    if not node_modules_path then return nil end -    local try_exe_path = node_modules_path.."/.bin/"..exe -    local exe_path = get_exe(try_exe_path) -    if exe_path then return clean_path(exe_path) end -    return nil -end - -local function escape_space(str) -    return (string.gsub(str, " ", "\\ " )) -end +local is_win = vim.fn.has("win32") ~= 0  return { -    clean_path = clean_path, -    get_exe = get_exe, -    walk_up = walk_up, -    find_node_modules = find_node_modules, -    find_npm_exe = find_npm_exe, -    escape_space = escape_space +    is_win = is_win  } - diff --git a/configs/nvim/lua/crupest/system/find.lua b/configs/nvim/lua/crupest/system/find.lua new file mode 100644 index 0000000..857c6e7 --- /dev/null +++ b/configs/nvim/lua/crupest/system/find.lua @@ -0,0 +1,55 @@ +local system = require("crupest.system") +local fs = require("crupest.system.fs"); + +local function get_exe(path) +    if system.is_win then +        local exts = { "exe", "CMD", "cmd", "ps1" } +        for _, ext in ipairs(exts) do +            if string.find(path, "%."..ext.."$") and fs.isfile(path) then +                return path +            end +        end +        for _, ext in ipairs(exts) do +            local p = path.."."..ext +            if fs.isfile(p) then return p end +        end +        return nil +    end + +    if vim.fn.executable(path) ~= 0 then +        return path +    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" +        if fs.isdir(node_modules_path) then +            return node_modules_path +        end +        return nil +    end) +end + +local function find_npm_exe(path, exe) +    local node_modules_path = find_node_modules(path) +    if not node_modules_path then return nil end +    local try_exe_path = node_modules_path.."/.bin/"..exe +    local exe_path = get_exe(try_exe_path) +    if exe_path then return exe_path end +    return nil +end + +local function escape_space(str) +    return (string.gsub(str, " ", "\\ " )) +end + +return { +    get_exe = get_exe, +    find_node_modules = find_node_modules, +    find_npm_exe = find_npm_exe, +    escape_space = escape_space +} + diff --git a/configs/nvim/lua/crupest/system/fs.lua b/configs/nvim/lua/crupest/system/fs.lua index 25ce02c..b52a822 100644 --- a/configs/nvim/lua/crupest/system/fs.lua +++ b/configs/nvim/lua/crupest/system/fs.lua @@ -1,11 +1,39 @@ +local function clean_path(path) +    return path and (string.gsub(path, "[/\\]+", "/")) +end + +local function full_path(name) +    return vim.fn.fnamemodify(name, ":p:gs?\\?/?") +end + +local function path_get_dir(path) +    return vim.fn.fnamemodify(path, ":p:h:gs?\\?/?") +end + +local function walk_up(path, func) +    local current_path = full_path(path) +    while true do +        local result = func(current_path) +        if result ~= nil then +            return result +        end +        local new_path = path_get_dir(current_path) +        if new_path == current_path then +            break +        end +        current_path = new_path +    end +    return nil +end +  local function exist(path)      return vim.uv.fs_stat(path)  end  local function isfile(path) -   local s = vim.uv.fs_stat(path) -   if not s then return false end -   return s.type == "file" +    local s = vim.uv.fs_stat(path) +    if not s then return false end +    return s.type == "file"  end  local function isdir(path) @@ -16,24 +44,25 @@ end  local function mkdir(dir)      local parents = {} -    require("crupest/system").walk_up(dir, function(p) + +    walk_up(dir, function(p)          table.insert(parents, 1, p)      end)      for _, v in ipairs(parents) do          if exist(v) and not isdir(v) then -            vim.notify(v.." is not a dir. Can't make dir "..dir, vim.log.levels.ERROR) +            vim.notify(v .. " is not a dir. Can't make dir " .. dir, vim.log.levels.ERROR)              return          end          if not exist(v) then -            vim.notify("Creating dir "..v) +            vim.notify("Creating dir " .. v)              assert(vim.uv.fs_mkdir(v, 504)) -- mode = 0770          end      end  end  local function copy(old, new) -    mkdir(vim.fn.fnamemodify(new, ":p:h")) +    mkdir(path_get_dir(new))      assert(vim.uv.fs_copyfile(old, new))  end @@ -42,11 +71,15 @@ local function remove(path)  end  local function move(old, new) -    mkdir(vim.fn.fnamemodify(new, ":p:h")) +    mkdir(path_get_dir(new))      assert(vim.uv.fs_rename(old, new))  end  return { +    clean_path = clean_path, +    full_path = full_path, +    path_get_dir = path_get_dir, +    walk_up = walk_up,      exist = exist,      isfile = isfile,      isdir = isdir, @@ -55,4 +88,3 @@ return {      remove = remove,      move = move  } -  | 
