Skip to content

Commit

Permalink
stricter definition for what a trival snippet it (#165355)
Browse files Browse the repository at this point in the history
fixes #163808
  • Loading branch information
jrieken authored Nov 3, 2022
1 parent 46face3 commit 16f30d9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/vs/editor/contrib/snippet/browser/snippetParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,13 @@ export abstract class Marker {
return this._children;
}

get rightMostDescendant(): Marker {
if (this._children.length > 0) {
return this._children[this._children.length - 1].rightMostDescendant;
}
return this;
}

get snippet(): TextmateSnippet | undefined {
let candidate: Marker = this;
while (true) {
Expand Down
18 changes: 16 additions & 2 deletions src/vs/editor/contrib/snippet/browser/snippetSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,23 @@ export class OneSnippet {
return this._snippet.placeholders.length > 0;
}

/**
* A snippet is trivial when it has no placeholder or only a final placeholder at
* its very end
*/
get isTrivialSnippet(): boolean {
return this._snippet.placeholders.length === 0
|| (this._snippet.placeholders.length === 1 && this._snippet.placeholders[0].isFinalTabstop);
if (this._snippet.placeholders.length === 0) {
return true;
}
if (this._snippet.placeholders.length === 1) {
const [placeholder] = this._snippet.placeholders;
if (placeholder.isFinalTabstop) {
if (this._snippet.rightMostDescendant === placeholder) {
return true;
}
}
}
return false;
}

computePossibleSelections() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -692,4 +692,18 @@ suite('SnippetController2', function () {
assert.deepStrictEqual(editor.getSelections(), [new Selection(1, 5, 1, 5), new Selection(1, 10, 1, 10), new Selection(2, 5, 2, 5), new Selection(2, 10, 2, 10)]);
});
});

test('Bug: cursor position $0 with user snippets #163808', function () {

const ctrl = instaService.createInstance(SnippetController2, editor);
model.setValue('');

ctrl.insert('<Element1 Attr1="foo" $1>\n <Element2 Attr1="$2"/>\n$0"\n</Element1>');
assert.deepStrictEqual(editor.getSelections(), [new Selection(1, 23, 1, 23)]);

ctrl.insert('Qualifier="$0"');
assert.strictEqual(model.getValue(), '<Element1 Attr1="foo" Qualifier="">\n <Element2 Attr1=""/>\n"\n</Element1>');
assert.deepStrictEqual(editor.getSelections(), [new Selection(1, 34, 1, 34)]);

});
});

0 comments on commit 16f30d9

Please sign in to comment.