Skip to content

Commit

Permalink
experiment: support tabstops after each case clause
Browse files Browse the repository at this point in the history
  • Loading branch information
gabritto committed Nov 22, 2022
1 parent 89f6f6b commit 1894d2e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ import {
TypePredicate, TypePredicateKind, TypeReferenceNode, unescapeLeadingUnderscores, UnionOrIntersectionTypeNode,
ValidImportTypeNode, VariableDeclaration, VariableDeclarationInitializedTo, VariableDeclarationList,
VariableLikeDeclaration, VariableStatement, version, WhileStatement, WithStatement, WriteFileCallback,
WriteFileCallbackData, YieldExpression, ResolutionMode, isIdentifierStart,
WriteFileCallbackData, YieldExpression, ResolutionMode, isIdentifierStart, getSnippetElement, SnippetKind,
} from "./_namespaces/ts";

/** @internal */
Expand Down Expand Up @@ -8691,3 +8691,8 @@ export function canUsePropertyAccess(name: string, languageVersion: ScriptTarget
name.length > 1 && isIdentifierStart(name.charCodeAt(1), languageVersion) :
isIdentifierStart(firstChar, languageVersion);
}

/** @internal */
export function hasTabstop(node: Node): boolean {
return getSnippetElement(node)?.kind === SnippetKind.TabStop;
}
10 changes: 8 additions & 2 deletions src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,12 @@ function getExhaustiveCaseSnippets(
return undefined;
}

const newClauses = map(elements, element => {
const newClauses = map(elements, (element, i) => {
if (preferences.includeCompletionsWithSnippetText) {
const tabstopStmt = factory.createEmptyStatement();
setSnippetElement(tabstopStmt, { kind: SnippetKind.TabStop, order: i + 1 });
return factory.createCaseClause(element, [tabstopStmt]);
}
return factory.createCaseClause(element, []);
});
const printer = createSnippetPrinter({
Expand All @@ -752,7 +757,7 @@ function getExhaustiveCaseSnippets(
factory.createNodeArray(newClauses),
sourceFile);

const firstClause = printer.printSnippetList(ListFormat.SingleLine, factory.createNodeArray([first(newClauses)!]), sourceFile);
const firstClause = printer.printSnippetList(ListFormat.SingleLine, factory.createNodeArray([factory.createCaseClause(first(elements), [])]), sourceFile);
return {
entry: {
name: `${firstClause} ...`,
Expand All @@ -761,6 +766,7 @@ function getExhaustiveCaseSnippets(
insertText,
hasAction: importAdder.hasFixes() || undefined,
source: CompletionSource.SwitchCases,
isSnippet: preferences.includeCompletionsWithSnippetText ? true : undefined,
},
importAdder,
};
Expand Down
5 changes: 4 additions & 1 deletion src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import {
TextRange, TextSpan, textSpanEnd, timestamp, TodoComment, TodoCommentDescriptor, Token, toPath, tracing,
TransformFlags, TransientSymbol, Type, TypeChecker, TypeFlags, TypeNode, TypeParameter, TypePredicate,
TypeReference, typeToDisplayParts, UnderscoreEscapedMap, UnionOrIntersectionType, UnionType, updateSourceFile,
UserPreferences, VariableDeclaration,
UserPreferences, VariableDeclaration, hasTabstop,
} from "./_namespaces/ts";

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

0 comments on commit 1894d2e

Please sign in to comment.