1- -- TODO: completion for anchor, blocks
21-- TODO: memoize?
32
43local obsidian = require " obsidian"
@@ -27,15 +26,6 @@ local RefPatterns = {
2726 -- heading = "[[## "
2827}
2928
30- -- TODO:
31- local function insert_snippet_marker (text , style )
32- if style == " markdown" then
33- local pos = text :find " ]"
34- local a , b = sub (text , 1 , pos - 1 ), sub (text , pos )
35- return a .. " $1" .. b
36- end
37- end
38-
3929--- Collect matching anchor links.
4030--- @param note obsidian.Note
4131--- @param anchor_link string ?
@@ -61,22 +51,8 @@ local function collect_matching_anchors(note, anchor_link)
6151 return matching_anchors
6252end
6353
64- --- @return function
65- local function get_format_func ()
66- local format_func
67- local style = Obsidian .opts .preferred_link_style
68- if style == " markdown" then
69- format_func = Obsidian .opts .markdown_link_func
70- elseif style == " wiki" then
71- format_func = Obsidian .opts .wiki_link_func
72- else
73- error " unimplemented"
74- end
75- return format_func
76- end
77-
7854-- A more generic pure function, don't require label to exist
79- local function format_link (label )
55+ local function label_to_new_text (label )
8056 local path = util .urlencode (label ) .. " .md"
8157 local opts = { label = label , path = path }
8258
9773--- @param new_text string
9874--- @param range lsp.Range
9975--- @return lsp.CompletionItem
100- local function gen_ref_item (label , path , new_text , range , is_snippet )
76+ local function gen_ref_item (label , path , new_text , range )
10177 return {
10278 kind = 17 ,
10379 label = label ,
10480 textEdit = {
10581 range = range ,
10682 newText = new_text ,
107- -- insert_snippet_marker(new_text, style),
10883 },
10984 labelDetails = { description = " Obsidian" },
11085 data = {
11186 file = path ,
11287 kind = " ref" ,
11388 },
114- -- insertTextFormat = 1, -- is snippet TODO: extract to config option
11589 }
11690end
11791
@@ -134,7 +108,7 @@ local function gen_create_item(label, range)
134108 label = label .. " (create)" ,
135109 textEdit = {
136110 range = range ,
137- newText = format_link (label ),
111+ newText = label_to_new_text (label ),
138112 },
139113 labelDetails = { description = " Obsidian" },
140114 command = {
@@ -147,16 +121,11 @@ local function gen_create_item(label, range)
147121 }
148122end
149123
150- local handle_bare_links = function (partial , range , handler )
124+ local handle_bare_links = function (partial , notes , range , handler )
151125 local items = {}
152126 items [# items + 1 ] = gen_create_item (partial , range )
153127
154128 local pattern = vim .pesc (lower (partial ))
155- local notes = Search .find_notes (pattern )
156-
157- if # notes == 0 then
158- return handler (nil , { items = items })
159- end
160129
161130 local note_lookup = {}
162131 local queries = {}
@@ -183,22 +152,22 @@ local handle_bare_links = function(partial, range, handler)
183152 handler (nil , { items = items })
184153end
185154
186- local function handle_anchor_links (partial , anchor_link , handler )
187- -- state.current_note = state.current_note or client:find_notes(partial)[2]
155+ --- @param partial string
156+ --- @param notes obsidian.Note[]
157+ --- @param anchor_link string
158+ --- @param callback function
159+ local function handle_anchor_links (partial , notes , anchor_link , callback )
188160 -- TODO: calc current_note once
189161 -- TODO: handle two cases:
190162 -- 1. typing partial note name, no completeed text after cursor, insert the full link
191163 -- 2. jumped to heading, only insert anchor
192164 -- TODO: need to do more textEdit to insert additional #title to path so that app supports?
193165 local items = {}
194- local notes = Search .resolve_note (partial )
195166
196167 for _ , note in ipairs (notes ) do
197168 local id = note .id
198169 local pattern = vim .pesc (lower (partial ))
199170 if id and find (lower (id ), pattern ) then
200- note = Note .from_file (note .path .filename , { collect_anchor_links = true })
201-
202171 local note_anchors = collect_matching_anchors (note , anchor_link )
203172 if not note_anchors then
204173 return
@@ -224,7 +193,7 @@ local function handle_anchor_links(partial, anchor_link, handler)
224193 }
225194 end
226195 end
227- handler (nil , { items = items })
196+ callback (nil , { items = items })
228197 end
229198end
230199
@@ -254,12 +223,28 @@ handlers[CmpType.ref] = function(prefix, range, handler)
254223 local anchor_link , block_link
255224 prefix , anchor_link = util .strip_anchor_links (prefix )
256225 prefix , block_link = util .strip_block_links (prefix )
226+
227+ local search_opts = Search ._defaults
228+ search_opts .ignore_case = true
229+
230+ local notes = Search .find_notes (prefix , {
231+ search = search_opts ,
232+ notes = {
233+ collect_anchor_links = anchor_link ~= nil ,
234+ collect_blocks = block_link ~= nil ,
235+ },
236+ })
237+
238+ if # notes == 0 then
239+ return handler (nil , { items = {} })
240+ end
241+
257242 if anchor_link then
258- handle_anchor_links (prefix , anchor_link , handler )
243+ handle_anchor_links (prefix , notes , anchor_link , handler )
259244 elseif block_link then
260- handle_block_links (prefix , block_link , handler )
245+ -- handle_block_links(prefix, block_link, handler)
261246 else
262- handle_bare_links (prefix , range , handler )
247+ handle_bare_links (prefix , notes , range , handler )
263248 end
264249end
265250
0 commit comments