Skip to content

Commit

Permalink
feat: use vim.system() instead of plenary.job
Browse files Browse the repository at this point in the history
  • Loading branch information
uga-rosa committed Aug 19, 2023
1 parent c73a902 commit f076c6b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ This plugin provides one of the easiest way to add desired completion candidates

# Requirements

`plenary.nvim` is not required if neovim is the version with `vim.system()` available.

- neovim >= 0.7
- nvim-cmp
- [plenary.nvim](https://github.com/nvim-lua/plenary.nvim) (only document feature)
- [sqlite.lua](https://github.com/kkharji/sqlite.lua) (only if sqlite option is enabled)
- [plenary.nvim](https://github.com/nvim-lua/plenary.nvim) (only document feature)

# Setting

Expand Down
5 changes: 5 additions & 0 deletions doc/cmp-dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,14 @@ Requirements
- neovim >= 0.7
- nvim-cmp
- https://github.com/hrsh7th/nvim-cmp
- sqlite.lua (only if sqlite option is enabled)
- https://github.com/kkharji/sqlite.lua
- plenary.nvim (only document feature)
- https://github.com/nvim-lua/plenary.nvim

plenary.nvim is not required if neovim is the version with |vim.system()|
available.


==============================================================================
Commands *cmp-dictionary-commands*
Expand Down
40 changes: 24 additions & 16 deletions lua/cmp_dictionary/document.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,37 @@ end
---@param completion_item lsp.CompletionItem
---@param callback fun(completion_item: lsp.CompletionItem|nil)
local function get_document(completion_item, callback)
local ok, Job = pcall(require, "plenary.job")
if not ok then
vim.notify("[cmp-dictionary] document feature requires plenary.nvim")
return
end

local word = completion_item.label
local command, args = get_command(word)
if not command then
callback(completion_item)
return
end

Job:new({
command = command,
args = args,
on_exit = vim.schedule_wrap(function(j)
local result = table.concat(j:result(), "\n")
document_cache:set(word, result)
completion_item.documentation = result
if vim.system then
local cmd = args
table.insert(cmd, 1, command)
vim.system(cmd, {}, function(result)
document_cache:set(word, result.stdout)
completion_item.documentation = result.stdout
callback(completion_item)
end),
}):start()
end)
else
local ok, Job = pcall(require, "plenary.job")
if not ok then
vim.notify("[cmp-dictionary] document feature requires plenary.nvim")
return
end
Job:new({
command = command,
args = args,
on_exit = vim.schedule_wrap(function(j)
local result = table.concat(j:result(), "\n")
document_cache:set(word, result)
completion_item.documentation = result
callback(completion_item)
end),
}):start()
end
end

---@param completion_item lsp.CompletionItem
Expand Down

0 comments on commit f076c6b

Please sign in to comment.