From f076c6b071e6117d2cbb26d53327ff21fc22fdb8 Mon Sep 17 00:00:00 2001 From: uga-rosa Date: Sat, 19 Aug 2023 23:04:37 +0900 Subject: [PATCH] feat: use vim.system() instead of plenary.job --- README.md | 4 +++- doc/cmp-dictionary.txt | 5 +++++ lua/cmp_dictionary/document.lua | 40 ++++++++++++++++++++------------- 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 3cb61ed..60a04a2 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/doc/cmp-dictionary.txt b/doc/cmp-dictionary.txt index 7451b8d..edf82ca 100644 --- a/doc/cmp-dictionary.txt +++ b/doc/cmp-dictionary.txt @@ -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* diff --git a/lua/cmp_dictionary/document.lua b/lua/cmp_dictionary/document.lua index 7a39e08..2849dbe 100644 --- a/lua/cmp_dictionary/document.lua +++ b/lua/cmp_dictionary/document.lua @@ -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