diff options
| author | crupest <crupest@outlook.com> | 2024-09-07 03:09:26 +0800 | 
|---|---|---|
| committer | crupest <crupest@outlook.com> | 2024-09-07 03:09:26 +0800 | 
| commit | 4b64e3d37ece2d7251347b3df8551e98363f845f (patch) | |
| tree | 003c295cfd5b2615e566a07db76478aad427b906 /configs/nvim/config-root/lua/crupest/utils | |
| parent | faeef2cd919939ad9a3a276f2c603d7be9acb9ba (diff) | |
| download | crupest-4b64e3d37ece2d7251347b3df8551e98363f845f.tar.gz crupest-4b64e3d37ece2d7251347b3df8551e98363f845f.tar.bz2 crupest-4b64e3d37ece2d7251347b3df8551e98363f845f.zip  | |
config(nvim): create new file structure.
Diffstat (limited to 'configs/nvim/config-root/lua/crupest/utils')
| -rw-r--r-- | configs/nvim/config-root/lua/crupest/utils/find.lua | 152 | ||||
| -rw-r--r-- | configs/nvim/config-root/lua/crupest/utils/fs.lua | 101 | ||||
| -rw-r--r-- | configs/nvim/config-root/lua/crupest/utils/nvim.lua | 44 | ||||
| -rw-r--r-- | configs/nvim/config-root/lua/crupest/utils/table.lua | 42 | 
4 files changed, 339 insertions, 0 deletions
diff --git a/configs/nvim/config-root/lua/crupest/utils/find.lua b/configs/nvim/config-root/lua/crupest/utils/find.lua new file mode 100644 index 0000000..83968d2 --- /dev/null +++ b/configs/nvim/config-root/lua/crupest/utils/find.lua @@ -0,0 +1,152 @@ +local fs = require("crupest.utils.fs"); +local is_win = vim.fn.has("win32") ~= 0 + +local win_exe_exts = { "exe", "CMD", "cmd", "ps1" } + +local function get_exe(path) +    if is_win then +        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(win_exe_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_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) +        if exe then return exe end +    end + +    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" +        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 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/config-root/lua/crupest/utils/fs.lua b/configs/nvim/config-root/lua/crupest/utils/fs.lua new file mode 100644 index 0000000..d34c2f6 --- /dev/null +++ b/configs/nvim/config-root/lua/crupest/utils/fs.lua @@ -0,0 +1,101 @@ +local function clean_path(path) +    if path == "/" then return path end +    path = string.gsub(path, "[/\\]+", "/") +    if string.sub(path, string.len(path)) == '/' then +        path = string.sub(path, 1, string.len(path) - 1) +    end +    return path +end + +local function full_path(name) +    local path = vim.fn.fnamemodify(name, ":p") +    return clean_path(path) +end + +local function escape_space(str) +    return (string.gsub(str, " ", "\\ ")) +end + +local function path_get_dir(path) +    return full_path(vim.fn.fnamemodify(clean_path(path), ":h")) +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" +end + +local function isdir(path) +    local s = vim.uv.fs_stat(path) +    if not s then return false end +    return s.type == "directory" +end + +local function mkdir(dir) +    local parents = {} + +    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) +            return +        end +        if not exist(v) then +            vim.notify("Creating dir " .. v) +            assert(vim.uv.fs_mkdir(v, 504)) -- mode = 0770 +        end +    end +end + +local function copy(old, new) +    mkdir(path_get_dir(new)) +    assert(vim.uv.fs_copyfile(old, new)) +end + +local function remove(path) +    assert(vim.uv.fs_unlink(path)) +end + +local function move(old, new) +    mkdir(path_get_dir(new)) +    assert(vim.uv.fs_rename(old, new)) +end + +return { +    clean_path = clean_path, +    full_path = full_path, +    escape_space = escape_space, +    path_get_dir = path_get_dir, +    walk_up = walk_up, +    exist = exist, +    isfile = isfile, +    isdir = isdir, +    mkdir = mkdir, +    copy = copy, +    remove = remove, +    move = move +} diff --git a/configs/nvim/config-root/lua/crupest/utils/nvim.lua b/configs/nvim/config-root/lua/crupest/utils/nvim.lua new file mode 100644 index 0000000..ac732fd --- /dev/null +++ b/configs/nvim/config-root/lua/crupest/utils/nvim.lua @@ -0,0 +1,44 @@ +local function list_listed_bufs() +    local bufs = vim.api.nvim_list_bufs() +    local result = {} +    for _, v in ipairs(bufs) do +        if vim.fn.buflisted(v) ~= 0 then +            table.insert(result, v) +        end +    end +    return result +end + +-- list the windows that are currently editing the given buffer +local function list_wins_editing_buf(buf) +    local wins = vim.api.nvim_list_wins() +    local result = {} +    for _, win in ipairs(wins) do +        if vim.api.nvim_win_get_buf(win) == buf then +            table.insert(result, win) +        end +    end +    return result +end + +local function buf_is_normal(buf) +    return vim.fn.bufexists(buf) ~= 0 and vim.fn.buflisted(buf) ~= 0 +end + + +local function close_float() +    local wins = vim.api.nvim_list_wins() +    for _, v in ipairs(wins) do +        if vim.api.nvim_win_get_config(v).relative ~= '' then +            vim.api.nvim_win_close(v, false) +        end +    end +end + +return { +    list_listed_bufs = list_listed_bufs, +    buf_is_normal = buf_is_normal, +    list_wins_editing_buf = list_wins_editing_buf, +    close_float = close_float, +} + diff --git a/configs/nvim/config-root/lua/crupest/utils/table.lua b/configs/nvim/config-root/lua/crupest/utils/table.lua new file mode 100644 index 0000000..22419b0 --- /dev/null +++ b/configs/nvim/config-root/lua/crupest/utils/table.lua @@ -0,0 +1,42 @@ +local function remove_element(tbl, element) +    local index = nil +    for i, v in ipairs(tbl) do +        if element == v then +            index = i +            break +        end +    end +    if index then +        table.remove(tbl, index) +    end +    return tbl +end + +local function element_at(tbl, element) +    local at = nil +    for i, v in ipairs(tbl) do +        if element == v then +            at = i +            break +        end +    end +    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, +}  | 
