Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stricter definition for what a trival snippet it #165355

Merged
merged 1 commit into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)]);

});
});