Skip to content

Commit

Permalink
made replace parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan Badragan committed May 7, 2024
1 parent 9749418 commit 18c9be7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
42 changes: 24 additions & 18 deletions lua/grug-far/actions/replace.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local fetchFilesWithMatches = require('grug-far/rg/fetchFilesWithMatches')
local fetchReplacedFileContent = require('grug-far/rg/fetchReplacedFileContent')
local renderResultsHeader = require('grug-far/render/resultsHeader')
local resultsList = require('grug-far/render/resultsList')
local uv = vim.loop

local function replaceInFile(params)
local context = params.context
Expand Down Expand Up @@ -41,48 +42,51 @@ end

local function replaceInMatchedFiles(params)
local context = params.context
local files = params.files
local files = vim.deepcopy(params.files)
local reportProgress = params.reportProgress
local on_finish = params.on_finish
local engagedWorkers = 0
local errorMessages = ''

if #files == 0 then
on_finish('success')
end
local function replaceNextFile()
local file = table.remove(files)
if file == nil then
if engagedWorkers == 0 then
on_finish(#errorMessages > 0 and 'error' or 'success', errorMessages)
end
return
end

-- TODO (sbadragan): make it do multiple in parallel
local function replaceInFileAtIndex(index)
engagedWorkers = engagedWorkers + 1
replaceInFile({
file = files[index],
file = file,
context = context,
on_done = vim.schedule_wrap(function(err)
if err then
-- optimistically try to continue
errorMessages = errorMessages .. '\n' .. err
end

reportProgress(index)

if (index < #files) then
replaceInFileAtIndex(index + 1)
else
on_finish(#errorMessages > 0 and 'error' or 'success', errorMessages)
end
reportProgress()
engagedWorkers = engagedWorkers - 1
replaceNextFile()
end)
})
end

replaceInFileAtIndex(1)
for _ = 1, context.options.maxWorkers do
replaceNextFile()
end
end

local function getActionMessage(err, count, total)
local function getActionMessage(err, count, total, time)
local msg = 'applying replacements'
if err then
return msg .. ' failed!'
end

if count == total and total ~= 0 then
return msg .. ' completed!'
return msg .. ' completed in ' .. time .. 'ms!'
end

return msg .. ' ' .. count .. ' / ' .. total .. ' (buffer temporarily not modifiable)'
Expand All @@ -94,6 +98,7 @@ local function replace(params)
local state = context.state
local filesCount = 0
local filesTotal = 0
local startTime = uv.now()

-- initiate replace in UI
vim.schedule(function()
Expand Down Expand Up @@ -140,8 +145,9 @@ local function replace(params)

state.status = status
state.progressCount = nil
local time = uv.now() - startTime
-- not passing in total as 3rd arg cause of paranoia if counts don't end up matching
state.actionMessage = getActionMessage(nil, filesCount, filesCount)
state.actionMessage = getActionMessage(nil, filesCount, filesCount, time)
renderResultsHeader(buf, context)
end)

Expand Down
3 changes: 3 additions & 0 deletions lua/grug-far/opts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ local defaultOptions = {
resultsActionMessage = ''
},

-- max number of parallel replacements tasks
maxWorkers = 4,

keymaps = {
replace = '<c-enter>',
}
Expand Down

0 comments on commit 18c9be7

Please sign in to comment.