Skip to content

Commit

Permalink
Fixed #5454: modified "replace-all" to save changes without opening e…
Browse files Browse the repository at this point in the history
…ditors

- In `search-in-workspace` widget, doing the "replace-all" used to open all the editor files in dirty state.
- With this patch, "replace-all" will do all the text replacement, and simply save the changes in the editors without opening them.

Signed-off-by: fangnx <[email protected]>
  • Loading branch information
fangnx authored and fangnx committed Jun 28, 2019
1 parent 9c74cd1 commit 1593f87
Showing 1 changed file with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ export class SearchInWorkspaceResultTreeWidget extends TreeWidget {
const needConfirm = !SearchInWorkspaceFileNode.is(node) && !SearchInWorkspaceResultLineNode.is(node);
if (!needConfirm || await this.confirmReplaceAll(this.getResultCount(replaceForNode), this.getFileCount(replaceForNode))) {
(node ? [node] : Array.from(this.resultTree.values())).forEach(n => {
this.replaceResult(n);
this.replaceResult(n, !!node);
this.removeNode(n);
});
}
Expand All @@ -461,10 +461,15 @@ export class SearchInWorkspaceResultTreeWidget extends TreeWidget {
rightPositionedNodes.map(r => r.character += diff);
}

protected async replaceResult(node: TreeNode) {
/**
* Replace text either in all search matches under a node or in all search matches, and save the changes.
* @param node - node in the tree widget in which the "replace all" is performed.
* @param {boolean} replaceOne - whether the function is to replace all matches under a node. If it is false, replace all.
*/
protected async replaceResult(node: TreeNode, replaceOne: boolean): Promise<void> {
const toReplace: SearchInWorkspaceResultLineNode[] = [];
if (SearchInWorkspaceRootFolderNode.is(node)) {
node.children.forEach(fileNode => this.replaceResult(fileNode));
node.children.forEach(fileNode => this.replaceResult(fileNode, replaceOne));
} else if (SearchInWorkspaceFileNode.is(node)) {
toReplace.push(...node.children);
} else if (SearchInWorkspaceResultLineNode.is(node)) {
Expand All @@ -473,8 +478,9 @@ export class SearchInWorkspaceResultTreeWidget extends TreeWidget {
}

if (toReplace.length > 0) {
const widget = await this.doOpen(toReplace[0]);
const source = widget.editor.document.getText();
// Open the file only if the function is called to replace all matches under a specific node.
const widget: EditorWidget = replaceOne ? await this.doOpen(toReplace[0]) : await this.doGetWidget(toReplace[0]);
const source: string = widget.editor.document.getText();
const replaceOperations = toReplace.map(resultLineNode => ({
text: this._replaceTerm,
range: {
Expand All @@ -488,10 +494,13 @@ export class SearchInWorkspaceResultTreeWidget extends TreeWidget {
}
}
} as ReplaceOperation));
// Replace the text.
await widget.editor.replaceText({
source,
replaceOperations
});
// Save the text replacement changes in the editor.
await widget.saveable.save();
}
}

Expand Down Expand Up @@ -614,6 +623,17 @@ export class SearchInWorkspaceResultTreeWidget extends TreeWidget {
</React.Fragment>;
}

/**
* Get the editor widget by the node.
* @param {SearchInWorkspaceResultLineNode} node - the node representing a match in the search results.
* @returns The editor widget to which the text replace will be done.
*/
protected async doGetWidget(node: SearchInWorkspaceResultLineNode): Promise<EditorWidget> {
const fileUri = new URI(node.fileUri);
const editorWidget = await this.editorManager.getOrCreateByUri(fileUri);
return editorWidget;
}

protected async doOpen(node: SearchInWorkspaceResultLineNode, preview: boolean = false): Promise<EditorWidget> {
let fileUri: URI;
const resultNode = node.parent;
Expand Down

0 comments on commit 1593f87

Please sign in to comment.