@@ -21,6 +21,21 @@ module ts.server {
21
21
}
22
22
return spaceCache [ n ] ;
23
23
}
24
+
25
+ export function generateIndentString ( n : number , editorOptions : EditorOptions ) : string {
26
+ if ( editorOptions . ConvertTabsToSpaces ) {
27
+ return generateSpaces ( n ) ;
28
+ } else {
29
+ var result = "" ;
30
+ for ( var i = 0 ; i < Math . floor ( n / editorOptions . TabSize ) ; i ++ ) {
31
+ result += "\t" ;
32
+ }
33
+ for ( var i = 0 ; i < n % editorOptions . TabSize ; i ++ ) {
34
+ result += " " ;
35
+ }
36
+ return result ;
37
+ }
38
+ }
24
39
25
40
interface FileStart {
26
41
file : string ;
@@ -541,31 +556,29 @@ module ts.server {
541
556
var editorOptions : ts . EditorOptions = {
542
557
IndentSize : formatOptions . IndentSize ,
543
558
TabSize : formatOptions . TabSize ,
544
- NewLineCharacter : "\n" ,
559
+ NewLineCharacter : formatOptions . NewLineCharacter ,
545
560
ConvertTabsToSpaces : formatOptions . ConvertTabsToSpaces ,
546
561
} ;
547
- var indentPosition =
548
- compilerService . languageService . getIndentationAtPosition ( file , position , editorOptions ) ;
562
+ var preferredIndent = compilerService . languageService . getIndentationAtPosition ( file , position , editorOptions ) ;
563
+ var hasIndent = 0 ;
549
564
for ( var i = 0 , len = lineText . length ; i < len ; i ++ ) {
550
565
if ( lineText . charAt ( i ) == " " ) {
551
- indentPosition -- ;
566
+ hasIndent ++ ;
552
567
}
553
568
else if ( lineText . charAt ( i ) == "\t" ) {
554
- indentPosition - = editorOptions . IndentSize ;
569
+ hasIndent + = editorOptions . TabSize ;
555
570
}
556
571
else {
557
572
break ;
558
573
}
559
574
}
560
- if ( indentPosition > 0 ) {
561
- var spaces = generateSpaces ( indentPosition ) ;
562
- edits . push ( { span : ts . createTextSpanFromBounds ( position , position ) , newText : spaces } ) ;
563
- }
564
- else if ( indentPosition < 0 ) {
565
- edits . push ( {
566
- span : ts . createTextSpanFromBounds ( position , position - indentPosition ) ,
567
- newText : ""
568
- } ) ;
575
+ // i points to the first non whitespace character
576
+ if ( preferredIndent !== hasIndent ) {
577
+ var firstNoWhiteSpacePosition = lineInfo . offset + i ;
578
+ edits . push ( {
579
+ span : ts . createTextSpanFromBounds ( lineInfo . offset , firstNoWhiteSpacePosition ) ,
580
+ newText : generateIndentString ( preferredIndent , editorOptions )
581
+ } ) ;
569
582
}
570
583
}
571
584
}
0 commit comments