-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcmp-source.lua
82 lines (69 loc) · 2.09 KB
/
cmp-source.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
local core = require "obs.utils"
local source = {}
function source.new()
local self = setmetatable({
obs = require "obs",
}, { __index = source })
return self
end
---@return boolean
function source:is_available()
local vault_home_path = self.obs.vault._home_path:expand()
local file_dir = vim.fn.expand "%:p"
local is_in_vault = core.string_has_prefix(file_dir, vault_home_path, true)
return vim.bo.filetype == "markdown" and is_in_vault
end
---@return string
function source:get_debug_name()
return "obs"
end
---find notes to perform completion.
---@param params cmp.SourceCompletionApiParams
---@param callback fun(response: lsp.CompletionResponse|nil)
function source:complete(params, callback)
local before_cursor = params.context.cursor_before_line
if not core.string_has_suffix(before_cursor, "[[", true) then
callback {
items = {},
isIncomplete = false,
}
return
end
local files = self.obs.vault:list_notes()
local items = core.array_map(files, function(file)
local completion_ending = self._get_completion_ending(params)
---@type lsp.CompletionItem
local item = {
label = file:name(),
kind = 17,
insertText = file:name() .. completion_ending,
data = file,
}
return item
end)
callback {
items = items,
isIncomplete = false,
}
end
---@param params cmp.SourceCompletionApiParams
---@return string
function source._get_completion_ending(params)
local after_cursor = params.context.cursor_after_line
if core.string_has_prefix(after_cursor, "]]", true) then
return ""
end
return "]]"
end
---Resolve doc as content of the file.
---@param completion_item lsp.CompletionItem
---@param callback fun(completion_item: lsp.CompletionItem|nil)
function source:resolve(completion_item, callback)
local file = completion_item.data
completion_item.documentation = {
kind = "markdown",
value = file:read(),
}
callback(completion_item)
end
return source