Skip to content

Commit

Permalink
fix behaviour with multiline vs singleline snippets and their final c…
Browse files Browse the repository at this point in the history
…ursor position, #11742
  • Loading branch information
jrieken committed Sep 9, 2016
1 parent e1b6d7d commit 46c8689
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/vs/editor/contrib/snippet/common/snippetController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,20 +484,21 @@ export class SnippetController {
const edits: editorCommon.IIdentifiedSingleEditOperation[] = [];
const selections = this._editor.getSelections();
let lineDelta = 0;
let columnDelta = 0;
let columnDeltaOrValue = 0;

for (let i = 0; i < selections.length; i++) {
let {adaptedSnippet, typeRange} = SnippetController._prepareSnippet(this._editor, selections[i], snippet, overwriteBefore, overwriteAfter, stripPrefix);
SnippetController._addCommandForSnippet(this._editor.getModel(), adaptedSnippet, typeRange, edits);

if (i === 0 && snippet.isSingleTabstopOnly) {
const finalCursorPos = SnippetController._getSnippetCursorOnly(adaptedSnippet);
const editEnd = typeRange.getEndPosition();
editEnd.lineNumber += adaptedSnippet.lines.length - 1;
editEnd.column = adaptedSnippet.lines[adaptedSnippet.lines.length - 1].length + 1;

lineDelta = finalCursorPos.lineNumber - editEnd.lineNumber;
columnDelta = finalCursorPos.column - editEnd.column;
lineDelta = finalCursorPos.lineNumber - typeRange.startLineNumber;
if (lineDelta === 0) {
columnDeltaOrValue = finalCursorPos.column - typeRange.startColumn;
} else {
columnDeltaOrValue = finalCursorPos.column;
}
}
}

Expand All @@ -509,11 +510,15 @@ export class SnippetController {

return inverseEdits.map((edit, i) => {

let {endLineNumber, endColumn} = edit.range;
endLineNumber += lineDelta;
endColumn += columnDelta;
let {startLineNumber, startColumn} = edit.range;
if (lineDelta === 0) {
startColumn += columnDeltaOrValue;
} else {
startLineNumber += lineDelta;
startColumn += columnDeltaOrValue;
}

return new Selection(endLineNumber, endColumn, endLineNumber, endColumn);
return new Selection(startLineNumber, startColumn, startLineNumber, startColumn);
});
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,5 +281,18 @@ suite('SnippetController', () => {
assert.ok(first.equalsRange({ startLineNumber: 2, startColumn: 1, endLineNumber: 2, endColumn: 1 }), first.toString());
assert.ok(second.equalsRange({ startLineNumber: 4, startColumn: 1, endLineNumber: 4, endColumn: 1 }), second.toString());
});

snippetTest((editor, cursor, codeSnippet, snippetController) => {
editor.setSelections([
new Selection(2, 7, 2, 7)
]);

codeSnippet = CodeSnippet.fromInternal('xx{{}}yy');
snippetController.run(codeSnippet, 1, 0, false);

assert.equal(editor.getSelections().length, 1);
const [first] = editor.getSelections();
assert.ok(first.equalsRange({ startLineNumber: 2, startColumn: 8, endLineNumber: 2, endColumn: 8 }), first.toString());
});
});
});

0 comments on commit 46c8689

Please sign in to comment.