Skip to content

Commit

Permalink
Improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
johnfn committed Oct 14, 2016
1 parent bdfffb2 commit 4122987
Showing 1 changed file with 70 additions and 68 deletions.
138 changes: 70 additions & 68 deletions src/mode/modeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1035,84 +1035,86 @@ export class ModeHandler implements vscode.Disposable {

let accumulatedPositionDifferences: { [key: number]: PositionDiff[] } = {};

if (areAnyTransformationsOverlapping(textTransformations)) {
// TODO: Select one transformation for every cursor and run them all
// in parallel. Repeat till there are no more transformations.
if (textTransformations.length > 0) {
if (areAnyTransformationsOverlapping(textTransformations)) {
// TODO: Select one transformation for every cursor and run them all
// in parallel. Repeat till there are no more transformations.

for (const command of textTransformations) {
await vscode.window.activeTextEditor.edit(edit => {
switch (command.type) {
case "insertText":
edit.insert(command.position, command.text);
break;

case "replaceText":
edit.replace(new vscode.Selection(command.end, command.start), command.text);
break;

case "deleteText":
edit.delete(new vscode.Range(command.position, command.position.getLeftThroughLineBreaks()));
break;

case "deleteRange":
edit.delete(new vscode.Selection(command.range.start, command.range.stop));
break;
}

if (command.cursorIndex === undefined) {
throw new Error("No cursor index - this should never ever happen!");
}
for (const command of textTransformations) {
await vscode.window.activeTextEditor.edit(edit => {
switch (command.type) {
case "insertText":
edit.insert(command.position, command.text);
break;

case "replaceText":
edit.replace(new vscode.Selection(command.end, command.start), command.text);
break;

case "deleteText":
edit.delete(new vscode.Range(command.position, command.position.getLeftThroughLineBreaks()));
break;

case "deleteRange":
edit.delete(new vscode.Selection(command.range.start, command.range.stop));
break;
}

if (command.diff) {
if (!accumulatedPositionDifferences[command.cursorIndex]) {
accumulatedPositionDifferences[command.cursorIndex] = [];
if (command.cursorIndex === undefined) {
throw new Error("No cursor index - this should never ever happen!");
}

accumulatedPositionDifferences[command.cursorIndex].push(command.diff);
}
});
};
} else {
// This is the common case!
if (command.diff) {
if (!accumulatedPositionDifferences[command.cursorIndex]) {
accumulatedPositionDifferences[command.cursorIndex] = [];
}

/**
* batch all text operations together as a single operation
* (this is primarily necessary for multi-cursor mode, since most
* actions will trigger at most one text operation).
*/
await vscode.window.activeTextEditor.edit(edit => {
for (const command of textTransformations) {
switch (command.type) {
case "insertText":
edit.insert(command.position, command.text);
break;

case "replaceText":
edit.replace(new vscode.Selection(command.end, command.start), command.text);
break;

case "deleteText":
edit.delete(new vscode.Range(command.position, command.position.getLeftThroughLineBreaks()));
break;

case "deleteRange":
edit.delete(new vscode.Selection(command.range.start, command.range.stop));
break;
}
accumulatedPositionDifferences[command.cursorIndex].push(command.diff);
}
});
};
} else {
// This is the common case!

if (command.cursorIndex === undefined) {
throw new Error("No cursor index - this should never ever happen!");
}
/**
* batch all text operations together as a single operation
* (this is primarily necessary for multi-cursor mode, since most
* actions will trigger at most one text operation).
*/
await vscode.window.activeTextEditor.edit(edit => {
for (const command of textTransformations) {
switch (command.type) {
case "insertText":
edit.insert(command.position, command.text);
break;

case "replaceText":
edit.replace(new vscode.Selection(command.end, command.start), command.text);
break;

case "deleteText":
edit.delete(new vscode.Range(command.position, command.position.getLeftThroughLineBreaks()));
break;

case "deleteRange":
edit.delete(new vscode.Selection(command.range.start, command.range.stop));
break;
}

if (command.diff) {
if (!accumulatedPositionDifferences[command.cursorIndex]) {
accumulatedPositionDifferences[command.cursorIndex] = [];
if (command.cursorIndex === undefined) {
throw new Error("No cursor index - this should never ever happen!");
}

accumulatedPositionDifferences[command.cursorIndex].push(command.diff);
if (command.diff) {
if (!accumulatedPositionDifferences[command.cursorIndex]) {
accumulatedPositionDifferences[command.cursorIndex] = [];
}

accumulatedPositionDifferences[command.cursorIndex].push(command.diff);
}
}
}
});
});
}
}

for (const command of otherTransformations) {
Expand Down

0 comments on commit 4122987

Please sign in to comment.