Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'sd' replace method #214

Merged
merged 1 commit into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,11 @@ require('spectre').setup({
desc = "ignore case"
},
}
}
},
['sd'] = {
cmd = "sd",
options = { },
},
},
default = {
find = {
Expand Down Expand Up @@ -325,7 +329,7 @@ option. This is the list of directories or files to search in, regardless of the

## Replace Method

There are two replace methods `sed` and `oxi`.
There are three replace methods `sed`, `oxi` and `sd`.

| Sed | oxi |
| -------------------------- | ----------------------------------- |
Expand Down
2 changes: 1 addition & 1 deletion doc/spectre.txt
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ CUSTOM FUNCTIONS ~

REPLACE METHOD *spectre-replace-method*

There are two replace methods `sed` and `oxi`.
There are three replace methods `sed`, `oxi` and `sd`.

│ Sed │ oxi │
│group number by '\0' │group number by '${0}' │
Expand Down
4 changes: 4 additions & 0 deletions lua/spectre/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ local config = {
},
},
},
['sd'] = {
cmd = "sd",
options = { },
},
},
default = {
find = {
Expand Down
52 changes: 52 additions & 0 deletions lua/spectre/replace/sd.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
local Path = require('plenary.path')
local log = require('spectre._log')

local sd = {}

sd.init = function(_, config)
return config
end

sd.replace = function(self, value)
local cwd = value.cwd or vim.loop.cwd()
if not value.filename:match('^%/') then
value.filename = Path:new(cwd):joinpath(value.filename):absolute()
end

-- Read the original file
local lines = {}
local file = io.open(value.filename, "r")
for line in file:lines() do
table.insert(lines, line)
end
file:close()

if value.lnum <= #lines then
-- Use `io.popen` to get the transformed line using `sd`
local command = string.format("echo '%s' | sd '%s' '%s'", lines[value.lnum], value.search_text, value.replace_text)
local handle = io.popen(command, 'r')
if handle then
local transformedLine = handle:read("*a")
handle:close()
-- Replace the line in memory
lines[value.lnum] = transformedLine:gsub("\n$", "") -- Remove trailing newline added by `echo`
else
self:on_error(false, value)
return
end
else
log.debug("Line number out of bounds.")
return
end

-- Write the modified lines back to the file
file = io.open(value.filename, "w")
for _, line in ipairs(lines) do
file:write(line, "\n")
end
file:close()

self:on_done(true, value)
end

return sd
Loading