-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathapply-edit.lua
More file actions
executable file
·62 lines (57 loc) · 1.72 KB
/
apply-edit.lua
File metadata and controls
executable file
·62 lines (57 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env -S nvim --headless -l
-- apply-edit.lua — Apply a single Edit (old_string → new_string) to a file.
--
-- Usage (via nvim --headless -l):
-- nvim --headless -l apply-edit.lua <file_path> <old_string> <new_string> <replace_all> <output_path>
--
-- replace_all: "true" or "false"
local file_path = arg[1]
local old_string = arg[2]
local new_string = arg[3]
local replace_all = arg[4] == "true"
local output_path = arg[5]
-- Read the file (empty string if it does not exist yet)
local content = ""
local fh = io.open(file_path, "r")
if fh then
content = fh:read("*a")
fh:close()
end
-- Literal replacement (string.find plain=true prevents pattern interpretation)
if replace_all then
-- Replace all occurrences
local result = {}
local search_start = 1
if old_string == "" then
-- Empty old_string: prepend new_string (handles "insert into empty file")
result = { new_string, content }
else
while true do
local s, e = string.find(content, old_string, search_start, true)
if not s then
table.insert(result, content:sub(search_start))
break
end
table.insert(result, content:sub(search_start, s - 1))
table.insert(result, new_string)
search_start = e + 1
end
end
content = table.concat(result)
else
-- Replace first occurrence only
if old_string == "" then
-- Empty old_string: prepend new_string (handles "insert into empty file")
content = new_string .. content
else
local s, e = string.find(content, old_string, 1, true)
if s then
content = content:sub(1, s - 1) .. new_string .. content:sub(e + 1)
end
end
end
-- Write the result
local out = assert(io.open(output_path, "w"))
out:write(content)
out:close()
os.exit(0)