1
- local util = require " obsidian.util"
2
1
local log = require " obsidian.log"
3
- local RefTypes = require (" obsidian.search" ).RefTypes
4
2
5
- --- @param client obsidian.Client
6
- --- @param picker obsidian.Picker
7
- --- @param note obsidian.Note
8
- --- @param opts { anchor : string |?, block : string |? }|?
9
- local function collect_backlinks (client , picker , note , opts )
10
- opts = opts or {}
11
-
12
- client :find_backlinks_async (note , function (backlinks )
13
- if vim .tbl_isempty (backlinks ) then
14
- if opts .anchor then
15
- log .info (" No backlinks found for anchor '%s' in note '%s'" , opts .anchor , note .id )
16
- elseif opts .block then
17
- log .info (" No backlinks found for block '%s' in note '%s'" , opts .block , note .id )
18
- else
19
- log .info (" No backlinks found for note '%s'" , note .id )
20
- end
21
- return
22
- end
23
-
24
- local entries = {}
25
- for _ , matches in ipairs (backlinks ) do
26
- for _ , match in ipairs (matches .matches ) do
27
- entries [# entries + 1 ] = {
28
- value = { path = matches .path , line = match .line },
29
- filename = tostring (matches .path ),
30
- lnum = match .line ,
31
- }
32
- end
33
- end
34
-
35
- --- @type string
36
- local prompt_title
37
- if opts .anchor then
38
- prompt_title = string.format (" Backlinks to '%s%s'" , note .id , opts .anchor )
39
- elseif opts .block then
40
- prompt_title = string.format (" Backlinks to '%s#%s'" , note .id , util .standardize_block (opts .block ))
41
- else
42
- prompt_title = string.format (" Backlinks to '%s'" , note .id )
43
- end
44
-
45
- vim .schedule (function ()
46
- picker :pick (entries , {
47
- prompt_title = prompt_title ,
48
- callback = function (value )
49
- util .open_buffer (value .path , { line = value .line })
3
+ local telescope_on_list = function (data )
4
+ local pickers = require " telescope.pickers"
5
+ local finders = require " telescope.finders"
6
+ pickers
7
+ .new ({}, {
8
+ prompt_title = " References" ,
9
+ finder = finders .new_table {
10
+ results = data .items ,
11
+ entry_maker = function (value )
12
+ return {
13
+ value = value ,
14
+ display = value .text ,
15
+ path = value .filename ,
16
+ ordinal = value .text ,
17
+ lnum = value .lnum ,
18
+ }
50
19
end ,
51
- })
52
- end )
53
- end , { search = { sort = true }, anchor = opts . anchor , block = opts . block } )
20
+ },
21
+ } )
22
+ : find ( )
54
23
end
55
24
56
25
--- @param client obsidian.Client
@@ -60,77 +29,19 @@ return function(client)
60
29
log .err " No picker configured"
61
30
return
62
31
end
63
-
64
- local location , _ , ref_type = util .parse_cursor_link { include_block_ids = true }
65
-
66
- if
67
- location ~= nil
68
- and ref_type ~= RefTypes .NakedUrl
69
- and ref_type ~= RefTypes .FileUrl
70
- and ref_type ~= RefTypes .BlockID
71
- then
72
- -- Remove block links from the end if there are any.
73
- -- TODO: handle block links.
74
- --- @type string |?
75
- local block_link
76
- location , block_link = util .strip_block_links (location )
77
-
78
- -- Remove anchor links from the end if there are any.
79
- --- @type string |?
80
- local anchor_link
81
- location , anchor_link = util .strip_anchor_links (location )
82
-
83
- -- Assume 'location' is current buffer path if empty, like for TOCs.
84
- if string.len (location ) == 0 then
85
- location = vim .api .nvim_buf_get_name (0 )
86
- end
87
-
88
- local opts = { anchor = anchor_link , block = block_link }
89
-
90
- client :resolve_note_async (location , function (...)
91
- --- @type obsidian.Note[]
92
- local notes = { ... }
93
-
94
- if # notes == 0 then
95
- log .err (" No notes matching '%s'" , location )
96
- return
97
- elseif # notes == 1 then
98
- return collect_backlinks (client , picker , notes [1 ], opts )
99
- else
100
- return vim .schedule (function ()
101
- picker :pick_note (notes , {
102
- prompt_title = " Select note" ,
103
- callback = function (note )
104
- collect_backlinks (client , picker , note , opts )
105
- end ,
106
- })
107
- end )
108
- end
109
- end )
32
+ local picker_name = tostring (picker )
33
+
34
+ if picker_name == " TelescopePicker()" then
35
+ vim .lsp .buf .references ({
36
+ includeDeclaration = false ,
37
+ }, { on_list = telescope_on_list })
38
+ elseif picker_name == " SnacksPicker()" then
39
+ require (" snacks.picker" ).lsp_symbols ()
40
+ elseif picker_name == " FzfPicker()" then
41
+ require (" fzf-lua" ).lsp_document_symbols ()
42
+ elseif picker_name == " MiniPicker()" then
43
+ -- vim.lsp.buf.document_symbol { on_list = mini_pick_on_list }
110
44
else
111
- --- @type { anchor : string |?, block : string |? }
112
- local opts = {}
113
- --- @type obsidian.note.LoadOpts
114
- local load_opts = {}
115
-
116
- if ref_type == RefTypes .BlockID then
117
- opts .block = location
118
- else
119
- load_opts .collect_anchor_links = true
120
- end
121
-
122
- local note = client :current_note (0 , load_opts )
123
-
124
- -- Check if cursor is on a header, if so, use that anchor.
125
- local header_match = util .parse_header (vim .api .nvim_get_current_line ())
126
- if header_match then
127
- opts .anchor = header_match .anchor
128
- end
129
-
130
- if note == nil then
131
- log .err " Current buffer does not appear to be a note inside the vault"
132
- else
133
- collect_backlinks (client , picker , note , opts )
134
- end
45
+ vim .lsp .buf .document_symbol { loclist = false }
135
46
end
136
47
end
0 commit comments