Skip to content

Commit f4da5d7

Browse files
Move the smart indenter over to zero based math.
1 parent edbedc7 commit f4da5d7

2 files changed

Lines changed: 18 additions & 14 deletions

File tree

src/services/formatting/smartIndenter.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module ts.formatting {
2424
return 0;
2525
}
2626

27-
var lineAtPosition = sourceFile.getOneBasedLineAndCharacterOfPosition(position).line;
27+
var lineAtPosition = sourceFile.getZeroBasedLineAndCharacterOfPosition(position).line;
2828

2929
if (precedingToken.kind === SyntaxKind.CommaToken && precedingToken.parent.kind !== SyntaxKind.BinaryExpression) {
3030
// previous token is comma that separates items in list - find the previous item and try to derive indentation from it
@@ -43,7 +43,7 @@ module ts.formatting {
4343

4444
while (current) {
4545
if (positionBelongsToNode(current, position, sourceFile) && shouldIndentChildNode(current.kind, previous ? previous.kind : SyntaxKind.Unknown)) {
46-
currentStart = getOneBasedStartLineAndCharacterForNode(current, sourceFile);
46+
currentStart = getZeroBasedStartLineAndCharacterForNode(current, sourceFile);
4747

4848
if (nextTokenIsCurlyBraceOnSameLineAsCursor(precedingToken, current, lineAtPosition, sourceFile)) {
4949
indentationDelta = 0;
@@ -74,7 +74,7 @@ module ts.formatting {
7474
}
7575

7676
export function getIndentationForNode(n: Node, ignoreActualIndentationRange: TextRange, sourceFile: SourceFile, options: FormatCodeOptions): number {
77-
var start = sourceFile.getOneBasedLineAndCharacterOfPosition(n.getStart(sourceFile));
77+
var start = sourceFile.getZeroBasedLineAndCharacterOfPosition(n.getStart(sourceFile));
7878
return getIndentationForNodeWorker(n, start, ignoreActualIndentationRange, /*indentationDelta*/ 0, sourceFile, options);
7979
}
8080

@@ -135,10 +135,10 @@ module ts.formatting {
135135
function getParentStart(parent: Node, child: Node, sourceFile: SourceFile): LineAndCharacter {
136136
var containingList = getContainingList(child, sourceFile);
137137
if (containingList) {
138-
return sourceFile.getOneBasedLineAndCharacterOfPosition(containingList.pos);
138+
return sourceFile.getZeroBasedLineAndCharacterOfPosition(containingList.pos);
139139
}
140140

141-
return sourceFile.getOneBasedLineAndCharacterOfPosition(parent.getStart(sourceFile));
141+
return sourceFile.getZeroBasedLineAndCharacterOfPosition(parent.getStart(sourceFile));
142142
}
143143

144144
/*
@@ -196,15 +196,15 @@ module ts.formatting {
196196
// class A {
197197
// $}
198198

199-
var nextTokenStartLine = getOneBasedStartLineAndCharacterForNode(nextToken, sourceFile).line;
199+
var nextTokenStartLine = getZeroBasedStartLineAndCharacterForNode(nextToken, sourceFile).line;
200200
return lineAtPosition === nextTokenStartLine;
201201
}
202202

203203
return false;
204204
}
205205

206-
function getOneBasedStartLineAndCharacterForNode(n: Node, sourceFile: SourceFile): LineAndCharacter {
207-
return sourceFile.getOneBasedLineAndCharacterOfPosition(n.getStart(sourceFile));
206+
function getZeroBasedStartLineAndCharacterForNode(n: Node, sourceFile: SourceFile): LineAndCharacter {
207+
return sourceFile.getZeroBasedLineAndCharacterOfPosition(n.getStart(sourceFile));
208208
}
209209

210210
function positionBelongsToNode(candidate: Node, position: number, sourceFile: SourceFile): boolean {
@@ -216,7 +216,7 @@ module ts.formatting {
216216
var elseKeyword = findChildOfKind(parent, SyntaxKind.ElseKeyword, sourceFile);
217217
Debug.assert(elseKeyword !== undefined);
218218

219-
var elseKeywordStartLine = getOneBasedStartLineAndCharacterForNode(elseKeyword, sourceFile).line;
219+
var elseKeywordStartLine = getZeroBasedStartLineAndCharacterForNode(elseKeyword, sourceFile).line;
220220
return elseKeywordStartLine === childStartLine;
221221
}
222222

@@ -279,31 +279,30 @@ module ts.formatting {
279279
}
280280
}
281281

282-
283282
function deriveActualIndentationFromList(list: Node[], index: number, sourceFile: SourceFile, options: EditorOptions): number {
284283
Debug.assert(index >= 0 && index < list.length);
285284
var node = list[index];
286285

287286
// walk toward the start of the list starting from current node and check if the line is the same for all items.
288287
// if end line for item [i - 1] differs from the start line for item [i] - find column of the first non-whitespace character on the line of item [i]
289-
var lineAndCharacter = getOneBasedStartLineAndCharacterForNode(node, sourceFile);
288+
var lineAndCharacter = getZeroBasedStartLineAndCharacterForNode(node, sourceFile);
290289
for (var i = index - 1; i >= 0; --i) {
291290
if (list[i].kind === SyntaxKind.CommaToken) {
292291
continue;
293292
}
294293
// skip list items that ends on the same line with the current list element
295-
var prevEndLine = sourceFile.getOneBasedLineAndCharacterOfPosition(list[i].end).line;
294+
var prevEndLine = sourceFile.getZeroBasedLineAndCharacterOfPosition(list[i].end).line;
296295
if (prevEndLine !== lineAndCharacter.line) {
297296
return findColumnForFirstNonWhitespaceCharacterInLine(lineAndCharacter, sourceFile, options);
298297
}
299298

300-
lineAndCharacter = getOneBasedStartLineAndCharacterForNode(list[i], sourceFile);
299+
lineAndCharacter = getZeroBasedStartLineAndCharacterForNode(list[i], sourceFile);
301300
}
302301
return -1;
303302
}
304303

305304
function findColumnForFirstNonWhitespaceCharacterInLine(lineAndCharacter: LineAndCharacter, sourceFile: SourceFile, options: EditorOptions): number {
306-
var lineStart = sourceFile.getPositionOfOneBasedLineAndCharacter(lineAndCharacter.line, 1);
305+
var lineStart = sourceFile.getPositionOfZeroBasedLineAndCharacter(lineAndCharacter.line, 0);
307306
return findFirstNonWhitespaceColumn(lineStart, lineStart + lineAndCharacter.character, sourceFile, options);
308307
}
309308

src/services/services.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ module ts {
6464
getZeroBasedLineAndCharacterOfPosition(pos: number): LineAndCharacter;
6565
getOneBasedLineAndCharacterOfPosition(pos: number): LineAndCharacter;
6666
getLineStarts(): number[];
67+
getPositionOfZeroBasedLineAndCharacter(line: number, character: number): number;
6768
getPositionOfOneBasedLineAndCharacter(line: number, character: number): number;
6869
update(newText: string, textChangeRange: TextChangeRange): SourceFile;
6970
}
@@ -770,6 +771,10 @@ module ts {
770771
return ts.getPositionOfOneBasedLineAndCharacter(this, line, character);
771772
}
772773

774+
public getPositionOfZeroBasedLineAndCharacter(line: number, character: number): number {
775+
return ts.getPositionOfZeroBasedLineAndCharacter(this, line, character);
776+
}
777+
773778
public getNamedDeclarations() {
774779
if (!this.namedDeclarations) {
775780
var sourceFile = this;

0 commit comments

Comments
 (0)