Skip to content

Commit 09b20d5

Browse files
committed
Fixed according to code review feedback
1 parent e76d8f3 commit 09b20d5

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

src/compiler/emitter.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,10 @@ module ts {
201201
firstCommentLineIndent = calculateIndent(currentSourceFile.getPositionFromLineAndCharacter(firstCommentLineAndCharacter.line, /*character*/1),
202202
comment.pos);
203203
}
204-
var deltaIndent = calculateIndent(pos, nextLineStart) - firstCommentLineIndent;
205-
if (deltaIndent < 0) {
204+
205+
// Number of spacings this comment line differs from first comment line
206+
var deltaIndentSpacing = calculateIndent(pos, nextLineStart) - firstCommentLineIndent;
207+
if (deltaIndentSpacing < 0) {
206208
// we need to decrease indent to get the desired effect
207209
// Comment is left indented to first line
208210
// eg
@@ -214,34 +216,36 @@ module ts {
214216
// }
215217

216218
// Spaces to emit = indentSize - (numberof spaces in lastDeltaIndent) (in above eg (4 - 5%4) = 3)
217-
var spacesToEmit = (deltaIndent % getIndentSize()); // This is negative of spaces to emit = -1 in above case
219+
var spacesToEmit = (deltaIndentSpacing % getIndentSize()); // This is negative of spaces to emit = -1 in above case
218220
if (spacesToEmit) {
219221
spacesToEmit += getIndentSize(); // Adjust the delta with the indentSize (4 - 1) = 3
220222
}
221223

222-
// Change in delta indent = deltaIndent / indentSize, we will change the delta to upper integer value
223-
// In above eg. 5/4 = 1.75 so change the indent two times
224-
var changeInIndent = (-deltaIndent / getIndentSize());
224+
// This is number of times indent needs to be decremented before writing comment line text
225+
// and same number of times the indent needs to be increased
226+
// It = deltaIndentSpacing / indentSize, we will change this to upper integer value
227+
// In above eg. 5/4 = 1.75 so change the indent two times (decrease 2 times, write text and then increase 2 times)
228+
var indentChangeCount = (-deltaIndentSpacing / getIndentSize());
225229

226230
// If we cant go back as much as we want to, go to left most position
227-
if (changeInIndent > writer.getIndent()) {
228-
changeInIndent = writer.getIndent();
231+
if (indentChangeCount > writer.getIndent()) {
232+
indentChangeCount = writer.getIndent();
229233
spacesToEmit = 0;
230234
}
231235

232-
// Decrease the chaneInIndent number of times
233-
for (var i = 0; i < changeInIndent; i++) {
236+
// Decrease the indentChangeCount number of times
237+
for (var i = 0; i < indentChangeCount; i++) {
234238
writer.decreaseIndent();
235239
}
236240

237-
// Emit either delta spaces or indentSizeSpaces
241+
// Emit the spaces to maintain deltaIndentSpacing
238242
emitSpaces(spacesToEmit, writeNewLine);
239243

240244
// Write the comment line text
241245
writeNewLine = writeTrimmedCurrentLine(pos, nextLineStart);
242246

243247
// Revert the indent
244-
for (var i = 0; i < changeInIndent; i++) {
248+
for (var i = 0; i < indentChangeCount; i++) {
245249
writer.increaseIndent();
246250
}
247251
} else {
@@ -255,7 +259,7 @@ module ts {
255259
// }
256260
// In above eg for line 2 in the comment, the delta is single space and hence emit that and emit the trimmed line
257261
// but the third line has delta of 7 spaces and hence emit those spaces before emitting the trimmed line
258-
emitSpaces(deltaIndent, writeNewLine);
262+
emitSpaces(deltaIndentSpacing, writeNewLine);
259263
writeNewLine = writeTrimmedCurrentLine(pos, nextLineStart);
260264
}
261265
}

src/compiler/scanner.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ module ts {
265265
}
266266

267267
export function getPositionFromLineAndCharacter(lineStarts: number[], line: number, character: number): number {
268+
Debug.assert(line > 0);
268269
return lineStarts[line - 1] + character - 1;
269270
}
270271

0 commit comments

Comments
 (0)