feat(40233): add JS Doc types to smart selection (#40338)

This commit is contained in:
Alex T
2020-09-25 22:36:04 +03:00
committed by GitHub
parent dc8952d308
commit 8e86b24036
21 changed files with 426 additions and 10 deletions

View File

@@ -13,7 +13,7 @@ namespace ts.SmartSelectionRange {
const prevNode: Node | undefined = children[i - 1];
const node: Node = children[i];
const nextNode: Node | undefined = children[i + 1];
if (node.getStart(sourceFile) > pos) {
if (getTokenPosOfNode(node, sourceFile, /*includeJsDoc*/ true) > pos) {
break outer;
}
@@ -23,14 +23,14 @@ namespace ts.SmartSelectionRange {
// of things that should be considered independently.
// 3. A VariableStatements children are just a VaraiableDeclarationList and a semicolon.
// 4. A lone VariableDeclaration in a VaraibleDeclaration feels redundant with the VariableStatement.
//
// Dive in without pushing a selection range.
if (isBlock(node)
|| isTemplateSpan(node) || isTemplateHead(node) || isTemplateTail(node)
|| prevNode && isTemplateHead(prevNode)
|| isVariableDeclarationList(node) && isVariableStatement(parentNode)
|| isSyntaxList(node) && isVariableDeclarationList(parentNode)
|| isVariableDeclaration(node) && isSyntaxList(parentNode) && children.length === 1) {
|| isVariableDeclaration(node) && isSyntaxList(parentNode) && children.length === 1
|| isJSDocTypeExpression(node) || isJSDocSignature(node) || isJSDocTypeLiteral(node)) {
parentNode = node;
break;
}
@@ -44,16 +44,15 @@ namespace ts.SmartSelectionRange {
// Blocks with braces, brackets, parens, or JSX tags on separate lines should be
// selected from open to close, including whitespace but not including the braces/etc. themselves.
const isBetweenMultiLineBookends = isSyntaxList(node)
&& isListOpener(prevNode)
&& isListCloser(nextNode)
const isBetweenMultiLineBookends = isSyntaxList(node) && isListOpener(prevNode) && isListCloser(nextNode)
&& !positionsAreOnSameLine(prevNode.getStart(), nextNode.getStart(), sourceFile);
const jsDocCommentStart = hasJSDocNodes(node) && node.jsDoc![0].getStart();
const start = isBetweenMultiLineBookends ? prevNode.getEnd() : node.getStart();
const end = isBetweenMultiLineBookends ? nextNode.getStart() : node.getEnd();
if (isNumber(jsDocCommentStart)) {
pushSelectionRange(jsDocCommentStart, end);
const end = isBetweenMultiLineBookends ? nextNode.getStart() : getEndPos(sourceFile, node);
if (hasJSDocNodes(node) && node.jsDoc?.length) {
pushSelectionRange(first(node.jsDoc).getStart(), end);
}
pushSelectionRange(start, end);
// String literals should have a stop both inside and outside their quotes.
@@ -270,4 +269,17 @@ namespace ts.SmartSelectionRange {
|| kind === SyntaxKind.CloseParenToken
|| kind === SyntaxKind.JsxClosingElement;
}
function getEndPos(sourceFile: SourceFile, node: Node): number {
switch (node.kind) {
case SyntaxKind.JSDocParameterTag:
case SyntaxKind.JSDocCallbackTag:
case SyntaxKind.JSDocPropertyTag:
case SyntaxKind.JSDocTypedefTag:
case SyntaxKind.JSDocThisTag:
return sourceFile.getLineEndOfPosition(node.getStart());
default:
return node.getEnd();
}
}
}