From 3971ec58038fa8a1fbddc7e440c805ecc61538ed Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 25 Dec 2023 17:31:00 +0800 Subject: [PATCH] refactor(commons)!: migrate to commons 'paths' modules! (#511) refactor(commons)!: migrate to commons 'strings' module! (#511) refactor (commons)!: migrate to commons 'tables' module! (#511) break(fzfx.lib.paths)!: drop 'fzfx.lib.paths'! (#511) break(fzfx.lib.strings)!: drop 'fzfx.lib.strings'! (#511) break(fzfx.lib.tables)!: drop 'fzfx.lib.tables'! (#511) --- .github/workflows/ci.yml | 3 +- bin/general/previewer.lua | 20 +- bin/general/provider.lua | 32 +- docs/REFERENCES.md | 376 ++++++++++++---------- lua/fzfx/cfg/_lsp_locations.lua | 25 +- lua/fzfx/cfg/buffers.lua | 17 +- lua/fzfx/cfg/file_explorer.lua | 7 +- lua/fzfx/cfg/files.lua | 10 +- lua/fzfx/cfg/git_blame.lua | 8 - lua/fzfx/cfg/git_branches.lua | 18 +- lua/fzfx/cfg/git_commits.lua | 12 +- lua/fzfx/cfg/git_files.lua | 11 +- lua/fzfx/cfg/git_live_grep.lua | 8 +- lua/fzfx/cfg/git_status.lua | 11 +- lua/fzfx/cfg/live_grep.lua | 27 +- lua/fzfx/cfg/lsp_definitions.lua | 15 - lua/fzfx/cfg/lsp_diagnostics.lua | 14 +- lua/fzfx/cfg/lsp_implementations.lua | 14 - lua/fzfx/cfg/lsp_incoming_calls.lua | 14 - lua/fzfx/cfg/lsp_outgoing_calls.lua | 14 - lua/fzfx/cfg/lsp_references.lua | 14 - lua/fzfx/cfg/lsp_type_definitions.lua | 14 - lua/fzfx/cfg/vim_commands.lua | 59 ++-- lua/fzfx/cfg/vim_keymaps.lua | 66 ++-- lua/fzfx/commons/strings.lua | 65 +--- lua/fzfx/commons/tables.lua | 5 +- lua/fzfx/commons/version.txt | 2 +- lua/fzfx/config.lua | 6 +- lua/fzfx/detail/fzf_helpers.lua | 11 +- lua/fzfx/detail/general.lua | 41 ++- lua/fzfx/detail/rpcserver.lua | 5 +- lua/fzfx/detail/shell_helpers.lua | 3 +- lua/fzfx/detail/yanks.lua | 7 +- lua/fzfx/helper/actions.lua | 39 +-- lua/fzfx/helper/parsers.lua | 97 +++--- lua/fzfx/helper/previewer_labels.lua | 19 +- lua/fzfx/helper/previewers.lua | 11 +- lua/fzfx/helper/prompts.lua | 8 +- lua/fzfx/helper/providers.lua | 14 +- lua/fzfx/helper/queries.lua | 4 +- lua/fzfx/lib/log.lua | 3 +- lua/fzfx/lib/paths.lua | 60 ---- lua/fzfx/lib/spawn.lua | 205 ------------ lua/fzfx/lib/strings.lua | 253 --------------- lua/fzfx/lib/tables.lua | 58 ---- test/cfg/_lsp_locations_spec.lua | 16 +- test/cfg/buffers_spec.lua | 5 - test/cfg/file_explorer_spec.lua | 36 ++- test/cfg/files_spec.lua | 5 - test/cfg/git_blame_spec.lua | 15 +- test/cfg/git_branches_spec.lua | 10 +- test/cfg/git_commits_spec.lua | 14 +- test/cfg/git_files_spec.lua | 5 - test/cfg/git_live_grep_spec.lua | 8 +- test/cfg/git_status_spec.lua | 14 +- test/cfg/live_grep_spec.lua | 5 - test/cfg/lsp_diagnostics_spec.lua | 18 +- test/cfg/vim_commands_spec.lua | 33 +- test/cfg/vim_keymaps_spec.lua | 13 +- test/config_spec.lua | 10 +- test/detail/fzf_helpers_spec.lua | 22 +- test/detail/general_spec.lua | 63 ++-- test/detail/module_spec.lua | 7 +- test/detail/popup_spec.lua | 16 +- test/helper/actions_spec.lua | 59 ++-- test/helper/contexts_spec.lua | 4 +- test/helper/parsers_spec.lua | 102 +++--- test/helper/previewer_labels_spec.lua | 30 +- test/helper/previewers_spec.lua | 43 +-- test/helper/providers_spec.lua | 21 +- test/lib/commands_spec.lua | 4 +- test/lib/paths_spec.lua | 106 ------- test/lib/spawn_spec.lua | 438 -------------------------- test/lib/strings_spec.lua | 347 -------------------- test/lib/tables_spec.lua | 59 ---- test/schema_spec.lua | 1 - 76 files changed, 804 insertions(+), 2450 deletions(-) delete mode 100644 lua/fzfx/lib/paths.lua delete mode 100644 lua/fzfx/lib/spawn.lua delete mode 100644 lua/fzfx/lib/strings.lua delete mode 100644 lua/fzfx/lib/tables.lua delete mode 100644 test/lib/paths_spec.lua delete mode 100644 test/lib/spawn_spec.lua delete mode 100644 test/lib/strings_spec.lua delete mode 100644 test/lib/tables_spec.lua diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fa859e656..93bc89def 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -55,6 +55,8 @@ jobs: commit_message: "chore(pr): auto-commit" unit_test: name: Unit Test + needs: + - luacheck strategy: matrix: nvim_version: [stable, nightly, v0.7.0] @@ -101,7 +103,6 @@ jobs: name: Release if: ${{ github.ref == 'refs/heads/main' }} needs: - - luacheck - unit_test runs-on: ubuntu-latest steps: diff --git a/bin/general/previewer.lua b/bin/general/previewer.lua index 8ee0e1a83..78f4bfca4 100644 --- a/bin/general/previewer.lua +++ b/bin/general/previewer.lua @@ -6,11 +6,11 @@ if type(SELF_PATH) ~= "string" or string.len(SELF_PATH) == 0 then end vim.opt.runtimepath:append(SELF_PATH) -local tbls = require("fzfx.lib.tables") +local tables = require("fzfx.commons.tables") local fileios = require("fzfx.commons.fileios") local jsons = require("fzfx.commons.jsons") -local strs = require("fzfx.lib.strings") -local spawn = require("fzfx.lib.spawn") +local strings = require("fzfx.commons.strings") +local spawn = require("fzfx.commons.spawn") local shell_helpers = require("fzfx.detail.shell_helpers") shell_helpers.setup("previewer") @@ -68,7 +68,7 @@ shell_helpers.log_debug("metaopts:[%s]", vim.inspect(metaopts)) --- @param l string? local function println(l) if type(l) == "string" and string.len(vim.trim(l)) > 0 then - l = strs.rtrim(l) + l = strings.rtrim(l) io.write(string.format("%s\n", l)) end end @@ -76,7 +76,7 @@ end if metaopts.previewer_type == "command" then local cmd = fileios.readfile(resultfile, { trim = true }) shell_helpers.log_debug("cmd:%s", vim.inspect(cmd)) - if strs.empty(cmd) then + if strings.empty(cmd) then os.exit(0) else os.execute(cmd) @@ -84,24 +84,24 @@ if metaopts.previewer_type == "command" then elseif metaopts.previewer_type == "command_list" then local cmd = fileios.readfile(resultfile, { trim = true }) shell_helpers.log_debug("cmd:%s", vim.inspect(cmd)) - if strs.empty(cmd) then + if strings.empty(cmd) then os.exit(0) return end - local cmd_splits = jsons.decode(cmd) - if tbls.tbl_empty(cmd_splits) then + local cmd_splits = jsons.decode(cmd) --[[ @as string[] ]] + if tables.tbl_empty(cmd_splits) then os.exit(0) return end local sp = - spawn.Spawn:make(cmd_splits, { on_stdout = println, blocking = true }) + spawn.run(cmd_splits, { on_stdout = println, on_stderr = function() end }) shell_helpers.log_ensure( sp ~= nil, "failed to open async command: %s", vim.inspect(cmd_splits) ) - sp:run() + sp:wait() elseif metaopts.previewer_type == "list" then local f = io.open(resultfile, "r") shell_helpers.log_ensure( diff --git a/bin/general/provider.lua b/bin/general/provider.lua index c2cc946c1..226605da3 100644 --- a/bin/general/provider.lua +++ b/bin/general/provider.lua @@ -4,11 +4,11 @@ if type(SELF_PATH) ~= "string" or string.len(SELF_PATH) == 0 then end vim.opt.runtimepath:append(SELF_PATH) -local tbls = require("fzfx.lib.tables") +local tables = require("fzfx.commons.tables") local fileios = require("fzfx.commons.fileios") local jsons = require("fzfx.commons.jsons") -local strs = require("fzfx.lib.strings") -local spawn = require("fzfx.lib.spawn") +local strings = require("fzfx.commons.strings") +local spawn = require("fzfx.commons.spawn") local shell_helpers = require("fzfx.detail.shell_helpers") shell_helpers.setup("provider") @@ -68,18 +68,20 @@ vim.opt.runtimepath:append(vim.fn.stdpath("config")) --- @type {decorate:fun(line:string):string} local decorator_module = nil if metaopts.provider_decorator ~= nil then - if strs.not_empty(tbls.tbl_get(metaopts.provider_decorator, "rtp")) then - vim.opt.runtimepath:append(tbls.tbl_get(metaopts.provider_decorator, "rtp")) + if strings.not_empty(tables.tbl_get(metaopts.provider_decorator, "rtp")) then + vim.opt.runtimepath:append( + tables.tbl_get(metaopts.provider_decorator, "rtp") + ) end shell_helpers.log_ensure( - strs.not_empty(tbls.tbl_get(metaopts.provider_decorator, "module")), + strings.not_empty(tables.tbl_get(metaopts.provider_decorator, "module")), "decorator module cannot be empty: %s", vim.inspect(metaopts.provider_decorator) ) local module_name = metaopts.provider_decorator.module local ok, module_or_err = pcall(require, module_name) shell_helpers.log_ensure( - ok and tbls.tbl_not_empty(module_or_err), + ok and tables.tbl_not_empty(module_or_err), "failed to load decorator:%s, error:%s", vim.inspect(metaopts.provider_decorator), vim.inspect(module_or_err) @@ -89,11 +91,11 @@ end --- @param line string? local function println(line) - if strs.empty(line) then + if strings.empty(line) then return end - line = strs.rtrim(line --[[@as string]]) - if tbls.tbl_not_empty(decorator_module) then + line = strings.rtrim(line --[[@as string]]) + if tables.tbl_not_empty(decorator_module) then -- shell_helpers.log_debug("decorate line:%s", vim.inspect(line)) vim.schedule(function() local rendered_ok, rendered_line_or_err = @@ -117,7 +119,7 @@ if metaopts.provider_type == "plain" or metaopts.provider_type == "command" then --- @type string local cmd = fileios.readfile(resultfile, { trim = true }) --[[@as string]] shell_helpers.log_debug("plain/command cmd:%s", vim.inspect(cmd)) - if cmd == nil or string.len(cmd) == 0 then + if strings.empty(cmd) then os.exit(0) return end @@ -140,25 +142,25 @@ elseif then local cmd = fileios.readfile(resultfile, { trim = true }) --[[@as string]] shell_helpers.log_debug("plain_list/command_list cmd:%s", vim.inspect(cmd)) - if cmd == nil or string.len(cmd) == 0 then + if strings.empty(cmd) then os.exit(0) return end local cmd_splits = jsons.decode(cmd) --[[@as table]] - if tbls.tbl_empty(cmd_splits) then + if tables.tbl_empty(cmd_splits) then os.exit(0) return end local sp = - spawn.Spawn:make(cmd_splits, { on_stdout = println, blocking = true }) + spawn.run(cmd_splits, { on_stdout = println, on_stderr = function() end }) shell_helpers.log_ensure( sp ~= nil, "failed to open async command: %s", vim.inspect(cmd_splits) ) - sp:run() + sp:wait() elseif metaopts.provider_type == "list" then local reader = fileios.FileLineReader:open(resultfile) --[[@as commons.FileLineReader ]] shell_helpers.log_ensure( diff --git a/docs/REFERENCES.md b/docs/REFERENCES.md index df28edd81..847abd047 100644 --- a/docs/REFERENCES.md +++ b/docs/REFERENCES.md @@ -97,6 +97,7 @@ Each line is a git **branch name**. It's implemented with `git_branch` utilities: - [fzfx.helper.parsers.parse_git_branch](#parse_git_branch) +- [fzfx.helper.actions.git_checkout](#git_checkout) ### [git_commits](https://github.com/linrongbin16/fzfx.nvim/blob/main/lua/fzfx/cfg/git_commits.lua) @@ -109,6 +110,7 @@ Each line starts with a git **commit number**. It's implemented with `git_commit` utilities: - [fzfx.helper.parsers.parse_git_commit](#parse_git_commit) +- [fzfx.helper.actions.yank_git_commit](#yank_git_commit) ### [git_files](https://github.com/linrongbin16/fzfx.nvim/blob/main/lua/fzfx/cfg/git_files.lua) @@ -209,6 +211,7 @@ Each line is constructed with command **name**, **attributes** and **definition/ It's implemented with `vim_command` utilities: - [fzfx.helper.parsers.parse_vim_command](#parse_vim_command) +- [fzfx.helper.actions.feed_vim_command](#feed_vim_command) ### [vim_keymaps](https://github.com/linrongbin16/fzfx.nvim/blob/main/lua/fzfx/cfg/vim_keymaps.lua) @@ -221,6 +224,7 @@ Each line is constructed with key mapping **left hands**, **attributes** and **d It's implemented with `vim_keymap` utilities: - [fzfx.helper.parsers.parse_vim_keymap](#parse_vim_keymap) +- [fzfx.helper.actions.feed_vim_key](#feed_vim_key) ## [fzfx.helper](https://github.com/linrongbin16/fzfx.nvim/lua/fzfx/helper) @@ -306,7 +310,9 @@ Use `setqflist` command to send selected locations to qflist. Use `edit` and `setpos` command to open selected locations on `grep`, `git grep` results. -- `setqflist_grep(lines:string[], context:fzfx.PipelineContext):nil`: use `setqflist` command to send selected locations to qflist. +#### [`setqflist_grep`](https://github.com/linrongbin16/fzfx.nvim/blob/cdb88202d551f3986f6b2240908b975697e0f511/lua/fzfx/helper/actions.lua?plain=1#L178) + +Use `setqflist` command to send selected locations to qflist. #### `git status` @@ -319,61 +325,25 @@ The `git status` generated git status (changed file names), used by `FzfxGStatus ?? ../hello ``` -- `edit_git_status(lines:string[]):nil`: use `edit` command to open selected file names on `git status` results. - -#### `git branch` - -The `git branch (-r/-a)` generated git branches, used by `FzfxGBranches`. they look like: - -``` -* chore-lint - main - remotes/origin/HEAD -> origin/main - remotes/origin/chore-lint - remotes/origin/main -``` - -- `git_checkout(lines:string[], fzfx.GitBranchesPipelineContext):nil`: use `git checkout` shell command to checkout selected branch on `git branch` results. +#### [`edit_git_status`](https://github.com/linrongbin16/fzfx.nvim/blob/cdb88202d551f3986f6b2240908b975697e0f511/lua/fzfx/helper/actions.lua?plain=1#L372) -#### `git log`/`git blame` +Use `edit` command to open selected file names on `git status` results. -The `git log --short`, `git blame` generated git commits, used by `FzfxGCommits`, `FzfxGBlame`, they look like: +#### [`git_checkout`](https://github.com/linrongbin16/fzfx.nvim/blob/a8dff47d1c56385f4f2bb4c5f53e521fdbf3b2d7/lua/fzfx/helper/actions.lua?plain=1#L245) -``` -c2e32c 2023-11-30 linrongbin16 (HEAD -> chore-lint) -5fe6ad 2023-11-29 linrongbin16 chore -``` +Use `git checkout` shell command to checkout selected branch on `git branch` results. -- `yank_git_commit(lines:string[]):nil`: yank selected git commits on `git log`, `git blame` results. +#### [`yank_git_commit`](https://github.com/linrongbin16/fzfx.nvim/blob/a8dff47d1c56385f4f2bb4c5f53e521fdbf3b2d7/lua/fzfx/helper/actions.lua?plain=1#L265) -#### Builtin Vim Commands Renderer +Yank selected git commits on `git log`, `git blame` results. -The builtin renderer generated vim commands, used by `FzfxCommands`. they look like: +#### [`feed_vim_command`](https://github.com/linrongbin16/fzfx.nvim/blob/a8dff47d1c56385f4f2bb4c5f53e521fdbf3b2d7/lua/fzfx/helper/actions.lua?plain=1#L288) -``` -Name Bang|Bar|Nargs|Range|Complete Desc/Location -:! N |Y |N/A |N/A |N/A /opt/homebrew/Cellar/neovim/0.9.4/share/nvim/runtime/doc/index.txt:1122 -:Next N |Y |N/A |N/A |N/A /opt/homebrew/Cellar/neovim/0.9.4/share/nvim/runtime/doc/index.txt:1124 -:bdelete N |Y |N/A |N/A |N/A "delete buffer" -``` +Feed selected vim command to vim command line. -- `feed_vim_command(lines:string[], context:fzfx.VimCommandsPipelineContext):nil`: input selected command in vim command line. +#### [`feed_vim_key`](https://github.com/linrongbin16/fzfx.nvim/blob/a8dff47d1c56385f4f2bb4c5f53e521fdbf3b2d7/lua/fzfx/helper/actions.lua?plain=1#L336) -#### Builtin Vim Key Mappings Renderer - -The builtin renderer generated vim key mappings, used by `FzfxKeyMaps`. they look like: - -``` -Lhs Mode|Noremap|Nowait|Silent Rhs/Location - |N |N |N ~/.config/nvim/lazy/nvim-cmp/lua/cmp/utils/keymap.lua:127 - |N |N |N ~/.config/nvim/lazy/nvim-cmp/lua/cmp/utils/keymap.lua:127 -(YankyGPutAfterShiftRight) n |Y |N |Y ~/.config/nvim/lazy/yanky.nvim/lua/yanky.lua:369 -% n |N |N |Y "(matchup-%)" -& n |Y |N |N ":&&" -<2-LeftMouse> n |N |N |Y "(matchup-double-click)" -``` - -- `feed_vim_key(lines:string[], context:fzfx.VimKeyMapsPipelineContext):nil`: execute selected keys. +Execute selected vim key mappings. #### `eza`/`lsd`/`ls` @@ -387,7 +357,9 @@ drwxr-xr-x 4 linrongbin staff 128B Sep 22 10:11 bin -rw-r--r-- 1 linrongbin staff 120B Sep 5 14:14 codecov.yml ``` -- `edit_ls(lines:string[], context:fzfx.FileExplorerPipelineContext):nil`: use `edit` command to open selected file path on `lsd`, `eza`, `ls` results. +#### [`edit_ls`](https://github.com/linrongbin16/fzfx.nvim/blob/a8dff47d1c56385f4f2bb4c5f53e521fdbf3b2d7/lua/fzfx/helper/actions.lua?plain=1#L215) + +Use `edit` command to open selected file path on `ls`/`lsd`/`eza`/`exa` results. ### [fzfx.helper.previewer_labels](/lua/fzfx/helper/previewer_labels.lua) @@ -483,100 +455,199 @@ drwxr-xr-x 4 linrongbin staff 128B Sep 22 10:11 bin ### [fzfx.lib.commands](/lua/fzfx/lib/commands.lua) -- `CommandResult`: command line result +#### [`CommandResult`](https://github.com/linrongbin16/fzfx.nvim/blob/b9e31389cfc9ba816efa6603b96eabc5a2320ce1/lua/fzfx/lib/commands.lua?plain=1#L14) + +The command line result. + +Contains below fields: + +- `stdout:string[]|nil`: stdout lines. +- `stderr:string[]|nil`: stderr lines. +- `code:integer?`: exit code. +- `signal:integer?`: signal. + +Contains below methods: + +- `failed():boolean`: exit code `code ~= 0` and `stderr` not empty. - - fields: - - `stdout:string[]|nil`: stdout lines. - - `stderr:string[]|nil`: stderr lines. - - `code:integer?`: exit code. - - `signal:integer?`: signal. - - `failed():boolean`: exit code `code ~= 0` and `stderr` not empty. +#### [`Command`](https://github.com/linrongbin16/fzfx.nvim/blob/b9e31389cfc9ba816efa6603b96eabc5a2320ce1/lua/fzfx/lib/commands.lua?plain=1#L50) -- `Command`: command line (blocking mode spawn). +The command line (sync/blocking mode spawn.run). - - `run(cmds:string[]):Command`: run command line, return handle. - - `failed():boolean`: same with `CommandResult`, use `Command.result` to get command line result. +Contains below methods: -- `GitRootCommand` +- `run(cmds:string[]):Command`: run command line, return handle. +- `failed():boolean`: same with `CommandResult`, use `Command.result` to get command line result. - - `run():GitRootCommand`: run `git rev-parse --show-toplevel`, return handle. - - `failed():boolean`: same with `Command`, use `GitRootCommand.result` to get command line result. - - `output():string?`: get the command output. +#### [`GitRootCommand`](https://github.com/linrongbin16/fzfx.nvim/blob/b9e31389cfc9ba816efa6603b96eabc5a2320ce1/lua/fzfx/lib/commands.lua?plain=1#L98) -- `GitBranchesCommand` +Find git repository root directory. - - `run(remotes:boolean?):GitBranchesCommand`: run `git branch` or `git branch --remotes`, return handle. - - `failed():boolean`: same with `Command`, use `GitBranchesCommand.result` to get command line result. - - `output():string[]|nil`: get the command output. +Contains below methods: -- `GitCurrentBranchCommand` - - `run():GitCurrentBranchCommand`: run `git rev-parse --abbrev-ref HEAD`, return handle. - - `failed():boolean`: same with `Command`, use `GitCurrentBranchCommand.result` to get command line result. - - `output():string?`: get the command output. +- `run():GitRootCommand`: run `git rev-parse --show-toplevel`, return handle. +- `failed():boolean`: same with `Command`, use `GitRootCommand.result` to get command line result. +- `output():string?`: get the command output. + +#### [`GitBranchesCommand`](https://github.com/linrongbin16/fzfx.nvim/blob/b9e31389cfc9ba816efa6603b96eabc5a2320ce1/lua/fzfx/lib/commands.lua?plain=1#L134) + +Find git repository local or remote branches. + +Contains below methods: + +- `run(remotes:boolean?):GitBranchesCommand`: run `git branch` or `git branch --remotes`, return handle. +- `failed():boolean`: same with `Command`, use `GitBranchesCommand.result` to get command line result. +- `output():string[]|nil`: get the command output. + +#### [`GitCurrentBranchCommand`](https://github.com/linrongbin16/fzfx.nvim/blob/b9e31389cfc9ba816efa6603b96eabc5a2320ce1/lua/fzfx/lib/commands.lua?plain=1#L171) + +Find git repository current branch name. + +Contains below methods: + +- `run():GitCurrentBranchCommand`: run `git rev-parse --abbrev-ref HEAD`, return handle. +- `failed():boolean`: same with `Command`, use `GitCurrentBranchCommand.result` to get command line result. +- `output():string?`: get the command output. ### [fzfx.lib.constants](/lua/fzfx/lib/constants.lua) #### OS -- `IS_WINDOWS`: is Windows. -- `IS_MACOS`: is macOS. -- `IS_BSD`: is BSD. -- `IS_LINUX`: is UNIX or Linux. +##### `IS_WINDOWS` + +Whether is Windows. + +##### `IS_MACOS` + +Whether is macOS. + +##### `IS_BSD` + +Whether is BSD. + +##### `IS_LINUX` + +Whether is UNIX or Linux. + +#### Executables + +##### `HAS_BAT` + +Whether has `bat` command. + +##### `BAT` + +`bat` or `batcat` command. + +##### `HAS_CAT` + +Whether has `cat` command. + +##### `CAT` -#### Command Line +`cat` command. -bat/cat +##### `HAS_RG` -- `HAS_BAT`: has `bat` command. -- `BAT`: `bat` command. -- `HAS_CAT`: has `cat` command. -- `CAT`: `cat` command. +Whether has `rg` command. -rg/grep +##### `RG` -- `HAS_RG`: has `rg` command. -- `RG`: `rg` command. -- `HAS_GNU_GREP`: has gnu `grep`/`ggrep` command. -- `GNU_GREP`: `grep`/`ggrep` command. -- `HAS_GREP`: has `grep`/`ggrep` command. -- `GREP`: `grep`/`ggrep` command. +`rg` command. -fd/find +##### `HAS_GNU_GREP` -- `HAS_FD`: has `fd` command. -- `FD`: `fd` command. -- `HAS_FIND`: has `find`/`gfind` command. -- `FIND`: `find`/`gfind` command. +Whether has gnu `grep`/`ggrep` command. -ls/lsd/eza +##### `GNU_GREP` -- `HAS_LS`: has `ls` command. -- `LS`: `ls` command. -- `HAS_LSD`: has `lsd` command. -- `LSD`: `lsd` command. -- `HAS_EZA`: has `eza`/`exa` command. -- `EZA`: `eza`/`exa` command. +`grep`/`ggrep` command. -git/delta +Whether `HAS_GREP` -- `HAS_GIT`: has `git` command. -- `GIT`: `git` command. -- `HAS_DELTA`: has `delta` command. -- `DELTA`: `delta` command. +Whether has `grep`/`ggrep` command. -echo +##### `GREP` -- `HAS_ECHO`: has `echo` command. -- `ECHO`: `echo` command. +`grep`/`ggrep` command. -curl +##### `HAS_FD` -- `HAS_CURL`: has `curl` command. -- `CURL`: `curl` command. +Whether has `fd` command. + +##### `FD` + +`fd` or `fdfind` command. + +##### `HAS_FIND` + +Whether has `find`/`gfind` command. + +##### `FIND` + +`find` or `gfind` command. + +##### `HAS_LS` + +Whether has `ls` command. + +##### `LS` + +`ls` command. + +##### `HAS_LSD` + +Whether has `lsd` command. + +##### `LSD` + +`lsd` command. + +##### `HAS_EZA` + +Whether has `eza`/`exa` command. + +##### `EZA` + +`eza`/`exa` command. + +##### `HAS_GIT` + +Whether has `git` command. + +##### `GIT` + +`git` command. + +##### `HAS_DELTA` + +Whether has `delta` command. + +##### `DELTA` + +`delta` command. + +##### `HAS_ECHO` + +Whether has `echo` command. + +##### `ECHO` + +`echo` command. + +##### `HAS_CURL` + +Whether has `curl` command. + +##### `CURL` + +`curl` command. ### [fzfx.lib.deprecations](/lua/fzfx/lib/deprecations.lua) -- `notify(fmt:string, ...:any):nil`: print deprecation notifications to command line. +#### [`notify`](https://github.com/linrongbin16/fzfx.nvim/blob/b9e31389cfc9ba816efa6603b96eabc5a2320ce1/lua/fzfx/lib/deprecations.lua?plain=1#L7) + +Print deprecation notifications. ### [fzfx.lib.env](/lua/fzfx/lib/env.lua) @@ -585,17 +656,27 @@ curl ### [fzfx.lib.log](/lua/fzfx/lib/log.lua) -- `debug(fmt:string, ...)`: debug, the `fmt` is formatting messages in C style formatters, e.g. `%d`, `%s`. -- `info(fmt:string, ...)`: info. -- `warn(fmt:string, ...)`: warning. -- `err(fmt:string, ...)`: error -- `echo(level:LogLevels, fmt:string, ...)`: echo message in log `level`. this API will not been affected by log configs. - - `LogLevels.DEBUG`. - - `LogLevels.INFO`. - - `LogLevels.WARN`. - - `LogLevels.ERROR`. -- `throw(fmt:string, ...)`: same with `err`, additionally it invokes `error()` API, which throw an error to user command line, requires user to press `ENTER` to continue. -- `ensure(condition:boolean, fmt:string, ...)`: throw error to user if `condition` is false. +!> This module requires initialize before using its APIs, except the [echo](#echo) API. + +#### [`LogLevels`/`LogLevelNames`](https://github.com/linrongbin16/fzfx.nvim/blob/b9e31389cfc9ba816efa6603b96eabc5a2320ce1/lua/fzfx/lib/log.lua?plain=1#L5-L7) + +The logging level integer values and names. + +#### [`echo`](https://github.com/linrongbin16/fzfx.nvim/blob/b9e31389cfc9ba816efa6603b96eabc5a2320ce1/lua/fzfx/lib/log.lua?plain=1#L21) + +Echo message in log `level`. this API doesn't require initialize `fzfx.lib.log` (e.g. `setup`). + +#### [`debug`/`info`/`warn`/`err`](https://github.com/linrongbin16/fzfx.nvim/blob/b9e31389cfc9ba816efa6603b96eabc5a2320ce1/lua/fzfx/lib/log.lua?plain=1#L70-L134) + +Log debug, the message format placeholder `fmt` support lua string format, e.g. `%d`, `%s` placeholders. + +#### [`throw`](https://github.com/linrongbin16/fzfx.nvim/blob/b9e31389cfc9ba816efa6603b96eabc5a2320ce1/lua/fzfx/lib/log.lua?plain=1#L139) + +Same with `err`, additionally it throws an error to user via lua `error()`. + +#### [`ensure`](https://github.com/linrongbin16/fzfx.nvim/blob/b9e31389cfc9ba816efa6603b96eabc5a2320ce1/lua/fzfx/lib/log.lua?plain=1#L158) + +Write error logs and throw error to user only if `condition` is false. ### [fzfx.lib.bufs](https://github.com/linrongbin16/fzfx.nvim/lua/fzfx/lib/bufs.lua) @@ -608,54 +689,3 @@ Whether buffer is valid. #### [`shellescape`](https://github.com/linrongbin16/fzfx.nvim/blob/4a0fd372be81a5aa506c32c2cacb78a279b460e5/lua/fzfx/lib/shells.lua?plain=1#L71) Cross-platform escape shell strings. - -### [fzfx.lib.paths](/lua/fzfx/lib/paths.lua) - -- `SEPARATOR`: `\\` for Windows, `/` for Unix/Linux. -- `normalize(p:string, opts:{backslash:boolean?,expand:boolean?}?)`: normalize path string, replace `\\\\` to `\\`. - - set `opts.backslash=true` to replace `\\` to `/`, set `opts.expand=true` to expand path to full path, by default `opts={backslash=false, expand=false}`. -- `join(...):string`: join multiple parts into path string with `SEPARATOR`. -- `reduce2home(p:string):string`: reduce path string relative to `$HOME` directory. -- `reduce(p:string):string`: reduce path string relative to `$HOME` directory or `$PWD` directory. -- `shorten(p:string):string`: shorten path string to use single char to replace each level directories, e.g. `~/g/l/fzfx.nvim`. - -### [fzfx.lib.spawn](/lua/fzfx/lib/spawn.lua) - -- `Spawn`: run child process and process stdout/stderr line by line. - - `make(cmds:string[], opts:{on_stdout:fun(line:string):any, on_stderr:fun(line:string):any|nil, blocking:boolean}):Spawn`: prepare child process, return `Spawn` handle. - - `on_stdout(line:string):any`: invoke callback when there's a new line ready to process on `stdout` fd. - - `on_stderr(line:string):any`: invoke callback when there's a new line ready to process on `stderr` fd. - - `blocking`: set `blocking=true` if need to wait for child process finish, set `blocking=false` if no need to wait. - - `run():nil`: run child process, wait child process done for blocking mode, use `Spawn.result` to get the child process result. - -### [fzfx.lib.strings](/lua/fzfx/lib/strings.lua) - -- `empty(s:string?):boolean`/`not_empty(s:string?):boolean`: detect whether a string is empty or not. -- `blank(s:string?):boolean`/`not_blank(s:string?):boolean`: detect whether a string is blank or not. -- `find(s:string, t:string, start:integer?):integer?`: find first `t` in `s` start from `start`, by default `start=1`. -- `rfind(s:string, t:string, rstart:integer?):integer?`: reversely find last `t` in `s` start from `rstart`, by default `rstart=#s`. -- `ltrim(s:string, t:string):string`/`rtrim(s:string, t:string):string`: trim left/right `t` from `s`, by default `t` is whitespaces (`\n\t\r `). -- `split(s:string, delimiter:string, opts:{plain:boolean?,trimempty:boolean?}?):string`: split `s` by `delimiter`. - - set `opts.plain=false` to use lua pattern matching, set `opts.trimempty=false` to not remove whitespaces from results. by default `opts={plain=true, trimempty=true}`. -- `startswith(s:string, t:string):boolean`/`endswith(s:string, t:string):boolean`: detect whether `s` is start/end with `t`. -- `isspace(c:string):boolean`: detect whether character `c` is whitespace (`\n\t\r `), `c` length must be 1. -- `isalnum(c:string):boolean`: detect whether character `c` is letter or number (`a-zA-Z0-9`), `c` length must be 1. -- `isdigit(c:string):boolean`: detect whether character `c` is number (`0-9`), `c` length must be 1. -- `ishex(c:string):boolean`: detect whether character `c` is hex number (`a-eA-E0-9`), `c` length must be 1. -- `isalpha(c:string):boolean`: detect whether character `c` is letter (`a-zA-Z`), `c` length must be 1. -- `islower(c:string):boolean`/`isupper(c:string):boolean`: detect whether character `c` is lower letter (`a-z`) or upper letter (`A-Z`), `c` length must be 1. -- `uuid(delimiter:string?):string`: make uuid, by default `delimiter='-'`. - -### [fzfx.lib.tables](/lua/fzfx/lib/tables.lua) - -#### Table - -- `tbl_empty(t:any):boolean`/`tbl_not_empty(t:any):boolean`: detect whether a table is empty or not. -- `tbl_get(t:any, field:string):any`: retrieve value from table, with json-like field indexing via dot `.` delimiter, for example when parameter `t = {a = { b = 1 }}`, `field = 'a.b'`, this function will return `1`. When `field = ''` returns `t` itself, when field not exist returns `nil`. - -#### List - -- `list_empty(l:any):boolean`/`list_not_empty(l:any):boolean`: detect whether a list(table) is empty or not. -- `list_index(i:integer, n:integer):integer`: calculate list index for both positive or negative. `n` is the length of list. - - if `i > 0`, `i` is in range `[1,n]`. - - if `i < 0`, `i` is in range `[-1,-n]`, `-1` maps to last position (e.g. `n`), `-n` maps to first position (e.g. `1`). diff --git a/lua/fzfx/cfg/_lsp_locations.lua b/lua/fzfx/cfg/_lsp_locations.lua index dc20b9ea9..2bca33797 100644 --- a/lua/fzfx/cfg/_lsp_locations.lua +++ b/lua/fzfx/cfg/_lsp_locations.lua @@ -1,24 +1,17 @@ -local consts = require("fzfx.lib.constants") -local strs = require("fzfx.lib.strings") -local cmds = require("fzfx.lib.commands") +local tables = require("fzfx.commons.tables") local termcolors = require("fzfx.commons.termcolors") -local paths = require("fzfx.lib.paths") +local paths = require("fzfx.commons.paths") local fileios = require("fzfx.commons.fileios") -local tbls = require("fzfx.lib.tables") + local log = require("fzfx.lib.log") local LogLevels = require("fzfx.lib.log").LogLevels -local contexts_helper = require("fzfx.helper.contexts") -local parsers_helper = require("fzfx.helper.parsers") -local queries_helper = require("fzfx.helper.queries") local actions_helper = require("fzfx.helper.actions") local labels_helper = require("fzfx.helper.previewer_labels") local providers_helper = require("fzfx.helper.providers") local previewers_helper = require("fzfx.helper.previewers") -local ProviderTypeEnum = require("fzfx.schema").ProviderTypeEnum local PreviewerTypeEnum = require("fzfx.schema").PreviewerTypeEnum -local CommandFeedEnum = require("fzfx.schema").CommandFeedEnum local M = {} @@ -157,7 +150,7 @@ M._make_lsp_locations_provider = function(opts) --- @return string[]|nil local function impl(query, context) local lsp_clients = vim.lsp.get_active_clients({ bufnr = context.bufnr }) - if tbls.tbl_empty(lsp_clients) then + if tables.tbl_empty(lsp_clients) then log.echo(LogLevels.INFO, "no active lsp clients.") return nil end @@ -192,7 +185,7 @@ M._make_lsp_locations_provider = function(opts) log.echo(LogLevels.ERROR, err) return nil end - if tbls.tbl_empty(response) then + if tables.tbl_empty(response) then log.echo(LogLevels.INFO, "no lsp locations found.") return nil end @@ -203,7 +196,7 @@ M._make_lsp_locations_provider = function(opts) do if client_id ~= nil - and tbls.tbl_not_empty(tbls.tbl_get(client_response, "result")) + and tables.tbl_not_empty(tables.tbl_get(client_response, "result")) then local lsp_loc = client_response.result if M._is_lsp_location(lsp_loc) then @@ -222,7 +215,7 @@ M._make_lsp_locations_provider = function(opts) end end - if tbls.tbl_empty(results) then + if tables.tbl_empty(results) then log.echo(LogLevels.INFO, "no lsp locations found.") return nil end @@ -359,7 +352,7 @@ M._make_lsp_call_hierarchy_provider = function(opts) local function impl(query, context) ---@diagnostic disable-next-line: deprecated local lsp_clients = vim.lsp.get_active_clients({ bufnr = context.bufnr }) - if tbls.tbl_empty(lsp_clients) then + if tables.tbl_empty(lsp_clients) then log.echo(LogLevels.INFO, "no active lsp clients.") return nil end @@ -482,7 +475,7 @@ M._make_lsp_call_hierarchy_provider = function(opts) end end - if tbls.tbl_empty(results) then + if tables.tbl_empty(results) then log.echo(LogLevels.INFO, "no lsp call hierarchy found.") return nil end diff --git a/lua/fzfx/cfg/buffers.lua b/lua/fzfx/cfg/buffers.lua index 372ba4a54..579ffd48a 100644 --- a/lua/fzfx/cfg/buffers.lua +++ b/lua/fzfx/cfg/buffers.lua @@ -1,18 +1,13 @@ -local consts = require("fzfx.lib.constants") -local strs = require("fzfx.lib.strings") +local strings = require("fzfx.commons.strings") local apis = require("fzfx.commons.apis") +local paths = require("fzfx.commons.paths") + +local consts = require("fzfx.lib.constants") local bufs = require("fzfx.lib.bufs") -local cmds = require("fzfx.lib.commands") -local paths = require("fzfx.lib.paths") -local tbls = require("fzfx.lib.tables") -local log = require("fzfx.lib.log") -local LogLevels = require("fzfx.lib.log").LogLevels local parsers_helper = require("fzfx.helper.parsers") -local queries_helper = require("fzfx.helper.queries") local actions_helper = require("fzfx.helper.actions") local labels_helper = require("fzfx.helper.previewer_labels") -local providers_helper = require("fzfx.helper.providers") local previewers_helper = require("fzfx.helper.previewers") local ProviderTypeEnum = require("fzfx.schema").ProviderTypeEnum @@ -96,7 +91,7 @@ M._buffers_provider = function(query, context) local current_path = M._buf_valid(context.bufnr) and paths.reduce(vim.api.nvim_buf_get_name(context.bufnr)) or nil - if strs.not_empty(current_path) then + if strings.not_empty(current_path) then table.insert(bufpaths, current_path) end @@ -130,7 +125,7 @@ M._delete_buffer = function(line) local bufpath = paths.reduce(vim.api.nvim_buf_get_name(bufnr)) filenames[bufpath] = bufnr end - if strs.not_empty(line) then + if strings.not_empty(line) then local parsed = parsers_helper.parse_find(line) local bufnr = filenames[parsed.filename] if type(bufnr) == "number" and bufs.buf_is_valid(bufnr) then diff --git a/lua/fzfx/cfg/file_explorer.lua b/lua/fzfx/cfg/file_explorer.lua index 820e734d2..26ae04417 100644 --- a/lua/fzfx/cfg/file_explorer.lua +++ b/lua/fzfx/cfg/file_explorer.lua @@ -1,18 +1,13 @@ local consts = require("fzfx.lib.constants") -local strs = require("fzfx.lib.strings") local shells = require("fzfx.lib.shells") -local cmds = require("fzfx.lib.commands") -local paths = require("fzfx.lib.paths") +local paths = require("fzfx.commons.paths") local fileios = require("fzfx.commons.fileios") -local tbls = require("fzfx.lib.tables") local log = require("fzfx.lib.log") local LogLevels = require("fzfx.lib.log").LogLevels local parsers_helper = require("fzfx.helper.parsers") -local queries_helper = require("fzfx.helper.queries") local actions_helper = require("fzfx.helper.actions") local labels_helper = require("fzfx.helper.previewer_labels") -local providers_helper = require("fzfx.helper.providers") local previewers_helper = require("fzfx.helper.previewers") local ProviderTypeEnum = require("fzfx.schema").ProviderTypeEnum diff --git a/lua/fzfx/cfg/files.lua b/lua/fzfx/cfg/files.lua index f1b435075..747224a91 100644 --- a/lua/fzfx/cfg/files.lua +++ b/lua/fzfx/cfg/files.lua @@ -1,19 +1,11 @@ local consts = require("fzfx.lib.constants") -local strs = require("fzfx.lib.strings") -local cmds = require("fzfx.lib.commands") -local paths = require("fzfx.lib.paths") -local tbls = require("fzfx.lib.tables") -local log = require("fzfx.lib.log") -local LogLevels = require("fzfx.lib.log").LogLevels +local paths = require("fzfx.commons.paths") -local parsers_helper = require("fzfx.helper.parsers") -local queries_helper = require("fzfx.helper.queries") local actions_helper = require("fzfx.helper.actions") local labels_helper = require("fzfx.helper.previewer_labels") local providers_helper = require("fzfx.helper.providers") local previewers_helper = require("fzfx.helper.previewers") -local ProviderTypeEnum = require("fzfx.schema").ProviderTypeEnum local PreviewerTypeEnum = require("fzfx.schema").PreviewerTypeEnum local CommandFeedEnum = require("fzfx.schema").CommandFeedEnum diff --git a/lua/fzfx/cfg/git_blame.lua b/lua/fzfx/cfg/git_blame.lua index f4ce507e6..6923e5fff 100644 --- a/lua/fzfx/cfg/git_blame.lua +++ b/lua/fzfx/cfg/git_blame.lua @@ -1,22 +1,14 @@ local consts = require("fzfx.lib.constants") -local strs = require("fzfx.lib.strings") local shells = require("fzfx.lib.shells") local bufs = require("fzfx.lib.bufs") local cmds = require("fzfx.lib.commands") -local paths = require("fzfx.lib.paths") -local tbls = require("fzfx.lib.tables") local log = require("fzfx.lib.log") local LogLevels = require("fzfx.lib.log").LogLevels -local parsers_helper = require("fzfx.helper.parsers") -local queries_helper = require("fzfx.helper.queries") local actions_helper = require("fzfx.helper.actions") -local labels_helper = require("fzfx.helper.previewer_labels") -local providers_helper = require("fzfx.helper.providers") local previewers_helper = require("fzfx.helper.previewers") local ProviderTypeEnum = require("fzfx.schema").ProviderTypeEnum -local PreviewerTypeEnum = require("fzfx.schema").PreviewerTypeEnum local CommandFeedEnum = require("fzfx.schema").CommandFeedEnum local M = {} diff --git a/lua/fzfx/cfg/git_branches.lua b/lua/fzfx/cfg/git_branches.lua index 5ab01a32e..8f698305f 100644 --- a/lua/fzfx/cfg/git_branches.lua +++ b/lua/fzfx/cfg/git_branches.lua @@ -1,21 +1,15 @@ +local tables = require("fzfx.commons.tables") +local strings = require("fzfx.commons.strings") + local consts = require("fzfx.lib.constants") -local strs = require("fzfx.lib.strings") local shells = require("fzfx.lib.shells") local cmds = require("fzfx.lib.commands") -local paths = require("fzfx.lib.paths") -local tbls = require("fzfx.lib.tables") local log = require("fzfx.lib.log") local LogLevels = require("fzfx.lib.log").LogLevels -local parsers_helper = require("fzfx.helper.parsers") -local queries_helper = require("fzfx.helper.queries") local actions_helper = require("fzfx.helper.actions") -local labels_helper = require("fzfx.helper.previewer_labels") -local providers_helper = require("fzfx.helper.providers") -local previewers_helper = require("fzfx.helper.previewers") local ProviderTypeEnum = require("fzfx.schema").ProviderTypeEnum -local PreviewerTypeEnum = require("fzfx.schema").PreviewerTypeEnum local CommandFeedEnum = require("fzfx.schema").CommandFeedEnum local M = {} @@ -146,7 +140,7 @@ M._make_git_branches_provider = function(opts) string.format("* %s", git_current_branch_cmd:output()) ) local git_branches_cmd = cmds.GitBranchesCommand:run( - tbls.tbl_get(opts, "remote_branch") and true or false + tables.tbl_get(opts, "remote_branch") and true or false ) if git_branches_cmd:failed() then log.echo( @@ -188,7 +182,7 @@ local GIT_LOG_PRETTY_FORMAT = --- @param line string --- @return string M._git_branches_previewer = function(line) - local branch = strs.split(line, " ")[1] + local branch = strings.split(line, " ")[1] -- "git log --graph --date=short --color=always --pretty='%C(auto)%cd %h%d %s'", -- "git log --graph --color=always --date=relative", return string.format( @@ -225,7 +219,7 @@ M.fzf_opts = { if git_current_branch_cmd:failed() then return nil end - return strs.not_empty(git_current_branch_cmd:output()) + return strings.not_empty(git_current_branch_cmd:output()) and "--header-lines=1" or nil end, diff --git a/lua/fzfx/cfg/git_commits.lua b/lua/fzfx/cfg/git_commits.lua index e03d9c367..243acc954 100644 --- a/lua/fzfx/cfg/git_commits.lua +++ b/lua/fzfx/cfg/git_commits.lua @@ -1,21 +1,15 @@ +local tables = require("fzfx.commons.tables") + local consts = require("fzfx.lib.constants") -local strs = require("fzfx.lib.strings") local bufs = require("fzfx.lib.bufs") local cmds = require("fzfx.lib.commands") -local paths = require("fzfx.lib.paths") -local tbls = require("fzfx.lib.tables") local log = require("fzfx.lib.log") local LogLevels = require("fzfx.lib.log").LogLevels -local parsers_helper = require("fzfx.helper.parsers") -local queries_helper = require("fzfx.helper.queries") local actions_helper = require("fzfx.helper.actions") -local labels_helper = require("fzfx.helper.previewer_labels") -local providers_helper = require("fzfx.helper.providers") local previewers_helper = require("fzfx.helper.previewers") local ProviderTypeEnum = require("fzfx.schema").ProviderTypeEnum -local PreviewerTypeEnum = require("fzfx.schema").PreviewerTypeEnum local CommandFeedEnum = require("fzfx.schema").CommandFeedEnum local M = {} @@ -137,7 +131,7 @@ M._make_git_commits_provider = function(opts) log.echo(LogLevels.INFO, "not in git repo.") return nil end - if tbls.tbl_get(opts, "buffer") then + if tables.tbl_get(opts, "buffer") then if not bufs.buf_is_valid(context.bufnr) then log.echo( LogLevels.INFO, diff --git a/lua/fzfx/cfg/git_files.lua b/lua/fzfx/cfg/git_files.lua index 348bf96aa..ce2d27e81 100644 --- a/lua/fzfx/cfg/git_files.lua +++ b/lua/fzfx/cfg/git_files.lua @@ -1,16 +1,13 @@ +local tables = require("fzfx.commons.tables") +local paths = require("fzfx.commons.paths") + local consts = require("fzfx.lib.constants") -local strs = require("fzfx.lib.strings") local cmds = require("fzfx.lib.commands") -local paths = require("fzfx.lib.paths") -local tbls = require("fzfx.lib.tables") local log = require("fzfx.lib.log") local LogLevels = require("fzfx.lib.log").LogLevels -local parsers_helper = require("fzfx.helper.parsers") -local queries_helper = require("fzfx.helper.queries") local actions_helper = require("fzfx.helper.actions") local labels_helper = require("fzfx.helper.previewer_labels") -local providers_helper = require("fzfx.helper.providers") local previewers_helper = require("fzfx.helper.previewers") local ProviderTypeEnum = require("fzfx.schema").ProviderTypeEnum @@ -133,7 +130,7 @@ M._make_git_files_provider = function(opts) log.echo(LogLevels.INFO, "not in git repo.") return nil end - return tbls.tbl_get(opts, "current_folder") and { "git", "ls-files" } + return tables.tbl_get(opts, "current_folder") and { "git", "ls-files" } or { "git", "ls-files", ":/" } end return impl diff --git a/lua/fzfx/cfg/git_live_grep.lua b/lua/fzfx/cfg/git_live_grep.lua index f12629753..5835de3fd 100644 --- a/lua/fzfx/cfg/git_live_grep.lua +++ b/lua/fzfx/cfg/git_live_grep.lua @@ -1,16 +1,12 @@ local consts = require("fzfx.lib.constants") -local strs = require("fzfx.lib.strings") +local strings = require("fzfx.commons.strings") local cmds = require("fzfx.lib.commands") -local paths = require("fzfx.lib.paths") -local tbls = require("fzfx.lib.tables") local log = require("fzfx.lib.log") local LogLevels = require("fzfx.lib.log").LogLevels -local parsers_helper = require("fzfx.helper.parsers") local queries_helper = require("fzfx.helper.queries") local actions_helper = require("fzfx.helper.actions") local labels_helper = require("fzfx.helper.previewer_labels") -local providers_helper = require("fzfx.helper.providers") local previewers_helper = require("fzfx.helper.previewers") local ProviderTypeEnum = require("fzfx.schema").ProviderTypeEnum @@ -85,7 +81,7 @@ M._git_live_grep_provider = function(query, context) local args = { "git", "grep", "--color=always", "-n" } if type(option) == "string" and string.len(option) > 0 then - local option_splits = strs.split(option, " ") + local option_splits = strings.split(option, " ") for _, o in ipairs(option_splits) do if type(o) == "string" and string.len(o) > 0 then table.insert(args, o) diff --git a/lua/fzfx/cfg/git_status.lua b/lua/fzfx/cfg/git_status.lua index 0a05812e8..534a94e78 100644 --- a/lua/fzfx/cfg/git_status.lua +++ b/lua/fzfx/cfg/git_status.lua @@ -1,21 +1,16 @@ +local tables = require("fzfx.commons.tables") + local consts = require("fzfx.lib.constants") -local strs = require("fzfx.lib.strings") local shells = require("fzfx.lib.shells") local cmds = require("fzfx.lib.commands") -local paths = require("fzfx.lib.paths") -local tbls = require("fzfx.lib.tables") local log = require("fzfx.lib.log") local LogLevels = require("fzfx.lib.log").LogLevels local parsers_helper = require("fzfx.helper.parsers") -local queries_helper = require("fzfx.helper.queries") local actions_helper = require("fzfx.helper.actions") -local labels_helper = require("fzfx.helper.previewer_labels") -local providers_helper = require("fzfx.helper.providers") local previewers_helper = require("fzfx.helper.previewers") local ProviderTypeEnum = require("fzfx.schema").ProviderTypeEnum -local PreviewerTypeEnum = require("fzfx.schema").PreviewerTypeEnum local CommandFeedEnum = require("fzfx.schema").CommandFeedEnum local M = {} @@ -128,7 +123,7 @@ M.commands = { --- @return boolean M._is_current_folder_mode = function(opts) ---@diagnostic disable-next-line: need-check-nil - return tbls.tbl_not_empty(opts) and opts.current_folder --[[@as boolean]] + return tables.tbl_not_empty(opts) and opts.current_folder --[[@as boolean]] end --- @param opts {current_folder:boolean?}? diff --git a/lua/fzfx/cfg/live_grep.lua b/lua/fzfx/cfg/live_grep.lua index 790beafbd..4bdcfc599 100644 --- a/lua/fzfx/cfg/live_grep.lua +++ b/lua/fzfx/cfg/live_grep.lua @@ -1,9 +1,10 @@ +local tables = require("fzfx.commons.tables") +local strings = require("fzfx.commons.strings") +local paths = require("fzfx.commons.paths") + local consts = require("fzfx.lib.constants") -local strs = require("fzfx.lib.strings") local bufs = require("fzfx.lib.bufs") local cmds = require("fzfx.lib.commands") -local paths = require("fzfx.lib.paths") -local tbls = require("fzfx.lib.tables") local log = require("fzfx.lib.log") local LogLevels = require("fzfx.lib.log").LogLevels @@ -177,7 +178,7 @@ M._get_buf_path = function(bufnr) local bufpath = bufs.buf_is_valid(bufnr) and paths.reduce(vim.api.nvim_buf_get_name(bufnr)) or nil - if strs.empty(bufpath) then + if strings.empty(bufpath) then log.echo(LogLevels.INFO, M.INVALID_BUFFER_ERROR, vim.inspect(bufnr)) return nil end @@ -189,10 +190,10 @@ end --- @return string[] M._append_options = function(args_list, option) assert(type(args_list) == "table") - if strs.not_empty(option) then - local option_splits = strs.split(option --[[@as string]], " ") + if strings.not_empty(option) then + local option_splits = strings.split(option --[[@as string]], " ") for _, o in ipairs(option_splits) do - if strs.not_empty(o) then + if strings.not_empty(o) then table.insert(args_list, o) end end @@ -213,14 +214,16 @@ M._make_provider_rg = function(opts) local option = parsed.option local args = nil - if tbls.tbl_get(opts, "unrestricted") or tbls.tbl_get(opts, "buffer") then + if + tables.tbl_get(opts, "unrestricted") or tables.tbl_get(opts, "buffer") + then args = vim.deepcopy(providers_helper.UNRESTRICTED_RG) else args = vim.deepcopy(providers_helper.RESTRICTED_RG) end args = M._append_options(args, option) - if tbls.tbl_get(opts, "buffer") then + if tables.tbl_get(opts, "buffer") then local bufpath = M._get_buf_path(context.bufnr) if not bufpath then return nil @@ -247,14 +250,16 @@ M._make_provider_grep = function(opts) local option = parsed.option local args = nil - if tbls.tbl_get(opts, "unrestricted") or tbls.tbl_get(opts, "buffer") then + if + tables.tbl_get(opts, "unrestricted") or tables.tbl_get(opts, "buffer") + then args = vim.deepcopy(providers_helper.UNRESTRICTED_GREP) else args = vim.deepcopy(providers_helper.RESTRICTED_GREP) end args = M._append_options(args, option) - if tbls.tbl_get(opts, "buffer") then + if tables.tbl_get(opts, "buffer") then local bufpath = M._get_buf_path(context.bufnr) if not bufpath then return nil diff --git a/lua/fzfx/cfg/lsp_definitions.lua b/lua/fzfx/cfg/lsp_definitions.lua index d28b080fd..6cae977f1 100644 --- a/lua/fzfx/cfg/lsp_definitions.lua +++ b/lua/fzfx/cfg/lsp_definitions.lua @@ -1,21 +1,6 @@ local consts = require("fzfx.lib.constants") -local strs = require("fzfx.lib.strings") -local cmds = require("fzfx.lib.commands") -local paths = require("fzfx.lib.paths") -local tbls = require("fzfx.lib.tables") -local log = require("fzfx.lib.log") -local LogLevels = require("fzfx.lib.log").LogLevels - -local contexts_helper = require("fzfx.helper.contexts") -local parsers_helper = require("fzfx.helper.parsers") -local queries_helper = require("fzfx.helper.queries") -local actions_helper = require("fzfx.helper.actions") -local labels_helper = require("fzfx.helper.previewer_labels") -local providers_helper = require("fzfx.helper.providers") -local previewers_helper = require("fzfx.helper.previewers") local ProviderTypeEnum = require("fzfx.schema").ProviderTypeEnum -local PreviewerTypeEnum = require("fzfx.schema").PreviewerTypeEnum local CommandFeedEnum = require("fzfx.schema").CommandFeedEnum local _lsp_locations = require("fzfx.cfg._lsp_locations") diff --git a/lua/fzfx/cfg/lsp_diagnostics.lua b/lua/fzfx/cfg/lsp_diagnostics.lua index d06121d62..198bd41e9 100644 --- a/lua/fzfx/cfg/lsp_diagnostics.lua +++ b/lua/fzfx/cfg/lsp_diagnostics.lua @@ -1,9 +1,9 @@ +local tables = require("fzfx.commons.tables") +local termcolors = require("fzfx.commons.termcolors") +local paths = require("fzfx.commons.paths") + local consts = require("fzfx.lib.constants") -local strs = require("fzfx.lib.strings") local cmds = require("fzfx.lib.commands") -local termcolors = require("fzfx.commons.termcolors") -local paths = require("fzfx.lib.paths") -local tbls = require("fzfx.lib.tables") local log = require("fzfx.lib.log") local LogLevels = require("fzfx.lib.log").LogLevels @@ -180,7 +180,7 @@ M._make_lsp_diagnostic_signs = function() for _, signs in ipairs(LSP_DIAGNOSTICS_SIGNS) do local sign_def = vim.fn.sign_getdefined(signs.name) --[[@as table]] local item = vim.deepcopy(signs) - if not tbls.tbl_empty(sign_def) then + if not tables.tbl_empty(sign_def) then item.text = vim.trim(sign_def[1].text) item.texthl = sign_def[1].texthl end @@ -223,14 +223,14 @@ M._make_lsp_diagnostics_provider = function(opts) local function impl(query, context) ---@diagnostic disable-next-line: deprecated local lsp_clients = vim.lsp.get_active_clients() - if tbls.tbl_empty(lsp_clients) then + if tables.tbl_empty(lsp_clients) then log.echo(LogLevels.INFO, "no active lsp clients.") return nil end local diag_list = vim.diagnostic.get( (type(opts) == "table" and opts.buffer) and context.bufnr or nil ) - if tbls.tbl_empty(diag_list) then + if tables.tbl_empty(diag_list) then log.echo(LogLevels.INFO, "no lsp diagnostics found.") return nil end diff --git a/lua/fzfx/cfg/lsp_implementations.lua b/lua/fzfx/cfg/lsp_implementations.lua index e0e77a361..6e4a7b7db 100644 --- a/lua/fzfx/cfg/lsp_implementations.lua +++ b/lua/fzfx/cfg/lsp_implementations.lua @@ -1,20 +1,6 @@ local consts = require("fzfx.lib.constants") -local strs = require("fzfx.lib.strings") -local cmds = require("fzfx.lib.commands") -local paths = require("fzfx.lib.paths") -local tbls = require("fzfx.lib.tables") -local log = require("fzfx.lib.log") -local LogLevels = require("fzfx.lib.log").LogLevels - -local parsers_helper = require("fzfx.helper.parsers") -local queries_helper = require("fzfx.helper.queries") -local actions_helper = require("fzfx.helper.actions") -local labels_helper = require("fzfx.helper.previewer_labels") -local providers_helper = require("fzfx.helper.providers") -local previewers_helper = require("fzfx.helper.previewers") local ProviderTypeEnum = require("fzfx.schema").ProviderTypeEnum -local PreviewerTypeEnum = require("fzfx.schema").PreviewerTypeEnum local CommandFeedEnum = require("fzfx.schema").CommandFeedEnum local _lsp_locations = require("fzfx.cfg._lsp_locations") diff --git a/lua/fzfx/cfg/lsp_incoming_calls.lua b/lua/fzfx/cfg/lsp_incoming_calls.lua index 4ba5e3717..acb1f9444 100644 --- a/lua/fzfx/cfg/lsp_incoming_calls.lua +++ b/lua/fzfx/cfg/lsp_incoming_calls.lua @@ -1,20 +1,6 @@ local consts = require("fzfx.lib.constants") -local strs = require("fzfx.lib.strings") -local cmds = require("fzfx.lib.commands") -local paths = require("fzfx.lib.paths") -local tbls = require("fzfx.lib.tables") -local log = require("fzfx.lib.log") -local LogLevels = require("fzfx.lib.log").LogLevels - -local parsers_helper = require("fzfx.helper.parsers") -local queries_helper = require("fzfx.helper.queries") -local actions_helper = require("fzfx.helper.actions") -local labels_helper = require("fzfx.helper.previewer_labels") -local providers_helper = require("fzfx.helper.providers") -local previewers_helper = require("fzfx.helper.previewers") local ProviderTypeEnum = require("fzfx.schema").ProviderTypeEnum -local PreviewerTypeEnum = require("fzfx.schema").PreviewerTypeEnum local CommandFeedEnum = require("fzfx.schema").CommandFeedEnum local _lsp_locations = require("fzfx.cfg._lsp_locations") diff --git a/lua/fzfx/cfg/lsp_outgoing_calls.lua b/lua/fzfx/cfg/lsp_outgoing_calls.lua index 596609a96..11710e1bf 100644 --- a/lua/fzfx/cfg/lsp_outgoing_calls.lua +++ b/lua/fzfx/cfg/lsp_outgoing_calls.lua @@ -1,20 +1,6 @@ local consts = require("fzfx.lib.constants") -local strs = require("fzfx.lib.strings") -local cmds = require("fzfx.lib.commands") -local paths = require("fzfx.lib.paths") -local tbls = require("fzfx.lib.tables") -local log = require("fzfx.lib.log") -local LogLevels = require("fzfx.lib.log").LogLevels - -local parsers_helper = require("fzfx.helper.parsers") -local queries_helper = require("fzfx.helper.queries") -local actions_helper = require("fzfx.helper.actions") -local labels_helper = require("fzfx.helper.previewer_labels") -local providers_helper = require("fzfx.helper.providers") -local previewers_helper = require("fzfx.helper.previewers") local ProviderTypeEnum = require("fzfx.schema").ProviderTypeEnum -local PreviewerTypeEnum = require("fzfx.schema").PreviewerTypeEnum local CommandFeedEnum = require("fzfx.schema").CommandFeedEnum local _lsp_locations = require("fzfx.cfg._lsp_locations") diff --git a/lua/fzfx/cfg/lsp_references.lua b/lua/fzfx/cfg/lsp_references.lua index 513c6f107..9ed99f4cd 100644 --- a/lua/fzfx/cfg/lsp_references.lua +++ b/lua/fzfx/cfg/lsp_references.lua @@ -1,20 +1,6 @@ local consts = require("fzfx.lib.constants") -local strs = require("fzfx.lib.strings") -local cmds = require("fzfx.lib.commands") -local paths = require("fzfx.lib.paths") -local tbls = require("fzfx.lib.tables") -local log = require("fzfx.lib.log") -local LogLevels = require("fzfx.lib.log").LogLevels - -local parsers_helper = require("fzfx.helper.parsers") -local queries_helper = require("fzfx.helper.queries") -local actions_helper = require("fzfx.helper.actions") -local labels_helper = require("fzfx.helper.previewer_labels") -local providers_helper = require("fzfx.helper.providers") -local previewers_helper = require("fzfx.helper.previewers") local ProviderTypeEnum = require("fzfx.schema").ProviderTypeEnum -local PreviewerTypeEnum = require("fzfx.schema").PreviewerTypeEnum local CommandFeedEnum = require("fzfx.schema").CommandFeedEnum local _lsp_locations = require("fzfx.cfg._lsp_locations") diff --git a/lua/fzfx/cfg/lsp_type_definitions.lua b/lua/fzfx/cfg/lsp_type_definitions.lua index 16ee93e6a..2fbc14134 100644 --- a/lua/fzfx/cfg/lsp_type_definitions.lua +++ b/lua/fzfx/cfg/lsp_type_definitions.lua @@ -1,20 +1,6 @@ local consts = require("fzfx.lib.constants") -local strs = require("fzfx.lib.strings") -local cmds = require("fzfx.lib.commands") -local paths = require("fzfx.lib.paths") -local tbls = require("fzfx.lib.tables") -local log = require("fzfx.lib.log") -local LogLevels = require("fzfx.lib.log").LogLevels - -local parsers_helper = require("fzfx.helper.parsers") -local queries_helper = require("fzfx.helper.queries") -local actions_helper = require("fzfx.helper.actions") -local labels_helper = require("fzfx.helper.previewer_labels") -local providers_helper = require("fzfx.helper.providers") -local previewers_helper = require("fzfx.helper.previewers") local ProviderTypeEnum = require("fzfx.schema").ProviderTypeEnum -local PreviewerTypeEnum = require("fzfx.schema").PreviewerTypeEnum local CommandFeedEnum = require("fzfx.schema").CommandFeedEnum local _lsp_locations = require("fzfx.cfg._lsp_locations") diff --git a/lua/fzfx/cfg/vim_commands.lua b/lua/fzfx/cfg/vim_commands.lua index be387d846..3986894fd 100644 --- a/lua/fzfx/cfg/vim_commands.lua +++ b/lua/fzfx/cfg/vim_commands.lua @@ -1,9 +1,10 @@ +local tables = require("fzfx.commons.tables") +local strings = require("fzfx.commons.strings") +local paths = require("fzfx.commons.paths") +local fileios = require("fzfx.commons.fileios") + local consts = require("fzfx.lib.constants") -local strs = require("fzfx.lib.strings") local cmds = require("fzfx.lib.commands") -local paths = require("fzfx.lib.paths") -local fileios = require("fzfx.commons.fileios") -local tbls = require("fzfx.lib.tables") local log = require("fzfx.lib.log") local LogLevels = require("fzfx.lib.log").LogLevels @@ -175,7 +176,7 @@ M.commands = { --- @param line string --- @return string M._parse_vim_ex_command_name = function(line) - local name_stop_pos = strs.find(line, "|", 3) + local name_stop_pos = strings.find(line, "|", 3) return vim.trim(line:sub(3, name_stop_pos - 1)) end @@ -185,7 +186,7 @@ M._get_vim_ex_commands = function() ---@diagnostic disable-next-line: param-type-mismatch vim.fn.globpath(vim.env.VIMRUNTIME, "doc/index.txt", 0, 1) --[[@as table]] log.debug("|_get_vim_ex_commands| help docs:%s", vim.inspect(help_docs_list)) - if tbls.tbl_empty(help_docs_list) then + if tables.tbl_empty(help_docs_list) then log.echo(LogLevels.INFO, "no 'doc/index.txt' found.") return {} end @@ -194,7 +195,7 @@ M._get_vim_ex_commands = function() local lines = fileios.readlines(help_doc) --[[@as table]] for i = 1, #lines do local line = lines[i] - if strs.startswith(line, "|:") then + if strings.startswith(line, "|:") then log.debug("|_get_vim_ex_commands| line[%d]:%s", i, vim.inspect(line)) local name = M._parse_vim_ex_command_name(line) if type(name) == "string" and string.len(name) > 0 then @@ -217,11 +218,11 @@ end --- @param header string --- @return boolean M._is_ex_command_output_header = function(header) - local name_pos = strs.find(header, "Name") - local args_pos = strs.find(header, "Args") - local address_pos = strs.find(header, "Address") - local complete_pos = strs.find(header, "Complete") - local definition_pos = strs.find(header, "Definition") + local name_pos = strings.find(header, "Name") + local args_pos = strings.find(header, "Args") + local address_pos = strings.find(header, "Address") + local complete_pos = strings.find(header, "Complete") + local definition_pos = strings.find(header, "Definition") return type(name_pos) == "number" and type(args_pos) == "number" and type(address_pos) == "number" @@ -244,20 +245,20 @@ M._parse_ex_command_output_lua_function_definition = function(line, start_pos) ) local lua_flag = " 0 and content:sub(#content) == ">" then content = content:sub(1, #content - 1) @@ -266,7 +267,7 @@ M._parse_ex_command_output_lua_function_definition = function(line, start_pos) "|_parse_ex_command_output_lua_function_definition| content-2:%s", vim.inspect(content) ) - local content_splits = strs.split(content, ":") + local content_splits = strings.split(content, ":") log.debug( "|_parse_ex_command_output_lua_function_definition| split content:%s", vim.inspect(content_splits) @@ -281,11 +282,11 @@ end --- @param header string --- @return fzfx.VimExCommandOutputHeader M._parse_ex_command_output_header = function(header) - local name_pos = strs.find(header, "Name") - local args_pos = strs.find(header, "Args") - local address_pos = strs.find(header, "Address") - local complete_pos = strs.find(header, "Complete") - local definition_pos = strs.find(header, "Definition") + local name_pos = strings.find(header, "Name") + local args_pos = strings.find(header, "Args") + local address_pos = strings.find(header, "Address") + local complete_pos = strings.find(header, "Complete") + local definition_pos = strings.find(header, "Definition") return { name_pos = name_pos, args_pos = args_pos, @@ -359,7 +360,7 @@ M._parse_ex_command_output = function() vim.inspect(line), idx ) - while idx <= #line and not strs.isspace(line:sub(idx, idx)) do + while idx <= #line and not strings.isspace(line:sub(idx, idx)) do -- log.debug( -- "|fzfx.config - _parse_ex_command_output| parse non-spaces, idx:%d, char:%s(%s)", -- idx, @@ -370,7 +371,7 @@ M._parse_ex_command_output = function() -- "|fzfx.config - _parse_ex_command_output| parse non-spaces, isspace:%s", -- vim.inspect(strs.isspace(line:sub(idx, idx))) -- ) - if strs.isspace(line:sub(idx, idx)) then + if strings.isspace(line:sub(idx, idx)) then break end idx = idx + 1 @@ -525,13 +526,13 @@ end M._get_vim_commands = function(opts) local results = {} - if tbls.tbl_get(opts, "ex_commands") then + if tables.tbl_get(opts, "ex_commands") then local tmp = M._get_vim_ex_commands() for _, c in pairs(tmp) do table.insert(results, c) end end - if tbls.tbl_get(opts, "user_commands") then + if tables.tbl_get(opts, "user_commands") then local tmp = M._get_vim_user_commands() for _, c in pairs(tmp) do table.insert(results, c) @@ -612,8 +613,8 @@ M._vim_commands_previewer = function(line, context) -- vim.inspect(parsed) -- ) if - tbls.tbl_not_empty(parsed) - and strs.not_empty(parsed.filename) + tables.tbl_not_empty(parsed) + and strings.not_empty(parsed.filename) and type(parsed.lineno) == "number" then -- log.debug( @@ -624,7 +625,7 @@ M._vim_commands_previewer = function(line, context) parsed.filename, parsed.lineno ) - elseif consts.HAS_ECHO and tbls.tbl_not_empty(parsed) then + elseif consts.HAS_ECHO and tables.tbl_not_empty(parsed) then -- log.debug( -- "|fzfx.config - _vim_commands_previewer| desc:%s", -- vim.inspect(parsed) diff --git a/lua/fzfx/cfg/vim_keymaps.lua b/lua/fzfx/cfg/vim_keymaps.lua index b0dd5d716..02508797c 100644 --- a/lua/fzfx/cfg/vim_keymaps.lua +++ b/lua/fzfx/cfg/vim_keymaps.lua @@ -1,17 +1,15 @@ -local consts = require("fzfx.lib.constants") -local strs = require("fzfx.lib.strings") -local cmds = require("fzfx.lib.commands") -local paths = require("fzfx.lib.paths") +local tables = require("fzfx.commons.tables") local fileios = require("fzfx.commons.fileios") -local tbls = require("fzfx.lib.tables") +local strings = require("fzfx.commons.strings") +local paths = require("fzfx.commons.paths") + +local consts = require("fzfx.lib.constants") local log = require("fzfx.lib.log") local LogLevels = require("fzfx.lib.log").LogLevels local parsers_helper = require("fzfx.helper.parsers") -local queries_helper = require("fzfx.helper.queries") local actions_helper = require("fzfx.helper.actions") local labels_helper = require("fzfx.helper.previewer_labels") -local providers_helper = require("fzfx.helper.providers") local previewers_helper = require("fzfx.helper.previewers") local ProviderTypeEnum = require("fzfx.schema").ProviderTypeEnum @@ -260,33 +258,36 @@ M._parse_map_command_output_line = function(line) local first_space_pos = 1 while first_space_pos <= #line - and not strs.isspace(line:sub(first_space_pos, first_space_pos)) + and not strings.isspace(line:sub(first_space_pos, first_space_pos)) do first_space_pos = first_space_pos + 1 end -- local mode = vim.trim(line:sub(1, first_space_pos - 1)) while first_space_pos <= #line - and strs.isspace(line:sub(first_space_pos, first_space_pos)) + and strings.isspace(line:sub(first_space_pos, first_space_pos)) do first_space_pos = first_space_pos + 1 end local second_space_pos = first_space_pos while second_space_pos <= #line - and not strs.isspace(line:sub(second_space_pos, second_space_pos)) + and not strings.isspace(line:sub(second_space_pos, second_space_pos)) do second_space_pos = second_space_pos + 1 end local lhs = vim.trim(line:sub(first_space_pos, second_space_pos - 1)) local result = { lhs = lhs } local rhs_or_location = vim.trim(line:sub(second_space_pos)) - local lua_definition_pos = strs.find(rhs_or_location, "") then - local first_colon_pos = - strs.find(rhs_or_location, ":", lua_definition_pos + string.len("") then + local first_colon_pos = strings.find( + rhs_or_location, + ":", + lua_definition_pos + string.len(" 0 then - if strs.isalpha(line:sub(1, 1)) then + if strings.isalpha(line:sub(1, 1)) then local parsed = M._parse_map_command_output_line(line) keys_output_map[parsed.lhs] = parsed last_lhs = parsed.lhs elseif - strs.startswith(line, LAST_SET_FROM) - and strs.rfind(line, LINE) - and not strs.startswith(line, LAST_SET_FROM_LUA) + strings.startswith(line, LAST_SET_FROM) + and strings.rfind(line, LINE) + and not strings.startswith(line, LAST_SET_FROM_LUA) and last_lhs then - local line_pos = strs.rfind(line, LINE) + local line_pos = strings.rfind(line, LINE) local filename = vim.trim(line:sub(string.len(LAST_SET_FROM) + 1, line_pos - 1)) local lineno = vim.trim(line:sub(line_pos + string.len(LINE))) keys_output_map[last_lhs].filename = - paths.normalize(filename, { expand = true }) + paths.normalize(filename, { double_backslash = true, expand = true }) keys_output_map[last_lhs].lineno = tonumber(lineno) end end @@ -382,7 +384,9 @@ M._get_vim_keymaps = function() if keys[left] then return keys[left] end - if strs.startswith(left, "") or strs.startswith(left, "") then + if + strings.startswith(left, "") or strings.startswith(left, "") + then return keys[" " .. left:sub(string.len("") + 1)] end return nil @@ -505,15 +509,15 @@ M._make_vim_keymaps_provider = function(mode) elseif mode == "v" and ( - strs.find(k.mode, "v") - or strs.find(k.mode, "s") - or strs.find(k.mode, "x") + strings.find(k.mode, "v") + or strings.find(k.mode, "s") + or strings.find(k.mode, "x") ) then table.insert(filtered_keys, k) - elseif mode == "n" and strs.find(k.mode, "n") then + elseif mode == "n" and strings.find(k.mode, "n") then table.insert(filtered_keys, k) - elseif mode == "i" and strs.find(k.mode, "i") then + elseif mode == "i" and strings.find(k.mode, "i") then table.insert(filtered_keys, k) elseif mode == "n" and string.len(k.mode) == 0 then table.insert(filtered_keys, k) @@ -564,8 +568,8 @@ M._vim_keymaps_previewer = function(line, context) -- vim.inspect(parsed) -- ) if - tbls.tbl_not_empty(parsed) - and strs.not_empty(parsed.filename) + tables.tbl_not_empty(parsed) + and strings.not_empty(parsed.filename) and type(parsed.lineno) == "number" then -- log.debug( @@ -576,7 +580,7 @@ M._vim_keymaps_previewer = function(line, context) parsed.filename, parsed.lineno ) - elseif consts.HAS_ECHO and tbls.tbl_not_empty(parsed) then + elseif consts.HAS_ECHO and tables.tbl_not_empty(parsed) then -- log.debug( -- "|fzfx.config - vim_keymaps_previewer| desc:%s", -- vim.inspect(parsed) diff --git a/lua/fzfx/commons/strings.lua b/lua/fzfx/commons/strings.lua index 9bfcdd3b8..68e99aaa2 100644 --- a/lua/fzfx/commons/strings.lua +++ b/lua/fzfx/commons/strings.lua @@ -85,69 +85,36 @@ M.rfind = function(s, t, rstart) end --- @param s string ---- @param t string? by default t is whitespace +--- @param t string? by default is whitespace --- @return string M.ltrim = function(s, t) assert(type(s) == "string") assert(type(t) == "string" or t == nil) - local function has(idx) - if not t then - return M.isspace(s:sub(idx, idx)) - end - - local c = string.byte(s, idx) - local found = false - for j = 1, #t do - if string.byte(t, j) == c then - found = true - break - end - end - return found - end - - local i = 1 - while i <= #s do - if not has(i) then - break - end - i = i + 1 - end - return s:sub(i, #s) + t = t or "%s+" + ---@diagnostic disable-next-line: redundant-return-value + return string.gsub(s, "^" .. t, "") end --- @param s string ---- @param t string? by default t is whitespace +--- @param t string? by default is whitespace --- @return string M.rtrim = function(s, t) assert(type(s) == "string") assert(type(t) == "string" or t == nil) - local function has(idx) - if not t then - return M.isspace(s:sub(idx, idx)) - end - - local c = string.byte(s, idx) - local found = false - for j = 1, #t do - if string.byte(t, j) == c then - found = true - break - end - end - return found - end + t = t or "%s+" + ---@diagnostic disable-next-line: redundant-return-value + return string.gsub(s, t .. "$", "") +end - local i = #s - while i >= 1 do - if not has(i) then - break - end - i = i - 1 - end - return s:sub(1, i) +--- @param s string +--- @param t string? by default is whitespace +--- @return string +M.trim = function(s, t) + assert(type(s) == "string") + assert(type(t) == "string" or t == nil) + return M.rtrim(M.ltrim(s, t), t) end --- @param s string diff --git a/lua/fzfx/commons/tables.lua b/lua/fzfx/commons/tables.lua index 58a83a1b8..4be955cbc 100644 --- a/lua/fzfx/commons/tables.lua +++ b/lua/fzfx/commons/tables.lua @@ -12,15 +12,12 @@ M.tbl_not_empty = function(t) return type(t) == "table" and not vim.tbl_isempty(t) end --- retrieve value from table like json field indexing via dot `.` delimiter. --- for example when parameter `t = { a = { b = 1 } }` and `field = 'a.b'`, it will return `1`. --- --- @param t any? --- @param ... any --- @return any M.tbl_get = function(t, ...) if vim.fn.has("nvim-0.10") > 0 and type(vim.tbl_get) == "function" then - return vim.tbl_get(t, ...) + return type(t) == "table" and vim.tbl_get(t, ...) or nil end local e = t --[[@as table]] diff --git a/lua/fzfx/commons/version.txt b/lua/fzfx/commons/version.txt index 0aec50e6e..e4604e3af 100644 --- a/lua/fzfx/commons/version.txt +++ b/lua/fzfx/commons/version.txt @@ -1 +1 @@ -3.1.4 +3.2.1 diff --git a/lua/fzfx/config.lua b/lua/fzfx/config.lua index ab2fce6b2..7cafaeb2d 100644 --- a/lua/fzfx/config.lua +++ b/lua/fzfx/config.lua @@ -1,5 +1,4 @@ local consts = require("fzfx.lib.constants") -local paths = require("fzfx.lib.paths") local M = {} @@ -284,7 +283,10 @@ local Defaults = { }, cache = { - dir = paths.join(vim.fn.stdpath("data"), "fzfx.nvim"), + dir = require("fzfx.commons.paths").join( + vim.fn.stdpath("data"), + "fzfx.nvim" + ), }, -- debug diff --git a/lua/fzfx/detail/fzf_helpers.lua b/lua/fzfx/detail/fzf_helpers.lua index 0695df9e6..085bfe373 100644 --- a/lua/fzfx/detail/fzf_helpers.lua +++ b/lua/fzfx/detail/fzf_helpers.lua @@ -1,8 +1,9 @@ -local paths = require("fzfx.lib.paths") +local paths = require("fzfx.commons.paths") local termcolors = require("fzfx.commons.termcolors") +local strings = require("fzfx.commons.strings") local jsons = require("fzfx.commons.jsons") local fileios = require("fzfx.commons.fileios") -local strs = require("fzfx.lib.strings") + local shells = require("fzfx.lib.shells") local log = require("fzfx.lib.log") local yanks = require("fzfx.detail.yanks") @@ -122,8 +123,8 @@ local function get_command_feed(feed_type, input_args, pipeline_name) if type(data) ~= "string" or string.len(data) == 0 - or not strs.startswith(data, "{") - or not strs.endswith(data, "}") + or not strings.startswith(data, "{") + or not strings.endswith(data, "}") then return "", nil end @@ -344,7 +345,7 @@ end --- @return fzfx.FzfOptEventBinder function FzfOptEventBinder:append(opt) log.ensure( - strs.not_blank(opt), + strings.not_blank(opt), "|FzfOptEventBinder:append| opt must not blank:%s", vim.inspect(opt) ) diff --git a/lua/fzfx/detail/general.lua b/lua/fzfx/detail/general.lua index f4077b2be..b90bc43fb 100644 --- a/lua/fzfx/detail/general.lua +++ b/lua/fzfx/detail/general.lua @@ -1,12 +1,13 @@ -local consts = require("fzfx.lib.constants") -local termcolors = require("fzfx.commons.termcolors") -local env = require("fzfx.lib.env") -local paths = require("fzfx.lib.paths") +local tables = require("fzfx.commons.tables") +local paths = require("fzfx.commons.paths") local jsons = require("fzfx.commons.jsons") -local strs = require("fzfx.lib.strings") +local strings = require("fzfx.commons.strings") +local termcolors = require("fzfx.commons.termcolors") local fileios = require("fzfx.commons.fileios") -local tbls = require("fzfx.lib.tables") -local spawn = require("fzfx.lib.spawn") +local spawn = require("fzfx.commons.spawn") + +local consts = require("fzfx.lib.constants") +local env = require("fzfx.lib.env") local log = require("fzfx.lib.log") local shells = require("fzfx.lib.shells") @@ -72,7 +73,7 @@ local function make_provider_meta_opts(pipeline, provider_config) } -- provider_decorator - if tbls.tbl_get(provider_config, "provider_decorator") then + if tables.tbl_get(provider_config, "provider_decorator") then o.provider_decorator = vim.deepcopy(provider_config.provider_decorator) if o.provider_decorator.builtin then o.provider_decorator.module = "fzfx.helper.provider_decorators." @@ -212,7 +213,7 @@ function ProviderSwitch:provide(query, context) vim.inspect(self), vim.inspect(provider_config) ) - if tbls.tbl_empty(provider_config.provider) then + if tables.tbl_empty(provider_config.provider) then fileios.writefile(self.resultfile, "") else fileios.writefile( @@ -276,7 +277,7 @@ function ProviderSwitch:provide(query, context) vim.inspect(result) ) else - if tbls.tbl_empty(result) then + if tables.tbl_empty(result) then fileios.writefile(self.resultfile, "") else fileios.writefile( @@ -310,7 +311,7 @@ function ProviderSwitch:provide(query, context) vim.inspect(self), vim.inspect(result) ) - if tbls.tbl_empty(result) then + if tables.tbl_empty(result) then fileios.writefile(self.resultfile, "") else fileios.writelines(self.resultfile, result) @@ -475,7 +476,7 @@ function PreviewerSwitch:preview(line, context) vim.inspect(self), vim.inspect(result) ) - if tbls.tbl_empty(result) then + if tables.tbl_empty(result) then fileios.writefile(self.resultfile, "") else fileios.writefile( @@ -525,10 +526,7 @@ end --- @param port string --- @param body string local function _send_http_post(port, body) - -- if SendHttpPostContext.send then - -- return - -- end - local sp = spawn.Spawn:make({ + spawn.run({ "curl", "-s", "-S", @@ -561,9 +559,8 @@ local function _send_http_post(port, body) -- vim.inspect(line) -- ) end, - blocking = false, + function(completed) end, }) - sp:run() end --- @param line string? @@ -628,9 +625,9 @@ function PreviewerSwitch:preview_label(line, context) if type(last_label) ~= "string" then return end - self.fzf_port = strs.not_empty(self.fzf_port) and self.fzf_port + self.fzf_port = strings.not_empty(self.fzf_port) and self.fzf_port or fileios.readfile(self.fzf_port_file, { trim = true }) --[[@as string]] - if strs.not_empty(self.fzf_port) then + if strings.not_empty(self.fzf_port) then _send_http_post( self.fzf_port, string.format("change-preview-label(%s)", vim.trim(last_label)) @@ -657,7 +654,7 @@ local HeaderSwitch = {} local function _render_help(name, action) return termcolors.magenta(string.upper(action), "Special") .. " to " - .. table.concat(strs.split(name, "_"), " ") + .. table.concat(strings.split(name, "_"), " ") end --- @param excludes string[]|nil @@ -1009,7 +1006,7 @@ local function general(name, query, bang, pipeline_configs, default_pipeline) fzf_opts = fzf_helpers.preprocess_fzf_opts(fzf_opts) local actions = pipeline_configs.actions local win_opts = nil - if not tbls.tbl_empty(pipeline_configs.win_opts) then + if not tables.tbl_empty(pipeline_configs.win_opts) then win_opts = vim.tbl_deep_extend( "force", vim.deepcopy(win_opts or {}), diff --git a/lua/fzfx/detail/rpcserver.lua b/lua/fzfx/detail/rpcserver.lua index 59ccd90bd..b2a66505b 100644 --- a/lua/fzfx/detail/rpcserver.lua +++ b/lua/fzfx/detail/rpcserver.lua @@ -1,5 +1,5 @@ local consts = require("fzfx.lib.constants") -local paths = require("fzfx.lib.paths") +local paths = require("fzfx.commons.paths") local numbers = require("fzfx.commons.numbers") local log = require("fzfx.lib.log") @@ -13,8 +13,7 @@ local RpcServer = {} --- @return fzfx.RpcServer function RpcServer:new() - local address = consts.IS_WINDOWS - and vim.fn.serverstart(paths.make_pipe_name()) + local address = consts.IS_WINDOWS and vim.fn.serverstart(paths.pipename()) or vim.fn.serverstart() --[[@as string]] -- log.debug( -- "|fzfx.rpcserver - RpcServer:new| start server on socket address:%s", diff --git a/lua/fzfx/detail/shell_helpers.lua b/lua/fzfx/detail/shell_helpers.lua index f3ff0343c..a4fd8fc60 100644 --- a/lua/fzfx/detail/shell_helpers.lua +++ b/lua/fzfx/detail/shell_helpers.lua @@ -27,10 +27,11 @@ local LoggerContext = { --- @param name string M.setup = function(name) + local paths = require("fzfx.commons.paths") LoggerContext.file_path = string.format( "%s%s%s", vim.fn.stdpath("data"), - require("fzfx.lib.paths").SEPARATOR, + paths.SEPARATOR, string.format("fzfx_bin_%s.log", name) ) end diff --git a/lua/fzfx/detail/yanks.lua b/lua/fzfx/detail/yanks.lua index d4d1375ba..71a642f2d 100644 --- a/lua/fzfx/detail/yanks.lua +++ b/lua/fzfx/detail/yanks.lua @@ -1,6 +1,6 @@ local env = require("fzfx.lib.env") local ringbuf = require("fzfx.commons.ringbuf") -local paths = require("fzfx.lib.paths") +local paths = require("fzfx.commons.paths") local log = require("fzfx.lib.log") local bufs = require("fzfx.lib.bufs") @@ -87,7 +87,10 @@ M.save_yank = function() r.regtext, r.regtype, bufs.buf_is_valid(0) - and paths.normalize(vim.api.nvim_buf_get_name(0), { expand = true }) + and paths.normalize( + vim.api.nvim_buf_get_name(0), + { double_backslash = true, expand = true } + ) or nil, vim.bo.filetype ) diff --git a/lua/fzfx/helper/actions.lua b/lua/fzfx/helper/actions.lua index 23bd3314d..d1f3dcbc2 100644 --- a/lua/fzfx/helper/actions.lua +++ b/lua/fzfx/helper/actions.lua @@ -1,8 +1,9 @@ -local consts = require("fzfx.lib.constants") -local strs = require("fzfx.lib.strings") -local tbls = require("fzfx.lib.tables") +local tables = require("fzfx.commons.tables") +local strings = require("fzfx.commons.strings") local numbers = require("fzfx.commons.numbers") +local consts = require("fzfx.lib.constants") + local parsers = require("fzfx.helper.parsers") local prompts = require("fzfx.helper.prompts") @@ -228,9 +229,9 @@ end M._make_git_checkout = function(lines, context) log.debug("|_make_git_checkout| lines:%s", vim.inspect(lines)) - if tbls.list_not_empty(lines) then + if tables.list_not_empty(lines) then local line = lines[#lines] - if strs.not_empty(line) then + if strings.not_empty(line) then local parsed = parsers.parse_git_branch(line, context) return string.format([[!git checkout %s]], parsed.local_branch) end @@ -243,7 +244,7 @@ end --- @param context fzfx.GitBranchesPipelineContext M.git_checkout = function(lines, context) local checkout = M._make_git_checkout(lines, context) --[[@as string]] - if strs.not_empty(checkout) then + if strings.not_empty(checkout) then local ok, result = pcall(vim.cmd --[[@as function]], checkout) assert(ok, vim.inspect(result)) end @@ -252,7 +253,7 @@ end --- @param lines string[] --- @return string? M._make_yank_git_commit = function(lines) - if tbls.list_not_empty(lines) then + if tables.list_not_empty(lines) then local line = lines[#lines] local parsed = parsers.parse_git_commit(line) return string.format("let @+ = '%s'", parsed.commit) @@ -274,7 +275,7 @@ end --- @param context fzfx.VimCommandsPipelineContext --- @return {input:string, mode:string}? M._make_feed_vim_command = function(lines, context) - if tbls.list_not_empty(lines) then + if tables.list_not_empty(lines) then local line = lines[#lines] local parsed = parsers.parse_vim_command(line, context) return { input = string.format([[:%s]], parsed.command), mode = "n" } @@ -286,7 +287,7 @@ end --- @param context fzfx.VimCommandsPipelineContext M.feed_vim_command = function(lines, context) local feed = M._make_feed_vim_command(lines, context) --[[@as table]] - if tbls.tbl_not_empty(feed) then + if tables.tbl_not_empty(feed) then local ok, result = pcall(vim.fn.feedkeys, feed.input, feed.mode) assert(ok, vim.inspect(result)) end @@ -297,19 +298,19 @@ end --- @param context fzfx.VimKeyMapsPipelineContext --- @return {fn:"cmd"|"feedkeys"|nil, input:string?, mode:string?}? M._make_feed_vim_key = function(lines, context) - if tbls.list_not_empty(lines) then + if tables.list_not_empty(lines) then local line = lines[#lines] local parsed = parsers.parse_vim_keymap(line, context) - if strs.find(parsed.mode, "n") ~= nil then - if strs.startswith(parsed.lhs:lower(), "") then + if strings.find(parsed.mode, "n") ~= nil then + if strings.startswith(parsed.lhs, "", { ignorecase = true }) then return { fn = "cmd", input = string.format([[execute "normal \%s"]], parsed.lhs), mode = "n", } elseif - strs.startswith(parsed.lhs, "<") - and numbers.gt(strs.rfind(parsed.lhs, ">"), 0) + strings.startswith(parsed.lhs, "<") + and numbers.gt(strings.rfind(parsed.lhs, ">"), 0) then local tcodes = vim.api.nvim_replace_termcodes(parsed.lhs, true, false, true) @@ -335,17 +336,17 @@ end M.feed_vim_key = function(lines, context) local parsed = M._make_feed_vim_key(lines, context) --[[@as table]] if - tbls.tbl_not_empty(parsed) + tables.tbl_not_empty(parsed) and parsed.fn == "cmd" - and strs.not_empty(parsed.input) + and strings.not_empty(parsed.input) then local ok, result = pcall(vim.cmd --[[@as function]], parsed.input) assert(ok, vim.inspect(result)) elseif - tbls.tbl_not_empty(parsed) + tables.tbl_not_empty(parsed) and parsed.fn == "feedkeys" - and strs.not_empty(parsed.input) - and strs.not_empty(parsed.mode) + and strings.not_empty(parsed.input) + and strings.not_empty(parsed.mode) then local ok, result = pcall(vim.fn.feedkeys, parsed.input, parsed.mode) assert(ok, vim.inspect(result)) diff --git a/lua/fzfx/helper/parsers.lua b/lua/fzfx/helper/parsers.lua index 8fb6f18bf..973e2b0b5 100644 --- a/lua/fzfx/helper/parsers.lua +++ b/lua/fzfx/helper/parsers.lua @@ -1,10 +1,11 @@ -local consts = require("fzfx.lib.constants") -local paths = require("fzfx.lib.paths") -local env = require("fzfx.lib.env") -local strs = require("fzfx.lib.strings") +local tables = require("fzfx.commons.tables") +local strings = require("fzfx.commons.strings") local numbers = require("fzfx.commons.numbers") local fileios = require("fzfx.commons.fileios") -local tbls = require("fzfx.lib.tables") +local paths = require("fzfx.commons.paths") + +local consts = require("fzfx.lib.constants") +local env = require("fzfx.lib.env") local M = {} @@ -25,13 +26,18 @@ local M = {} M.parse_find = function(line) local filename = nil if env.icon_enabled() then - local first_icon_pos = strs.find(line, " ") + local first_icon_pos = strings.find(line, " ") assert(type(first_icon_pos) == "number") filename = line:sub(first_icon_pos + 1) else filename = line end - return { filename = paths.normalize(filename, { expand = true }) } + return { + filename = paths.normalize( + filename, + { double_backslash = true, expand = true } + ), + } end -- parse lines from grep. looks like: @@ -49,14 +55,14 @@ M.parse_grep = function(line) local lineno = nil local text = nil - local first_colon_pos = strs.find(line, ":") + local first_colon_pos = strings.find(line, ":") assert( type(first_colon_pos) == "number", string.format("failed to parse grep lines:%s", vim.inspect(line)) ) filename = line:sub(1, first_colon_pos - 1) - local second_colon_pos = strs.find(line, ":", first_colon_pos + 1) + local second_colon_pos = strings.find(line, ":", first_colon_pos + 1) if numbers.gt(second_colon_pos, 0) then lineno = line:sub(first_colon_pos + 1, second_colon_pos - 1) text = line:sub(second_colon_pos + 1) @@ -95,21 +101,21 @@ M.parse_rg = function(line) local column = nil local text = nil - local first_colon_pos = strs.find(line, ":") + local first_colon_pos = strings.find(line, ":") assert( type(first_colon_pos) == "number", string.format("failed to parse rg lines:%s", vim.inspect(line)) ) filename = line:sub(1, first_colon_pos - 1) - local second_colon_pos = strs.find(line, ":", first_colon_pos + 1) + local second_colon_pos = strings.find(line, ":", first_colon_pos + 1) assert( type(second_colon_pos) == "number", string.format("failed to parse rg lines:%s", vim.inspect(line)) ) lineno = line:sub(first_colon_pos + 1, second_colon_pos - 1) - local third_colon_pos = strs.find(line, ":", second_colon_pos + 1) + local third_colon_pos = strings.find(line, ":", second_colon_pos + 1) if numbers.gt(third_colon_pos, 0) then column = line:sub(second_colon_pos + 1, third_colon_pos - 1) text = line:sub(third_colon_pos + 1) @@ -146,10 +152,15 @@ end M.parse_git_status = function(line) line = vim.trim(line) local i = 1 - while i <= #line and not strs.isspace(line:sub(i, i)) do + while i <= #line and not strings.isspace(line:sub(i, i)) do i = i + 1 end - return { filename = paths.normalize(line:sub(i), { expand = true }) } + return { + filename = paths.normalize( + line:sub(i), + { double_backslash = true, expand = true } + ), + } end -- parse lines from `git branch` and `git branch --remotes`. looks like: @@ -170,7 +181,7 @@ M.parse_git_branch = function(line, context) --- @param t string --- @return boolean, string local function _remove_prefix(s, t) - if strs.startswith(s, t) then + if strings.startswith(s, t) then return true, vim.trim(s:sub(#t + 1)) else return false, s @@ -205,7 +216,7 @@ M.parse_git_branch = function(line, context) --- @param l string --- @return string local function _remove_remotes_origin_slash(l) - if tbls.list_not_empty(context.remotes) then + if tables.list_not_empty(context.remotes) then for _, r in ipairs(context.remotes) do local success, l1 = _remove_prefix(l, string.format("remotes/%s/", r)) if success then @@ -228,7 +239,7 @@ M.parse_git_branch = function(line, context) --- @param l string --- @return string local function _remove_origin_slash(l) - if tbls.list_not_empty(context.remotes) then + if tables.list_not_empty(context.remotes) then for _, r in ipairs(context.remotes) do local success, l1 = _remove_prefix(l, string.format("%s/", r)) if success then @@ -248,7 +259,7 @@ M.parse_git_branch = function(line, context) --- @param l string --- @return string local function _remove_right_arrow(l) - local arrow_pos = strs.find(l, "->") + local arrow_pos = strings.find(l, "->") if numbers.gt(arrow_pos, 0) then return vim.trim(l:sub(arrow_pos + 3)) end @@ -262,7 +273,7 @@ M.parse_git_branch = function(line, context) local_branch = _remove_star(local_branch) remote_branch = local_branch - if strs.find(local_branch, "->") ~= nil then + if strings.find(local_branch, "->") ~= nil then -- remove right arrow ("->") prefix local_branch = _remove_right_arrow(local_branch) remote_branch = local_branch @@ -286,7 +297,7 @@ end --- @param line string --- @return {commit:string} M.parse_git_commit = function(line) - local first_space_pos = strs.find(line, " ") + local first_space_pos = strings.find(line, " ") assert( numbers.gt(first_space_pos, 0), string.format("failed to parse git commit line:%s", vim.inspect(line)) @@ -368,7 +379,7 @@ M._make_parse_ls = function(start_pos) local function impl(line, context) local cwd = fileios.readfile(context.cwd, { trim = true }) assert( - strs.not_empty(cwd), + strings.not_empty(cwd), string.format( "failed to parse file explorer context:%s", vim.inspect(cwd) @@ -376,7 +387,7 @@ M._make_parse_ls = function(start_pos) ) local pos = 1 for i = 1, start_pos do - pos = strs.find(line, " ", pos) --[[@as integer]] + pos = strings.find(line, " ", pos) --[[@as integer]] assert( numbers.gt(pos, 0), string.format("failed to parse ls/eza/lsd lines:%s", vim.inspect(line)) @@ -391,9 +402,12 @@ M._make_parse_ls = function(start_pos) end -- remove extra single/double quotes - local result = strs.trim_quotes(vim.trim(line:sub(pos))) + local result = strings.trim(vim.trim(line:sub(pos)), "['\"]+") return { - filename = paths.normalize(paths.join(cwd, result), { expand = true }), + filename = paths.normalize( + paths.join(cwd, result), + { double_backslash = true, expand = true } + ), } end return impl @@ -418,7 +432,7 @@ M.parse_lsd = M._make_parse_ls(10) --- @param context fzfx.VimCommandsPipelineContext --- @return {command:string,filename:string,lineno:integer?}|{command:string,definition:string} M.parse_vim_command = function(line, context) - local first_space_pos = strs.find(line, " ") + local first_space_pos = strings.find(line, " ") assert( numbers.gt(first_space_pos, 0), string.format("failed to parse vim command lines:%s", vim.inspect(line)) @@ -432,10 +446,10 @@ M.parse_vim_command = function(line, context) -- ) if string.len(desc_or_loc) > 0 - and not strs.startswith(desc_or_loc, '"') - and not strs.endswith(desc_or_loc, '"') + and not strings.startswith(desc_or_loc, '"') + and not strings.endswith(desc_or_loc, '"') then - local split_pos = strs.rfind(desc_or_loc, ":") + local split_pos = strings.rfind(desc_or_loc, ":") local splits = { desc_or_loc:sub(1, split_pos - 1), desc_or_loc:sub(split_pos + 1), @@ -444,7 +458,8 @@ M.parse_vim_command = function(line, context) -- "|fzfx.helper.parsers - parse_vim_commands| splits:%s", -- vim.inspect(splits) -- ) - local filename = paths.normalize(splits[1], { expand = true }) + local filename = + paths.normalize(splits[1], { double_backslash = true, expand = true }) local lineno = tonumber(splits[2]) -- log.debug( -- "|fzfx.helper.parsers - parse_vim_commands| filename:%s, lineno:%s", @@ -453,7 +468,10 @@ M.parse_vim_command = function(line, context) -- ) return { command = command, filename = filename, lineno = lineno } else - return { command = command, definition = strs.trim_quotes(desc_or_loc) } + return { + command = command, + definition = strings.trim(desc_or_loc, "['\"]+"), + } end end @@ -474,13 +492,13 @@ end --- @param context fzfx.VimKeyMapsPipelineContext --- @return {lhs:string,mode:string,filename:string,lineno:integer?}|{lhs:string,definition:string} M.parse_vim_keymap = function(line, context) - local first_space_pos = strs.find(line, " ") + local first_space_pos = strings.find(line, " ") assert( numbers.gt(first_space_pos, 0), string.format("failed to parse vim keymap lines:%s", vim.inspect(line)) ) local lhs = vim.trim(line:sub(1, first_space_pos - 1)) - local first_bar_pos = strs.find(line, "|", first_space_pos + 1) + local first_bar_pos = strings.find(line, "|", first_space_pos + 1) local mode = vim.trim(line:sub(first_space_pos + 1, first_bar_pos - 1)) local rhs_or_loc = vim.trim(line:sub(context.key_width + 1 + context.opts_width + 1 + 1)) @@ -490,10 +508,10 @@ M.parse_vim_keymap = function(line, context) -- ) if string.len(rhs_or_loc) > 0 - and not strs.startswith(rhs_or_loc, '"') - and not strs.endswith(rhs_or_loc, '"') + and not strings.startswith(rhs_or_loc, '"') + and not strings.endswith(rhs_or_loc, '"') then - local split_pos = strs.rfind(rhs_or_loc, ":") + local split_pos = strings.rfind(rhs_or_loc, ":") local splits = { rhs_or_loc:sub(1, split_pos - 1), rhs_or_loc:sub(split_pos + 1), @@ -502,7 +520,8 @@ M.parse_vim_keymap = function(line, context) -- "|fzfx.helper.parsers - parse_vim_commands| splits:%s", -- vim.inspect(splits) -- ) - local filename = paths.normalize(splits[1], { expand = true }) + local filename = + paths.normalize(splits[1], { double_backslash = true, expand = true }) local lineno = tonumber(splits[2]) -- log.debug( -- "|fzfx.helper.parsers - parse_vim_commands| filename:%s, lineno:%s", @@ -511,7 +530,11 @@ M.parse_vim_keymap = function(line, context) -- ) return { lhs = lhs, mode = mode, filename = filename, lineno = lineno } else - return { lhs = lhs, mode = mode, definition = strs.trim_quotes(rhs_or_loc) } + return { + lhs = lhs, + mode = mode, + definition = strings.trim(rhs_or_loc, "['\"]+"), + } end end diff --git a/lua/fzfx/helper/previewer_labels.lua b/lua/fzfx/helper/previewer_labels.lua index ac93b1f87..9e1cc6b05 100644 --- a/lua/fzfx/helper/previewer_labels.lua +++ b/lua/fzfx/helper/previewer_labels.lua @@ -1,13 +1,14 @@ +local tables = require("fzfx.commons.tables") +local strings = require("fzfx.commons.strings") + local parsers = require("fzfx.helper.parsers") -local strs = require("fzfx.lib.strings") -local tbls = require("fzfx.lib.tables") local M = {} --- @param line string? --- @return string M.label_find = function(line) - if strs.empty(line) then + if strings.empty(line) then return "" end local parsed = parsers.parse_find(line --[[@as string]]) @@ -17,7 +18,7 @@ end --- @param line string? --- @return string M.label_rg = function(line) - if strs.empty(line) then + if strings.empty(line) then return "" end local parsed = parsers.parse_rg(line --[[@as string]]) @@ -33,7 +34,7 @@ end --- @param line string? --- @return string? M.label_grep = function(line) - if strs.empty(line) then + if strings.empty(line) then return "" end local parsed = parsers.parse_grep(line --[[@as string]]) @@ -52,13 +53,13 @@ M._make_label_vim_command_or_keymap = function(parser, default_value) --- @param context fzfx.VimCommandsPipelineContext --- @return string local function impl(line, context) - if strs.empty(line) then + if strings.empty(line) then return "" end local parsed = parser(line --[[@as string]], context) if - tbls.tbl_not_empty(parsed) - and strs.not_empty(parsed.filename) + tables.tbl_not_empty(parsed) + and strings.not_empty(parsed.filename) and type(parsed.lineno) == "number" then return string.format( @@ -84,7 +85,7 @@ M._make_label_ls = function(parser) --- @param context fzfx.FileExplorerPipelineContext --- @return string? local function impl(line, context) - if strs.empty(line) then + if strings.empty(line) then return "" end local parsed = parser(line, context) --[[@as table]] diff --git a/lua/fzfx/helper/previewers.lua b/lua/fzfx/helper/previewers.lua index adfba2b24..e497e6cb1 100644 --- a/lua/fzfx/helper/previewers.lua +++ b/lua/fzfx/helper/previewers.lua @@ -1,9 +1,6 @@ -local consts = require("fzfx.lib.constants") -local strs = require("fzfx.lib.strings") -local cmds = require("fzfx.lib.commands") -local paths = require("fzfx.lib.paths") -local tbls = require("fzfx.lib.tables") +local strings = require("fzfx.commons.strings") +local consts = require("fzfx.lib.constants") local log = require("fzfx.lib.log") local LogLevels = require("fzfx.lib.log").LogLevels @@ -134,10 +131,10 @@ M._make_preview_git_commit = function(commit) end M.preview_git_commit = function(line) - if strs.isspace(line:sub(1, 1)) then + if strings.isspace(line:sub(1, 1)) then return nil end - local first_space_pos = strs.find(line, " ") + local first_space_pos = strings.find(line, " ") local commit = line:sub(1, first_space_pos - 1) return M._make_preview_git_commit(commit) end diff --git a/lua/fzfx/helper/prompts.lua b/lua/fzfx/helper/prompts.lua index f505043d1..5c03ec1ad 100644 --- a/lua/fzfx/helper/prompts.lua +++ b/lua/fzfx/helper/prompts.lua @@ -1,4 +1,4 @@ -local strs = require("fzfx.lib.strings") +local strings = require("fzfx.commons.strings") local apis = require("fzfx.commons.apis") local log = require("fzfx.lib.log") @@ -16,7 +16,11 @@ M.confirm_discard_modified = function(bufnr, callback) prompt = "[fzfx] buffer has been modified, continue? (y/n) ", cancelreturn = "n", }) - if ok and strs.not_empty(input) and strs.startswith(input:lower(), "y") then + if + ok + and strings.not_empty(input) + and strings.startswith(input, "y", { ignorecase = true }) + then callback() else log.echo(LogLevels.INFO, CANCELLED_MESSAGE) diff --git a/lua/fzfx/helper/providers.lua b/lua/fzfx/helper/providers.lua index 67c4de07f..5dda59059 100644 --- a/lua/fzfx/helper/providers.lua +++ b/lua/fzfx/helper/providers.lua @@ -1,17 +1,5 @@ -local consts = require("fzfx.lib.constants") -local strs = require("fzfx.lib.strings") -local cmds = require("fzfx.lib.commands") local termcolors = require("fzfx.commons.termcolors") -local paths = require("fzfx.lib.paths") -local tbls = require("fzfx.lib.tables") - -local log = require("fzfx.lib.log") -local LogLevels = require("fzfx.lib.log").LogLevels - -local parsers_helper = require("fzfx.helper.parsers") -local queries_helper = require("fzfx.helper.queries") -local actions_helper = require("fzfx.helper.actions") -local labels_helper = require("fzfx.helper.previewer_labels") +local consts = require("fzfx.lib.constants") local M = {} diff --git a/lua/fzfx/helper/queries.lua b/lua/fzfx/helper/queries.lua index fb185994e..2f256977b 100644 --- a/lua/fzfx/helper/queries.lua +++ b/lua/fzfx/helper/queries.lua @@ -1,4 +1,4 @@ -local strs = require("fzfx.lib.strings") +local strings = require("fzfx.commons.strings") local M = {} @@ -10,7 +10,7 @@ M.parse_flagged = function(query, flag) local payload = "" local option = nil - local flag_pos = strs.find(query, flag) + local flag_pos = strings.find(query, flag) if type(flag_pos) == "number" and flag_pos > 0 then payload = vim.trim(string.sub(query, 1, flag_pos - 1)) option = vim.trim(string.sub(query, flag_pos + 2)) diff --git a/lua/fzfx/lib/log.lua b/lua/fzfx/lib/log.lua index ccbb0396f..a9d2339bf 100644 --- a/lua/fzfx/lib/log.lua +++ b/lua/fzfx/lib/log.lua @@ -1,3 +1,5 @@ +local numbers = require("fzfx.commons.numbers") + local M = {} M.LogLevels = require("fzfx.commons.logging").LogLevels @@ -17,7 +19,6 @@ local LogHighlights = { --- @param fmt string --- @param ... any M.echo = function(level, fmt, ...) - local numbers = require("fzfx.commons.numbers") level = numbers.bound(level, M.LogLevels.TRACE, M.LogLevels.OFF) local msg = string.format(fmt, ...) diff --git a/lua/fzfx/lib/paths.lua b/lua/fzfx/lib/paths.lua deleted file mode 100644 index 79a228c46..000000000 --- a/lua/fzfx/lib/paths.lua +++ /dev/null @@ -1,60 +0,0 @@ -local consts = require("fzfx.lib.constants") -local strs = require("fzfx.lib.strings") - -local M = {} - -M.SEPARATOR = consts.IS_WINDOWS and "\\" or "/" - ---- @param p string ---- @param opts {backslash:boolean?,expand:boolean?}? ---- @return string -M.normalize = function(p, opts) - opts = opts or { backslash = false, expand = false } - opts.backslash = type(opts.backslash) == "boolean" and opts.backslash or false - opts.expand = type(opts.expand) == "boolean" and opts.expand or false - - local result = p - if string.match(result, [[\\]]) then - result = string.gsub(result, [[\\]], [[\]]) - end - if opts.backslash and string.match(result, [[\]]) then - result = string.gsub(result, [[\]], [[/]]) - end - return opts.expand and vim.fn.expand(vim.trim(result)) --[[@as string]] - or vim.trim(result) -end - ---- @param ... any ---- @return string -M.join = function(...) - return table.concat({ ... }, M.SEPARATOR) -end - ---- @param p string? ---- @return string -M.reduce2home = function(p) - return vim.fn.fnamemodify(p or vim.fn.getcwd(), ":~") --[[@as string]] -end - ---- @param p string? ---- @return string -M.reduce = function(p) - return vim.fn.fnamemodify(p or vim.fn.getcwd(), ":~:.") --[[@as string]] -end - ---- @param p string? ---- @return string -M.shorten = function(p) - return vim.fn.pathshorten(M.reduce(p)) --[[@as string]] -end - ---- @return string -M.make_pipe_name = function() - if consts.IS_WINDOWS then - return string.format([[\\.\pipe\nvim-pipe-%s]], strs.uuid()) - else - return vim.fn.tempname() --[[@as string]] - end -end - -return M diff --git a/lua/fzfx/lib/spawn.lua b/lua/fzfx/lib/spawn.lua deleted file mode 100644 index a020e59c4..000000000 --- a/lua/fzfx/lib/spawn.lua +++ /dev/null @@ -1,205 +0,0 @@ -local strs = require("fzfx.lib.strings") -local numbers = require("fzfx.commons.numbers") -local uv = (vim.fn.has("nvim-0.10") > 0 and vim.uv ~= nil) and vim.uv - or vim.loop - -local M = {} - ---- @alias fzfx.SpawnLineProcessor fun(line:string):any ---- @alias fzfx.SpawnOnStdout fzfx.SpawnLineProcessor ---- @alias fzfx.SpawnOnStderr fzfx.SpawnLineProcessor ---- @alias fzfx.SpawnOnExit fun(code:integer?,signal:integer?):any ---- @class fzfx.Spawn ---- @field cmds string[] ---- @field fn_on_exit fzfx.SpawnOnExit ---- @field result {code:integer?,signal:integer?}? ---- @field fn_on_stdout fzfx.SpawnOnStdout ---- @field fn_on_stderr fzfx.SpawnOnStderr ---- @field out_pipe uv_pipe_t ---- @field err_pipe uv_pipe_t ---- @field out_buffer string? ---- @field err_buffer string? ---- @field process_handle uv_process_t? ---- @field process_id integer|string|nil ---- @field _close_count integer ---- @field _blocking boolean -local Spawn = {} - ---- @param cmds string[] ---- @param opts {on_stdout:fzfx.SpawnOnStdout,on_stderr:fzfx.SpawnOnStderr?,on_exit:fzfx.SpawnOnExit?,blocking:boolean} ---- @return fzfx.Spawn -function Spawn:make(cmds, opts) - assert(type(opts) == "table") - assert(type(opts.on_stdout) == "function") - assert(type(opts.on_stderr) == "function" or opts.on_stderr == nil) - assert(type(opts.on_exit) == "function" or opts.on_exit == nil) - assert(type(opts.blocking) == "boolean") - - local out_pipe = uv.new_pipe(false) --[[@as uv_pipe_t]] - local err_pipe = uv.new_pipe(false) --[[@as uv_pipe_t]] - assert(out_pipe ~= nil) - assert(err_pipe ~= nil) - - local o = { - cmds = cmds, - fn_on_exit = opts.on_exit, - result = nil, - fn_on_stdout = opts.on_stdout, - fn_on_stderr = opts.on_stderr or function() end, - out_pipe = out_pipe, - err_pipe = err_pipe, - out_buffer = nil, - err_buffer = nil, - process_handle = nil, - process_id = nil, - _close_count = 0, - _blocking = opts.blocking, - } - setmetatable(o, self) - self.__index = self - return o -end - ---- @param buffer string ---- @param fn_line_processor fzfx.SpawnLineProcessor ---- @return integer -function Spawn:_consume_line(buffer, fn_line_processor) - local i = 1 - while i <= #buffer do - local newline_pos = strs.find(buffer, "\n", i) - if not newline_pos then - break - end - local line = buffer:sub(i, newline_pos - 1) - fn_line_processor(line) - i = newline_pos + 1 - end - return i -end - ---- @param handle uv_handle_t ---- @param code integer? ---- @param signal integer? -function Spawn:_close_handle(handle, code, signal) - if handle and not handle:is_closing() then - handle:close(function() - self._close_count = self._close_count + 1 - if self._blocking and self._close_count >= 3 then - uv.stop() - end - end) - end -end - ---- @param err string? ---- @param data string? -function Spawn:_on_stdout(err, data) - if err then - self.out_pipe:read_stop() - self:_close_handle(self.out_pipe) - return - end - - if data then - -- append data to data_buffer - self.out_buffer = self.out_buffer and (self.out_buffer .. data) or data - self.out_buffer = self.out_buffer:gsub("\r\n", "\n") - -- foreach the data_buffer and find every line - local i = self:_consume_line(self.out_buffer, self.fn_on_stdout) - -- truncate the printed lines if found any - self.out_buffer = i <= #self.out_buffer - and self.out_buffer:sub(i, #self.out_buffer) - or nil - else - if self.out_buffer then - -- foreach the data_buffer and find every line - local i = self:_consume_line(self.out_buffer, self.fn_on_stdout) - if i <= #self.out_buffer then - local line = self.out_buffer:sub(i, #self.out_buffer) - self.fn_on_stdout(line) - self.out_buffer = nil - end - end - self.out_pipe:read_stop() - self:_close_handle(self.out_pipe) - end -end - ---- @param err string? ---- @param data string? -function Spawn:_on_stderr(err, data) - if err then - io.write( - string.format( - "Spawn:_on_stderr, err:%s, data:%s", - vim.inspect(err), - vim.inspect(data) - ) - ) - error( - string.format( - "Spawn:_on_stderr, err:%s, data:%s", - vim.inspect(err), - vim.inspect(data) - ) - ) - self.err_pipe:read_stop() - self:_close_handle(self.err_pipe) - return - end - - if data then - -- append data to data_buffer - self.err_buffer = self.err_buffer and (self.err_buffer .. data) or data - self.err_buffer = self.err_buffer:gsub("\r\n", "\n") - -- foreach the data_buffer and find every line - local i = self:_consume_line(self.err_buffer, self.fn_on_stderr) - -- truncate the printed lines if found any - self.err_buffer = i <= #self.err_buffer - and self.err_buffer:sub(i, #self.err_buffer) - or nil - else - if self.err_buffer then - -- foreach the data_buffer and find every line - local i = self:_consume_line(self.err_buffer, self.fn_on_stderr) - if i <= #self.err_buffer then - local line = self.err_buffer:sub(i, #self.err_buffer) - self.fn_on_stderr(line) - self.err_buffer = nil - end - end - self.err_pipe:read_stop() - self:_close_handle(self.err_pipe) - end -end - -function Spawn:run() - self.process_handle, self.process_id = uv.spawn(self.cmds[1], { - args = vim.list_slice(self.cmds, 2), - stdio = { nil, self.out_pipe, self.err_pipe }, - hide = true, - }, function(code, signal) - self.result = { code = code, signal = signal } - if type(self.fn_on_exit) == "function" then - self.fn_on_exit(code, signal) - end - self:_close_handle(self.process_handle, code, signal) - end) - - self.out_pipe:read_start(function(err, data) - self:_on_stdout(err, data) - end) - self.err_pipe:read_start(function(err, data) - self:_on_stderr(err, data) - end) - if self._blocking then - uv.run() - vim.wait(numbers.INT32_MAX, function() - return self._close_count == 3 - end) - end -end - -M.Spawn = Spawn - -return M diff --git a/lua/fzfx/lib/strings.lua b/lua/fzfx/lib/strings.lua deleted file mode 100644 index 56e06cd5b..000000000 --- a/lua/fzfx/lib/strings.lua +++ /dev/null @@ -1,253 +0,0 @@ -local M = {} - ---- @param s any ---- @return boolean -M.empty = function(s) - return type(s) ~= "string" or string.len(s) == 0 -end - ---- @param s any ---- @return boolean -M.not_empty = function(s) - return type(s) == "string" and string.len(s) > 0 -end - ---- @param s any ---- @return boolean -M.blank = function(s) - return type(s) ~= "string" or string.len(vim.trim(s)) == 0 -end - ---- @param s any ---- @return boolean -M.not_blank = function(s) - return type(s) == "string" and string.len(vim.trim(s)) > 0 -end - ---- @param s string ---- @param t string ---- @param start integer? by default start=1 ---- @return integer? -M.find = function(s, t, start) - start = start or 1 - for i = start, #s do - local match = true - for j = 1, #t do - if i + j - 1 > #s then - match = false - break - end - local a = string.byte(s, i + j - 1) - local b = string.byte(t, j) - if a ~= b then - match = false - break - end - end - if match then - return i - end - end - return nil -end - ---- @param s string ---- @param t string ---- @param rstart integer? by default rstart=#s ---- @return integer? -M.rfind = function(s, t, rstart) - rstart = rstart or #s - for i = rstart, 1, -1 do - local match = true - for j = 1, #t do - if i + j - 1 > #s then - match = false - break - end - local a = string.byte(s, i + j - 1) - local b = string.byte(t, j) - if a ~= b then - match = false - break - end - end - if match then - return i - end - end - return nil -end - ---- @param s string ---- @param t string? by default t is whitespace ---- @return string -M.ltrim = function(s, t) - t = t or "\n\t\r " - local i = 1 - while i <= #s do - local c = string.byte(s, i) - local contains = false - for j = 1, #t do - if string.byte(t, j) == c then - contains = true - break - end - end - if not contains then - break - end - i = i + 1 - end - return s:sub(i, #s) -end - ---- @param s string ---- @param t string? by default t is whitespace ---- @return string -M.rtrim = function(s, t) - t = t or "\n\t\r " - local i = #s - while i >= 1 do - local c = string.byte(s, i) - local contains = false - for j = 1, #t do - if string.byte(t, j) == c then - contains = true - break - end - end - if not contains then - break - end - i = i - 1 - end - return s:sub(1, i) -end - ---- @param s string ---- @param opts {single:boolean?,double:boolean?}? by default `opts={single=true,double=true}` ---- @return string -M.trim_quotes = function(s, opts) - opts = opts or { single = true, double = true } - opts.single = type(opts.single) == "boolean" and opts.single or true - opts.double = type(opts.double) == "boolean" and opts.double or true - - if - string.len(s) >= 2 - and ( - (opts.single and M.startswith(s, "'") and M.endswith(s, "'")) - or (opts.double and M.startswith(s, '"') and M.endswith(s, '"')) - ) - then - return s:sub(2, #s - 1) - else - return s - end -end - ---- @param s string ---- @param delimiter string ---- @param opts {plain:boolean?,trimempty:boolean?}|nil by default opts={plain=true,trimempty=true} ---- @return string[] -M.split = function(s, delimiter, opts) - opts = opts or { - plain = true, - trimempty = true, - } - opts.plain = opts.plain == nil and true or opts.plain - opts.trimempty = opts.trimempty == nil and true or opts.trimempty - -- print( - -- string.format( - -- "trim s:%s, delimiter:%s, opts:%s\n", - -- vim.inspect(s), - -- vim.inspect(delimiter), - -- vim.inspect(opts) - -- ) - -- ) - return vim.split(s, delimiter, opts) -end - ---- @param s string ---- @param t string ---- @return boolean -M.startswith = function(s, t) - assert(type(s) == "string") - assert(type(t) == "string") - return string.len(s) >= string.len(t) and s:sub(1, #t) == t -end - ---- @param s string ---- @param t string ---- @return boolean -M.endswith = function(s, t) - assert(type(s) == "string") - assert(type(t) == "string") - return string.len(s) >= string.len(t) and s:sub(#s - #t + 1) == t -end - ---- @param s string ---- @return boolean -M.isspace = function(s) - assert(string.len(s) == 1) - return s:match("%s") ~= nil -end - ---- @param s string ---- @return boolean -M.isalnum = function(s) - assert(string.len(s) == 1) - return s:match("%w") ~= nil -end - ---- @param s string ---- @return boolean -M.isdigit = function(s) - assert(string.len(s) == 1) - return s:match("%d") ~= nil -end - ---- @param s string ---- @return boolean -M.ishex = function(s) - assert(string.len(s) == 1) - return s:match("%x") ~= nil -end - ---- @param s string ---- @return boolean -M.isalpha = function(s) - assert(string.len(s) == 1) - return s:match("%a") ~= nil -end - ---- @param s string ---- @return boolean -M.islower = function(s) - assert(string.len(s) == 1) - return s:match("%l") ~= nil -end - ---- @param s string ---- @return boolean -M.isupper = function(s) - assert(string.len(s) == 1) - return s:match("%u") ~= nil -end - ---- @param delimiter string? by default '-' ---- @return string -M.uuid = function(delimiter) - delimiter = delimiter or "-" - local secs, ms = vim.loop.gettimeofday() - return table.concat({ - string.format("%x", vim.loop.os_getpid()), - string.format("%x", secs), - string.format("%x", ms), - string.format( - "%x", - math.random(1, require("fzfx.commons.numbers").INT32_MAX) - ), - }, delimiter) -end - -return M diff --git a/lua/fzfx/lib/tables.lua b/lua/fzfx/lib/tables.lua deleted file mode 100644 index 5e1202ced..000000000 --- a/lua/fzfx/lib/tables.lua +++ /dev/null @@ -1,58 +0,0 @@ -local M = {} - ---- @param t any? ---- @return boolean -M.tbl_empty = function(t) - return type(t) ~= "table" or vim.tbl_isempty(t) -end - ---- @param t any? ---- @return boolean -M.tbl_not_empty = function(t) - return type(t) == "table" and not vim.tbl_isempty(t) -end - --- retrieve value from table like json field indexing via dot `.` delimiter. --- for example when parameter `t = { a = { b = 1 } }` and `field = 'a.b'`, it will return `1`. --- ---- @param t any? ---- @param field string ---- @return any -M.tbl_get = function(t, field) - local cur = t --[[@as table]] - local field_splits = vim.split(field, ".", { plain = true, trimempty = true }) - for _, f in ipairs(field_splits) do - if M.tbl_not_empty(cur) and cur[f] ~= nil then - cur = cur[f] - else - return nil - end - end - return cur -end - ---- @param l any? ---- @return boolean -M.list_empty = function(l) - return type(l) ~= "table" or #l == 0 -end - ---- @param l any? ---- @return boolean -M.list_not_empty = function(l) - return type(l) == "table" and #l > 0 -end - --- list index `i` support both positive or negative. `n` is the length of list. --- if i > 0, i is in range [1,n]. --- if i < 0, i is in range [-1,-n], -1 maps to last position (e.g. n), -n maps to first position (e.g. 1). ---- @param i integer ---- @param n integer ---- @return integer -M.list_index = function(i, n) - assert(n > 0) - assert((i >= 1 and i <= n) or (i <= -1 and i >= -n)) - return i > 0 and i or (n + i + 1) -end - -return M diff --git a/test/cfg/_lsp_locations_spec.lua b/test/cfg/_lsp_locations_spec.lua index 0d7c2306f..5f8514b68 100644 --- a/test/cfg/_lsp_locations_spec.lua +++ b/test/cfg/_lsp_locations_spec.lua @@ -14,18 +14,14 @@ describe("cfg._lsp_locations", function() local github_actions = os.getenv("GITHUB_ACTIONS") == "true" - require("fzfx").setup() - local tbls = require("fzfx.lib.tables") - local consts = require("fzfx.lib.constants") - local strs = require("fzfx.lib.strings") - local paths = require("fzfx.lib.paths") + local strings = require("fzfx.commons.strings") local termcolors = require("fzfx.commons.termcolors") - + local consts = require("fzfx.lib.constants") local contexts = require("fzfx.helper.contexts") local providers = require("fzfx.helper.providers") local fzf_helpers = require("fzfx.detail.fzf_helpers") - local _lsp_locations = require("fzfx.cfg._lsp_locations") + require("fzfx").setup() describe("_lsp_locations", function() local RANGE = { @@ -67,8 +63,8 @@ describe("cfg._lsp_locations", function() ) -- print(string.format("lsp render line:%s\n", vim.inspect(loc))) assert_eq(type(loc), "string") - assert_true(strs.startswith(loc, "describe")) - assert_true(strs.endswith(loc, "function()")) + assert_true(strings.startswith(loc, "describe")) + assert_true(strings.endswith(loc, "function()")) end) it("renders location", function() local actual = _lsp_locations._render_lsp_location_line(LOCATION) @@ -101,7 +97,7 @@ describe("cfg._lsp_locations", function() assert_eq(type(ctx.position_params.textDocument), "table") assert_eq(type(ctx.position_params.textDocument.uri), "string") assert_true( - strs.endswith(ctx.position_params.textDocument.uri, "README.md") + strings.endswith(ctx.position_params.textDocument.uri, "README.md") ) end) it("_make_lsp_locations_provider", function() diff --git a/test/cfg/buffers_spec.lua b/test/cfg/buffers_spec.lua index e0193a94a..180196897 100644 --- a/test/cfg/buffers_spec.lua +++ b/test/cfg/buffers_spec.lua @@ -15,11 +15,6 @@ describe("cfg.buffers", function() local github_actions = os.getenv("GITHUB_ACTIONS") == "true" require("fzfx").setup() - local tbls = require("fzfx.lib.tables") - local consts = require("fzfx.lib.constants") - local strs = require("fzfx.lib.strings") - local paths = require("fzfx.lib.paths") - local contexts = require("fzfx.helper.contexts") local providers = require("fzfx.helper.providers") local fzf_helpers = require("fzfx.detail.fzf_helpers") diff --git a/test/cfg/file_explorer_spec.lua b/test/cfg/file_explorer_spec.lua index b4b75cc43..748ac3786 100644 --- a/test/cfg/file_explorer_spec.lua +++ b/test/cfg/file_explorer_spec.lua @@ -15,10 +15,10 @@ describe("cfg.file_explorer", function() local github_actions = os.getenv("GITHUB_ACTIONS") == "true" require("fzfx").setup() - local tbls = require("fzfx.lib.tables") + local strings = require("fzfx.commons.strings") + local paths = require("fzfx.commons.paths") + local consts = require("fzfx.lib.constants") - local strs = require("fzfx.lib.strings") - local paths = require("fzfx.lib.paths") local contexts = require("fzfx.helper.contexts") local providers = require("fzfx.helper.providers") @@ -71,14 +71,19 @@ describe("cfg.file_explorer", function() -- ) -- ) assert_eq(type(actual1), "string") - assert_true(strs.find(actual1, "echo") > 0) + assert_true(strings.find(actual1, "echo") > 0) assert_true( - type(strs.find(actual1, "eza")) == "number" - or type(strs.find(actual1, "ls")) == "number" + type(strings.find(actual1, "eza")) == "number" + or type(strings.find(actual1, "ls")) == "number" ) assert_true( - strs.find(actual1, paths.normalize(vim.fn.getcwd(), { expand = true })) - > 0 + strings.find( + actual1, + paths.normalize( + vim.fn.getcwd(), + { double_backslash = true, expand = true } + ) + ) > 0 ) local f2 = file_explorer_cfg._make_file_explorer_provider("-lha") assert_eq(type(f2), "function") @@ -90,14 +95,19 @@ describe("cfg.file_explorer", function() -- ) -- ) assert_eq(type(actual2), "string") - assert_true(strs.find(actual2, "echo") > 0) + assert_true(strings.find(actual2, "echo") > 0) assert_true( - type(strs.find(actual2, "eza")) == "number" - or type(strs.find(actual2, "ls")) == "number" + type(strings.find(actual2, "eza")) == "number" + or type(strings.find(actual2, "ls")) == "number" ) assert_true( - strs.find(actual2, paths.normalize(vim.fn.getcwd(), { expand = true })) - > 0 + strings.find( + actual2, + paths.normalize( + vim.fn.getcwd(), + { double_backslash = true, expand = true } + ) + ) > 0 ) end) it("_directory_previewer", function() diff --git a/test/cfg/files_spec.lua b/test/cfg/files_spec.lua index 6a2b6ad20..0fefbea04 100644 --- a/test/cfg/files_spec.lua +++ b/test/cfg/files_spec.lua @@ -14,11 +14,6 @@ describe("cfg.files", function() local github_actions = os.getenv("GITHUB_ACTIONS") == "true" - local tbls = require("fzfx.lib.tables") - local consts = require("fzfx.lib.constants") - local strs = require("fzfx.lib.strings") - local paths = require("fzfx.lib.paths") - local contexts = require("fzfx.helper.contexts") local providers = require("fzfx.helper.providers") local fzf_helpers = require("fzfx.detail.fzf_helpers") diff --git a/test/cfg/git_blame_spec.lua b/test/cfg/git_blame_spec.lua index 8eb64bd6d..64f15f2dc 100644 --- a/test/cfg/git_blame_spec.lua +++ b/test/cfg/git_blame_spec.lua @@ -13,18 +13,13 @@ describe("cfg.git_blame", function() end) local github_actions = os.getenv("GITHUB_ACTIONS") == "true" - - require("fzfx").setup() - local tbls = require("fzfx.lib.tables") + local strings = require("fzfx.commons.strings") local consts = require("fzfx.lib.constants") - local strs = require("fzfx.lib.strings") - local paths = require("fzfx.lib.paths") - local contexts = require("fzfx.helper.contexts") local providers = require("fzfx.helper.providers") local fzf_helpers = require("fzfx.detail.fzf_helpers") - local git_blame_cfg = require("fzfx.cfg.git_blame") + require("fzfx").setup() describe("git_blame", function() it("_git_blame_provider", function() @@ -32,15 +27,15 @@ describe("cfg.git_blame", function() git_blame_cfg._git_blame_provider("", contexts.make_pipeline_context()) if actual ~= nil then assert_eq(type(actual), "string") - assert_true(strs.find(actual, "git blame") == 1) + assert_true(strings.find(actual, "git blame") == 1) if consts.HAS_DELTA then assert_true( - strs.find(actual, "delta -n --tabs 4 --blame-format") + strings.find(actual, "delta -n --tabs 4 --blame-format") > string.len("git blame") ) else assert_true( - strs.find(actual, "git blame --date=short --color-lines") == 1 + strings.find(actual, "git blame --date=short --color-lines") == 1 ) end end diff --git a/test/cfg/git_branches_spec.lua b/test/cfg/git_branches_spec.lua index 37387b145..8eb5b795f 100644 --- a/test/cfg/git_branches_spec.lua +++ b/test/cfg/git_branches_spec.lua @@ -14,17 +14,13 @@ describe("cfg.git_branches", function() local github_actions = os.getenv("GITHUB_ACTIONS") == "true" - require("fzfx").setup() - local tbls = require("fzfx.lib.tables") + local strings = require("fzfx.commons.strings") local consts = require("fzfx.lib.constants") - local strs = require("fzfx.lib.strings") - local paths = require("fzfx.lib.paths") - local contexts = require("fzfx.helper.contexts") local providers = require("fzfx.helper.providers") local fzf_helpers = require("fzfx.detail.fzf_helpers") - local git_branches_cfg = require("fzfx.cfg.git_branches") + require("fzfx").setup() describe("git_branches", function() it("_make_git_branches_provider local", function() @@ -50,7 +46,7 @@ describe("cfg.git_branches", function() } for i, line in ipairs(lines) do local actual = git_branches_cfg._git_branches_previewer(line) - assert_true(strs.find(actual, "git log --pretty") == 1) + assert_true(strings.find(actual, "git log --pretty") == 1) end end) end) diff --git a/test/cfg/git_commits_spec.lua b/test/cfg/git_commits_spec.lua index 51c862f03..7772059e2 100644 --- a/test/cfg/git_commits_spec.lua +++ b/test/cfg/git_commits_spec.lua @@ -14,17 +14,13 @@ describe("cfg.git_commits", function() local github_actions = os.getenv("GITHUB_ACTIONS") == "true" - require("fzfx").setup() - local tbls = require("fzfx.lib.tables") + local strings = require("fzfx.commons.strings") local consts = require("fzfx.lib.constants") - local strs = require("fzfx.lib.strings") - local paths = require("fzfx.lib.paths") - local contexts = require("fzfx.helper.contexts") local providers = require("fzfx.helper.providers") local fzf_helpers = require("fzfx.detail.fzf_helpers") - local git_commits_cfg = require("fzfx.cfg.git_commits") + require("fzfx").setup() describe("[_make_git_commits_provider]", function() it("all commits", function() @@ -34,7 +30,7 @@ describe("cfg.git_commits", function() assert_eq(type(actual), "table") assert_eq(actual[1], "git") assert_eq(actual[2], "log") - assert_true(strs.startswith(actual[3], "--pretty=")) + assert_true(strings.startswith(actual[3], "--pretty=")) assert_eq(actual[4], "--date=short") assert_eq(actual[5], "--color=always") end @@ -46,11 +42,11 @@ describe("cfg.git_commits", function() assert_eq(type(actual), "table") assert_eq(actual[1], "git") assert_eq(actual[2], "log") - assert_true(strs.startswith(actual[3], "--pretty=")) + assert_true(strings.startswith(actual[3], "--pretty=")) assert_eq(actual[4], "--date=short") assert_eq(actual[5], "--color=always") assert_eq(actual[6], "--") - assert_true(strs.endswith(actual[7], "README.md")) + assert_true(strings.endswith(actual[7], "README.md")) end end) end) diff --git a/test/cfg/git_files_spec.lua b/test/cfg/git_files_spec.lua index 2cd545c97..dfcac92d9 100644 --- a/test/cfg/git_files_spec.lua +++ b/test/cfg/git_files_spec.lua @@ -15,11 +15,6 @@ describe("cfg.git_files", function() local github_actions = os.getenv("GITHUB_ACTIONS") == "true" require("fzfx").setup() - local tbls = require("fzfx.lib.tables") - local consts = require("fzfx.lib.constants") - local strs = require("fzfx.lib.strings") - local paths = require("fzfx.lib.paths") - local contexts = require("fzfx.helper.contexts") local providers = require("fzfx.helper.providers") local fzf_helpers = require("fzfx.detail.fzf_helpers") diff --git a/test/cfg/git_live_grep_spec.lua b/test/cfg/git_live_grep_spec.lua index 67396bc66..3062028c8 100644 --- a/test/cfg/git_live_grep_spec.lua +++ b/test/cfg/git_live_grep_spec.lua @@ -14,17 +14,13 @@ describe("cfg.git_live_grep", function() local github_actions = os.getenv("GITHUB_ACTIONS") == "true" - require("fzfx").setup() - local tbls = require("fzfx.lib.tables") + local tables = require("fzfx.commons.tables") local consts = require("fzfx.lib.constants") - local strs = require("fzfx.lib.strings") - local paths = require("fzfx.lib.paths") - local contexts = require("fzfx.helper.contexts") local providers = require("fzfx.helper.providers") local fzf_helpers = require("fzfx.detail.fzf_helpers") - local git_live_grep_cfg = require("fzfx.cfg.git_live_grep") + require("fzfx").setup() describe("git_live_grep", function() it("_git_live_grep_provider", function() diff --git a/test/cfg/git_status_spec.lua b/test/cfg/git_status_spec.lua index 78493e1c2..8d895ebe3 100644 --- a/test/cfg/git_status_spec.lua +++ b/test/cfg/git_status_spec.lua @@ -14,17 +14,13 @@ describe("cfg.git_status", function() local github_actions = os.getenv("GITHUB_ACTIONS") == "true" - require("fzfx").setup() - local tbls = require("fzfx.lib.tables") + local strings = require("fzfx.commons.strings") local consts = require("fzfx.lib.constants") - local strs = require("fzfx.lib.strings") - local paths = require("fzfx.lib.paths") - local contexts = require("fzfx.helper.contexts") local providers = require("fzfx.helper.providers") local fzf_helpers = require("fzfx.detail.fzf_helpers") - local git_status_cfg = require("fzfx.cfg.git_status") + require("fzfx").setup() describe("[git_status]", function() it("_git_status_previewer", function() @@ -38,11 +34,11 @@ describe("cfg.git_status", function() for _, line in ipairs(lines) do local actual = git_status_cfg._git_status_previewer(line) assert_eq(type(actual), "string") - assert_true(strs.find(actual, "git diff") > 0) + assert_true(strings.find(actual, "git diff") > 0) if vim.fn.executable("delta") > 0 then - assert_true(strs.find(actual, "delta") > 0) + assert_true(strings.find(actual, "delta") > 0) else - assert_true(strs.find(actual, "delta") == nil) + assert_true(strings.find(actual, "delta") == nil) end end end) diff --git a/test/cfg/live_grep_spec.lua b/test/cfg/live_grep_spec.lua index ece28a87d..3548a30d0 100644 --- a/test/cfg/live_grep_spec.lua +++ b/test/cfg/live_grep_spec.lua @@ -15,13 +15,8 @@ describe("cfg.live_grep", function() local github_actions = os.getenv("GITHUB_ACTIONS") == "true" require("fzfx").setup() - local tbls = require("fzfx.lib.tables") local consts = require("fzfx.lib.constants") - local strs = require("fzfx.lib.strings") - local paths = require("fzfx.lib.paths") - local contexts = require("fzfx.helper.contexts") - local live_grep_cfg = require("fzfx.cfg.live_grep") describe("[_get_buf_path]", function() diff --git a/test/cfg/lsp_diagnostics_spec.lua b/test/cfg/lsp_diagnostics_spec.lua index 92fc2e057..3d53f0198 100644 --- a/test/cfg/lsp_diagnostics_spec.lua +++ b/test/cfg/lsp_diagnostics_spec.lua @@ -14,17 +14,13 @@ describe("cfg.lsp_diagnostics", function() local github_actions = os.getenv("GITHUB_ACTIONS") == "true" - require("fzfx").setup() - local tbls = require("fzfx.lib.tables") + local strings = require("fzfx.commons.strings") local consts = require("fzfx.lib.constants") - local strs = require("fzfx.lib.strings") - local paths = require("fzfx.lib.paths") - local contexts = require("fzfx.helper.contexts") local providers = require("fzfx.helper.providers") local fzf_helpers = require("fzfx.detail.fzf_helpers") - local lsp_diagnostics_cfg = require("fzfx.cfg.lsp_diagnostics") + require("fzfx").setup() describe("lsp_diagnostics", function() it("_make_lsp_diagnostic_signs", function() @@ -36,13 +32,13 @@ describe("cfg.lsp_diagnostics", function() assert_true(sign_item.severity >= 1 and sign_item.severity <= 4) assert_true( string.len(sign_item.name) > 0 - and strs.startswith(sign_item.name, "DiagnosticSign") + and strings.startswith(sign_item.name, "DiagnosticSign") ) assert_true( - strs.endswith(sign_item.name, "Error") - or strs.endswith(sign_item.name, "Warn") - or strs.endswith(sign_item.name, "Info") - or strs.endswith(sign_item.name, "Hint") + strings.endswith(sign_item.name, "Error") + or strings.endswith(sign_item.name, "Warn") + or strings.endswith(sign_item.name, "Info") + or strings.endswith(sign_item.name, "Hint") ) end end) diff --git a/test/cfg/vim_commands_spec.lua b/test/cfg/vim_commands_spec.lua index 073724164..0c4be9400 100644 --- a/test/cfg/vim_commands_spec.lua +++ b/test/cfg/vim_commands_spec.lua @@ -14,15 +14,12 @@ describe("cfg.vim_commands", function() local github_actions = os.getenv("GITHUB_ACTIONS") == "true" - require("fzfx").setup() - local tbls = require("fzfx.lib.tables") + local strings = require("fzfx.commons.strings") + local paths = require("fzfx.commons.paths") local consts = require("fzfx.lib.constants") - local strs = require("fzfx.lib.strings") - local paths = require("fzfx.lib.paths") - local contexts = require("fzfx.helper.contexts") - local vim_commands_cfg = require("fzfx.cfg.vim_commands") + require("fzfx").setup() describe("[commands]", function() it("_parse_vim_ex_command_name", function() @@ -75,10 +72,10 @@ describe("cfg.vim_commands", function() local actual3 = vim_commands_cfg._parse_ex_command_output_header(line) assert_eq(type(actual3), "table") assert_eq(actual3.name_pos, 1) - assert_eq(actual3.args_pos, strs.find(line, "Args")) - assert_eq(actual3.address_pos, strs.find(line, "Address")) - assert_eq(actual3.complete_pos, strs.find(line, "Complete")) - assert_eq(actual3.definition_pos, strs.find(line, "Definition")) + assert_eq(actual3.args_pos, strings.find(line, "Args")) + assert_eq(actual3.address_pos, strings.find(line, "Address")) + assert_eq(actual3.complete_pos, strings.find(line, "Complete")) + assert_eq(actual3.definition_pos, strings.find(line, "Definition")) end ) it("_parse_ex_command_output_lua_function_definition", function() @@ -101,7 +98,7 @@ describe("cfg.vim_commands", function() " Find buffers", " Find buffers by yank text", } - local def_pos = strs.find(header, "Definition") + local def_pos = strings.find(header, "Definition") for _, line in ipairs(success_lines) do local actual = vim_commands_cfg._parse_ex_command_output_lua_function_definition( @@ -248,17 +245,17 @@ describe("cfg.vim_commands", function() -- ) assert_eq(type(actual), "table") assert_eq(#actual, 3) - assert_true(strs.startswith(actual[1], "Name")) - assert_true(strs.endswith(actual[1], "Definition/Location")) - assert_true(strs.startswith(actual[2], "FzfxGBranches")) + assert_true(strings.startswith(actual[1], "Name")) + assert_true(strings.endswith(actual[1], "Definition/Location")) + assert_true(strings.startswith(actual[2], "FzfxGBranches")) local expect = string.format( "%s:%d", paths.reduce(commands[1].loc.filename), commands[1].loc.lineno ) - assert_true(strs.endswith(actual[2], expect)) - assert_true(strs.startswith(actual[3], "bnext")) - assert_true(strs.endswith(actual[3], '"next buffer"')) + assert_true(strings.endswith(actual[2], expect)) + assert_true(strings.startswith(actual[3], "bnext")) + assert_true(strings.endswith(actual[3], '"next buffer"')) end) end) @@ -288,7 +285,7 @@ describe("cfg.vim_commands", function() assert_eq(type(act.name), "string") assert_true(string.len(act.name) > 0) assert_true(vim.fn.exists(":" .. act.name) >= 0) - if strs.isalpha(act.name:sub(1, 1)) and act.name ~= "range" then + if strings.isalpha(act.name:sub(1, 1)) and act.name ~= "range" then assert_true(vim.fn.exists(":" .. act.name) > 0) end end diff --git a/test/cfg/vim_keymaps_spec.lua b/test/cfg/vim_keymaps_spec.lua index f65ff3035..36af1c2b0 100644 --- a/test/cfg/vim_keymaps_spec.lua +++ b/test/cfg/vim_keymaps_spec.lua @@ -13,18 +13,13 @@ describe("cfg.vim_keymaps", function() end) local github_actions = os.getenv("GITHUB_ACTIONS") == "true" - - require("fzfx").setup() - local tbls = require("fzfx.lib.tables") + local strings = require("fzfx.commons.strings") local consts = require("fzfx.lib.constants") - local strs = require("fzfx.lib.strings") - local paths = require("fzfx.lib.paths") - local contexts = require("fzfx.helper.contexts") local providers = require("fzfx.helper.providers") local fzf_helpers = require("fzfx.detail.fzf_helpers") - local vim_keymaps_cfg = require("fzfx.cfg.vim_keymaps") + require("fzfx").setup() describe("[keymaps]", function() it("_make_vim_keymaps_provider all", function() @@ -135,8 +130,8 @@ describe("cfg.vim_keymaps", function() -- print(string.format("render vim keymaps:%s\n", vim.inspect(actual))) assert_eq(type(actual), "table") assert_true(#actual >= 1) - assert_true(strs.startswith(actual[1], "Key")) - assert_true(strs.endswith(actual[1], "Definition/Location")) + assert_true(strings.startswith(actual[1], "Key")) + assert_true(strings.endswith(actual[1], "Definition/Location")) for i = 2, #actual do assert_true(string.len(actual[i]) > 0) end diff --git a/test/config_spec.lua b/test/config_spec.lua index 36fac8df1..13f02029d 100644 --- a/test/config_spec.lua +++ b/test/config_spec.lua @@ -22,20 +22,16 @@ describe("config", function() } end - local tbls = require("fzfx.lib.tables") + local tables = require("fzfx.commons.tables") local consts = require("fzfx.lib.constants") - local strs = require("fzfx.lib.strings") - local paths = require("fzfx.lib.paths") - local fzf_helpers = require("fzfx.detail.fzf_helpers") - local conf = require("fzfx.config") describe("[get_config]", function() it("test", function() conf.setup() assert_eq(type(conf.get_config()), "table") - assert_false(tbls.tbl_empty(conf.get_config())) + assert_false(tables.tbl_empty(conf.get_config())) assert_eq(type(conf.get_config().live_grep), "table") assert_eq(type(conf.get_config().debug), "table") assert_eq(type(conf.get_config().debug.enable), "boolean") @@ -58,7 +54,7 @@ describe("config", function() it("get defaults", function() conf.setup() assert_eq(type(conf.get_defaults()), "table") - assert_false(tbls.tbl_empty(conf.get_defaults())) + assert_false(tables.tbl_empty(conf.get_defaults())) assert_eq(type(conf.get_defaults().live_grep), "table") assert_eq(type(conf.get_defaults().debug), "table") assert_eq(type(conf.get_defaults().debug.enable), "boolean") diff --git a/test/detail/fzf_helpers_spec.lua b/test/detail/fzf_helpers_spec.lua index d46541c4a..bef9d0c9e 100644 --- a/test/detail/fzf_helpers_spec.lua +++ b/test/detail/fzf_helpers_spec.lua @@ -14,19 +14,17 @@ describe("detail.fzf_helpers", function() end end) - local CommandFeedEnum = require("fzfx.schema").CommandFeedEnum - - local tbls = require("fzfx.lib.tables") - local strs = require("fzfx.lib.strings") + local tables = require("fzfx.commons.tables") + local strings = require("fzfx.commons.strings") local fzf_helpers = require("fzfx.detail.fzf_helpers") - + local CommandFeedEnum = require("fzfx.schema").CommandFeedEnum require("fzfx.config").setup() require("fzfx.detail.module").setup() describe("[make_last_query_cache]", function() it("makes", function() local actual = fzf_helpers.make_last_query_cache("test") - assert_true(strs.endswith(actual, "_test_last_query_cache")) + assert_true(strings.endswith(actual, "_test_last_query_cache")) end) end) @@ -140,7 +138,7 @@ describe("detail.fzf_helpers", function() }) print(string.format("preprocess nil opts: %s\n", vim.inspect(actual))) assert_true(type(actual) == "table") - assert_false(tbls.tbl_empty(actual)) + assert_false(tables.tbl_empty(actual)) assert_eq(#actual, 1) end) it("preprocess string opts", function() @@ -152,7 +150,7 @@ describe("detail.fzf_helpers", function() }) print(string.format("preprocess string opts: %s\n", vim.inspect(actual))) assert_true(type(actual) == "table") - assert_false(tbls.tbl_empty(actual)) + assert_false(tables.tbl_empty(actual)) assert_eq(#actual, 2) end) end) @@ -191,7 +189,7 @@ describe("detail.fzf_helpers", function() print(string.format("make default opts: %s\n", vim.inspect(actual))) assert_eq(type(actual), "string") assert_true(string.len(actual --[[@as string]]) > 0) - assert_true(strs.find(actual, "--border") > 0) + assert_true(strings.find(actual, "--border") > 0) end) end) @@ -203,7 +201,7 @@ describe("detail.fzf_helpers", function() print(string.format("default fzf opts:%s\n", vim.inspect(actual))) assert_eq(type(actual), "string") assert_true(string.len(actual --[[@as string]]) > 0) - assert_true(strs.find(actual, "--border") > 0) + assert_true(strings.find(actual, "--border") > 0) end) end) @@ -216,7 +214,7 @@ describe("detail.fzf_helpers", function() assert_true(actual:gmatch("general") ~= nil) assert_true(actual:gmatch("provider") ~= nil) assert_true( - strs.find(actual, "nvim -n -u NONE --clean --headless -l") >= 1 + strings.find(actual, "nvim -n -u NONE --clean --headless -l") >= 1 ) end) end) @@ -240,7 +238,7 @@ describe("detail.fzf_helpers", function() binder:append("a"):append("b") local actual = binder:build() assert_eq(actual[1], "--bind") - assert_true(strs.startswith(actual[2], "focus:a+b")) + assert_true(strings.startswith(actual[2], "focus:a+b")) end) end) end) diff --git a/test/detail/general_spec.lua b/test/detail/general_spec.lua index abb989b5c..38fa7cd8e 100644 --- a/test/detail/general_spec.lua +++ b/test/detail/general_spec.lua @@ -15,11 +15,11 @@ describe("detail.general", function() local github_actions = os.getenv("GITHUB_ACTIONS") == "true" - local tbls = require("fzfx.lib.tables") - local strs = require("fzfx.lib.strings") + local tables = require("fzfx.commons.tables") + local strings = require("fzfx.commons.strings") local fileios = require("fzfx.commons.fileios") local jsons = require("fzfx.commons.jsons") - local paths = require("fzfx.lib.paths") + local paths = require("fzfx.commons.paths") local schema = require("fzfx.schema") local conf = require("fzfx.config") @@ -34,9 +34,9 @@ describe("detail.general", function() provider = "ls -1", }) assert_eq(type(ps), "table") - assert_false(tbls.tbl_empty(ps)) + assert_false(tables.tbl_empty(ps)) assert_eq(type(ps.provider_configs.default), "table") - assert_false(tbls.tbl_empty(ps.provider_configs.default)) + assert_false(tables.tbl_empty(ps.provider_configs.default)) assert_eq(ps.provider_configs.default.key, "ctrl-k") assert_eq(ps.provider_configs.default.provider, "ls -1") assert_eq(ps.provider_configs.default.provider_type, "plain") @@ -63,9 +63,9 @@ describe("detail.general", function() provider = { "ls", "-lh", "~" }, }) assert_eq(type(ps), "table") - assert_false(tbls.tbl_empty(ps)) + assert_false(tables.tbl_empty(ps)) assert_eq(type(ps.provider_configs.default), "table") - assert_false(tbls.tbl_empty(ps.provider_configs.default)) + assert_false(tables.tbl_empty(ps.provider_configs.default)) assert_eq(ps.provider_configs.default.key, "ctrl-k") assert_eq(type(ps.provider_configs.default.provider), "table") assert_eq(#ps.provider_configs.default.provider, 3) @@ -106,10 +106,10 @@ describe("detail.general", function() }, }) assert_eq(type(ps), "table") - assert_false(tbls.tbl_empty(ps)) + assert_false(tables.tbl_empty(ps)) assert_eq(type(ps.provider_configs.p1), "table") - assert_false(tbls.tbl_empty(ps.provider_configs.p1)) + assert_false(tables.tbl_empty(ps.provider_configs.p1)) assert_eq(ps.provider_configs.p1.key, "ctrl-p") assert_eq(type(ps.provider_configs.p1.provider), "string") assert_eq(ps.provider_configs.p1.provider, "p1") @@ -131,7 +131,7 @@ describe("detail.general", function() end assert_eq(type(ps.provider_configs.p2), "table") - assert_false(tbls.tbl_empty(ps.provider_configs.p2)) + assert_false(tables.tbl_empty(ps.provider_configs.p2)) assert_eq(ps.provider_configs.p2.key, "ctrl-q") assert_eq(type(ps.provider_configs.p2.provider), "table") assert_eq(type(ps.provider_configs.p2.provider), "table") @@ -274,9 +274,9 @@ describe("detail.general", function() end, }, FZFPORTFILE) assert_eq(type(ps), "table") - assert_false(tbls.tbl_empty(ps)) + assert_false(tables.tbl_empty(ps)) assert_eq(type(ps.previewer_configs), "table") - assert_false(tbls.tbl_empty(ps.previewer_configs)) + assert_false(tables.tbl_empty(ps.previewer_configs)) assert_eq(type(ps.previewer_configs.default.previewer), "function") assert_eq(ps.previewer_configs.default.previewer(), "ls -1") assert_eq(ps.previewer_configs.default.previewer_type, "command") @@ -297,16 +297,16 @@ describe("detail.general", function() }, }, FZFPORTFILE) assert_eq(type(ps), "table") - assert_false(tbls.tbl_empty(ps)) + assert_false(tables.tbl_empty(ps)) assert_eq(type(ps.previewer_configs), "table") - assert_false(tbls.tbl_empty(ps.previewer_configs)) + assert_false(tables.tbl_empty(ps.previewer_configs)) assert_eq(type(ps.previewer_configs.p1.previewer), "function") assert_eq(ps.previewer_configs.p1.previewer(), "p1") assert_eq(ps.previewer_configs.p1.previewer_type, "command") assert_eq(ps:switch("p1"), nil) assert_eq(type(ps.previewer_configs), "table") - assert_false(tbls.tbl_empty(ps.previewer_configs)) + assert_false(tables.tbl_empty(ps.previewer_configs)) assert_eq(type(ps.previewer_configs.p2.previewer), "function") assert_eq(ps.previewer_configs.p2.previewer(), "p2") assert_eq(ps.previewer_configs.p2.previewer_type, "command") @@ -370,21 +370,25 @@ describe("detail.general", function() print(string.format("render help1:%s\n", actual)) assert_true(actual:gmatch("to doc1") ~= nil) assert_true(actual:gmatch("BS") ~= nil) - assert_true(strs.find(actual, "to doc1") > strs.find(actual, "BS")) + assert_true(strings.find(actual, "to doc1") > strings.find(actual, "BS")) end) it("renders2", function() local actual = general._render_help("do_it", "ctrl") print(string.format("render help2:%s\n", actual)) assert_true(actual:gmatch("to do it") ~= nil) assert_true(actual:gmatch("CTRL") ~= nil) - assert_true(strs.find(actual, "to do it") > strs.find(actual, "CTRL")) + assert_true( + strings.find(actual, "to do it") > strings.find(actual, "CTRL") + ) end) it("renders3", function() local actual = general._render_help("ok_ok", "alt") print(string.format("render help3:%s\n", actual)) assert_true(actual:gmatch("to ok ok") ~= nil) assert_true(actual:gmatch("ALT") ~= nil) - assert_true(strs.find(actual, "to ok ok") > strs.find(actual, "ALT")) + assert_true( + strings.find(actual, "to ok ok") > strings.find(actual, "ALT") + ) end) end) describe("[_should_skip_help]", function() @@ -415,13 +419,14 @@ describe("detail.general", function() assert_eq(type(actual), "table") assert_eq(#actual, 2) assert_true( - strs.find(actual[1], "to action1") > strs.find(actual[1], "CTRL-L") + strings.find(actual[1], "to action1") + > strings.find(actual[1], "CTRL-L") ) - assert_true(strs.endswith(actual[1], "to action1")) + assert_true(strings.endswith(actual[1], "to action1")) assert_true( - strs.find(actual[2], "to upper") > strs.find(actual[2], "CTRL-U") + strings.find(actual[2], "to upper") > strings.find(actual[2], "CTRL-U") ) - assert_true(strs.endswith(actual[2], "to upper")) + assert_true(strings.endswith(actual[2], "to upper")) end) it("make2", function() local action_configs = { @@ -439,17 +444,19 @@ describe("detail.general", function() assert_eq(type(actual), "table") assert_eq(#actual, 3) assert_true( - strs.find(actual[1], "to action1") > strs.find(actual[1], "CTRL-L") + strings.find(actual[1], "to action1") + > strings.find(actual[1], "CTRL-L") ) - assert_true(strs.endswith(actual[1], "to action1")) + assert_true(strings.endswith(actual[1], "to action1")) assert_true( - strs.find(actual[2], "to goto inter") > strs.find(actual[2], "ALT-P") + strings.find(actual[2], "to goto inter") + > strings.find(actual[2], "ALT-P") ) - assert_true(strs.endswith(actual[2], "to goto inter")) + assert_true(strings.endswith(actual[2], "to goto inter")) assert_true( - strs.find(actual[3], "to upper") > strs.find(actual[3], "CTRL-U") + strings.find(actual[3], "to upper") > strings.find(actual[3], "CTRL-U") ) - assert_true(strs.endswith(actual[3], "to upper")) + assert_true(strings.endswith(actual[3], "to upper")) end) end) describe("[_make_cache_filename]", function() diff --git a/test/detail/module_spec.lua b/test/detail/module_spec.lua index cf1d2d130..12e066f49 100644 --- a/test/detail/module_spec.lua +++ b/test/detail/module_spec.lua @@ -10,7 +10,7 @@ describe("detail.module", function() vim.api.nvim_command("cd " .. cwd) end) - local strs = require("fzfx.lib.strings") + local strings = require("fzfx.commons.strings") local conf = require("fzfx.config") local module = require("fzfx.detail.module") @@ -57,7 +57,10 @@ describe("detail.module", function() or ( type(vim.env._FZFX_NVIM_DEVICONS_PATH) == "string" and type( - strs.find(vim.env._FZFX_NVIM_DEVICONS_PATH, "nvim-web-devicons") + strings.find( + vim.env._FZFX_NVIM_DEVICONS_PATH, + "nvim-web-devicons" + ) ) == "number" ) diff --git a/test/detail/popup_spec.lua b/test/detail/popup_spec.lua index 38416064c..f01991219 100644 --- a/test/detail/popup_spec.lua +++ b/test/detail/popup_spec.lua @@ -14,8 +14,8 @@ describe("detail.popup", function() end end) - local tbls = require("fzfx.lib.tables") - local strs = require("fzfx.lib.strings") + local tables = require("fzfx.commons.tables") + local strings = require("fzfx.commons.strings") local fzf_helpers = require("fzfx.detail.fzf_helpers") local popup = require("fzfx.detail.popup") @@ -165,10 +165,10 @@ describe("detail.popup", function() it("get all instances", function() popup._remove_all_popup_window_instances() assert_eq(type(popup._get_all_popup_window_instances()), "table") - assert_true(tbls.tbl_empty(popup._get_all_popup_window_instances())) + assert_true(tables.tbl_empty(popup._get_all_popup_window_instances())) local pw = popup.PopupWindow:new() assert_eq(type(popup._get_all_popup_window_instances()), "table") - assert_false(tbls.tbl_empty(popup._get_all_popup_window_instances())) + assert_false(tables.tbl_empty(popup._get_all_popup_window_instances())) local instances = popup._get_all_popup_window_instances() for _, p in pairs(instances) do assert_eq(p.winnr, pw.winnr) @@ -184,7 +184,7 @@ describe("detail.popup", function() it("create and remove instances", function() popup._remove_all_popup_window_instances() assert_eq(type(popup._get_all_popup_window_instances()), "table") - assert_true(tbls.tbl_empty(popup._get_all_popup_window_instances())) + assert_true(tables.tbl_empty(popup._get_all_popup_window_instances())) assert_eq(popup._count_all_popup_window_instances(), 0) local pw = popup.PopupWindow:new() assert_eq(popup._count_all_popup_window_instances(), 1) @@ -266,9 +266,9 @@ describe("detail.popup", function() print(string.format("make fzf command:%s\n", vim.inspect(actual))) assert_eq(type(actual), "string") assert_true(string.len(actual) > 0) - assert_true(strs.startswith(actual, "fzf ")) - assert_eq(strs.find(actual, fzfopts), 5) - assert_true(strs.find(actual, "--expect") > string.len(fzfopts)) + assert_true(strings.startswith(actual, "fzf ")) + assert_eq(strings.find(actual, fzfopts), 5) + assert_true(strings.find(actual, "--expect") > string.len(fzfopts)) end) end) end) diff --git a/test/helper/actions_spec.lua b/test/helper/actions_spec.lua index 53e36fd03..39bf931fa 100644 --- a/test/helper/actions_spec.lua +++ b/test/helper/actions_spec.lua @@ -21,8 +21,8 @@ describe("helper.actions", function() local DEVICONS_PATH = "~/github/linrongbin16/.config/nvim/lazy/nvim-web-devicons" - local strs = require("fzfx.lib.strings") - local paths = require("fzfx.lib.paths") + local strings = require("fzfx.commons.strings") + local paths = require("fzfx.commons.paths") local actions = require("fzfx.helper.actions") local parsers = require("fzfx.helper.parsers") @@ -50,8 +50,10 @@ describe("helper.actions", function() assert_eq(type(actual), "table") assert_eq(#actual, #lines) for i, line in ipairs(lines) do - local expect = - string.format("edit! %s", paths.normalize(line, { expand = true })) + local expect = string.format( + "edit! %s", + paths.normalize(line, { double_backslash = true, expand = true }) + ) assert_eq(actual[i], expect) end end) @@ -68,10 +70,13 @@ describe("helper.actions", function() assert_eq(type(actual), "table") assert_eq(#actual, #lines) for i, line in ipairs(lines) do - local first_space_pos = strs.find(line, " ") + local first_space_pos = strings.find(line, " ") local expect = string.format( "edit! %s", - paths.normalize(line:sub(first_space_pos + 1), { expand = true }) + paths.normalize( + line:sub(first_space_pos + 1), + { double_backslash = true, expand = true } + ) ) assert_eq(actual[i], expect) end @@ -189,7 +194,10 @@ describe("helper.actions", function() if i <= #lines then local expect = string.format( "edit! %s", - paths.normalize(strs.split(lines[i], ":")[1], { expand = true }) + paths.normalize( + strings.split(lines[i], ":")[1], + { double_backslash = true, expand = true } + ) ) assert_eq(act, expect) else @@ -211,20 +219,20 @@ describe("helper.actions", function() assert_eq(#actual, #lines + 1) for i = 1, 5 do local line = lines[i] - local first_space_pos = strs.find(line, " ") + local first_space_pos = strings.find(line, " ") local expect = string.format( "edit! %s", paths.normalize( line:sub( first_space_pos + 1, - strs.find(line, ":", first_space_pos + 1) - 1 + strings.find(line, ":", first_space_pos + 1) - 1 ), - { expand = true } + { double_backslash = true, expand = true } ) ) assert_eq(actual[i], expect) end - assert_true(strs.find(actual[6], "setpos") > 0) + assert_true(strings.find(actual[6], "setpos") > 0) end) it("run without icon", function() vim.env._FZFX_NVIM_DEVICONS_PATH = nil @@ -271,7 +279,7 @@ describe("helper.actions", function() assert_eq(act.filename, expect.filename) assert_eq(act.lnum, expect.lineno) assert_eq(act.col, 1) - assert_eq(act.text, line:sub(strs.rfind(line, ":") + 1)) + assert_eq(act.text, line:sub(strings.rfind(line, ":") + 1)) end end) it("test with icon", function() @@ -293,7 +301,7 @@ describe("helper.actions", function() assert_eq(act.filename, expect.filename) assert_eq(act.lnum, expect.lineno) assert_eq(act.col, 1) - assert_eq(act.text, line:sub(strs.rfind(line, ":") + 1)) + assert_eq(act.text, line:sub(strings.rfind(line, ":") + 1)) end end) it("run without icon", function() @@ -339,7 +347,10 @@ describe("helper.actions", function() if i <= #lines then local expect = string.format( "edit! %s", - paths.normalize(strs.split(lines[i], ":")[1], { expand = true }) + paths.normalize( + strings.split(lines[i], ":")[1], + { double_backslash = true, expand = true } + ) ) assert_eq(act, expect) else @@ -362,15 +373,15 @@ describe("helper.actions", function() for i, act in ipairs(actual) do if i <= #lines then local line = lines[i] - local first_space_pos = strs.find(line, " ") + local first_space_pos = strings.find(line, " ") local expect = string.format( "edit! %s", paths.normalize( line:sub( first_space_pos + 1, - strs.find(line, ":", first_space_pos + 1) - 1 + strings.find(line, ":", first_space_pos + 1) - 1 ), - { expand = true } + { double_backslash = true, expand = true } ) ) assert_eq(act, expect) @@ -425,7 +436,7 @@ describe("helper.actions", function() assert_eq(act.filename, expect.filename) assert_eq(act.lnum, expect.lineno) assert_eq(act.col, expect.column) - assert_eq(act.text, line:sub(strs.rfind(line, ":") + 1)) + assert_eq(act.text, line:sub(strings.rfind(line, ":") + 1)) end end) it("test with icon", function() @@ -447,7 +458,7 @@ describe("helper.actions", function() assert_eq(act.filename, expect.filename) assert_eq(act.lnum, expect.lineno) assert_eq(act.col, expect.column) - assert_eq(act.text, line:sub(strs.rfind(line, ":") + 1)) + assert_eq(act.text, line:sub(strings.rfind(line, ":") + 1)) end end) it("run without icon", function() @@ -487,7 +498,7 @@ describe("helper.actions", function() }, CONTEXT) print(string.format("feed vim command:%s\n", vim.inspect(actual))) assert_eq(type(actual), "table") - assert_true(strs.startswith(actual.input, ":")) + assert_true(strings.startswith(actual.input, ":")) assert_eq(actual.mode, "n") end) end) @@ -546,7 +557,7 @@ describe("helper.actions", function() -- ) assert_eq(parsed.fn, "cmd") assert_eq(type(parsed.input), "string") - assert_true(strs.startswith(parsed.input, [[execute "normal \]])) + assert_true(strings.startswith(parsed.input, [[execute "normal \]])) assert_eq(parsed.mode, "n") end) end) @@ -583,7 +594,7 @@ describe("helper.actions", function() "origin/release-please--branches--main--components--fzfx.nvim", } for i, line in ipairs(lines) do - if strs.find(line, "origin/main") then + if strings.find(line, "origin/main") then local actual = actions._make_git_checkout({ line }, CONTEXT) print(string.format("git checkout remote[%d]:%s\n", i, actual)) assert_eq(string.format("!git checkout main"), actual) @@ -609,7 +620,7 @@ describe("helper.actions", function() "remotes/origin/ci-verbose", } for i, line in ipairs(lines) do - if strs.find(line, "main") then + if strings.find(line, "main") then local actual = actions._make_git_checkout({ line }, CONTEXT) print( string.format( @@ -623,7 +634,7 @@ describe("helper.actions", function() else local actual = actions._make_git_checkout({ line }, CONTEXT) print(string.format("git checkout all[%d]:%s\n", i, actual)) - local split_pos = strs.find(line, "remotes/origin/") + local split_pos = strings.find(line, "remotes/origin/") if split_pos then assert_eq( string.format( diff --git a/test/helper/contexts_spec.lua b/test/helper/contexts_spec.lua index b449d9ccc..739094d0c 100644 --- a/test/helper/contexts_spec.lua +++ b/test/helper/contexts_spec.lua @@ -11,13 +11,13 @@ describe("helper.contexts", function() vim.opt.swapfile = false end) - local tbls = require("fzfx.lib.tables") + local tables = require("fzfx.commons.tables") local contexts = require("fzfx.helper.contexts") describe("[make_pipeline_context]", function() it("test", function() local actual = contexts.make_pipeline_context() - assert_true(tbls.tbl_not_empty(actual)) + assert_true(tables.tbl_not_empty(actual)) assert_eq(type(actual.bufnr), "number") assert_eq(type(actual.winnr), "number") assert_eq(type(actual.tabnr), "number") diff --git a/test/helper/parsers_spec.lua b/test/helper/parsers_spec.lua index f238ad1d1..3e308c3b1 100644 --- a/test/helper/parsers_spec.lua +++ b/test/helper/parsers_spec.lua @@ -10,8 +10,8 @@ describe("helper.parsers", function() vim.api.nvim_command("cd " .. cwd) end) - local strs = require("fzfx.lib.strings") - local paths = require("fzfx.lib.paths") + local strings = require("fzfx.commons.strings") + local paths = require("fzfx.commons.paths") local fileios = require("fzfx.commons.fileios") local parsers_helper = require("fzfx.helper.parsers") @@ -29,7 +29,8 @@ describe("helper.parsers", function() "~/github/linrongbin16/fzfx.nvim/test/goodbye world/goodbye.lua", } for i, line in ipairs(lines) do - local expect = paths.normalize(line, { expand = true }) + local expect = + paths.normalize(line, { double_backslash = true, expand = true }) local actual = parsers_helper.parse_find(expect) assert_eq(type(actual), "table") assert_eq(expect, actual.filename) @@ -45,10 +46,10 @@ describe("helper.parsers", function() "󰢱 ~/github/linrongbin16/fzfx.nvim/lua/fzfx/test/goodbye world/world.txt", } for i, line in ipairs(lines) do - local first_space_pos = strs.find(line, " ") + local first_space_pos = strings.find(line, " ") local expect = paths.normalize( vim.trim(line:sub(first_space_pos + 1)), - { expand = true } + { double_backslash = true, expand = true } ) local actual = parsers_helper.parse_find(line) assert_eq(type(actual), "table") @@ -79,11 +80,14 @@ describe("helper.parsers", function() assert_eq(type(actual.filename), "string") assert_eq(type(actual.lineno), "number") - local line_splits = strs.split(line, ":") + local line_splits = strings.split(line, ":") assert_eq(actual.lineno, tonumber(line_splits[2])) assert_eq( actual.filename, - paths.normalize(line_splits[1], { expand = true }) + paths.normalize( + line_splits[1], + { double_backslash = true, expand = true } + ) ) end end) @@ -101,7 +105,7 @@ describe("helper.parsers", function() assert_eq(type(actual), "table") assert_eq(type(actual.filename), "string") assert_eq(type(actual.lineno), "number") - local line_splits = strs.split(line, ":") + local line_splits = strings.split(line, ":") assert_eq(actual.lineno, tonumber(line_splits[2])) assert_eq( actual.filename, @@ -127,7 +131,7 @@ describe("helper.parsers", function() assert_eq(type(actual.filename), "string") assert_eq(type(actual.lineno), "number") assert_eq(type(actual.column), "number") - local line_splits = strs.split(line, ":") + local line_splits = strings.split(line, ":") assert_eq( actual.filename, parsers_helper.parse_find(line_splits[1]).filename @@ -151,7 +155,7 @@ describe("helper.parsers", function() assert_eq(type(actual.filename), "string") assert_eq(type(actual.lineno), "number") assert_eq(type(actual.column), "number") - local line_splits = strs.split(line, ":") + local line_splits = strings.split(line, ":") assert_eq( actual.filename, parsers_helper.parse_find(line_splits[1]).filename @@ -204,7 +208,10 @@ describe("helper.parsers", function() assert_eq(type(actual), "table") assert_eq( actual.filename, - paths.normalize(paths.join(cwd, expect), { expand = true }) + paths.normalize( + paths.join(cwd, expect), + { double_backslash = true, expand = true } + ) ) end end) @@ -233,7 +240,10 @@ describe("helper.parsers", function() assert_eq(type(actual), "table") assert_eq( actual.filename, - paths.normalize(paths.join(cwd, expect), { expand = true }) + paths.normalize( + paths.join(cwd, expect), + { double_backslash = true, expand = true } + ) ) end end) @@ -257,7 +267,10 @@ describe("helper.parsers", function() local expect = expects[i] assert_eq( actual, - paths.normalize(paths.join(cwd, expect), { expand = true }) + paths.normalize( + paths.join(cwd, expect), + { double_backslash = true, expand = true } + ) ) end end) @@ -288,8 +301,10 @@ describe("helper.parsers", function() local parse_eza_on_macos_linux = parsers_helper._make_parse_ls(6) for i, line in ipairs(lines) do local actual = parse_eza_on_macos_linux(line, CONTEXT).filename - local expect = - paths.normalize(paths.join(cwd, expects[i]), { expand = true }) + local expect = paths.normalize( + paths.join(cwd, expects[i]), + { double_backslash = true, expand = true } + ) assert_eq(actual, expect) end end) @@ -309,16 +324,19 @@ describe("helper.parsers", function() ":Next N |Y |N/A |N/A |N/A /opt/homebrew/Cellar/neovim/0.9.4/share/nvim/runtime/doc/index.txt:1124", } for _, line in ipairs(lines) do - local first_space_pos = strs.find(line, " ") + local first_space_pos = strings.find(line, " ") local expect_command = line:sub(1, first_space_pos - 1) - local last_space = strs.rfind(line, " ") - local expect_splits = strs.split(line:sub(last_space + 1), ":") + local last_space = strings.rfind(line, " ") + local expect_splits = strings.split(line:sub(last_space + 1), ":") local actual = parsers_helper.parse_vim_command(line, CONTEXT) assert_eq(type(actual), "table") assert_eq(actual.command, expect_command) assert_eq( actual.filename, - paths.normalize(expect_splits[1], { expand = true }) + paths.normalize( + expect_splits[1], + { double_backslash = true, expand = true } + ) ) assert_eq(actual.lineno, tonumber(expect_splits[2])) end @@ -328,9 +346,9 @@ describe("helper.parsers", function() ':bdelete N |Y |N/A |N/A |N/A "delete buffer"', } for _, line in ipairs(lines) do - local first_space_pos = strs.find(line, " ") + local first_space_pos = strings.find(line, " ") local expect_command = line:sub(1, first_space_pos - 1) - local double_quote_before_last = strs.rfind(line, '"', #line - 1) + local double_quote_before_last = strings.rfind(line, '"', #line - 1) local expect_def = vim.trim(line:sub(double_quote_before_last + 1, #line - 1)) local actual = parsers_helper.parse_vim_command(line, CONTEXT) @@ -346,16 +364,19 @@ describe("helper.parsers", function() "Barbecue Y |Y |N/A |N/A |N/A ~/.config/nvim/lazy/barbecue/lua/barbecue.lua:73", } for _, line in ipairs(lines) do - local first_space_pos = strs.find(line, " ") + local first_space_pos = strings.find(line, " ") local expect_command = line:sub(1, first_space_pos - 1) - local last_space_pos = strs.rfind(line, " ") - local expect_splits = strs.split(line:sub(last_space_pos + 1), ":") + local last_space_pos = strings.rfind(line, " ") + local expect_splits = strings.split(line:sub(last_space_pos + 1), ":") local actual = parsers_helper.parse_vim_command(line, CONTEXT) assert_eq(type(actual), "table") assert_eq(actual.command, expect_command) assert_eq( actual.filename, - paths.normalize(expect_splits[1], { expand = true }) + paths.normalize( + expect_splits[1], + { double_backslash = true, expand = true } + ) ) assert_eq(actual.lineno, tonumber(expect_splits[2])) end @@ -365,9 +386,9 @@ describe("helper.parsers", function() 'Bdelete N |Y |N/A |N/A |N/A "delete buffer"', } for _, line in ipairs(lines) do - local first_space_pos = strs.find(line, " ") + local first_space_pos = strings.find(line, " ") local expect_command = line:sub(1, first_space_pos - 1) - local double_quote_before_last = strs.rfind(line, '"', #line - 1) + local double_quote_before_last = strings.rfind(line, '"', #line - 1) local expect_def = vim.trim(line:sub(double_quote_before_last + 1, #line - 1)) local actual = parsers_helper.parse_vim_command(line, CONTEXT) @@ -392,16 +413,19 @@ describe("helper.parsers", function() "(YankyGPutAfterShiftRight) n |Y |N |Y ~/.config/nvim/lazy/yanky.nvim/lua/yanky.lua:369", } for _, line in ipairs(lines) do - local first_space_pos = strs.find(line, " ") + local first_space_pos = strings.find(line, " ") local expect_lhs = line:sub(1, first_space_pos - 1) - local last_space_pos = strs.rfind(line, " ") - local expect_splits = strs.split(line:sub(last_space_pos + 1), ":") + local last_space_pos = strings.rfind(line, " ") + local expect_splits = strings.split(line:sub(last_space_pos + 1), ":") local actual = parsers_helper.parse_vim_keymap(line, CONTEXT) assert_eq(type(actual), "table") assert_eq(actual.lhs, expect_lhs) assert_eq( actual.filename, - paths.normalize(expect_splits[1], { expand = true }) + paths.normalize( + expect_splits[1], + { double_backslash = true, expand = true } + ) ) assert_eq(actual.lineno, tonumber(expect_splits[2])) end @@ -413,9 +437,9 @@ describe("helper.parsers", function() '<2-LeftMouse> n |N |N |Y "(matchup-double-click)"', } for _, line in ipairs(lines) do - local first_space_pos = strs.find(line, " ") + local first_space_pos = strings.find(line, " ") local expect_lhs = line:sub(1, first_space_pos - 1) - local double_quote_before_last = strs.rfind(line, '"', #line - 1) + local double_quote_before_last = strings.rfind(line, '"', #line - 1) local expect_def = vim.trim(line:sub(double_quote_before_last + 1, #line - 1)) local actual = parsers_helper.parse_vim_keymap(line, CONTEXT) @@ -437,8 +461,10 @@ describe("helper.parsers", function() } for _, line in ipairs(lines) do local actual = parsers_helper.parse_git_status(line) - local expect = - paths.normalize(strs.split(line, " ")[2], { expand = true }) + local expect = paths.normalize( + strings.split(line, " ")[2], + { double_backslash = true, expand = true } + ) assert_eq(type(actual), "table") assert_eq(expect, actual.filename) end @@ -460,9 +486,9 @@ describe("helper.parsers", function() } for i, line in ipairs(lines) do local actual = parsers_helper.parse_git_branch(line, CONTEXT) - local expect_splits = strs.split(line, " ") + local expect_splits = strings.split(line, " ") local expect_remote = expect_splits[#expect_splits] - local slash_splits = strs.split(expect_remote, "/") + local slash_splits = strings.split(expect_remote, "/") local expect_local = slash_splits[#slash_splits] print( string.format( @@ -490,7 +516,7 @@ describe("helper.parsers", function() } for _, line in ipairs(lines) do local actual = parsers_helper.parse_git_commit(line) - local expect = strs.split(line, " ")[1] + local expect = strings.split(line, " ")[1] assert_eq(actual.commit, expect) end end) diff --git a/test/helper/previewer_labels_spec.lua b/test/helper/previewer_labels_spec.lua index b1d7afd87..133d268c7 100644 --- a/test/helper/previewer_labels_spec.lua +++ b/test/helper/previewer_labels_spec.lua @@ -10,7 +10,7 @@ describe("helper.previewer_labels", function() vim.api.nvim_command("cd " .. cwd) end) - local strs = require("fzfx.lib.strings") + local strings = require("fzfx.commons.strings") local parsers = require("fzfx.helper.parsers") local labels = require("fzfx.helper.previewer_labels") local fileios = require("fzfx.commons.fileios") @@ -28,7 +28,7 @@ describe("helper.previewer_labels", function() for _, line in ipairs(lines) do local actual = labels.label_find(line) assert_eq(type(actual), "string") - assert_true(strs.endswith(line, actual)) + assert_true(strings.endswith(line, actual)) end end) end) @@ -46,8 +46,8 @@ describe("helper.previewer_labels", function() for _, line in ipairs(lines) do local actual = labels.label_rg(line) assert_eq(type(actual), "string") - assert_eq(type(strs.find(line, actual)), "number") - assert_true(strs.find(line, actual) > 0) + assert_eq(type(strings.find(line, actual)), "number") + assert_true(strings.find(line, actual) > 0) end end) end) @@ -65,8 +65,8 @@ describe("helper.previewer_labels", function() for _, line in ipairs(lines) do local actual = labels.label_grep(line) assert_eq(type(actual), "string") - assert_eq(type(strs.find(line, actual)), "number") - assert_true(strs.find(line, actual) > 0) + assert_eq(type(strings.find(line, actual)), "number") + assert_true(strings.find(line, actual) > 0) end end) end) @@ -85,10 +85,10 @@ describe("helper.previewer_labels", function() for _, line in ipairs(lines) do local actual = labels.label_vim_command(line, CONTEXT) assert_eq(type(actual), "string") - local actual_splits = strs.split(actual, ":") + local actual_splits = strings.split(actual, ":") assert_eq(#actual_splits, 2) - assert_true(strs.find(line, actual_splits[1]) > 0) - assert_true(strs.endswith(line, actual_splits[2])) + assert_true(strings.find(line, actual_splits[1]) > 0) + assert_true(strings.endswith(line, actual_splits[2])) end end) it("test definition", function() @@ -117,10 +117,10 @@ describe("helper.previewer_labels", function() for _, line in ipairs(lines) do local actual = labels.label_vim_keymap(line, CONTEXT) assert_eq(type(actual), "string") - local actual_splits = strs.split(actual, ":") + local actual_splits = strings.split(actual, ":") assert_eq(#actual_splits, 2) - assert_true(strs.find(line, actual_splits[1]) > 0) - assert_true(strs.endswith(line, actual_splits[2])) + assert_true(strings.find(line, actual_splits[1]) > 0) + assert_true(strings.endswith(line, actual_splits[2])) end end) it("test definition", function() @@ -174,7 +174,7 @@ describe("helper.previewer_labels", function() for i, line in ipairs(lines) do local actual = labels.label_ls(line, CONTEXT) local expect = expects[i] - assert_true(strs.endswith(actual, expect)) + assert_true(strings.endswith(actual, expect)) end end) it("lsd -lh --header --icon=never", function() @@ -199,7 +199,7 @@ describe("helper.previewer_labels", function() for i, line in ipairs(lines) do local actual = labels.label_lsd(line, CONTEXT) local expect = expects[i] - assert_true(strs.endswith(actual, expect)) + assert_true(strings.endswith(actual, expect)) end end) it("eza -lh for macOS/linux", function() @@ -229,7 +229,7 @@ describe("helper.previewer_labels", function() for i, line in ipairs(lines) do local actual = labels.label_eza(line, CONTEXT) local expect = expects[i] - assert_true(strs.endswith(actual, expect)) + assert_true(strings.endswith(actual, expect)) end end) end) diff --git a/test/helper/previewers_spec.lua b/test/helper/previewers_spec.lua index cacb6f7b7..40f593b42 100644 --- a/test/helper/previewers_spec.lua +++ b/test/helper/previewers_spec.lua @@ -13,11 +13,10 @@ describe("helper.previewers", function() vim.cmd([[edit README.md]]) end) - local tbls = require("fzfx.lib.tables") + local tables = require("fzfx.commons.tables") + local strings = require("fzfx.commons.strings") + local paths = require("fzfx.commons.paths") local consts = require("fzfx.lib.constants") - local strs = require("fzfx.lib.strings") - local paths = require("fzfx.lib.paths") - local previewers = require("fzfx.helper.previewers") local conf = require("fzfx.config") local fzf_helpers = require("fzfx.detail.fzf_helpers") @@ -83,10 +82,16 @@ describe("helper.previewers", function() assert_eq(actual[4], "--color=always") assert_eq(actual[5], "--pager=never") assert_eq(actual[6], "--") - assert_eq(actual[7], paths.normalize(line, { expand = true })) + assert_eq( + actual[7], + paths.normalize(line, { double_backslash = true, expand = true }) + ) else assert_eq(actual[1], "cat") - assert_eq(actual[2], paths.normalize(line, { expand = true })) + assert_eq( + actual[2], + paths.normalize(line, { double_backslash = true, expand = true }) + ) end end end) @@ -103,8 +108,10 @@ describe("helper.previewers", function() } for _, line in ipairs(lines) do local actual = previewers.preview_files_grep(line) - local expect = - paths.normalize(strs.split(line, ":")[1], { expand = true }) + local expect = paths.normalize( + strings.split(line, ":")[1], + { double_backslash = true, expand = true } + ) print(string.format("normalize:%s\n", vim.inspect(expect))) print(string.format("file previewer grep:%s\n", vim.inspect(actual))) if actual[1] == "bat" then @@ -113,9 +120,9 @@ describe("helper.previewers", function() assert_eq(actual[3], "--theme=base16") assert_eq(actual[4], "--color=always") assert_eq(actual[5], "--pager=never") - assert_true(strs.startswith(actual[6], "--highlight-line")) + assert_true(strings.startswith(actual[6], "--highlight-line")) assert_eq(actual[7], "--") - assert_true(strs.startswith(actual[8], expect)) + assert_true(strings.startswith(actual[8], expect)) else assert_eq(actual[1], "cat") assert_eq(actual[2], expect) @@ -142,11 +149,11 @@ describe("helper.previewers", function() local actual = previewers._make_preview_git_commit(line) if actual ~= nil then assert_eq(type(actual), "string") - assert_true(strs.find(actual, "git show") > 0) + assert_true(strings.find(actual, "git show") > 0) if vim.fn.executable("delta") > 0 then - assert_true(strs.find(actual, "delta") > 0) + assert_true(strings.find(actual, "delta") > 0) else - assert_true(strs.find(actual, "delta") == nil) + assert_true(strings.find(actual, "delta") == nil) end end end @@ -162,11 +169,11 @@ describe("helper.previewers", function() local actual = previewers.preview_git_commit(line) if actual ~= nil then assert_eq(type(actual), "string") - assert_true(strs.find(actual, "git show") > 0) + assert_true(strings.find(actual, "git show") > 0) if vim.fn.executable("delta") > 0 then - assert_true(strs.find(actual, "delta") > 0) + assert_true(strings.find(actual, "delta") > 0) else - assert_true(strs.find(actual, "delta") == nil) + assert_true(strings.find(actual, "delta") == nil) end end end @@ -188,8 +195,8 @@ describe("helper.previewers", function() assert_eq(actual[4], "--color=always") assert_eq(actual[5], "--pager=never") assert_eq(actual[6], "--highlight-line=135") - assert_true(strs.startswith(actual[7], "--line-range")) - assert_true(strs.endswith(actual[8], ":")) + assert_true(strings.startswith(actual[7], "--line-range")) + assert_true(strings.endswith(actual[8], ":")) assert_eq(actual[9], "--") assert_eq(actual[10], "lua/fzfx/config.lua") else diff --git a/test/helper/providers_spec.lua b/test/helper/providers_spec.lua index 8fb5eccd2..4ff1775cd 100644 --- a/test/helper/providers_spec.lua +++ b/test/helper/providers_spec.lua @@ -14,30 +14,27 @@ describe("helper.providers", function() local github_actions = os.getenv("GITHUB_ACTIONS") == "true" - local tbls = require("fzfx.lib.tables") + local tables = require("fzfx.commons.tables") local consts = require("fzfx.lib.constants") - local strs = require("fzfx.lib.strings") - local paths = require("fzfx.lib.paths") - local contexts = require("fzfx.helper.contexts") local providers = require("fzfx.helper.providers") local fzf_helpers = require("fzfx.detail.fzf_helpers") describe("[files]", function() it("test", function() - assert_true(tbls.tbl_not_empty(providers.RESTRICTED_FD)) - assert_true(tbls.tbl_not_empty(providers.RESTRICTED_FIND)) - assert_true(tbls.tbl_not_empty(providers.UNRESTRICTED_FD)) - assert_true(tbls.tbl_not_empty(providers.UNRESTRICTED_FIND)) + assert_true(tables.tbl_not_empty(providers.RESTRICTED_FD)) + assert_true(tables.tbl_not_empty(providers.RESTRICTED_FIND)) + assert_true(tables.tbl_not_empty(providers.UNRESTRICTED_FD)) + assert_true(tables.tbl_not_empty(providers.UNRESTRICTED_FIND)) end) end) describe("[live_grep]", function() it("test", function() - assert_true(tbls.tbl_not_empty(providers.RESTRICTED_RG)) - assert_true(tbls.tbl_not_empty(providers.RESTRICTED_GREP)) - assert_true(tbls.tbl_not_empty(providers.UNRESTRICTED_RG)) - assert_true(tbls.tbl_not_empty(providers.UNRESTRICTED_GREP)) + assert_true(tables.tbl_not_empty(providers.RESTRICTED_RG)) + assert_true(tables.tbl_not_empty(providers.RESTRICTED_GREP)) + assert_true(tables.tbl_not_empty(providers.UNRESTRICTED_RG)) + assert_true(tables.tbl_not_empty(providers.UNRESTRICTED_GREP)) end) end) end) diff --git a/test/lib/commands_spec.lua b/test/lib/commands_spec.lua index bf7a65ffc..7d01666b1 100644 --- a/test/lib/commands_spec.lua +++ b/test/lib/commands_spec.lua @@ -10,11 +10,11 @@ describe("lib.commands", function() vim.api.nvim_command("cd " .. cwd) end) - require("fzfx").setup() + local tbls = require("fzfx.commons.tables") local CommandResult = require("fzfx.lib.commands").CommandResult local Command = require("fzfx.lib.commands").Command local cmds = require("fzfx.lib.commands") - local tbls = require("fzfx.lib.tables") + require("fzfx").setup() describe("[CommandResult]", function() it("new result is empty", function() diff --git a/test/lib/paths_spec.lua b/test/lib/paths_spec.lua deleted file mode 100644 index d66979681..000000000 --- a/test/lib/paths_spec.lua +++ /dev/null @@ -1,106 +0,0 @@ ----@diagnostic disable: undefined-field, unused-local -local cwd = vim.fn.getcwd() - -describe("lib.paths", function() - local assert_eq = assert.is_equal - local assert_true = assert.is_true - local assert_false = assert.is_false - - before_each(function() - vim.api.nvim_command("cd " .. cwd) - end) - - local ps = require("fzfx.lib.paths") - describe("[normalize]", function() - it("unix", function() - local expect1 = "~/github/linrongbin16/fzfx.nvim/lua/tests" - local actual1 = ps.normalize(expect1) - local expect2 = "~/github/linrongbin16/fzfx.nvim/lua/tests/test_path.lua" - local actual2 = ps.normalize(expect2) - assert_eq(actual1, expect1) - assert_eq(actual2, expect2) - - local expect3 = "test/lib/paths_spec.lua" - local actual3 = ps.normalize(expect3, { expand = true }) - -- print( - -- string.format( - -- "paths normalize, expect3:%s, actual3:%s\n", - -- vim.inspect(vim.fn.expand(expect3)), - -- vim.inspect(actual3) - -- ) - -- ) - assert_eq(vim.fn.expand(expect3), actual3) - end) - it("windows", function() - local actual1 = ps.normalize( - [[C:\Users\linrongbin\github\linrongbin16\fzfx.nvim\lua\tests]] - ) - local expect1 = - [[C:\Users\linrongbin\github\linrongbin16\fzfx.nvim\lua\tests]] - assert_eq(actual1, expect1) - local actual2 = ps.normalize( - [[C:\Users\linrongbin\github\linrongbin16\fzfx.nvim\lua\tests]], - { backslash = true } - ) - local expect2 = - [[C:/Users/linrongbin/github/linrongbin16/fzfx.nvim/lua/tests]] - assert_eq(actual2, expect2) - local actual3 = ps.normalize( - [[C:\\Users\\linrongbin\\github\\linrongbin16\\fzfx.nvim\\lua\\tests\test_path.lua]] - ) - local expect3 = - [[C:\Users\linrongbin\github\linrongbin16\fzfx.nvim\lua\tests\test_path.lua]] - assert_eq(actual3, expect3) - local actual4 = ps.normalize( - [[C:\\Users\\linrongbin\\github\\linrongbin16\\fzfx.nvim\\lua\\tests\\test_path.lua]], - { backslash = true } - ) - local expect4 = - [[C:/Users/linrongbin/github/linrongbin16/fzfx.nvim/lua/tests/test_path.lua]] - assert_eq(actual4, expect4) - end) - end) - describe("[join]", function() - it("test", function() - local actual1 = ps.join("a", "b", "c") - local expect1 = "a/b/c" - assert_eq(actual1, expect1) - local actual2 = ps.join("a") - local expect2 = "a" - assert_eq(actual2, expect2) - end) - end) - describe("[shorten]", function() - it("test", function() - local expect1 = "~/.config/nvim/lazy/fzfx.nvim/test/path_spec.lua" - local actual1 = ps.shorten(expect1) - print(string.format("expect(%s) shorten: %s\n", expect1, actual1)) - assert_eq(type(actual1), "string") - assert_true(string.len(actual1) < string.len(expect1)) - end) - end) - describe("[reduce]", function() - it("test", function() - local expect1 = "~/.config/nvim/lazy/fzfx.nvim/test/path_spec.lua" - local actual1 = ps.reduce(expect1) - print(string.format("expect(%s) reduce: %s\n", expect1, actual1)) - assert_eq(type(actual1), "string") - assert_eq(expect1, actual1) - end) - end) - describe("[reduce2home]", function() - it("test", function() - local expect1 = "~/.config/nvim/lazy/fzfx.nvim/test/path_spec.lua" - local actual1 = ps.reduce2home(expect1) - print(string.format("expect(%s) reduce2home: %s\n", expect1, actual1)) - assert_eq(type(actual1), "string") - assert_eq(expect1, actual1) - end) - end) - describe("[make_pipe_name]", function() - it("test", function() - local tmp = ps.make_pipe_name() - assert_true(string.len(tmp) > 0) - end) - end) -end) diff --git a/test/lib/spawn_spec.lua b/test/lib/spawn_spec.lua deleted file mode 100644 index f12886cca..000000000 --- a/test/lib/spawn_spec.lua +++ /dev/null @@ -1,438 +0,0 @@ ----@diagnostic disable: undefined-field, unused-local -local cwd = vim.fn.getcwd() - -describe("lib.spawn", function() - local assert_eq = assert.is_equal - local assert_true = assert.is_true - local assert_false = assert.is_false - - before_each(function() - vim.api.nvim_command("cd " .. cwd) - end) - - require("fzfx").setup() - local strs = require("fzfx.lib.strings") - local fileios = require("fzfx.commons.fileios") - local spawn = require("fzfx.lib.spawn") - - local dummy = function() end - - describe("[blocking Spawn]", function() - it("open", function() - local sp = spawn.Spawn:make( - { "cat", "README.md" }, - { on_stdout = dummy, blocking = true } - ) - assert_eq(type(sp), "table") - assert_eq(type(sp.cmds), "table") - assert_eq(#sp.cmds, 2) - assert_eq(sp.cmds[1], "cat") - assert_eq(sp.cmds[2], "README.md") - assert_eq(type(sp.out_pipe), "userdata") - assert_eq(type(sp.err_pipe), "userdata") - end) - it("consume line", function() - local content = fileios.readfile("README.md", { trim = true }) --[[@as string]] - local lines = fileios.readlines("README.md") --[[@as table]] - - local i = 1 - local function process_line(line) - -- print(string.format("[%d]%s", i, line)) - assert_eq(type(line), "string") - assert_eq(line, lines[i]) - i = i + 1 - end - local sp = spawn.Spawn:make( - { "cat", "README.md" }, - { on_stdout = process_line, blocking = true } - ) - local pos = sp:_consume_line(content, process_line) - if pos <= #content then - local line = content:sub(pos, #content) - process_line(line) - end - end) - it("stdout on newline", function() - local content = fileios.readfile("README.md", { trim = true }) --[[@as string]] - local lines = fileios.readlines("README.md") --[[@as table]] - - local i = 1 - local function process_line(line) - -- print(string.format("[%d]%s\n", i, line)) - assert_eq(type(line), "string") - assert_eq(line, lines[i]) - i = i + 1 - end - local sp = spawn.Spawn:make( - { "cat", "README.md" }, - { on_stdout = process_line, blocking = true } - ) - local content_splits = strs.split(content, "\n", { trimempty = false }) - for j, splits in ipairs(content_splits) do - sp:_on_stdout(nil, splits) - if j < #content_splits then - sp:_on_stdout(nil, "\n") - end - end - sp:_on_stdout(nil, nil) - assert_true(sp.out_pipe:is_closing()) - end) - it("stdout on whitespace", function() - local content = fileios.readfile("README.md", { trim = true }) --[[@as string]] - local lines = fileios.readlines("README.md") --[[@as table]] - - local i = 1 - local function process_line(line) - -- print(string.format("[%d]%s\n", i, line)) - assert_eq(type(line), "string") - assert_eq(line, lines[i]) - i = i + 1 - end - local sp = spawn.Spawn:make( - { "cat", "README.md" }, - { on_stdout = process_line, blocking = true } - ) - local content_splits = strs.split(content, " ", { trimempty = false }) - for j, splits in ipairs(content_splits) do - sp:_on_stdout(nil, splits) - if j < #content_splits then - sp:_on_stdout(nil, " ") - end - end - sp:_on_stdout(nil, nil) - assert_true(sp.out_pipe:is_closing()) - end) - local delimiter_i = 0 - while delimiter_i <= 25 do - -- lower case: a - local lower_char = string.char(97 + delimiter_i) - it(string.format("stdout on %s", lower_char), function() - local content = fileios.readfile("README.md", { trim = true }) --[[@as string]] - local lines = fileios.readlines("README.md") --[[@as table]] - - local i = 1 - local function process_line(line) - -- print(string.format("[%d]%s\n", i, line)) - assert_eq(type(line), "string") - assert_eq(line, lines[i]) - i = i + 1 - end - local sp = spawn.Spawn:make( - { "cat", "README.md" }, - { on_stdout = process_line, on_stderr = dummy, blocking = true } - ) - local content_splits = - strs.split(content, lower_char, { trimempty = false }) - for j, splits in ipairs(content_splits) do - sp:_on_stdout(nil, splits) - if j < #content_splits then - sp:_on_stdout(nil, lower_char) - end - end - sp:_on_stdout(nil, nil) - assert_true(sp.out_pipe:is_closing()) - end) - -- upper case: A - local upper_char = string.char(65 + delimiter_i) - it(string.format("stdout on %s", upper_char), function() - local content = fileios.readfile("README.md", { trim = true }) --[[@as string]] - local lines = fileios.readlines("README.md") --[[@as table]] - - local i = 1 - local function process_line(line) - -- print(string.format("[%d]%s\n", i, line)) - assert_eq(type(line), "string") - assert_eq(line, lines[i]) - i = i + 1 - end - local sp = spawn.Spawn:make( - { "cat", "README.md" }, - { on_stdout = process_line, on_stderr = dummy, blocking = true } - ) - local content_splits = - strs.split(content, upper_char, { trimempty = false }) - for j, splits in ipairs(content_splits) do - sp:_on_stdout(nil, splits) - if j < #content_splits then - sp:_on_stdout(nil, upper_char) - end - end - sp:_on_stdout(nil, nil) - assert_true(sp.out_pipe:is_closing()) - end) - delimiter_i = delimiter_i + math.random(1, 5) - end - it("stderr", function() - local sp = spawn.Spawn:make( - { "cat", "README.md" }, - { on_stdout = dummy, on_stderr = dummy, blocking = true } - ) - sp:_on_stderr(nil, nil) - assert_true(sp.err_pipe:is_closing()) - end) - it("stderr2", function() - local i = 1 - local function process_line(line) - -- print(string.format("process[%d]:%s\n", i, line)) - end - local sp = spawn.Spawn:make( - { "cat", "non_exists.txt" }, - { on_stdout = process_line, on_stderr = process_line, blocking = true } - ) - sp:run() - end) - it("iterate on README.md", function() - local lines = fileios.readlines("README.md") --[[@as table]] - - local i = 1 - local function process_line(line) - -- print(string.format("[%d]%s\n", i, line)) - assert_eq(type(line), "string") - assert_eq(lines[i], line) - i = i + 1 - end - - local sp = spawn.Spawn:make( - { "cat", "README.md" }, - { on_stdout = process_line, on_stderr = dummy, blocking = true } - ) - sp:run() - end) - it("iterate on lua/fzfx/config.lua", function() - local lines = fileios.readlines("lua/fzfx/config.lua") --[[@as table]] - - local i = 1 - local function process_line(line) - -- print(string.format("[%d]%s\n", i, line)) - assert_eq(type(line), "string") - assert_eq(lines[i], line) - i = i + 1 - end - - local sp = spawn.Spawn:make( - { "cat", "lua/fzfx/config.lua" }, - { on_stdout = process_line, on_stderr = dummy, blocking = true } - ) - sp:run() - end) - it("close handle", function() - local sp = spawn.Spawn:make( - { "cat", "lua/fzfx/config.lua" }, - { on_stdout = dummy, on_stderr = dummy, blocking = true } - ) - sp:run() - assert_true(sp.process_handle ~= nil) - sp:_close_handle(sp.process_handle) - assert_true(sp.process_handle:is_closing()) - end) - end) - describe("[nonblocking Spawn]", function() - it("open", function() - local sp = spawn.Spawn:make( - { "cat", "README.md" }, - { on_stdout = dummy, blocking = false } - ) - assert_eq(type(sp), "table") - assert_eq(type(sp.cmds), "table") - assert_eq(#sp.cmds, 2) - assert_eq(sp.cmds[1], "cat") - assert_eq(sp.cmds[2], "README.md") - assert_eq(type(sp.out_pipe), "userdata") - assert_eq(type(sp.err_pipe), "userdata") - end) - it("consume line", function() - local content = fileios.readfile("README.md", { trim = true }) --[[@as string]] - local lines = fileios.readlines("README.md") --[[@as table]] - - local i = 1 - local function process_line(line) - -- print(string.format("[%d]%s", i, line)) - assert_eq(type(line), "string") - assert_eq(line, lines[i]) - i = i + 1 - end - local sp = spawn.Spawn:make( - { "cat", "README.md" }, - { on_stdout = process_line, blocking = false } - ) - local pos = sp:_consume_line(content, process_line) - if pos <= #content then - local line = content:sub(pos, #content) - process_line(line) - end - end) - it("stdout on newline", function() - local content = fileios.readfile("README.md", { trim = true }) --[[@as string]] - local lines = fileios.readlines("README.md") --[[@as table]] - - local i = 1 - local function process_line(line) - -- print(string.format("[%d]%s\n", i, line)) - assert_eq(type(line), "string") - assert_eq(line, lines[i]) - i = i + 1 - end - local sp = spawn.Spawn:make( - { "cat", "README.md" }, - { on_stdout = process_line, blocking = false } - ) - local content_splits = strs.split(content, "\n", { trimempty = false }) - for j, splits in ipairs(content_splits) do - sp:_on_stdout(nil, splits) - if j < #content_splits then - sp:_on_stdout(nil, "\n") - end - end - sp:_on_stdout(nil, nil) - assert_true(sp.out_pipe:is_closing()) - end) - it("stdout on whitespace", function() - local content = fileios.readfile("README.md", { trim = true }) --[[@as string]] - local lines = fileios.readlines("README.md") --[[@as table]] - - local i = 1 - local function process_line(line) - -- print(string.format("[%d]%s\n", i, line)) - assert_eq(type(line), "string") - assert_eq(line, lines[i]) - i = i + 1 - end - local sp = spawn.Spawn:make( - { "cat", "README.md" }, - { on_stdout = process_line, blocking = false } - ) - local content_splits = strs.split(content, " ", { trimempty = false }) - for j, splits in ipairs(content_splits) do - sp:_on_stdout(nil, splits) - if j < #content_splits then - sp:_on_stdout(nil, " ") - end - end - sp:_on_stdout(nil, nil) - assert_true(sp.out_pipe:is_closing()) - end) - local delimiter_i = 0 - while delimiter_i <= 25 do - -- lower case: a - local lower_char = string.char(97 + delimiter_i) - it(string.format("stdout on %s", lower_char), function() - local content = fileios.readfile("README.md", { trim = true }) --[[@as string]] - local lines = fileios.readlines("README.md") --[[@as table]] - - local i = 1 - local function process_line(line) - -- print(string.format("[%d]%s\n", i, line)) - assert_eq(type(line), "string") - assert_eq(line, lines[i]) - i = i + 1 - end - local sp = spawn.Spawn:make( - { "cat", "README.md" }, - { on_stdout = process_line, on_stderr = dummy, blocking = false } - ) - local content_splits = - strs.split(content, lower_char, { trimempty = false }) - for j, splits in ipairs(content_splits) do - sp:_on_stdout(nil, splits) - if j < #content_splits then - sp:_on_stdout(nil, lower_char) - end - end - sp:_on_stdout(nil, nil) - assert_true(sp.out_pipe:is_closing()) - end) - -- upper case: A - local upper_char = string.char(65 + delimiter_i) - it(string.format("stdout on %s", upper_char), function() - local content = fileios.readfile("README.md", { trim = true }) --[[@as string]] - local lines = fileios.readlines("README.md") --[[@as table]] - - local i = 1 - local function process_line(line) - -- print(string.format("[%d]%s\n", i, line)) - assert_eq(type(line), "string") - assert_eq(line, lines[i]) - i = i + 1 - end - local sp = spawn.Spawn:make( - { "cat", "README.md" }, - { on_stdout = process_line, on_stderr = dummy, blocking = false } - ) - local content_splits = - strs.split(content, upper_char, { trimempty = false }) - for j, splits in ipairs(content_splits) do - sp:_on_stdout(nil, splits) - if j < #content_splits then - sp:_on_stdout(nil, upper_char) - end - end - sp:_on_stdout(nil, nil) - assert_true(sp.out_pipe:is_closing()) - end) - delimiter_i = delimiter_i + math.random(1, 5) - end - it("stderr", function() - local sp = spawn.Spawn:make( - { "cat", "README.md" }, - { on_stdout = dummy, on_stderr = dummy, blocking = false } - ) - sp:_on_stderr(nil, nil) - assert_true(sp.err_pipe:is_closing()) - end) - it("stderr2", function() - local i = 1 - local function process_line(line) - -- print(string.format("process[%d]:%s\n", i, line)) - end - local sp = spawn.Spawn:make( - { "cat", "non_exists.txt" }, - { on_stdout = process_line, on_stderr = process_line, blocking = false } - ) - sp:run() - end) - it("iterate on README.md", function() - local lines = fileios.readlines("README.md") --[[@as table]] - - local i = 1 - local function process_line(line) - -- print(string.format("[%d]%s\n", i, line)) - assert_eq(type(line), "string") - assert_eq(lines[i], line) - i = i + 1 - end - - local sp = spawn.Spawn:make( - { "cat", "README.md" }, - { on_stdout = process_line, on_stderr = dummy, blocking = false } - ) - sp:run() - end) - it("iterate on lua/fzfx/config.lua", function() - local lines = fileios.readlines("lua/fzfx/config.lua") --[[@as table]] - - local i = 1 - local function process_line(line) - -- print(string.format("[%d]%s\n", i, line)) - assert_eq(type(line), "string") - assert_eq(lines[i], line) - i = i + 1 - end - - local sp = spawn.Spawn:make( - { "cat", "lua/fzfx/config.lua" }, - { on_stdout = process_line, on_stderr = dummy, blocking = false } - ) - sp:run() - end) - it("close handle", function() - local sp = spawn.Spawn:make( - { "cat", "lua/fzfx/config.lua" }, - { on_stdout = dummy, on_stderr = dummy, blocking = false } - ) - sp:run() - assert_true(sp.process_handle ~= nil) - sp:_close_handle(sp.process_handle) - assert_true(sp.process_handle:is_closing()) - end) - end) -end) diff --git a/test/lib/strings_spec.lua b/test/lib/strings_spec.lua deleted file mode 100644 index dfc123ec5..000000000 --- a/test/lib/strings_spec.lua +++ /dev/null @@ -1,347 +0,0 @@ ----@diagnostic disable: undefined-field, unused-local -local cwd = vim.fn.getcwd() - -describe("lib.strings", function() - local assert_eq = assert.is_equal - local assert_true = assert.is_true - local assert_false = assert.is_false - - before_each(function() - vim.api.nvim_command("cd " .. cwd) - end) - - local strs = require("fzfx.lib.strings") - - describe("uuid", function() - it("test", function() - assert_true(string.len(strs.uuid()) > 0) - assert_true(string.len(strs.uuid(".")) > 0) - end) - it("test2", function() - local actual = strs.uuid() - assert_eq(type(actual), "string") - local actual_splits = strs.split(actual, "-") - assert_eq(type(actual_splits), "table") - assert_eq(#actual_splits, 4) - end) - end) - describe("[empty/not_empty/blank/not_blank]", function() - it("empty", function() - assert_true(strs.empty()) - assert_true(strs.empty(nil)) - assert_true(strs.empty("")) - assert_false(strs.not_empty()) - assert_false(strs.not_empty(nil)) - assert_false(strs.not_empty("")) - end) - it("not empty", function() - assert_true(strs.not_empty(" ")) - assert_true(strs.not_empty(" asdf ")) - assert_false(strs.empty(" ")) - assert_false(strs.empty(" asdf ")) - end) - it("blank", function() - assert_true(strs.blank()) - assert_true(strs.blank(nil)) - assert_true(strs.blank(" ")) - assert_true(strs.blank("\n")) - assert_false(strs.not_blank()) - assert_false(strs.not_blank(nil)) - assert_false(strs.not_blank("")) - end) - it("not blank", function() - assert_true(strs.not_blank(" x")) - assert_true(strs.not_blank(" asdf ")) - assert_false(strs.blank("y ")) - assert_false(strs.blank(" asdf ")) - end) - end) - describe("[find]", function() - it("found", function() - assert_eq(strs.find("abcdefg", "a"), 1) - assert_eq(strs.find("abcdefg", "a", 1), 1) - assert_eq(strs.find("abcdefg", "g"), 7) - assert_eq(strs.find("abcdefg", "g", 1), 7) - assert_eq(strs.find("abcdefg", "g", 7), 7) - assert_eq(strs.find("fzfx -- -w -g *.lua", "--"), 6) - assert_eq(strs.find("fzfx -- -w -g *.lua", "--", 1), 6) - assert_eq(strs.find("fzfx -- -w -g *.lua", "--", 2), 6) - assert_eq(strs.find("fzfx -- -w -g *.lua", "--", 3), 6) - assert_eq(strs.find("fzfx -- -w -g *.lua", "--", 6), 6) - assert_eq(strs.find("fzfx -w -- -g *.lua", "--"), 9) - assert_eq(strs.find("fzfx -w -- -g *.lua", "--", 1), 9) - assert_eq(strs.find("fzfx -w -- -g *.lua", "--", 2), 9) - assert_eq(strs.find("fzfx -w ---g *.lua", "--", 8), 9) - assert_eq(strs.find("fzfx -w ---g *.lua", "--", 9), 9) - end) - it("not found", function() - assert_eq(strs.find("abcdefg", "a", 2), nil) - assert_eq(strs.find("abcdefg", "a", 7), nil) - assert_eq(strs.find("abcdefg", "g", 8), nil) - assert_eq(strs.find("abcdefg", "g", 9), nil) - assert_eq(strs.find("fzfx -- -w -g *.lua", "--", 7), nil) - assert_eq(strs.find("fzfx -- -w -g *.lua", "--", 8), nil) - assert_eq(strs.find("fzfx -w -- -g *.lua", "--", 10), nil) - assert_eq(strs.find("fzfx -w -- -g *.lua", "--", 11), nil) - assert_eq(strs.find("fzfx -w ---g *.lua", "--", 11), nil) - assert_eq(strs.find("fzfx -w ---g *.lua", "--", 12), nil) - assert_eq(strs.find("", "--"), nil) - assert_eq(strs.find("", "--", 1), nil) - assert_eq(strs.find("-", "--"), nil) - assert_eq(strs.find("--", "---", 1), nil) - end) - end) - describe("[rfind]", function() - it("found", function() - assert_eq(strs.rfind("abcdefg", "a"), 1) - assert_eq(strs.rfind("abcdefg", "a", 1), 1) - assert_eq(strs.rfind("abcdefg", "a", 7), 1) - assert_eq(strs.rfind("abcdefg", "a", 2), 1) - assert_eq(strs.rfind("abcdefg", "g"), 7) - assert_eq(strs.rfind("abcdefg", "g", 7), 7) - assert_eq(strs.rfind("fzfx -- -w -g *.lua", "--"), 6) - assert_eq(strs.rfind("fzfx -- -w -g *.lua", "--", 6), 6) - assert_eq(strs.rfind("fzfx -- -w -g *.lua", "--", 7), 6) - assert_eq(strs.rfind("fzfx -- -w -g *.lua", "--", 8), 6) - assert_eq(strs.rfind("fzfx -w -- -g *.lua", "--"), 9) - assert_eq(strs.rfind("fzfx -w -- -g *.lua", "--", 10), 9) - assert_eq(strs.rfind("fzfx -w -- -g *.lua", "--", 9), 9) - assert_eq(strs.rfind("fzfx -w -- -g *.lua", "--", 10), 9) - assert_eq(strs.rfind("fzfx -w -- -g *.lua", "--", 11), 9) - assert_eq(strs.rfind("fzfx -w ---g *.lua", "--", 9), 9) - assert_eq(strs.rfind("fzfx -w ---g *.lua", "--", 10), 10) - assert_eq(strs.rfind("fzfx -w ---g *.lua", "--", 11), 10) - end) - it("not found", function() - assert_eq(strs.rfind("abcdefg", "a", 0), nil) - assert_eq(strs.rfind("abcdefg", "a", -1), nil) - assert_eq(strs.rfind("abcdefg", "g", 6), nil) - assert_eq(strs.rfind("abcdefg", "g", 5), nil) - assert_eq(strs.rfind("fzfx -- -w -g *.lua", "--", 5), nil) - assert_eq(strs.rfind("fzfx -- -w -g *.lua", "--", 4), nil) - assert_eq(strs.rfind("fzfx -- -w -g *.lua", "--", 1), nil) - assert_eq(strs.rfind("fzfx -w -- -g *.lua", "--", 8), nil) - assert_eq(strs.rfind("fzfx -w -- -g *.lua", "--", 7), nil) - assert_eq(strs.rfind("fzfx -w ---g *.lua", "--", 8), nil) - assert_eq(strs.rfind("fzfx -w ---g *.lua", "--", 7), nil) - assert_eq(strs.rfind("", "--"), nil) - assert_eq(strs.rfind("", "--", 1), nil) - assert_eq(strs.rfind("-", "--"), nil) - assert_eq(strs.rfind("--", "---", 1), nil) - end) - end) - describe("[ltrim/rtrim]", function() - it("trim left", function() - assert_eq(strs.ltrim("asdf"), "asdf") - assert_eq(strs.ltrim(" asdf"), "asdf") - assert_eq(strs.ltrim(" \nasdf"), "asdf") - assert_eq(strs.ltrim("\tasdf"), "asdf") - assert_eq(strs.ltrim(" asdf "), "asdf ") - assert_eq(strs.ltrim(" \nasdf\n"), "asdf\n") - assert_eq(strs.ltrim("\tasdf\t"), "asdf\t") - end) - it("trim right", function() - assert_eq(strs.rtrim("asdf"), "asdf") - assert_eq(strs.rtrim(" asdf "), " asdf") - assert_eq(strs.rtrim(" \nasdf"), " \nasdf") - assert_eq(strs.rtrim(" \nasdf\n"), " \nasdf") - assert_eq(strs.rtrim("\tasdf\t"), "\tasdf") - end) - end) - describe("[split]", function() - it("splits rg options-1", function() - local actual = strs.split("-w -g *.md", " ") - local expect = { "-w", "-g", "*.md" } - assert_eq(#actual, #expect) - for i, v in ipairs(actual) do - assert_eq(v, expect[i]) - end - end) - it("splits rg options-2", function() - local actual = strs.split(" -w -g *.md ", " ") - local expect = { "-w", "-g", "*.md" } - assert_eq(#actual, #expect) - for i, v in ipairs(actual) do - assert_eq(v, expect[i]) - end - end) - it("splits rg options-3", function() - local actual = - strs.split(" -w -g *.md ", " ", { plain = false, trimempty = false }) - local expect = { "", "", "-w", "-g", "*.md", "", "" } - print(string.format("splits rg3, actual:%s\n", vim.inspect(actual))) - print(string.format("splits rg3, expect:%s\n", vim.inspect(expect))) - assert_eq(#actual, #expect) - for i, v in ipairs(actual) do - assert_eq(v, expect[i]) - end - end) - end) - describe("[startswith]", function() - it("start", function() - assert_true(strs.startswith("hello world", "hello")) - assert_false(strs.startswith("hello world", "ello")) - end) - end) - describe("[endswith]", function() - it("end", function() - assert_true(strs.endswith("hello world", "world")) - assert_false(strs.endswith("hello world", "hello")) - end) - end) - describe("[isxxx]", function() - local function _contains_char(s, c) - assert(string.len(s) > 0) - assert(string.len(c) == 1) - for i = 1, #s do - if string.byte(s, i) == string.byte(c, 1) then - return true - end - end - return false - end - - local function _contains_code(s, c) - for _, i in ipairs(s) do - if i == c then - return true - end - end - return false - end - - it("isspace", function() - local whitespaces = "\r\n \t" - local char_codes = { 11, 12 } - for i = 1, 255 do - if - _contains_char(whitespaces, string.char(i)) - or _contains_code(char_codes, i) - then - assert_true(strs.isspace(string.char(i))) - else - -- print( - -- string.format( - -- "isspace: %d: %s\n", - -- i, - -- vim.inspect(strs.isspace(string.char(i))) - -- ) - -- ) - assert_false(strs.isspace(string.char(i))) - end - end - end) - it("isalpha", function() - local a = "a" - local z = "z" - local A = "A" - local Z = "Z" - for i = 1, 255 do - if - (i >= string.byte(a) and i <= string.byte(z)) - or (i >= string.byte(A) and i <= string.byte(Z)) - then - assert_true(strs.isalpha(string.char(i))) - else - assert_false(strs.isalpha(string.char(i))) - end - end - end) - it("isdigit", function() - local _0 = "0" - local _9 = "9" - for i = 1, 255 do - if i >= string.byte(_0) and i <= string.byte(_9) then - assert_true(strs.isdigit(string.char(i))) - else - assert_false(strs.isdigit(string.char(i))) - end - end - end) - it("isalnum", function() - local a = "a" - local z = "z" - local A = "A" - local Z = "Z" - local _0 = "0" - local _9 = "9" - for i = 1, 255 do - if - (i >= string.byte(a) and i <= string.byte(z)) - or (i >= string.byte(A) and i <= string.byte(Z)) - or (i >= string.byte(_0) and i <= string.byte(_9)) - then - assert_true(strs.isalnum(string.char(i))) - else - assert_false(strs.isalnum(string.char(i))) - end - end - end) - it("ishex", function() - local a = "a" - local f = "f" - local A = "A" - local F = "F" - local _0 = "0" - local _9 = "9" - for i = 1, 255 do - if - (i >= string.byte(a) and i <= string.byte(f)) - or (i >= string.byte(A) and i <= string.byte(F)) - or (i >= string.byte(_0) and i <= string.byte(_9)) - then - assert_true(strs.ishex(string.char(i))) - else - -- print( - -- string.format( - -- "ishex, %d:%s\n", - -- i, - -- vim.inspect(strs.ishex(string.char(i))) - -- ) - -- ) - assert_false(strs.ishex(string.char(i))) - end - end - end) - it("islower", function() - local a = "a" - local z = "z" - for i = 1, 255 do - if i >= string.byte(a) and i <= string.byte(z) then - assert_true(strs.islower(string.char(i))) - else - assert_false(strs.islower(string.char(i))) - end - end - end) - it("isupper", function() - local A = "A" - local Z = "Z" - for i = 1, 255 do - if i >= string.byte(A) and i <= string.byte(Z) then - assert_true(strs.isupper(string.char(i))) - else - assert_false(strs.isupper(string.char(i))) - end - end - end) - end) - describe("[trim_quotes]", function() - it("single quotes", function() - assert_eq(strs.trim_quotes(""), "") - assert_eq(strs.trim_quotes("'"), "'") - assert_eq(strs.trim_quotes("''"), "") - assert_eq(strs.trim_quotes("'asdf'"), "asdf") - assert_eq(strs.trim_quotes("asdf'"), "asdf'") - assert_eq(strs.trim_quotes("asdf"), "asdf") - end) - it("double quotes", function() - assert_eq(strs.trim_quotes(""), "") - assert_eq(strs.trim_quotes('"'), '"') - assert_eq(strs.trim_quotes('""'), "") - assert_eq(strs.trim_quotes('"asdf"'), "asdf") - assert_eq(strs.trim_quotes('asdf"'), 'asdf"') - assert_eq(strs.trim_quotes("asdf"), "asdf") - end) - end) -end) diff --git a/test/lib/tables_spec.lua b/test/lib/tables_spec.lua deleted file mode 100644 index 6c4670715..000000000 --- a/test/lib/tables_spec.lua +++ /dev/null @@ -1,59 +0,0 @@ ----@diagnostic disable: undefined-field, unused-local -local cwd = vim.fn.getcwd() - -describe("lib.tables", function() - local assert_eq = assert.is_equal - local assert_true = assert.is_true - local assert_false = assert.is_false - - before_each(function() - vim.api.nvim_command("cd " .. cwd) - end) - - local tbls = require("fzfx.lib.tables") - - describe("strings", function() - it("tbl_empty/tbl_not_empty", function() - assert_true(tbls.tbl_empty(nil)) - assert_true(tbls.tbl_empty({})) - assert_false(tbls.tbl_not_empty(nil)) - assert_false(tbls.tbl_not_empty({})) - assert_false(tbls.tbl_empty({ 1, 2, 3 })) - assert_false(tbls.tbl_empty({ a = 1 })) - assert_true(tbls.tbl_not_empty({ 1, 2, 3 })) - assert_true(tbls.tbl_not_empty({ a = 1 })) - end) - it("tbl_get", function() - assert_true(tbls.tbl_get({ a = { b = true } }, "a.b")) - assert_eq(tbls.tbl_get({ a = { b = true } }, "a.b.c"), nil) - assert_eq(tbls.tbl_get({ a = { b = true } }, "a.c"), nil) - assert_eq(tbls.tbl_get({ a = { b = true } }, "c"), nil) - assert_eq(tbls.tbl_get({ c = { d = 1 } }, "c.d"), 1) - assert_eq(tbls.tbl_get({ c = { d = 1, e = "e" } }, "c.e"), "e") - end) - it("list", function() - assert_true(tbls.list_empty(nil)) - assert_true(tbls.list_empty({})) - assert_false(tbls.list_empty({ 1, 2, 3 })) - assert_true(tbls.list_empty({ a = 1 })) - end) - it("list index", function() - assert_eq(tbls.list_index(-1, 10), 10) - assert_eq(tbls.list_index(-2, 10), 9) - for i = 1, 10 do - assert_eq(tbls.list_index(i, 10), i) - assert_eq(tbls.list_index(-i, 10), 10 - i + 1) - end - for i = 1, 10 do - assert_eq(tbls.list_index(i, 10), i) - end - for i = -1, -10, -1 do - assert_eq(tbls.list_index(i, 10), 10 + i + 1) - end - assert_eq(tbls.list_index(-1, 10), 10) - assert_eq(tbls.list_index(-10, 10), 1) - assert_eq(tbls.list_index(-3, 10), 8) - assert_eq(tbls.list_index(-5, 10), 6) - end) - end) -end) diff --git a/test/schema_spec.lua b/test/schema_spec.lua index 399a50e38..5113dc23e 100644 --- a/test/schema_spec.lua +++ b/test/schema_spec.lua @@ -10,7 +10,6 @@ describe("schema", function() vim.api.nvim_command("cd " .. cwd) end) - local tbls = require("fzfx.lib.tables") local schema = require("fzfx.schema") describe("[ProviderConfig]", function() it("makes a plain provider", function()