Skip to content

Commit e469f32

Browse files
author
neo451
committed
idea: auto accept?
1 parent 8ac064b commit e469f32

File tree

1 file changed

+54
-24
lines changed

1 file changed

+54
-24
lines changed

lua/obsidian/lsp/handlers/completion.lua

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,18 @@
33
local obsidian = require "obsidian"
44
local util = obsidian.util
55
local Search = obsidian.search
6-
local Note = obsidian.Note
76
local find, sub, lower = string.find, string.sub, string.lower
87

9-
-- util.BLOCK_PATTERN = "%^[%w%d][%w%d-]*"
10-
local anchor_trigger_pattern = {
11-
markdown = "%[%S+#(%w*)",
12-
}
13-
14-
local heading_trigger_pattern = "[##"
15-
16-
-- TODO:
178
local CmpType = {
189
ref = 1,
1910
tag = 2,
20-
anchor = 3,
11+
-- heading = 3,
2112
}
2213

2314
local RefPatterns = {
2415
[CmpType.ref] = "[[",
2516
[CmpType.tag] = "#",
26-
-- heading = "[[## "
17+
-- [CmpType.heading] = "[[## ",
2718
}
2819

2920
---Collect matching anchor links.
@@ -121,11 +112,44 @@ local function gen_create_item(label, range)
121112
}
122113
end
123114

124-
local handle_bare_links = function(partial, notes, range, handler)
115+
local function auto_accept(note, range)
116+
local edit = {
117+
documentChanges = {
118+
{
119+
textDocument = {
120+
uri = vim.uri_from_fname(tostring(note.path)),
121+
version = vim.NIL,
122+
},
123+
edits = {
124+
{
125+
range = range,
126+
newText = "[[" .. note.id .. "#",
127+
},
128+
},
129+
},
130+
},
131+
}
132+
vim.schedule(function()
133+
vim.lsp.util.apply_workspace_edit(edit, "utf-8")
134+
vim.api.nvim_win_set_cursor(0, {
135+
range.start.line + 1, -- 0 index to 1 index
136+
range["end"].character + 1, -- one char after
137+
})
138+
end)
139+
end
140+
141+
local handle_bare_links = function(prefix, notes, range, handler)
125142
local items = {}
126-
items[#items + 1] = gen_create_item(partial, range)
127143

128-
local pattern = vim.pesc(lower(partial))
144+
local auto = false
145+
if vim.endswith(prefix, "#") then
146+
prefix = sub(prefix, 0, -2)
147+
auto = true
148+
else
149+
items[#items + 1] = gen_create_item(prefix, range) -- TODO: ?
150+
end
151+
152+
local pattern = vim.pesc(lower(prefix))
129153

130154
local note_lookup = {}
131155
local queries = {}
@@ -140,16 +164,22 @@ local handle_bare_links = function(partial, notes, range, handler)
140164

141165
local matches = vim.fn.matchfuzzy(queries, pattern, { limit = 10 }) -- TOOD: config? lower?
142166

143-
for _, match in ipairs(matches) do
144-
local note = note_lookup[match]
145-
if not res_lookup[note] then
146-
local link_text = note:format_link()
147-
items[#items + 1] = gen_ref_item(note.id, note.path.filename, link_text, range) -- TODO: label -> id title fname?
148-
res_lookup[note] = true
167+
if auto then
168+
local note = note_lookup[matches[1]]
169+
if note then
170+
auto_accept(note, range)
171+
end
172+
else
173+
for _, match in ipairs(matches) do
174+
local note = note_lookup[match]
175+
if not res_lookup[note] then
176+
local link_text = note:format_link()
177+
items[#items + 1] = gen_ref_item(note.id, note.path.filename, link_text, range) -- TODO: label -> id title fname?
178+
res_lookup[note] = true
179+
end
149180
end
181+
handler(nil, { items = items })
150182
end
151-
152-
handler(nil, { items = items })
153183
end
154184

155185
---@param partial string
@@ -164,9 +194,10 @@ local function handle_anchor_links(partial, notes, anchor_link, callback)
164194
-- TODO: need to do more textEdit to insert additional #title to path so that app supports?
165195
local items = {}
166196

197+
local pattern = vim.pesc(lower(partial))
198+
167199
for _, note in ipairs(notes) do
168200
local id = note.id
169-
local pattern = vim.pesc(lower(partial))
170201
if id and find(lower(id), pattern) then
171202
local note_anchors = collect_matching_anchors(note, anchor_link)
172203
if not note_anchors then
@@ -202,7 +233,6 @@ local function handle_block_links() end
202233
local handlers = {}
203234

204235
handlers[CmpType.tag] = function(partial, range, handler)
205-
vim.print(handler)
206236
local items = {}
207237
local tags = vim
208238
.iter(Search.find_tags("", {}))

0 commit comments

Comments
 (0)