replace -1 in SmartIndenter with const enum

This commit is contained in:
Vladimir Matveev 2015-02-24 12:41:54 -08:00
parent 2e86eedfbc
commit 6055dea93e

View file

@ -2,6 +2,11 @@
module ts.formatting {
export module SmartIndenter {
const enum Value {
Unknown = -1
}
export function getIndentation(position: number, sourceFile: SourceFile, options: EditorOptions): number {
if (position > sourceFile.text.length) {
return 0; // past EOF
@ -29,7 +34,7 @@ module ts.formatting {
if (precedingToken.kind === SyntaxKind.CommaToken && precedingToken.parent.kind !== SyntaxKind.BinaryExpression) {
// previous token is comma that separates items in list - find the previous item and try to derive indentation from it
var actualIndentation = getActualIndentationForListItemBeforeComma(precedingToken, sourceFile, options);
if (actualIndentation !== -1) {
if (actualIndentation !== Value.Unknown) {
return actualIndentation;
}
}
@ -57,7 +62,7 @@ module ts.formatting {
// check if current node is a list item - if yes, take indentation from it
var actualIndentation = getActualIndentationForListItem(current, sourceFile, options);
if (actualIndentation !== -1) {
if (actualIndentation !== Value.Unknown) {
return actualIndentation;
}
@ -101,7 +106,7 @@ module ts.formatting {
if (useActualIndentation) {
// check if current node is a list item - if yes, take indentation from it
var actualIndentation = getActualIndentationForListItem(current, sourceFile, options);
if (actualIndentation !== -1) {
if (actualIndentation !== Value.Unknown) {
return actualIndentation + indentationDelta;
}
}
@ -113,7 +118,7 @@ module ts.formatting {
if (useActualIndentation) {
// try to fetch actual indentation for current node from source text
var actualIndentation = getActualIndentationForNode(current, parent, currentStart, parentAndChildShareLine, sourceFile, options);
if (actualIndentation !== -1) {
if (actualIndentation !== Value.Unknown) {
return actualIndentation + indentationDelta;
}
}
@ -142,18 +147,22 @@ module ts.formatting {
}
/*
* Function returns -1 if indentation cannot be determined
* Function returns Value.Unknown if indentation cannot be determined
*/
function getActualIndentationForListItemBeforeComma(commaToken: Node, sourceFile: SourceFile, options: EditorOptions): number {
// previous token is comma that separates items in list - find the previous item and try to derive indentation from it
var commaItemInfo = findListItemInfo(commaToken);
Debug.assert(commaItemInfo && commaItemInfo.listItemIndex > 0);
// The item we're interested in is right before the comma
return deriveActualIndentationFromList(commaItemInfo.list.getChildren(), commaItemInfo.listItemIndex - 1, sourceFile, options);
if (commaItemInfo && commaItemInfo.listItemIndex > 0) {
deriveActualIndentationFromList(commaItemInfo.list.getChildren(), commaItemInfo.listItemIndex - 1, sourceFile, options);
}
else {
// handle broken code gracefully
return Value.Unknown;
}
}
/*
* Function returns -1 if actual indentation for node should not be used (i.e because node is nested expression)
* Function returns Value.Unknown if actual indentation for node should not be used (i.e because node is nested expression)
*/
function getActualIndentationForNode(current: Node,
parent: Node,
@ -170,7 +179,7 @@ module ts.formatting {
(parent.kind === SyntaxKind.SourceFile || !parentAndChildShareLine);
if (!useActualIndentation) {
return -1;
return Value.Unknown;
}
return findColumnForFirstNonWhitespaceCharacterInLine(currentLineAndChar, sourceFile, options);
@ -271,11 +280,11 @@ module ts.formatting {
function getActualIndentationForListItem(node: Node, sourceFile: SourceFile, options: EditorOptions): number {
var containingList = getContainingList(node, sourceFile);
return containingList ? getActualIndentationFromList(containingList) : -1;
return containingList ? getActualIndentationFromList(containingList) : Value.Unknown;
function getActualIndentationFromList(list: Node[]): number {
var index = indexOf(list, node);
return index !== -1 ? deriveActualIndentationFromList(list, index, sourceFile, options) : -1;
return index !== -1 ? deriveActualIndentationFromList(list, index, sourceFile, options) : Value.Unknown;
}
}
@ -298,7 +307,7 @@ module ts.formatting {
lineAndCharacter = getStartLineAndCharacterForNode(list[i], sourceFile);
}
return -1;
return Value.Unknown;
}
function findColumnForFirstNonWhitespaceCharacterInLine(lineAndCharacter: LineAndCharacter, sourceFile: SourceFile, options: EditorOptions): number {