Skip to content

Commit

Permalink
Merge pull request #214 from Zananok/master
Browse files Browse the repository at this point in the history
Add 'sd' replace method
  • Loading branch information
EpsilonKu authored Mar 22, 2024
2 parents 1d23be1 + 2e6bf39 commit 31f62d7
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
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

0 comments on commit 31f62d7

Please sign in to comment.