Skip to content

Commit

Permalink
Merge pull request #263 from eric-wieser/fix-jumpy-selections
Browse files Browse the repository at this point in the history
Do not move the selection if abbreviation substitution fails
  • Loading branch information
gebner authored May 10, 2021
2 parents 26f3c7f + 8c318c4 commit 462a528
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/abbreviation/rewriter/AbbreviationRewriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,9 @@ export class AbbreviationRewriter {

// We don't want replaced symbols (e.g. "\") to trigger abbreviations.
this.dontTrackNewAbbr = true;
let ok = false;
try {
await this.textEditor.edit((builder) => {
ok = await this.textEditor.edit((builder) => {
for (const r of replacements) {
builder.replace(
toVsCodeRange(r.range, this.textEditor.document),
Expand All @@ -170,10 +171,17 @@ export class AbbreviationRewriter {
}
this.dontTrackNewAbbr = false;

this.textEditor.selections = newSelections.map((s) => {
const vr = toVsCodeRange(s, this.textEditor.document);
return new vscode.Selection(vr.start, vr.end);
});
if (ok) {
this.textEditor.selections = newSelections.map((s) => {
const vr = toVsCodeRange(s, this.textEditor.document);
return new vscode.Selection(vr.start, vr.end);
});
}
else {
// Our edit did not succeed, do not update the selections.
// This can happen if `waitForNextTick` waits too long.
console.warn('Unable to replace abbreviation');
}

this.updateState();
}
Expand Down

0 comments on commit 462a528

Please sign in to comment.