Skip to content

Commit ba76cfb

Browse files
author
neo451
committed
feat: proper return empty, tests
1 parent 37e4614 commit ba76cfb

File tree

2 files changed

+79
-3
lines changed

2 files changed

+79
-3
lines changed

lua/obsidian/lsp/handlers/completion.lua

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ local function gen_tag_item(tag)
119119
return {
120120
kind = 1,
121121
label = tag,
122-
filterText = tag,
123122
insertText = tag,
124123
labelDetails = { description = "ObsidianTag" },
125124
data = { kind = "tag" },
@@ -133,7 +132,6 @@ local function gen_create_item(label, range)
133132
return {
134133
kind = 17,
135134
label = label .. " (create)",
136-
filterText = label,
137135
textEdit = {
138136
range = range,
139137
newText = format_link(label),
@@ -298,10 +296,16 @@ return function(params, callback, _)
298296
local text_before = sub(line_text, 1, cursor_col)
299297
local t, prefix, ref_start = get_cmp_type(text_before, min_chars)
300298

299+
callback = vim.schedule_wrap(callback)
300+
301+
if not t then
302+
return callback(nil, {})
303+
end
304+
301305
local range = {
302306
start = { line = line_num, character = ref_start },
303307
["end"] = { line = line_num, character = cursor_col }, -- if auto parired
304308
}
305309

306-
handlers[t](prefix, range, vim.schedule_wrap(callback))
310+
handlers[t](prefix, range, callback)
307311
end

tests/lsp/test_completion.lua

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
local eq = MiniTest.expect.equality
2+
local h = dofile "tests/helpers.lua"
3+
4+
local T, child = h.child_vault()
5+
6+
T["trigger with [["] = function()
7+
local referencer = [==[
8+
9+
[[tar
10+
]==]
11+
12+
local root = child.Obsidian.dir
13+
local referencer_path = root / "referencer.md"
14+
h.write(referencer, referencer_path)
15+
16+
local target_path = root / "target.md"
17+
h.write("", target_path)
18+
19+
child.cmd(string.format("edit %s", referencer_path))
20+
child.api.nvim_win_set_cursor(0, { 2, 0 })
21+
child.type_keys "A"
22+
23+
local res = child.lsp.buf_request_sync(
24+
0,
25+
"textDocument/completion",
26+
child.lua_get "vim.lsp.util.make_position_params(0, 'utf-8')"
27+
)
28+
eq(1, #res)
29+
local response = res[1]
30+
local result = response.result
31+
32+
eq(2, #result.items)
33+
eq("tar (create)", result.items[1].label)
34+
eq("target", result.items[2].label)
35+
end
36+
37+
T["trigger with #"] = function()
38+
local referencer = [==[
39+
---
40+
tags:
41+
- this/is/a/tag
42+
---
43+
44+
#thi
45+
]==]
46+
47+
local root = child.Obsidian.dir
48+
local referencer_path = root / "referencer.md"
49+
h.write(referencer, referencer_path)
50+
51+
local target_path = root / "target.md"
52+
h.write("", target_path)
53+
54+
child.cmd(string.format("edit %s", referencer_path))
55+
child.api.nvim_win_set_cursor(0, { 6, 0 })
56+
child.type_keys "A"
57+
58+
local res = child.lsp.buf_request_sync(
59+
0,
60+
"textDocument/completion",
61+
child.lua_get "vim.lsp.util.make_position_params(0, 'utf-8')"
62+
)
63+
eq(1, #res)
64+
local response = res[1]
65+
local result = response.result
66+
67+
eq(2, #result.items)
68+
eq("this/is/a/tag", result.items[1].label)
69+
eq("thi", result.items[2].label) -- NOTE: here because in test it is written to disk, in real case it will not duplicate
70+
end
71+
72+
return T

0 commit comments

Comments
 (0)