Skip to content

Commit 6ad8fa6

Browse files
committed
Simplify comment alignment logic
1 parent 09b20d5 commit 6ad8fa6

File tree

1 file changed

+54
-85
lines changed

1 file changed

+54
-85
lines changed

src/compiler/emitter.ts

Lines changed: 54 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
module ts {
77
interface EmitTextWriter extends TextWriter {
8+
rawWrite(s: string): void;
89
writeLiteral(s: string): void;
910
getTextPos(): number;
1011
getLine(): number;
@@ -114,6 +115,15 @@ module ts {
114115
}
115116
}
116117

118+
function rawWrite(s: string) {
119+
if (s !== undefined) {
120+
if (lineStart) {
121+
lineStart = false;
122+
}
123+
output += s;
124+
}
125+
}
126+
117127
function writeLiteral(s: string) {
118128
if (s && s.length) {
119129
write(s);
@@ -145,14 +155,15 @@ module ts {
145155
return {
146156
write: write,
147157
writeSymbol: writeSymbol,
158+
rawWrite: rawWrite,
148159
writeLiteral: writeLiteral,
149160
writeLine: writeLine,
150161
increaseIndent: () => indent++,
151162
decreaseIndent: () => indent--,
152163
getIndent: () => indent,
153164
getTextPos: () => output.length,
154165
getLine: () => lineCount + 1,
155-
getColumn: () => lineStart ? indent * 4 + 1 : output.length - linePos + 1,
166+
getColumn: () => lineStart ? indent * getIndentSize() + 1 : output.length - linePos + 1,
156167
getText: () => output,
157168
};
158169
}
@@ -187,87 +198,56 @@ module ts {
187198
if (currentSourceFile.text.charCodeAt(comment.pos + 1) === CharacterCodes.asterisk) {
188199
var firstCommentLineAndCharacter = currentSourceFile.getLineAndCharacterFromPosition(comment.pos);
189200
var firstCommentLineIndent: number;
190-
var writeNewLine: boolean;
191201
for (var pos = comment.pos, currentLine = firstCommentLineAndCharacter.line; pos < comment.end; currentLine++) {
192202
var nextLineStart = currentSourceFile.getPositionFromLineAndCharacter(currentLine + 1, /*character*/1);
193203

194204
if (pos !== comment.pos) {
195-
// If we are not emitting first line, we need to adjust the indent
196-
if (writeNewLine) {
197-
writer.writeLine();
198-
}
199-
205+
// If we are not emitting first line, we need to write the spaces to adjust the alignment
200206
if (firstCommentLineIndent === undefined) {
201207
firstCommentLineIndent = calculateIndent(currentSourceFile.getPositionFromLineAndCharacter(firstCommentLineAndCharacter.line, /*character*/1),
202208
comment.pos);
203209
}
204210

205-
// Number of spacings this comment line differs from first comment line
206-
var deltaIndentSpacing = calculateIndent(pos, nextLineStart) - firstCommentLineIndent;
207-
if (deltaIndentSpacing < 0) {
208-
// we need to decrease indent to get the desired effect
209-
// Comment is left indented to first line
210-
// eg
211-
// module m {
212-
// /* this is line 1
213-
// * line
214-
// More left indented comment */
215-
// class c { }
216-
// }
217-
218-
// Spaces to emit = indentSize - (numberof spaces in lastDeltaIndent) (in above eg (4 - 5%4) = 3)
219-
var spacesToEmit = (deltaIndentSpacing % getIndentSize()); // This is negative of spaces to emit = -1 in above case
220-
if (spacesToEmit) {
221-
spacesToEmit += getIndentSize(); // Adjust the delta with the indentSize (4 - 1) = 3
222-
}
223-
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());
229-
230-
// If we cant go back as much as we want to, go to left most position
231-
if (indentChangeCount > writer.getIndent()) {
232-
indentChangeCount = writer.getIndent();
233-
spacesToEmit = 0;
211+
// These are number of spaces writer is going to write at current indent
212+
var currentWriterIndentSpacing = writer.getIndent() * getIndentSize();
213+
214+
// Number of spaces we want to be writing
215+
// eg: Assume writer indent
216+
// module m {
217+
// /* starts at character 9 this is line 1
218+
// * starts at character pos 4 line --1 = 8 - 8 + 3
219+
// More left indented comment */ --2 = 8 - 8 + 2
220+
// class c { }
221+
// }
222+
// module m {
223+
// /* this is line 1 -- Assume current writer indent 8
224+
// * line --3 = 8 - 4 + 5
225+
// More right indented comment */ --4 = 8 - 4 + 11
226+
// class c { }
227+
// }
228+
var spacesToEmit = currentWriterIndentSpacing - firstCommentLineIndent + calculateIndent(pos, nextLineStart);
229+
if (spacesToEmit > 0) {
230+
var numberOfSingleSpacesToEmit = spacesToEmit % getIndentSize();
231+
var indentSizeSpaceString = getIndentString((spacesToEmit - numberOfSingleSpacesToEmit) / getIndentSize());
232+
233+
// Write indent size string ( in eg 1: = "", 2: "" , 3: string with 8 spaces 4: string with 12 spaces
234+
writer.rawWrite(indentSizeSpaceString);
235+
236+
// Emit the single spaces (in eg: 1: 3 spaces, 2: 2 spaces, 3: 1 space, 4: 3 spaces)
237+
while (numberOfSingleSpacesToEmit) {
238+
writer.rawWrite(" ");
239+
numberOfSingleSpacesToEmit--;
234240
}
235-
236-
// Decrease the indentChangeCount number of times
237-
for (var i = 0; i < indentChangeCount; i++) {
238-
writer.decreaseIndent();
239-
}
240-
241-
// Emit the spaces to maintain deltaIndentSpacing
242-
emitSpaces(spacesToEmit, writeNewLine);
243-
244-
// Write the comment line text
245-
writeNewLine = writeTrimmedCurrentLine(pos, nextLineStart);
246-
247-
// Revert the indent
248-
for (var i = 0; i < indentChangeCount; i++) {
249-
writer.increaseIndent();
250-
}
251-
} else {
252-
// Comment is right indented to first line
253-
// eg
254-
// module m {
255-
// /* this is line 1
256-
// * line
257-
// More right indented comment */
258-
// class c { }
259-
// }
260-
// In above eg for line 2 in the comment, the delta is single space and hence emit that and emit the trimmed line
261-
// but the third line has delta of 7 spaces and hence emit those spaces before emitting the trimmed line
262-
emitSpaces(deltaIndentSpacing, writeNewLine);
263-
writeNewLine = writeTrimmedCurrentLine(pos, nextLineStart);
264241
}
265-
}
266-
else {
267-
// First comment line, emit as it is
268-
writeNewLine = writeTrimmedCurrentLine(pos, nextLineStart);
242+
else {
243+
// No spaces to emit write empty string
244+
writer.rawWrite("");
245+
}
269246
}
270247

248+
// Write the comment line text
249+
writeTrimmedCurrentLine(pos, nextLineStart);
250+
271251
pos = nextLineStart;
272252
}
273253
}
@@ -276,26 +256,15 @@ module ts {
276256
writer.write(currentSourceFile.text.substring(comment.pos, comment.end));
277257
}
278258

279-
function emitSpaces(count: number, writeNewLine: boolean) {
280-
if (!writeNewLine) {
281-
// If we didnot use WriteLine but instead used writeLiteral to writeNewLine, then we need to make sure we emit indent correctly
282-
writer.write(getIndentString(writer.getIndent()));
283-
}
284-
285-
// Write spaces
286-
while (count) {
287-
writer.write(" ");
288-
count--;
289-
}
290-
}
291-
292-
// Returns true if writer should write new line before emitting next line of comment
293259
function writeTrimmedCurrentLine(pos: number, nextLineStart: number) {
294-
var currentLineText = currentSourceFile.text.substring(pos, Math.min(comment.end, nextLineStart - 1)).replace(/^\s+|\s+$/g, '');
260+
var end = Math.min(comment.end, nextLineStart - 1);
261+
var currentLineText = currentSourceFile.text.substring(pos, end).replace(/^\s+|\s+$/g, '');
295262
if (currentLineText) {
296263
// trimmed forward and ending spaces text
297264
writer.write(currentLineText);
298-
return true;
265+
if (end !== comment.end) {
266+
writer.writeLine();
267+
}
299268
}
300269
else {
301270
// Empty string - make sure we write empty line

0 commit comments

Comments
 (0)