1
- -- TODO: move to the idea of textEdits
2
-
3
1
local lsp = vim .lsp
4
- local ms = lsp .protocol .Methods
5
-
6
- --- @type lsp.WorkspaceEdit
7
- local edits = {
8
- documentChanges = {
9
- {
10
- kind = " rename" ,
11
- oldUri = vim .uri_from_bufnr (vim .api .nvim_get_current_buf ()),
12
- newUri = vim .uri_from_fname " /home/n451/test3.lua" ,
13
- },
14
- },
15
- }
16
-
17
- -- lsp.util.apply_workspace_edit(edits, "utf-8")
18
-
19
- -- local function rename()
20
- -- lsp.buf_request(0, ms.workspace_didRenameFiles, params, function(...)
21
- -- vim.print(...)
22
- -- end)
23
- -- end
24
-
25
- -- Search notes on disk for any references to `cur_note_id`.
26
- -- We look for the following forms of references:
27
- -- * '[[cur_note_id]]'
28
- -- * '[[cur_note_id|ALIAS]]'
29
- -- * '[[cur_note_id\|ALIAS]]' (a wiki link within a table)
30
- -- * '[ALIAS](cur_note_id)'
31
- -- And all of the above with relative paths (from the vault root) to the note instead of just the note ID,
32
- -- with and without the ".md" suffix.
33
- -- Another possible form is [[ALIAS]], but we don't change the note's aliases when renaming
34
- -- so those links will still be valid.
35
- --- @param ref_link string
36
- --- @return string[]
37
- local function get_ref_forms (ref_link )
38
- return {
39
- " [[" .. ref_link .. " ]]" ,
40
- " [[" .. ref_link .. " |" ,
41
- " [[" .. ref_link .. " \\ |" ,
42
- " [[" .. ref_link .. " #" ,
43
- " ](" .. ref_link .. " )" ,
44
- " ](" .. ref_link .. " #" ,
45
- }
46
- end
2
+ local Path = require " obsidian.path"
3
+ local Note = require " obsidian.note"
4
+ local search = require " obsidian.search"
47
5
6
+ --- @param old_uri string
7
+ --- @param new_uri string
48
8
local function rename_file (old_uri , new_uri )
49
9
--- @type lsp.WorkspaceEdit
50
10
local edit = {
@@ -57,12 +17,27 @@ local function rename_file(old_uri, new_uri)
57
17
},
58
18
}
59
19
60
- vim . lsp .util .apply_workspace_edit (edit , " utf-8" )
20
+ lsp .util .apply_workspace_edit (edit , " utf-8" )
61
21
end
62
22
63
- local Path = require " obsidian.path"
64
- local Note = require " obsidian.note"
65
- local search = require " obsidian.search"
23
+ -- Search notes on disk for any references to `cur_note_id`.
24
+ -- We look for the following forms of references:
25
+ -- * '[[cur_note_id]]'
26
+ -- * '[[cur_note_id|ALIAS]]'
27
+ -- * '[[cur_note_id\|ALIAS]]' (a wiki link within a table)
28
+ -- * '[ALIAS](cur_note_id)'
29
+ -- And all of the above with relative paths (from the vault root) to the note instead of just the note ID,
30
+ -- with and without the ".md" suffix.
31
+ -- Another possible form is [[ALIAS]], but we don't change the note's aliases when renaming
32
+ -- so those links will still be valid.
33
+ local ref_patterns = {
34
+ " [[%s]]" , -- wiki
35
+ " [[%s|" , -- wiki with alias
36
+ " [[%s\\ |" , -- wiki link within a table
37
+ " [[%s#" , -- wiki with heading
38
+ " ](%s)" , -- markdown
39
+ " ](%s#" , -- markdown with heading
40
+ }
66
41
67
42
--- @param client obsidian.Client
68
43
--- @param params table
@@ -83,63 +58,64 @@ local function rename_current_note(client, params)
83
58
local cur_note_rel_path = tostring (client :vault_relative_path (cur_note_path , { strict = true }))
84
59
local new_note_rel_path = tostring (client :vault_relative_path (new_note_path , { strict = true }))
85
60
86
- local pats = {
87
- " [[%s]]" , -- wiki
88
- " [[%s|" , -- wiki with display
89
- " [[%s\\ |" , -- ?
90
- " [[%s#" , -- wiki with heading
91
- " ](%s)" , -- markdown
92
- " ](%s#" , -- markdown with heading
93
- }
94
-
95
61
local replace_lookup = {}
96
62
97
- for _ , pat in ipairs (pats ) do
63
+ for _ , pat in ipairs (ref_patterns ) do
98
64
replace_lookup [pat :format (cur_note_id )] = pat :format (new_note_id )
99
65
replace_lookup [pat :format (cur_note_rel_path )] = pat :format (new_note_rel_path )
100
66
replace_lookup [pat :format (cur_note_rel_path :sub (1 , - 4 ))] = pat :format (new_note_rel_path :sub (1 , - 4 ))
101
67
end
102
68
103
69
local reference_forms = vim .tbl_keys (replace_lookup )
104
70
105
- -- search.search_async(
106
- -- client.dir,
107
- -- reference_forms,
108
- -- search.SearchOpts.from_tbl { fixed_strings = true, max_count_per_file = 1 },
109
- -- function(match)
110
- -- local file = match.path.text
111
- -- local line = match.line_number
112
- -- local start, _end = match.submatches[1].start, match.submatches[1]["end"]
113
- -- local matched = match.submatches[1].match.text
114
- --
115
- -- handler(nil, {
116
- -- changes = {
117
- -- [vim.uri_from_fname(file)] = {
118
- -- range = {
119
- -- start = { line = line, character = start },
120
- -- ["end"] = { line = line, character = _end },
121
- -- },
122
- -- newText = replace_lookup[matched],
123
- -- },
124
- -- },
125
- -- })
126
- -- end,
127
- -- function(_)
128
- -- -- all_tasks_submitted = true
129
- -- end
130
- -- )
131
-
71
+ search .search_async (
72
+ client .dir ,
73
+ reference_forms ,
74
+ search .SearchOpts .from_tbl { fixed_strings = true , max_count_per_file = 1 },
75
+ vim .schedule_wrap (function (match )
76
+ local file = match .path .text
77
+ local line = match .line_number - 1
78
+ local start , _end = match .submatches [1 ].start , match .submatches [1 ][" end" ]
79
+ local matched = match .submatches [1 ].match .text
80
+ local edit = {
81
+ documentChanges = {
82
+ {
83
+ textDocument = {
84
+ uri = vim .uri_from_fname (file ),
85
+ },
86
+ edits = {
87
+ {
88
+ range = {
89
+ start = { line = line , character = start },
90
+ [" end" ] = { line = line , character = _end },
91
+ },
92
+ newText = replace_lookup [matched ],
93
+ },
94
+ },
95
+ },
96
+ },
97
+ }
98
+ lsp .util .apply_workspace_edit (edit , " utf-8" )
99
+ end ),
100
+ function (_ )
101
+ -- TODO: conclude the rename
102
+ -- all_tasks_submitted = true
103
+ end
104
+ )
132
105
rename_file (uri , vim .uri_from_fname (new_path ))
106
+
107
+ local note = client :current_note ()
108
+ note .id = new_note_id
133
109
end
134
110
135
111
local function rename_note_at_cursor (params ) end
136
112
137
113
--- @param client obsidian.Client
138
114
--- @param params table
139
- --- @param handler function
140
- return function (client , params , handler , _ )
115
+ return function (client , params , _ , _ )
141
116
local position = params .position
142
117
118
+ -- TODO: check if cursor on link
143
119
rename_current_note (client , params )
144
120
145
121
-- require "obsidian.commands.rename"(obsidian_client, { args = params.newName })
0 commit comments