using plenary to fetch synonyms instead of systemlist
This commit is contained in:
parent
9dcd475716
commit
0d886acb1d
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
A [Telescope](https://github.com/nvim-telescope/telescope.nvim) extension that loads a list of synonyms for the word under the cusor in the current buffer, and shows their definition in the preview window. Selecting one of the synonyms replaces the word in the buffer.
|
A [Telescope](https://github.com/nvim-telescope/telescope.nvim) extension that loads a list of synonyms for the word under the cusor in the current buffer, and shows their definition in the preview window. Selecting one of the synonyms replaces the word in the buffer.
|
||||||
|
|
||||||
|
![Screenshot](https://raw.githubusercontent.com/wiki/rudism/telescope-dict.nvim/img/screen_shot.png)
|
||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
|
|
||||||
These should be available through your package manager (or likely already installed, in the case of `perl`):
|
These should be available through your package manager (or likely already installed, in the case of `perl`):
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
-- command
|
||||||
local telescope = require('telescope')
|
local telescope = require('telescope')
|
||||||
local pickers = require('telescope.pickers')
|
local pickers = require('telescope.pickers')
|
||||||
local finders = require('telescope.finders')
|
local finders = require('telescope.finders')
|
||||||
|
@ -6,13 +7,14 @@ local previewers = require('telescope.previewers')
|
||||||
local previewer_utils = require('telescope.previewers.utils')
|
local previewer_utils = require('telescope.previewers.utils')
|
||||||
local actions = require('telescope.actions')
|
local actions = require('telescope.actions')
|
||||||
local action_state = require('telescope.actions.state')
|
local action_state = require('telescope.actions.state')
|
||||||
|
local Job = require('plenary.job')
|
||||||
|
|
||||||
function script_path()
|
local function script_path()
|
||||||
local str = debug.getinfo(2, "S").source:sub(2)
|
local str = debug.getinfo(2, "S").source:sub(2)
|
||||||
return str:match("(.*/)")
|
return str:match("(.*/)")
|
||||||
end
|
end
|
||||||
|
|
||||||
function upper_first(word)
|
local function upper_first(word)
|
||||||
return word:gsub("(%a)([%w_']*)", function(first, rest) return first:upper()..rest:lower() end)
|
return word:gsub("(%a)([%w_']*)", function(first, rest) return first:upper()..rest:lower() end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -21,13 +23,11 @@ return telescope.register_extension {
|
||||||
synonyms = function(opts)
|
synonyms = function(opts)
|
||||||
opts = opts or {}
|
opts = opts or {}
|
||||||
local cursor_word = vim.fn.expand("<cword>")
|
local cursor_word = vim.fn.expand("<cword>")
|
||||||
local command = table.concat({
|
Job:new({
|
||||||
"perl ",
|
command = 'perl',
|
||||||
script_path().."/cmd.pl ",
|
args = { script_path().."/cmd.pl", "synonyms", cursor_word },
|
||||||
"synonyms ",
|
on_exit = vim.schedule_wrap(function(job)
|
||||||
vim.fn.shellescape(cursor_word)
|
local synonyms = job:result() or {}
|
||||||
})
|
|
||||||
local synonyms = vim.fn.systemlist(command) or {}
|
|
||||||
table.insert(synonyms, cursor_word:lower())
|
table.insert(synonyms, cursor_word:lower())
|
||||||
pickers.new(opts, {
|
pickers.new(opts, {
|
||||||
prompt_title = upper_first(cursor_word).." Synonyms",
|
prompt_title = upper_first(cursor_word).." Synonyms",
|
||||||
|
@ -36,12 +36,12 @@ return telescope.register_extension {
|
||||||
},
|
},
|
||||||
sorter = sorters.get_substr_matcher(),
|
sorter = sorters.get_substr_matcher(),
|
||||||
previewer = previewers.new_buffer_previewer({
|
previewer = previewers.new_buffer_previewer({
|
||||||
define_preview = function(self, entry, status)
|
define_preview = function(self, entry, _)
|
||||||
vim.api.nvim_win_set_option(self.state.winid, 'wrap', true)
|
vim.api.nvim_win_set_option(self.state.winid, 'wrap', true)
|
||||||
vim.api.nvim_win_set_option(self.state.winid, 'lbr', true)
|
vim.api.nvim_win_set_option(self.state.winid, 'lbr', true)
|
||||||
local cmd = { "perl", script_path().."/cmd.pl", "definitions", entry.value }
|
local cmd = { "perl", script_path().."/cmd.pl", "definitions", entry.value }
|
||||||
return previewer_utils.job_maker(cmd, self.state.bufnr, {
|
return previewer_utils.job_maker(cmd, self.state.bufnr, {
|
||||||
callback = function(bufnr, content)
|
callback = function(bufnr, _)
|
||||||
previewer_utils.highlighter(bufnr, 'yaml')
|
previewer_utils.highlighter(bufnr, 'yaml')
|
||||||
end
|
end
|
||||||
})
|
})
|
||||||
|
@ -59,6 +59,8 @@ return telescope.register_extension {
|
||||||
return true
|
return true
|
||||||
end,
|
end,
|
||||||
}):find()
|
}):find()
|
||||||
|
end)
|
||||||
|
}):start()
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue