diff --git a/lua/grug-far/actions/replace.lua b/lua/grug-far/actions/replace.lua index a7c6914d..3f37c6bd 100644 --- a/lua/grug-far/actions/replace.lua +++ b/lua/grug-far/actions/replace.lua @@ -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) diff --git a/lua/grug-far/rg/blacklistedReplaceFlags.lua b/lua/grug-far/rg/blacklistedReplaceFlags.lua index 912f3adc..fc308e4e 100644 --- a/lua/grug-far/rg/blacklistedReplaceFlags.lua +++ b/lua/grug-far/rg/blacklistedReplaceFlags.lua @@ -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' } diff --git a/lua/grug-far/rg/fetchFilesWithMatches.lua b/lua/grug-far/rg/fetchFilesWithMatches.lua index e1b144f5..d1d7da3a 100644 --- a/lua/grug-far/rg/fetchFilesWithMatches.lua +++ b/lua/grug-far/rg/fetchFilesWithMatches.lua @@ -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({ @@ -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 diff --git a/lua/grug-far/rg/getArgs.lua b/lua/grug-far/rg/getArgs.lua index a98f2cfe..940c3695 100644 --- a/lua/grug-far/rg/getArgs.lua +++ b/lua/grug-far/rg/getArgs.lua @@ -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 @@ -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 @@ -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) @@ -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