Skip to content

Commit

Permalink
feat: resolve help tags ourselves in cmdline
Browse files Browse the repository at this point in the history
Closes #631
  • Loading branch information
Saghen committed Dec 18, 2024
1 parent 59ef8a4 commit 02051bf
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 90 deletions.
4 changes: 3 additions & 1 deletion lua/blink/cmp/lib/async.lua
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ end
--- utils

function task.await_all(tasks)
if #tasks == 0 then return task.empty() end
if #tasks == 0 then
return task.new(function(resolve) resolve({}) end)
end

local all_task
all_task = task.new(function(resolve, reject)
Expand Down
47 changes: 47 additions & 0 deletions lua/blink/cmp/sources/cmdline/help.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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

function help.get_completions()
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(results) return require('blink.cmp.lib.utils').flatten(results) end)
end

return help
77 changes: 48 additions & 29 deletions lua/blink/cmp/sources/cmdline/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
-- https://github.com/hrsh7th/cmp-cmdline
-- License: MIT

local async = require('blink.cmp.lib.async')

--- @class blink.cmp.Source
local cmdline = {}

Expand Down Expand Up @@ -31,39 +33,56 @@ function cmdline:get_completions(context, callback)
)
local current_arg_prefix = current_arg:sub(1, keyword.start_col - #text_before_cursor - 1)

local query = (text_before_cursor .. current_arg_prefix):gsub([[\\]], [[\\\\]])
local completions = vim.fn.getcompletion(query, 'cmdline')
local task = async.task
.empty()
:map(function()
-- Special case for help where we read all the tags ourselves
if vim.tbl_contains({ 'h', 'he', 'hel', 'help' }, arguments[1] or '') then
return require('blink.cmp.sources.cmdline.help').get_completions()
end

local query = (text_before_cursor .. current_arg_prefix):gsub([[\\]], [[\\\\]])
return vim.fn.getcompletion(query, 'cmdline')
end)
:map(function(completions)
local items = {}
for _, completion in ipairs(completions) do
-- remove prefix from the label
if string.find(completion, current_arg_prefix, 1, true) == 1 then
completion = completion:sub(#current_arg_prefix + 1)
end

local items = {}
for _, completion in ipairs(completions) do
-- remove prefix from the label
if string.find(completion, current_arg_prefix, 1, true) == 1 then
completion = completion:sub(#current_arg_prefix + 1)
end
-- add prefix to the newText
local new_text = completion
if string.find(new_text, current_arg_prefix, 1, true) ~= 1 then new_text = current_arg_prefix .. completion end

-- add prefix to the newText
local new_text = completion
if string.find(new_text, current_arg_prefix, 1, true) ~= 1 then new_text = current_arg_prefix .. completion end
table.insert(items, {
label = completion,
insertText = completion,
sortText = completion,
textEdit = {
newText = new_text,
range = {
start = { line = 0, character = #text_before_cursor },
['end'] = { line = 0, character = #text_before_cursor + #current_arg },
},
},
kind = require('blink.cmp.types').CompletionItemKind.Property,
})
end

table.insert(items, {
label = completion,
insertText = completion,
textEdit = {
newText = new_text,
range = {
start = { line = 0, character = #text_before_cursor },
['end'] = { line = 0, character = #text_before_cursor + #current_arg },
},
},
kind = require('blink.cmp.types').CompletionItemKind.Property,
})
end
callback({
is_incomplete_backward = true,
is_incomplete_forward = false,
items = items,
})
end)
:catch(function(err)
vim.notify('Error while fetching completions: ' .. err, vim.log.levels.ERROR)
callback({ is_incomplete_backward = false, is_incomplete_forward = false, items = {} })
end)

callback({
is_incomplete_backward = false,
is_incomplete_forward = false,
items = items,
})
return function() task:cancel() end
end

return cmdline
60 changes: 0 additions & 60 deletions lua/blink/cmp/sources/cmdline/regex.lua

This file was deleted.

0 comments on commit 02051bf

Please sign in to comment.