PR comments and minor bugs

This commit is contained in:
Armando Aguirre 2020-07-10 17:44:02 -07:00
parent a534f2aa97
commit 0d38f09e36
5 changed files with 52 additions and 8 deletions

View file

@ -2068,6 +2068,7 @@ namespace ts {
const textChanges: TextChange[] = [];
const { text } = sourceFile;
let hasComment = false;
let isCommenting = insertComment || false;
const positions = [] as number[] as SortedArray<number>;
@ -2098,6 +2099,7 @@ namespace ts {
positions.push(commentRange.end);
}
hasComment = true;
pos = commentRange.end + 1;
}
else { // If it's not in a comment range, then we need to comment the uncommented portions.
@ -2110,7 +2112,9 @@ namespace ts {
}
}
if (isCommenting) {
// If it didn't found a comment and isCommenting is false means is only empty space.
// We want to insert comment in this scenario.
if (isCommenting || !hasComment) {
if (isInComment(sourceFile, textRange.pos)?.kind !== SyntaxKind.SingleLineCommentTrivia) {
insertSorted(positions, textRange.pos, compareValues);
}

View file

@ -1321,7 +1321,7 @@ namespace ts {
}
export function isInsideJsxElement(sourceFile: SourceFile, position: number): boolean {
function isInsideJsxElementRecursion(node: Node): boolean {
function isInsideJsxElementTraversal(node: Node): boolean {
while (node) {
if (node.kind >= SyntaxKind.JsxSelfClosingElement && node.kind <= SyntaxKind.JsxExpression
|| node.kind === SyntaxKind.JsxText
@ -1334,7 +1334,9 @@ namespace ts {
node = node.parent;
}
else if (node.kind === SyntaxKind.JsxElement) {
return position > node.getStart(sourceFile) || isInsideJsxElementRecursion(node.parent);
if (position > node.getStart(sourceFile)) return true;
node = node.parent;
}
else {
return false;
@ -1344,7 +1346,7 @@ namespace ts {
return false;
}
return isInsideJsxElementRecursion(getTokenAtPosition(sourceFile, position));
return isInsideJsxElementTraversal(getTokenAtPosition(sourceFile, position));
}
export function findPrecedingMatchingToken(token: Node, matchingTokenKind: SyntaxKind, sourceFile: SourceFile) {
@ -2279,8 +2281,8 @@ namespace ts {
// This only happens for leaf nodes - internal nodes always see their children change.
const clone =
isStringLiteral(node) ? setOriginalNode(factory.createStringLiteralFromNode(node), node) as Node as T :
isNumericLiteral(node) ? setOriginalNode(factory.createNumericLiteral(node.text, node.numericLiteralFlags), node) as Node as T :
factory.cloneNode(node);
isNumericLiteral(node) ? setOriginalNode(factory.createNumericLiteral(node.text, node.numericLiteralFlags), node) as Node as T :
factory.cloneNode(node);
return setTextRange(clone, node);
}

View file

@ -11,6 +11,8 @@
//// [|/*let var7 = 1;
//// let var8 = 2;
//// let var9 = 3;*/|]
////
//// let var10[||] = 10;
verify.toggleMultilineComment(
`let var1/* = 1;
@ -23,4 +25,6 @@ let var6 = 3;
let var7 = 1;
let var8 = 2;
let var9 = 3;`);
let var9 = 3;
let var10/**/ = 10;`);

View file

@ -0,0 +1,30 @@
// When there's is only whitespace, insert comment. If there is whitespace but theres a comment in bewteen, then uncomment.
//// /*let var1[| = 1;*/
//// |]
////
//// [|
//// /*let var2 = 2;*/|]
////
//// [|
////
//// |]
////
//// [||]
////
//// let var3[||] = 3;
verify.toggleMultilineComment(
`let var1 = 1;
let var2 = 2;
/*
*/
/**/
let var3/**/ = 3;`);

View file

@ -17,6 +17,8 @@
//// let var11[||]/* = 1;
//// let var12 = 2;
//// let var13 */= 3;
////
//// ////let var14 [||]= 14;
verify.uncommentSelection(
`let var1 = 1;
@ -35,4 +37,6 @@ let var10 = 3;
let var11 = 1;
let var12 = 2;
let var13 = 3;`);
let var13 = 3;
//let var14 = 14;`);