Skip to content

Commit

Permalink
apply suggested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
saschanaz committed Nov 9, 2018
1 parent 8ed21a8 commit 7995f91
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 91 deletions.
6 changes: 3 additions & 3 deletions src/services/formatting/formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace ts.formatting {
* the first token in line so it should be indented
*/
interface DynamicIndentation {
getIndentationForToken(tokenLine: number, tokenKind: SyntaxKind, container: Node, suppressDelta?: boolean): number;
getIndentationForToken(tokenLine: number, tokenKind: SyntaxKind, container: Node, suppressDelta: boolean): number;
getIndentationForComment(owningToken: SyntaxKind, tokenIndentation: number, container: Node): number;
/**
* Indentation for open and close tokens of the node if it is block or another node that needs special indentation
Expand Down Expand Up @@ -735,7 +735,6 @@ namespace ts.formatting {

let listDynamicIndentation = parentDynamicIndentation;
let startLine = parentStartLine;
let indentationOnListStartToken = parentDynamicIndentation.getIndentation();

if (listStartToken !== SyntaxKind.Unknown) {
// introduce a new indentation scope for lists (including list start and end tokens)
Expand All @@ -751,6 +750,7 @@ namespace ts.formatting {

consumeTokenAndAdvanceScanner(tokenInfo, parent, parentDynamicIndentation, parent);

let indentationOnListStartToken: number;
if (indentationOnLastIndentedLine !== Constants.Unknown) {
// scanner just processed list start token so consider last indentation as list indentation
// function foo(): { // last indentation was 0, list item will be indented based on this value
Expand Down Expand Up @@ -835,7 +835,7 @@ namespace ts.formatting {

if (indentToken) {
const tokenIndentation = (isTokenInRange && !rangeContainsError(currentTokenInfo.token)) ?
dynamicIndentation.getIndentationForToken(tokenStart.line, currentTokenInfo.token.kind, container, isListEndToken) :
dynamicIndentation.getIndentationForToken(tokenStart.line, currentTokenInfo.token.kind, container, !!isListEndToken) :
Constants.Unknown;

let indentNextTokenOrTrivia = true;
Expand Down
36 changes: 18 additions & 18 deletions src/services/formatting/smartIndenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ namespace ts.formatting {
}
}

const containerList = getListByPosition(position, precedingToken.parent);
const containerList = getListByPosition(position, precedingToken.parent, sourceFile);
// use list position if the preceding token is before any list items
if (containerList && !rangeContainsRange(containerList, precedingToken)) {
return getActualIndentationForListStartLine(containerList, sourceFile, options) + options.indentSize!; // TODO: GH#18217
Expand Down Expand Up @@ -322,15 +322,15 @@ namespace ts.formatting {
return false;
}

function getListIfVisualStartEndIsInListRange(list: NodeArray<Node> | undefined, start: number, end: number, node: Node) {
function getListIfVisualStartEndIsInListRange(list: NodeArray<Node> | undefined, start: number, end: number, node: Node, sourceFile: SourceFile) {
return list && rangeContainsVisualStartEnd(list) ? list : undefined;

// Assumes a list is wrapped by list tokens
function rangeContainsVisualStartEnd(textRange: TextRange): boolean {
const children = node.getChildren();
for (let i = 1; i < children.length - 1; i++) {
if (children[i].pos === textRange.pos && children[i].end === textRange.end) {
return rangeContainsStartEnd({ pos: children[i - 1].end, end: children[i + 1].end - children[i + 1].getWidth() }, start, end);
return rangeContainsStartEnd({ pos: children[i - 1].end, end: children[i + 1].getStart(sourceFile) }, start, end);
}
}
return rangeContainsStartEnd(textRange, start, end);
Expand All @@ -343,28 +343,28 @@ namespace ts.formatting {

export function getContainingList(node: Node, sourceFile: SourceFile): NodeArray<Node> | undefined {
if (node.parent) {
return getListByRange(node.getStart(sourceFile), node.getEnd(), node.parent);
return getListByRange(node.getStart(sourceFile), node.getEnd(), node.parent, sourceFile);
}
return undefined;
}

function getListByPosition(pos: number, node: Node): NodeArray<Node> | undefined {
function getListByPosition(pos: number, node: Node, sourceFile: SourceFile): NodeArray<Node> | undefined {
if (!node) {
return;
}
return getListByRange(pos, pos, node);
return getListByRange(pos, pos, node, sourceFile);
}

function getListByRange(start: number, end: number, node: Node): NodeArray<Node> | undefined {
function getListByRange(start: number, end: number, node: Node, sourceFile: SourceFile): NodeArray<Node> | undefined {
switch (node.kind) {
case SyntaxKind.TypeReference:
return getListIfVisualStartEndIsInListRange((<TypeReferenceNode>node).typeArguments, start, end, node);
return getListIfVisualStartEndIsInListRange((<TypeReferenceNode>node).typeArguments, start, end, node, sourceFile);
case SyntaxKind.ObjectLiteralExpression:
return getListIfVisualStartEndIsInListRange((<ObjectLiteralExpression>node).properties, start, end, node);
return getListIfVisualStartEndIsInListRange((<ObjectLiteralExpression>node).properties, start, end, node, sourceFile);
case SyntaxKind.ArrayLiteralExpression:
return getListIfVisualStartEndIsInListRange((<ArrayLiteralExpression>node).elements, start, end, node);
return getListIfVisualStartEndIsInListRange((<ArrayLiteralExpression>node).elements, start, end, node, sourceFile);
case SyntaxKind.TypeLiteral:
return getListIfVisualStartEndIsInListRange((<TypeLiteralNode>node).members, start, end, node);
return getListIfVisualStartEndIsInListRange((<TypeLiteralNode>node).members, start, end, node, sourceFile);
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.FunctionExpression:
case SyntaxKind.ArrowFunction:
Expand All @@ -374,30 +374,30 @@ namespace ts.formatting {
case SyntaxKind.Constructor:
case SyntaxKind.ConstructorType:
case SyntaxKind.ConstructSignature: {
return getListIfVisualStartEndIsInListRange((<SignatureDeclaration>node).typeParameters, start, end, node) ||
getListIfVisualStartEndIsInListRange((<SignatureDeclaration>node).parameters, start, end, node);
return getListIfVisualStartEndIsInListRange((<SignatureDeclaration>node).typeParameters, start, end, node, sourceFile) ||
getListIfVisualStartEndIsInListRange((<SignatureDeclaration>node).parameters, start, end, node, sourceFile);
}
case SyntaxKind.ClassDeclaration:
case SyntaxKind.ClassExpression:
case SyntaxKind.InterfaceDeclaration:
case SyntaxKind.TypeAliasDeclaration:
case SyntaxKind.JSDocTemplateTag: {
const { typeParameters } = <ClassDeclaration | ClassExpression | InterfaceDeclaration | TypeAliasDeclaration | JSDocTemplateTag>node;
return getListIfStartEndIsInListRange(typeParameters, start, end);
return getListIfVisualStartEndIsInListRange(typeParameters, start, end, node, sourceFile);
}
case SyntaxKind.NewExpression:
case SyntaxKind.CallExpression: {
return getListIfVisualStartEndIsInListRange((<CallExpression>node).typeArguments, start, end, node) ||
getListIfVisualStartEndIsInListRange((<CallExpression>node).arguments, start, end, node);
return getListIfVisualStartEndIsInListRange((<CallExpression>node).typeArguments, start, end, node, sourceFile) ||
getListIfVisualStartEndIsInListRange((<CallExpression>node).arguments, start, end, node, sourceFile);
}
case SyntaxKind.VariableDeclarationList:
return getListIfStartEndIsInListRange((<VariableDeclarationList>node).declarations, start, end);
case SyntaxKind.NamedImports:
case SyntaxKind.NamedExports:
return getListIfVisualStartEndIsInListRange((<NamedImportsOrExports>node).elements, start, end, node);
return getListIfVisualStartEndIsInListRange((<NamedImportsOrExports>node).elements, start, end, node, sourceFile);
case SyntaxKind.ObjectBindingPattern:
case SyntaxKind.ArrayBindingPattern:
return getListIfVisualStartEndIsInListRange((<ObjectBindingPattern | ArrayBindingPattern>node).elements, start, end, node);
return getListIfVisualStartEndIsInListRange((<ObjectBindingPattern | ArrayBindingPattern>node).elements, start, end, node, sourceFile);
}
}

Expand Down
140 changes: 70 additions & 70 deletions tests/cases/fourslash/formattingOnChainedCallbacks.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
/// <reference path='fourslash.ts' />

////Promise
//// .resolve()
//// .then(() => {/*1*/""/*2*/
////}).then(() => {/*3*//*4*/
////})/*semi1*//*semi2*/

////function foo() {
//// return Promise.resolve()
//// .then(function () {
//// ""/*a*/
//// })/*b*/
////}

////Promise
//// .then(
//// /*n1*/
//// )
//// /*n2*/
//// .then();

// @Filename: listSmart.ts
/// <reference path='fourslash.ts' />

////Promise
//// .resolve()
//// .then(() => {/*1*/""/*2*/
////}).then(() => {/*3*//*4*/
////})/*semi1*//*semi2*/

////function foo() {
//// return Promise.resolve()
//// .then(function () {
//// ""/*a*/
//// })/*b*/
////}

////Promise
//// .then(
//// /*n1*/
//// )
//// /*n2*/
//// .then();

// @Filename: listSmart.ts
////Promise
//// .resolve().then(
//// /*listSmart1*/
Expand All @@ -31,19 +31,19 @@
//// /*listSmart3*/
//// ]
//// /*listSmart4*/
//// );

// @Filename: listZeroIndent.ts
//// );

// @Filename: listZeroIndent.ts
////Promise.resolve([
////]).then(
//// /*listZeroIndent1*/
//// [
//// /*listZeroIndent2*/
//// 3
//// ]
//// );

// @Filename: listTypeParameter1.ts
//// );

// @Filename: listTypeParameter1.ts
////foo.then
//// <
//// /*listTypeParameter1*/
Expand All @@ -54,49 +54,49 @@
//// },
//// function (): void {
//// }
//// );

// @Filename: listComment.ts
//// );

// @Filename: listComment.ts
////Promise
//// .then(
//// // euphonium
//// "k"
//// // oboe
//// );


goTo.marker('1');
edit.insertLine('');
goTo.marker('2');
verify.currentLineContentIs(' ""');
edit.insertLine('');
verify.indentationIs(8);
goTo.marker('4');
edit.insertLine('');
goTo.marker('3');
verify.currentLineContentIs(' }).then(() => {');

goTo.marker("semi1");
edit.insert(';');
verify.currentLineContentIs(' });');
goTo.marker("semi2");
edit.insert(';');
verify.currentLineContentIs(' });;');

goTo.marker('a');
edit.insert(';');
verify.currentLineContentIs(' "";');
goTo.marker('b');
edit.insert(';');
verify.currentLineContentIs(' });');

goTo.marker('n1');
verify.indentationIs(8);
goTo.marker('n2');
verify.indentationIs(4);

goTo.file(1);
format.document();
//// );


goTo.marker('1');
edit.insertLine('');
goTo.marker('2');
verify.currentLineContentIs(' ""');
edit.insertLine('');
verify.indentationIs(8);
goTo.marker('4');
edit.insertLine('');
goTo.marker('3');
verify.currentLineContentIs(' }).then(() => {');

goTo.marker("semi1");
edit.insert(';');
verify.currentLineContentIs(' });');
goTo.marker("semi2");
edit.insert(';');
verify.currentLineContentIs(' });;');

goTo.marker('a');
edit.insert(';');
verify.currentLineContentIs(' "";');
goTo.marker('b');
edit.insert(';');
verify.currentLineContentIs(' });');

goTo.marker('n1');
verify.indentationIs(8);
goTo.marker('n2');
verify.indentationIs(4);

goTo.file("listSmart.ts");
format.document();
verify.currentFileContentIs(`Promise
.resolve().then(
Expand All @@ -117,7 +117,7 @@ verify.indentationIs(12);
goTo.marker("listSmart4");
verify.indentationIs(8);

goTo.file(2);
goTo.file("listZeroIndent.ts");
format.document();
verify.currentFileContentIs(`Promise.resolve([
]).then(
Expand All @@ -132,7 +132,7 @@ verify.indentationIs(4);
goTo.marker("listZeroIndent2");
verify.indentationIs(8);

goTo.file(3);
goTo.file("listTypeParameter1.ts");
format.document();
verify.currentFileContentIs(`foo.then
<
Expand All @@ -150,7 +150,7 @@ verify.indentationIs(8);
goTo.marker("listTypeParameter2");
verify.indentationIs(8);

goTo.file(4);
goTo.file("listComment.ts");
format.document();
verify.currentFileContentIs(`Promise
.then(
Expand Down

0 comments on commit 7995f91

Please sign in to comment.