summaryrefslogtreecommitdiff
path: root/lua/blink/cmp/sources/cmdline/help.lua
diff options
context:
space:
mode:
authorMike Vink <mike@pionative.com>2025-01-19 13:52:52 +0100
committerMike Vink <mike@pionative.com>2025-01-19 13:52:52 +0100
commitb77413ff8f59f380612074f0c9bd49093d8db695 (patch)
tree32c39a811ba96ed4ab0a1c81cce9f8d518ed7e31 /lua/blink/cmp/sources/cmdline/help.lua
Squashed 'mut/neovim/pack/plugins/start/blink.cmp/' content from commit 1cc3b1a
git-subtree-dir: mut/neovim/pack/plugins/start/blink.cmp git-subtree-split: 1cc3b1a908fbcfd15451c4772759549724f38524
Diffstat (limited to 'lua/blink/cmp/sources/cmdline/help.lua')
-rw-r--r--lua/blink/cmp/sources/cmdline/help.lua53
1 files changed, 53 insertions, 0 deletions
diff --git a/lua/blink/cmp/sources/cmdline/help.lua b/lua/blink/cmp/sources/cmdline/help.lua
new file mode 100644
index 0000000..bae4988
--- /dev/null
+++ b/lua/blink/cmp/sources/cmdline/help.lua
@@ -0,0 +1,53 @@
+local async = require('blink.cmp.lib.async')
+
+local help = {}
+
+--- Processes a help file and returns a list of tags asynchronously
+--- @param file string
+--- @return blink.cmp.Task
+--- TODO: rewrite using async lib, shared as a library in lib/fs.lua
+local function read_tags_from_file(file)
+ return async.task.new(function(resolve)
+ vim.uv.fs_open(file, 'r', 438, function(err, fd)
+ if err or fd == nil then return resolve({}) end
+
+ -- Read file content
+ vim.uv.fs_fstat(fd, function(stat_err, stat)
+ if stat_err or stat == nil then
+ vim.uv.fs_close(fd)
+ return resolve({})
+ end
+
+ vim.uv.fs_read(fd, stat.size, 0, function(read_err, data)
+ vim.uv.fs_close(fd)
+
+ if read_err or data == nil then return resolve({}) end
+
+ -- Process the file content
+ local tags = {}
+ for line in data:gmatch('[^\r\n]+') do
+ local tag = line:match('^([^\t]+)')
+ if tag then table.insert(tags, tag) end
+ end
+
+ resolve(tags)
+ end)
+ end)
+ end)
+ end)
+end
+
+--- @param arg_prefix string
+function help.get_completions(arg_prefix)
+ local help_files = vim.api.nvim_get_runtime_file('doc/tags', true)
+
+ return async.task
+ .await_all(vim.tbl_map(read_tags_from_file, help_files))
+ :map(function(tags_arrs) return require('blink.cmp.lib.utils').flatten(tags_arrs) end)
+ :map(function(tags)
+ -- TODO: remove after adding support for fuzzy matching on custom range
+ return vim.tbl_filter(function(tag) return vim.startswith(tag, arg_prefix) end, tags)
+ end)
+end
+
+return help