5
5
6
6
module ts {
7
7
interface EmitTextWriter extends TextWriter {
8
+ rawWrite ( s : string ) : void ;
8
9
writeLiteral ( s : string ) : void ;
9
10
getTextPos ( ) : number ;
10
11
getLine ( ) : number ;
@@ -114,6 +115,15 @@ module ts {
114
115
}
115
116
}
116
117
118
+ function rawWrite ( s : string ) {
119
+ if ( s !== undefined ) {
120
+ if ( lineStart ) {
121
+ lineStart = false ;
122
+ }
123
+ output += s ;
124
+ }
125
+ }
126
+
117
127
function writeLiteral ( s : string ) {
118
128
if ( s && s . length ) {
119
129
write ( s ) ;
@@ -145,14 +155,15 @@ module ts {
145
155
return {
146
156
write : write ,
147
157
writeSymbol : writeSymbol ,
158
+ rawWrite : rawWrite ,
148
159
writeLiteral : writeLiteral ,
149
160
writeLine : writeLine ,
150
161
increaseIndent : ( ) => indent ++ ,
151
162
decreaseIndent : ( ) => indent -- ,
152
163
getIndent : ( ) => indent ,
153
164
getTextPos : ( ) => output . length ,
154
165
getLine : ( ) => lineCount + 1 ,
155
- getColumn : ( ) => lineStart ? indent * 4 + 1 : output . length - linePos + 1 ,
166
+ getColumn : ( ) => lineStart ? indent * getIndentSize ( ) + 1 : output . length - linePos + 1 ,
156
167
getText : ( ) => output ,
157
168
} ;
158
169
}
@@ -187,87 +198,56 @@ module ts {
187
198
if ( currentSourceFile . text . charCodeAt ( comment . pos + 1 ) === CharacterCodes . asterisk ) {
188
199
var firstCommentLineAndCharacter = currentSourceFile . getLineAndCharacterFromPosition ( comment . pos ) ;
189
200
var firstCommentLineIndent : number ;
190
- var writeNewLine : boolean ;
191
201
for ( var pos = comment . pos , currentLine = firstCommentLineAndCharacter . line ; pos < comment . end ; currentLine ++ ) {
192
202
var nextLineStart = currentSourceFile . getPositionFromLineAndCharacter ( currentLine + 1 , /*character*/ 1 ) ;
193
203
194
204
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
200
206
if ( firstCommentLineIndent === undefined ) {
201
207
firstCommentLineIndent = calculateIndent ( currentSourceFile . getPositionFromLineAndCharacter ( firstCommentLineAndCharacter . line , /*character*/ 1 ) ,
202
208
comment . pos ) ;
203
209
}
204
210
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 -- ;
234
240
}
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 ) ;
264
241
}
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
+ }
269
246
}
270
247
248
+ // Write the comment line text
249
+ writeTrimmedCurrentLine ( pos , nextLineStart ) ;
250
+
271
251
pos = nextLineStart ;
272
252
}
273
253
}
@@ -276,26 +256,15 @@ module ts {
276
256
writer . write ( currentSourceFile . text . substring ( comment . pos , comment . end ) ) ;
277
257
}
278
258
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
293
259
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, '' ) ;
295
262
if ( currentLineText ) {
296
263
// trimmed forward and ending spaces text
297
264
writer . write ( currentLineText ) ;
298
- return true ;
265
+ if ( end !== comment . end ) {
266
+ writer . writeLine ( ) ;
267
+ }
299
268
}
300
269
else {
301
270
// Empty string - make sure we write empty line
0 commit comments