Skip to content

Commit 86e6a06

Browse files
committed
feat: use snippet marker to jump to anchor/block pos on accept
1 parent f492596 commit 86e6a06

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

lua/obsidian/lsp/handlers/completion.lua

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,32 @@ local util = require "obsidian.util"
1111

1212
local find, sub, lower = string.find, string.sub, string.lower
1313

14+
local function insert_snippet_marker(text, style)
15+
if style == "markdown" then
16+
local pos = text:find "]"
17+
local a, b = sub(text, 1, pos - 1), sub(text, pos)
18+
return a .. "$1" .. b
19+
end
20+
end
21+
1422
---@param note obsidian.Note
1523
---@param insert_text string
1624
---@param insert_start integer
1725
---@param insert_end integer
1826
---@param line_num integer
1927
---@return lsp.CompletionItem
20-
local function calc_ref_item(note, insert_text, insert_start, insert_end, line_num)
28+
local function calc_ref_item(note, insert_text, insert_start, insert_end, line_num, style)
2129
return {
2230
kind = 17,
2331
label = note.title,
2432
filterText = note.title,
33+
insertTextFormat = 2, -- is snippet
2534
textEdit = {
2635
range = {
2736
start = { line = line_num, character = insert_start },
2837
["end"] = { line = line_num, character = insert_end },
2938
},
30-
newText = insert_text,
39+
newText = insert_snippet_marker(insert_text, style),
3140
},
3241
labelDetails = { description = "Obsidian" },
3342
data = {
@@ -55,7 +64,8 @@ local function handle_ref(client, partial, ref_start, cursor_col, line_num, hand
5564
local pattern = vim.pesc(lower(partial))
5665
if title and find(lower(title), pattern) then
5766
local link_text = client:format_link(note)
58-
items[#items + 1] = calc_ref_item(note, link_text, ref_start, cursor_col, line_num)
67+
local style = client.opts.preferred_link_style
68+
items[#items + 1] = calc_ref_item(note, link_text, ref_start, cursor_col, line_num, style)
5969
end
6070
end
6171
handler(nil, { items = items })
@@ -103,9 +113,9 @@ return function(client, params, handler, _)
103113
local buf = vim.uri_to_bufnr(uri)
104114
local line_text = vim.api.nvim_buf_get_lines(buf, line_num, line_num + 1, false)[1]
105115

106-
print(util.strip_anchor_links(line_text))
107-
print(util.strip_block_links(line_text))
108-
116+
-- print(util.strip_anchor_links(line_text))
117+
-- print(util.strip_block_links(line_text))
118+
--
109119
local text_before_cursor = sub(line_text, 1, char_num)
110120

111121
local tag_start = find(text_before_cursor, "#", 1, true)

0 commit comments

Comments
 (0)