Skip to content

Commit 1894d2e

Browse files
committed
experiment: support tabstops after each case clause
1 parent 89f6f6b commit 1894d2e

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

src/compiler/utilities.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ import {
9090
TypePredicate, TypePredicateKind, TypeReferenceNode, unescapeLeadingUnderscores, UnionOrIntersectionTypeNode,
9191
ValidImportTypeNode, VariableDeclaration, VariableDeclarationInitializedTo, VariableDeclarationList,
9292
VariableLikeDeclaration, VariableStatement, version, WhileStatement, WithStatement, WriteFileCallback,
93-
WriteFileCallbackData, YieldExpression, ResolutionMode, isIdentifierStart,
93+
WriteFileCallbackData, YieldExpression, ResolutionMode, isIdentifierStart, getSnippetElement, SnippetKind,
9494
} from "./_namespaces/ts";
9595

9696
/** @internal */
@@ -8691,3 +8691,8 @@ export function canUsePropertyAccess(name: string, languageVersion: ScriptTarget
86918691
name.length > 1 && isIdentifierStart(name.charCodeAt(1), languageVersion) :
86928692
isIdentifierStart(firstChar, languageVersion);
86938693
}
8694+
8695+
/** @internal */
8696+
export function hasTabstop(node: Node): boolean {
8697+
return getSnippetElement(node)?.kind === SnippetKind.TabStop;
8698+
}

src/services/completions.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,12 @@ function getExhaustiveCaseSnippets(
732732
return undefined;
733733
}
734734

735-
const newClauses = map(elements, element => {
735+
const newClauses = map(elements, (element, i) => {
736+
if (preferences.includeCompletionsWithSnippetText) {
737+
const tabstopStmt = factory.createEmptyStatement();
738+
setSnippetElement(tabstopStmt, { kind: SnippetKind.TabStop, order: i + 1 });
739+
return factory.createCaseClause(element, [tabstopStmt]);
740+
}
736741
return factory.createCaseClause(element, []);
737742
});
738743
const printer = createSnippetPrinter({
@@ -752,7 +757,7 @@ function getExhaustiveCaseSnippets(
752757
factory.createNodeArray(newClauses),
753758
sourceFile);
754759

755-
const firstClause = printer.printSnippetList(ListFormat.SingleLine, factory.createNodeArray([first(newClauses)!]), sourceFile);
760+
const firstClause = printer.printSnippetList(ListFormat.SingleLine, factory.createNodeArray([factory.createCaseClause(first(elements), [])]), sourceFile);
756761
return {
757762
entry: {
758763
name: `${firstClause} ...`,
@@ -761,6 +766,7 @@ function getExhaustiveCaseSnippets(
761766
insertText,
762767
hasAction: importAdder.hasFixes() || undefined,
763768
source: CompletionSource.SwitchCases,
769+
isSnippet: preferences.includeCompletionsWithSnippetText ? true : undefined,
764770
},
765771
importAdder,
766772
};

src/services/services.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ import {
5454
TextRange, TextSpan, textSpanEnd, timestamp, TodoComment, TodoCommentDescriptor, Token, toPath, tracing,
5555
TransformFlags, TransientSymbol, Type, TypeChecker, TypeFlags, TypeNode, TypeParameter, TypePredicate,
5656
TypeReference, typeToDisplayParts, UnderscoreEscapedMap, UnionOrIntersectionType, UnionType, updateSourceFile,
57-
UserPreferences, VariableDeclaration,
57+
UserPreferences, VariableDeclaration, hasTabstop,
5858
} from "./_namespaces/ts";
5959

6060
/** The version of the language service API */
@@ -234,6 +234,9 @@ function addSyntheticNodes(nodes: Push<Node>, pos: number, end: number, parent:
234234
const textPos = scanner.getTextPos();
235235
if (textPos <= end) {
236236
if (token === SyntaxKind.Identifier) {
237+
if (hasTabstop(parent)) {
238+
continue;
239+
}
237240
Debug.fail(`Did not expect ${Debug.formatSyntaxKind(parent.kind)} to have an Identifier in its trivia`);
238241
}
239242
nodes.push(createNode(token, pos, textPos, parent));

0 commit comments

Comments
 (0)