Skip to content

Commit

Permalink
fixing dotall matching replacement separator
Browse files Browse the repository at this point in the history
  • Loading branch information
MagicDuck committed Dec 3, 2024
1 parent 9a2f782 commit 55333ff
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 13 deletions.
29 changes: 16 additions & 13 deletions lua/grug-far/engine/ripgrep/search.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,19 @@ local function getResultsWithReplaceDiff(params)

local stdin = uv.new_pipe()
local replaced_matches_text = nil
local match_separator = '\029'
local match_separator = '\0'

local replaceInputs = vim.deepcopy(params.inputs)
replaceInputs.paths = ''
replaceInputs.filesFilter = ''
local replaceArgs = getArgs(
replaceInputs,
params.options,
{ '--color=never', '--no-heading', '--no-line-number', '--no-column', '--no-filename' }
) --[[ @as string[] ]]
local replaceArgs = getArgs(replaceInputs, params.options, {
'--color=never',
'--no-heading',
'--no-line-number',
'--no-column',
'--no-filename',
'--null-data',
}) --[[ @as string[] ]]

local abort = fetchCommandOutput({
cmd_path = params.options.engines.ripgrep.path,
Expand Down Expand Up @@ -102,13 +105,13 @@ local function getResultsWithReplaceDiff(params)
end,
})

uv.write(
stdin,
vim.fn.join(matches_for_replacement, match_separator) .. match_separator,
function()
uv.shutdown(stdin)
end
)
local inputString = ''
for _, piece in ipairs(matches_for_replacement) do
inputString = inputString .. piece .. match_separator
end
uv.write(stdin, inputString, function()
uv.shutdown(stdin)
end)

return abort
end
Expand Down
23 changes: 23 additions & 0 deletions tests/base/test_search.lua
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,29 @@ T['can search with replace string'] = function()
helpers.childExpectBufLines(child)
end

-- NOTE: this checks that the match_separator in getResultsWithReplaceDiff() replacement logic works correctly
T['can search with replace string, with dotall'] = function()
helpers.writeTestFiles({
{ filename = 'file1', content = [[ grug walks ]] },
{
filename = 'file2',
content = [[
grug talks and grug drinks
then grug thinks
]],
},
})

helpers.childRunGrugFar(child, {
prefills = { search = 'grug(.*)', replacement = 'curly$1' },
})

helpers.childWaitForFinishedStatus(child)

helpers.childExpectScreenshot(child)
helpers.childExpectBufLines(child)
end

T['can search with empty replace string'] = function()
helpers.writeTestFiles({
{ filename = 'file1.txt', content = [[ grug walks ]] },
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
--|---------|---------|---------|---------|---------|---------|---------|---------|
01|  Actions / Help <g?>  Replace <,r>  Sync All <,s> ...
02|
03|  Search:
04| grug(.*)
05|  Replace:
06| curly$1
07|  Files Filter:
08|
09| 󰮚 Flags:
10|
11|  Paths:
12|
13|
14| STATUS_SUCCESS ⟪ ripgrep ⟫
15| 3 matches in 2 files
16|
17| file1
18|▒ 1:2: grug walks [1]
19|▒ 1:2: curly walks [2]
20|
21| file2
22|▒ 2:7: grug talks and grug drinks [3]
23|Grug FAR - 1: grug(.*) 2,9 Top
24|-- INSERT --

--|---------|---------|---------|---------|---------|---------|---------|---------|
01|00111111111111111111111222111111111111112221111111111111112222222222222222222222
02|00222222222222222222222222222222222222222222222222222222222222222222222222222222
03|33333333332222222222222222222222222222222222222222222222222222222222222222222222
04|00222242242222222222222222222222222222222222222222222222222222222222222222222222
05|33333333333222222222222222222222222222222222222222222222222222222222222222222222
06|00222222222222222222222222222222222222222222222222222222222222222222222222222222
07|33333333333333332222222222222222222222222222222222222222222222222222222222222222
08|00222222222222222222222222222222222222222222222222222222222222222222222222222222
09|33333333322222222222222222222222222222222222222222222222222222222222222222222222
10|00222222222222222222222222222222222222222222222222222222222222222222222222222222
11|33333333322222222222222222222222222222222222222222222222222222222222222222222222
12|00222222222222222222222222222222222222222222222222222222222222222222222222222222
13|22222222222222222222222222222222222222222222222222222222222222222222222222222222
14|55555555555555555555555555555555555555555555555555555555555555555555555555555555
15|55555555555555555555552222222222222222222222222222222222222222222222222222222222
16|00222222222222222222222222222222222222222222222222222222222222222222222222222222
17|00666662222222222222222222222222222222222222222222222222222222222222222222222222
18|77828227777777777722222222222222222222222222222222222222222222222222222222223333
19|11828221111111111112222222222222222222222222222222222222222222222222222222223333
20|00222222222222222222222222222222222222222222222222222222222222222222222222222222
21|00666662222222222222222222222222222222222222222222222222222222222222222222222222
22|77828222222277777777777777777777777777222222222222222222222222222222222222223333
23|99999999999999999999999999999999999999999999999999999999999999999999999999999999
24|111111111111::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
--|
01|
02|grug(.*)
03|curly$1
04|
05|
06|
07|
08|file1
09|1:2: grug walks
10|1:2: curly walks
11|
12|file2
13|2:7: grug talks and grug drinks
14|2:7: curly talks and grug drinks
15|3:12: then grug thinks
16|3:12: then curly thinks
17|

--|-
01|
02|
03|
04|
05|
06|
07|
08|
09|
10|
11|
12|
13|
14|
15|
16|
17|

0 comments on commit 55333ff

Please sign in to comment.