spacevim/bundle/cmp-dictionary/lua/cmp_dictionary/source.lua
JIe 2bb7059579
Some checks failed
Detach Plugins / check (FlyGrep.vim) (push) Has been cancelled
Detach Plugins / check (GitHub.vim) (push) Has been cancelled
Detach Plugins / check (JavaUnit.vim) (push) Has been cancelled
Detach Plugins / check (SourceCounter.vim) (push) Has been cancelled
Detach Plugins / check (cpicker.nvim) (push) Has been cancelled
Detach Plugins / check (dein-ui.vim) (push) Has been cancelled
Detach Plugins / check (git.vim) (push) Has been cancelled
Detach Plugins / check (iedit.vim) (push) Has been cancelled
Detach Plugins / check (scrollbar.vim) (push) Has been cancelled
Detach Plugins / check (vim-chat) (push) Has been cancelled
Detach Plugins / check (vim-cheat) (push) Has been cancelled
Detach Plugins / check (vim-todo) (push) Has been cancelled
Detach Plugins / check (xmake.vim) (push) Has been cancelled
test / Linux (nvim, nightly) (push) Has been cancelled
test / Linux (nvim, v0.3.8) (push) Has been cancelled
test / Linux (nvim, v0.4.0) (push) Has been cancelled
test / Linux (nvim, v0.4.2) (push) Has been cancelled
test / Linux (nvim, v0.4.3) (push) Has been cancelled
test / Linux (nvim, v0.4.4) (push) Has been cancelled
test / Linux (nvim, v0.5.0) (push) Has been cancelled
test / Linux (nvim, v0.5.1) (push) Has been cancelled
test / Linux (nvim, v0.6.0) (push) Has been cancelled
test / Linux (nvim, v0.6.1) (push) Has been cancelled
test / Linux (nvim, v0.7.0) (push) Has been cancelled
test / Linux (nvim, v0.7.2) (push) Has been cancelled
test / Linux (nvim, v0.8.0) (push) Has been cancelled
test / Linux (nvim, v0.8.1) (push) Has been cancelled
test / Linux (nvim, v0.8.2) (push) Has been cancelled
test / Linux (nvim, v0.8.3) (push) Has been cancelled
test / Linux (nvim, v0.9.0) (push) Has been cancelled
test / Linux (nvim, v0.9.1) (push) Has been cancelled
test / Linux (true, vim, v7.4.052) (push) Has been cancelled
test / Linux (true, vim, v7.4.1689) (push) Has been cancelled
test / Linux (true, vim, v7.4.629) (push) Has been cancelled
test / Linux (true, vim, v8.0.0027) (push) Has been cancelled
test / Linux (true, vim, v8.0.0183) (push) Has been cancelled
test / Linux (vim, nightly) (push) Has been cancelled
test / Linux (vim, v8.0.0184) (push) Has been cancelled
test / Linux (vim, v8.0.1453) (push) Has been cancelled
test / Linux (vim, v8.1.2269) (push) Has been cancelled
test / Linux (vim, v8.2.2434) (push) Has been cancelled
test / Linux (vim, v8.2.3995) (push) Has been cancelled
test / Windows (nvim, nightly) (push) Has been cancelled
test / Windows (nvim, v0.3.8) (push) Has been cancelled
test / Windows (nvim, v0.4.2) (push) Has been cancelled
test / Windows (nvim, v0.4.3) (push) Has been cancelled
test / Windows (nvim, v0.4.4) (push) Has been cancelled
test / Windows (nvim, v0.5.0) (push) Has been cancelled
test / Windows (nvim, v0.5.1) (push) Has been cancelled
test / Windows (nvim, v0.6.0) (push) Has been cancelled
test / Windows (nvim, v0.6.1) (push) Has been cancelled
test / Windows (nvim, v0.7.0) (push) Has been cancelled
test / Windows (nvim, v0.7.2) (push) Has been cancelled
test / Windows (nvim, v0.8.0) (push) Has been cancelled
test / Windows (nvim, v0.8.1) (push) Has been cancelled
test / Windows (nvim, v0.8.2) (push) Has been cancelled
test / Windows (nvim, v0.8.3) (push) Has been cancelled
test / Windows (nvim, v0.9.0) (push) Has been cancelled
test / Windows (nvim, v0.9.1) (push) Has been cancelled
test / Windows (vim, nightly) (push) Has been cancelled
test / Windows (vim, v7.4.1185) (push) Has been cancelled
test / Windows (vim, v7.4.1689) (push) Has been cancelled
test / Windows (vim, v8.0.0027) (push) Has been cancelled
test / Windows (vim, v8.0.1453) (push) Has been cancelled
test / Windows (vim, v8.1.2269) (push) Has been cancelled
test / Windows (vim, v8.2.2434) (push) Has been cancelled
test / Windows (vim, v8.2.3995) (push) Has been cancelled
docker / docker (push) Has been cancelled
mirror / check (coding) (push) Has been cancelled
mirror / check (gitee) (push) Has been cancelled
mirror / check (gitlab) (push) Has been cancelled
init
2024-08-21 14:17:26 +08:00

126 lines
2.9 KiB
Lua

local source = {}
local utf8 = require("cmp_dictionary.lib.utf8")
local config = require("cmp_dictionary.config")
local caches = require("cmp_dictionary.caches")
local db = require("cmp_dictionary.db")
function source.new()
return setmetatable({}, { __index = source })
end
---@return string
function source.get_keyword_pattern()
return [[\k\+]]
end
local candidate_cache = {
req = "",
items = {},
}
---@param str string
---@return boolean
local function is_capital(str)
return str:find("^%u") and true or false
end
---@param str string
---@return string
local function to_lower_first(str)
local l = str:gsub("^.", string.lower)
return l
end
---@param str string
---@return string
local function to_upper_first(str)
local u = str:gsub("^.", string.upper)
return u
end
---@param req string
---@param isIncomplete boolean
---@return table
function source.get_candidate(req, isIncomplete)
if candidate_cache.req == req then
return { items = candidate_cache.items, isIncomplete = isIncomplete }
end
local items
local request = config.get("sqlite") and db.request or caches.request
items, isIncomplete = request(req, isIncomplete)
if config.get("first_case_insensitive") then
local pre, post = to_upper_first, to_lower_first
if is_capital(req) then
pre, post = post, pre
end
for _, item in ipairs(request(pre(req), isIncomplete)) do
table.insert(items, { label = post(item.label), detail = item.detail })
end
end
candidate_cache.req = req
candidate_cache.items = items
return { items = items, isIncomplete = isIncomplete }
end
---@param request cmp.SourceCompletionApiParams
---@param callback fun(response: lsp.CompletionResponse|nil)
function source.complete(_, request, callback)
-- Clear the cache since the dictionary has been updated.
if config.get("sqlite") then
if db.is_just_updated() then
candidate_cache = {}
end
else
if caches.is_just_updated() then
candidate_cache = {}
end
end
local exact = config.get("exact")
---@type string
local line = request.context.cursor_before_line
local offset = request.offset
line = line:sub(offset)
if line == "" then
return
end
local req, isIncomplete
if exact > 0 then
local line_len = utf8.len(line)
if line_len <= exact then
req = line
isIncomplete = line_len < exact
else
local last = exact
if line_len ~= #line then
last = utf8.offset(line, exact + 1) - 1
end
req = line:sub(1, last)
isIncomplete = false
end
else
-- must be -1
req = line
isIncomplete = true
end
callback(source.get_candidate(req, isIncomplete))
end
function source.resolve(_, completion_item, callback)
if config.get("sqlite") then
db.document(completion_item, callback)
else
require("cmp_dictionary.document")(completion_item, callback)
end
end
return source