Skip to content

Commit cd663f0

Browse files
committed
minor code cleanup
1 parent 221f805 commit cd663f0

File tree

1 file changed

+10
-19
lines changed

1 file changed

+10
-19
lines changed

src/services/formatting/smartIndenter.ts

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,6 @@ module ts.formatting {
161161

162162
if (precedingListItemStartLineAndChar.line !== listStart.line) {
163163
return findColumnForFirstNonWhitespaceCharacterInLine(precedingListItemStartLineAndChar, sourceFile, options);
164-
// previous list item starts on the different line with list, find first non-whitespace character in this line and use its position as indentation
165-
var lineStartPosition = sourceFile.getPositionFromLineAndCharacter(precedingListItemStartLineAndChar.line, 1);
166-
for (var i = 0; i < precedingListItemStartLineAndChar.character; ++i) {
167-
if (!isWhiteSpace(sourceFile.text.charCodeAt(lineStartPosition + i))) {
168-
return i;
169-
}
170-
}
171-
172-
// seems that this is the first non-whitespace character on the line - return it
173-
return precedingListItemStartLineAndChar.character;
174164
}
175165

176166
return -1;
@@ -290,6 +280,8 @@ module ts.formatting {
290280
function getActualIndentationFromList(list: Node[]): number {
291281
var index = indexOf(list, node);
292282
if (index !== -1) {
283+
// walk toward the start of the list starting from current node and check if if line is the same for all items.
284+
// if line for item [i - 1] differs from the line for item [i] - find column of the first non-whitespace character on the line of item [i]
293285
var lineAndCharacter = getStartLineAndCharacterForNode(node, sourceFile);;
294286
for (var i = index - 1; i >= 0; --i) {
295287
var prevLineAndCharacter = getStartLineAndCharacterForNode(list[i], sourceFile);
@@ -312,7 +304,7 @@ module ts.formatting {
312304
return column;
313305
}
314306

315-
if (charCode === CharacterCodes.tab) {
307+
if (charCode === CharacterCodes.tab && !options.useTabs) {
316308
column += options.spacesPerTab;
317309
}
318310
else {
@@ -479,6 +471,7 @@ module ts.formatting {
479471
return false;
480472
}
481473

474+
// this function is alwasy called when position of the cursor is located after the node
482475
function isCompletedNode(n: Node, sourceFile: SourceFile): boolean {
483476
switch (n.kind) {
484477
case SyntaxKind.ClassDeclaration:
@@ -518,17 +511,15 @@ module ts.formatting {
518511
case SyntaxKind.DefaultClause:
519512
// there is no such thing as terminator token for CaseClause\DefaultClause so for simplicitly always consider them non-completed
520513
return false;
521-
case SyntaxKind.VariableStatement:
522-
// variable statement is considered completed if it either doesn'not have variable declarations or last variable declaration is completed
523-
var variableDeclarations = (<VariableStatement>n).declarations;
524-
return variableDeclarations.length === 0 || isCompletedNode(variableDeclarations[variableDeclarations.length - 1], sourceFile);
525-
case SyntaxKind.VariableDeclaration:
526-
// variable declaration is completed if it either doesn't have initializer or initializer is completed
527-
return !(<VariableDeclaration>n).initializer || isCompletedNode((<VariableDeclaration>n).initializer, sourceFile);
528514
case SyntaxKind.WhileStatement:
529515
return isCompletedNode((<WhileStatement>n).statement, sourceFile);
530516
case SyntaxKind.DoStatement:
531-
return isCompletedNode((<DoStatement>n).statement, sourceFile);
517+
// rough approximation: if DoStatement has While keyword - then if node is completed is checking the presence of ')';
518+
var hasWhileKeyword = forEach(n.getChildren(), c => c.kind === SyntaxKind.WhileKeyword && c);
519+
if(hasWhileKeyword) {
520+
return isNodeEndWith(n, SyntaxKind.CloseParenToken, sourceFile);
521+
}
522+
return isCompletedNode((<DoStatement>n).statement, sourceFile);
532523
default:
533524
return true;
534525
}

0 commit comments

Comments
 (0)