Skip to content

Commit bbc3895

Browse files
committed
fix: cleaned up rename logic, ref replace working
1 parent e8ae8a5 commit bbc3895

File tree

2 files changed

+66
-89
lines changed

2 files changed

+66
-89
lines changed

lua/obsidian/lsp/handlers/completion.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ local function gen_ref_item(label, path, new_text, range, style, is_snippet)
6969
insertTextFormat = 2, -- is snippet TODO: extract to config option
7070
textEdit = {
7171
range = range,
72-
newText = insert_snippet_marker(new_text, style),
72+
newText = new_text,
73+
-- insert_snippet_marker(new_text, style),
7374
},
7475
labelDetails = { description = "Obsidian" },
7576
data = {

lua/obsidian/lsp/handlers/rename.lua

Lines changed: 64 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,10 @@
1-
-- TODO: move to the idea of textEdits
2-
31
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"
475

6+
---@param old_uri string
7+
---@param new_uri string
488
local function rename_file(old_uri, new_uri)
499
---@type lsp.WorkspaceEdit
5010
local edit = {
@@ -57,12 +17,27 @@ local function rename_file(old_uri, new_uri)
5717
},
5818
}
5919

60-
vim.lsp.util.apply_workspace_edit(edit, "utf-8")
20+
lsp.util.apply_workspace_edit(edit, "utf-8")
6121
end
6222

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+
}
6641

6742
---@param client obsidian.Client
6843
---@param params table
@@ -83,63 +58,64 @@ local function rename_current_note(client, params)
8358
local cur_note_rel_path = tostring(client:vault_relative_path(cur_note_path, { strict = true }))
8459
local new_note_rel_path = tostring(client:vault_relative_path(new_note_path, { strict = true }))
8560

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-
9561
local replace_lookup = {}
9662

97-
for _, pat in ipairs(pats) do
63+
for _, pat in ipairs(ref_patterns) do
9864
replace_lookup[pat:format(cur_note_id)] = pat:format(new_note_id)
9965
replace_lookup[pat:format(cur_note_rel_path)] = pat:format(new_note_rel_path)
10066
replace_lookup[pat:format(cur_note_rel_path:sub(1, -4))] = pat:format(new_note_rel_path:sub(1, -4))
10167
end
10268

10369
local reference_forms = vim.tbl_keys(replace_lookup)
10470

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+
)
132105
rename_file(uri, vim.uri_from_fname(new_path))
106+
107+
local note = client:current_note()
108+
note.id = new_note_id
133109
end
134110

135111
local function rename_note_at_cursor(params) end
136112

137113
---@param client obsidian.Client
138114
---@param params table
139-
---@param handler function
140-
return function(client, params, handler, _)
115+
return function(client, params, _, _)
141116
local position = params.position
142117

118+
-- TODO: check if cursor on link
143119
rename_current_note(client, params)
144120

145121
-- require "obsidian.commands.rename"(obsidian_client, { args = params.newName })

0 commit comments

Comments
 (0)