Skip to content

Commit 16f30d9

Browse files
authored
stricter definition for what a trival snippet it (microsoft#165355)
fixes microsoft#163808
1 parent 46face3 commit 16f30d9

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

src/vs/editor/contrib/snippet/browser/snippetParser.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,13 @@ export abstract class Marker {
165165
return this._children;
166166
}
167167

168+
get rightMostDescendant(): Marker {
169+
if (this._children.length > 0) {
170+
return this._children[this._children.length - 1].rightMostDescendant;
171+
}
172+
return this;
173+
}
174+
168175
get snippet(): TextmateSnippet | undefined {
169176
let candidate: Marker = this;
170177
while (true) {

src/vs/editor/contrib/snippet/browser/snippetSession.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,23 @@ export class OneSnippet {
208208
return this._snippet.placeholders.length > 0;
209209
}
210210

211+
/**
212+
* A snippet is trivial when it has no placeholder or only a final placeholder at
213+
* its very end
214+
*/
211215
get isTrivialSnippet(): boolean {
212-
return this._snippet.placeholders.length === 0
213-
|| (this._snippet.placeholders.length === 1 && this._snippet.placeholders[0].isFinalTabstop);
216+
if (this._snippet.placeholders.length === 0) {
217+
return true;
218+
}
219+
if (this._snippet.placeholders.length === 1) {
220+
const [placeholder] = this._snippet.placeholders;
221+
if (placeholder.isFinalTabstop) {
222+
if (this._snippet.rightMostDescendant === placeholder) {
223+
return true;
224+
}
225+
}
226+
}
227+
return false;
214228
}
215229

216230
computePossibleSelections() {

src/vs/editor/contrib/snippet/test/browser/snippetController2.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,4 +692,18 @@ suite('SnippetController2', function () {
692692
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)]);
693693
});
694694
});
695+
696+
test('Bug: cursor position $0 with user snippets #163808', function () {
697+
698+
const ctrl = instaService.createInstance(SnippetController2, editor);
699+
model.setValue('');
700+
701+
ctrl.insert('<Element1 Attr1="foo" $1>\n <Element2 Attr1="$2"/>\n$0"\n</Element1>');
702+
assert.deepStrictEqual(editor.getSelections(), [new Selection(1, 23, 1, 23)]);
703+
704+
ctrl.insert('Qualifier="$0"');
705+
assert.strictEqual(model.getValue(), '<Element1 Attr1="foo" Qualifier="">\n <Element2 Attr1=""/>\n"\n</Element1>');
706+
assert.deepStrictEqual(editor.getSelections(), [new Selection(1, 34, 1, 34)]);
707+
708+
});
695709
});

0 commit comments

Comments
 (0)