Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

improve replace performance take 2 (this time I have confirmed substantial improvements in the profiler) #1051

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions lib/buffer-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,27 @@ class BufferSearch {
replacePattern = escapeHelper.unescapeEscapeSequence(replacePattern);
}

const replacementChunks = [];
const text = this.editor.getText();
const buffer = this.editor.getBuffer();

let end = 0;

for (let i = 0, n = markers.length; i < n; i++) {
const marker = markers[i]
const marker = markers[i];
const bufferRange = marker.getBufferRange();
const replacementText = findRegex ?
this.editor.getTextInBufferRange(bufferRange).replace(findRegex, replacePattern) :
replacePattern;
this.editor.setTextInBufferRange(bufferRange, replacementText);
const start = buffer.characterIndexForPosition(bufferRange.start);
replacementChunks.push(text.substring(end, start));
end = buffer.characterIndexForPosition(bufferRange.end);
replacementChunks.push(findRegex ?
text.substring(start, end).replace(findRegex, replacePattern)
: replacePattern);

marker.destroy();
this.markers.splice(this.markers.indexOf(marker), 1);
}
replacementChunks.push(text.substring(end));
this.editor.setText(replacementChunks.join(''));
});

return this.emitter.emit('did-update', this.markers.slice());
Expand Down