Skip to content

Commit

Permalink
fixing the blacklisting
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan Badragan committed May 9, 2024
1 parent 3ca4ea9 commit 35af0ee
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 30 deletions.
6 changes: 4 additions & 2 deletions lua/grug-far/actions/replace.lua
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,11 @@ local function replace(params)
inputs = context.state.inputs,
options = context.options,
on_fetch_chunk = reportMatchingFilesUpdate,
on_finish = vim.schedule_wrap(function(status, errorMessage, files)
on_finish = vim.schedule_wrap(function(status, errorMessage, files, blacklistedArgs)
if not status then
on_finish_all(nil, nil, 'replace cannot work due to improper flags! (blacklisted because of undesirable results)')
on_finish_all(nil, nil,
blacklistedArgs and 'replace cannot work with flags: ' .. vim.fn.join(blacklistedArgs, ', ') or
'replace aborted!')
return
elseif status == 'error' then
on_finish_all(status, errorMessage)
Expand Down
41 changes: 30 additions & 11 deletions lua/grug-far/rg/blacklistedReplaceFlags.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
-- TODO (sbadragan): might need to disable some flags, like:
-- --no-include-zero --no-byte-offset
-- --hyperlink-format=none
-- --max-columns=0
-- --no-max-columns-preview --no-trim
-- blacklist: --help --quiet
-- Hmmm, there are just too many things that could completely screw it up ... I think we need a whitelist of useful
-- flags that we allow the user to pass, otherwise replacing would not work
-- those are flags that would result in unexpected output when generating replaced file contents
return {
'--pre',
'--pre-glob',
'--search-zip', '-z',

'--help'
'-z', '--search-zip',
'-m', '--max-count',
'--null-data',
'--stop-on-nonmatch',
'-a', '--text',
'--binary',
' -L', '--follow',
'-b', '--byte-offset',
'--line-buffered',
'-M', '--max-columns',
'--max-columns-preview',
'-0', '--null',
'-o', '--only-matching',
'-q', '--quiet',
'--trim',
'--vimgrep',
'-c', '--count',
'--count-matches',
'--json',
'--debug',
'--no-ignore-messages',
'--stats',
'--trace',
'--files',
'--generate',
'--pcre2-version',
'--type-list',
'-V', '--version',
'-h', '--help'
}
7 changes: 4 additions & 3 deletions lua/grug-far/rg/fetchFilesWithMatches.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ local fetchWithRg = require('grug-far/rg/fetchWithRg')
local function fetchFilesWithMatches(params)
local filesWithMatches = {}

local args = getArgs(params.inputs, params.options, {
'--files-with-matches'
local args, blacklistedArgs = getArgs(params.inputs, params.options, {
'--files-with-matches',
'--color=never',
}, blacklistedReplaceFlags)

return fetchWithRg({
Expand All @@ -21,7 +22,7 @@ local function fetchFilesWithMatches(params)
params.on_fetch_chunk(lines)
end,
on_finish = function(status, errorMessage)
params.on_finish(status, errorMessage, filesWithMatches)
params.on_finish(status, errorMessage, filesWithMatches, blacklistedArgs)
end
})
end
Expand Down
30 changes: 16 additions & 14 deletions lua/grug-far/rg/getArgs.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
local function isProperFlag(arg)
return vim.startswith(arg, '-') and arg ~= '--'
end
-- TODO (sbadragan): check that sigkill actually works

local function isBlacklistedFlag(flag, blacklistedFlags)
if not blacklistedFlags then
Expand All @@ -25,18 +23,14 @@ local function getArgs(inputs, options, extraArgs, blacklistedFlags)

args = {}

-- user overridable args
table.insert(args, '--line-number')
table.insert(args, '--column')

-- user overrides
local blacklisted = {}
local extraUserArgs = options.extraRgArgs and vim.trim(options.extraRgArgs) or ''
if #extraUserArgs > 0 then
for arg in string.gmatch(extraUserArgs, "%S+") do
if isBlacklistedFlag(arg, blacklistedFlags) then
return nil
end
if isProperFlag(arg) then
table.insert(blacklisted, arg)
else
table.insert(args, arg)
end
end
Expand All @@ -45,16 +39,24 @@ local function getArgs(inputs, options, extraArgs, blacklistedFlags)
if #inputs.flags > 0 then
for flag in string.gmatch(inputs.flags, "%S+") do
if isBlacklistedFlag(flag, blacklistedFlags) then
return nil
end
if isProperFlag(flag) then
table.insert(blacklisted, flag)
else
table.insert(args, flag)
end
end
end

if #blacklisted > 0 then
return nil, blacklisted
end

-- required args
table.insert(args, '--line-number')
table.insert(args, '--heading')
table.insert(args, '--column')
table.insert(args, '--field-match-separator=:')
table.insert(args, '--hyperlink-format=none')
table.insert(args, '--block-buffered')

if #inputs.replacement > 0 then
table.insert(args, '--replace=' .. inputs.replacement)
Expand All @@ -70,7 +72,7 @@ local function getArgs(inputs, options, extraArgs, blacklistedFlags)

table.insert(args, '--regexp=' .. inputs.search)

return args
return args, nil
end

return getArgs

0 comments on commit 35af0ee

Please sign in to comment.