Skip to content

Commit

Permalink
dict/grep
Browse files Browse the repository at this point in the history
  • Loading branch information
uga-rosa committed Jan 11, 2024
1 parent 558390d commit 591a3ce
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
40 changes: 40 additions & 0 deletions lua/cmp_dictionary/dict/grep.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
local util = require("cmp_dictionary.util")

---@class CmpDictionaryDictGrep: CmpDictionaryDict
---@field command string[]
---@field paths string[]
local M = {}

---@param command string[]
---@return CmpDictionaryDictGrep
function M.new(command)
return setmetatable({
command = command,
paths = {},
}, { __index = M })
end

---@param paths string[]
function M:update(paths)
self.paths = paths
end

---@param prefix string
function M:search(prefix)
local items = {}
for _, path in ipairs(self.paths) do
local command = vim.tbl_map(function(c)
return c:gsub("${prefix}", prefix):gsub("${path}", path)
end, self.command)
local info = string.format("belong to `%s`", vim.fn.fnamemodify(path, ":t"))
items = vim.list_extend(
items,
vim.tbl_map(function(word)
return { label = word, info = info }
end, vim.split(util.system(command), "\n"))
)
end
return items
end

return M
7 changes: 6 additions & 1 deletion lua/cmp_dictionary/dict/init.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local trie = require("cmp_dictionary.dict.trie")
local grep = require("cmp_dictionary.dict.grep")

local M = {}

Expand All @@ -9,7 +10,11 @@ local M = {}
---@param opts CmpDictionaryOptions
---@return CmpDictionaryDict
function M.new(opts)
return trie.new()
if #opts.grep_command > 0 then
return grep.new(opts.grep_command)
else
return trie.new()
end
end

return M

0 comments on commit 591a3ce

Please sign in to comment.