@@ -21,6 +21,21 @@ namespace ts.server {
21
21
return spaceCache [ n ] ;
22
22
}
23
23
24
+ export function generateIndentString ( n : number , editorOptions : EditorOptions ) : string {
25
+ if ( editorOptions . ConvertTabsToSpaces ) {
26
+ return generateSpaces ( n ) ;
27
+ } else {
28
+ var result = "" ;
29
+ for ( var i = 0 ; i < Math . floor ( n / editorOptions . TabSize ) ; i ++ ) {
30
+ result += "\t" ;
31
+ }
32
+ for ( var i = 0 ; i < n % editorOptions . TabSize ; i ++ ) {
33
+ result += " " ;
34
+ }
35
+ return result ;
36
+ }
37
+ }
38
+
24
39
interface FileStart {
25
40
file : string ;
26
41
start : ILineInfo ;
@@ -608,27 +623,25 @@ namespace ts.server {
608
623
ConvertTabsToSpaces : formatOptions . ConvertTabsToSpaces ,
609
624
IndentStyle : ts . IndentStyle . Smart ,
610
625
} ;
611
- var indentPosition =
612
- compilerService . languageService . getIndentationAtPosition ( file , position , editorOptions ) ;
626
+ var preferredIndent = compilerService . languageService . getIndentationAtPosition ( file , position , editorOptions ) ;
627
+ var hasIndent = 0 ;
613
628
for ( var i = 0 , len = lineText . length ; i < len ; i ++ ) {
614
629
if ( lineText . charAt ( i ) == " " ) {
615
- indentPosition -- ;
630
+ hasIndent ++ ;
616
631
}
617
632
else if ( lineText . charAt ( i ) == "\t" ) {
618
- indentPosition - = editorOptions . IndentSize ;
633
+ hasIndent + = editorOptions . TabSize ;
619
634
}
620
635
else {
621
636
break ;
622
637
}
623
638
}
624
- if ( indentPosition > 0 ) {
625
- var spaces = generateSpaces ( indentPosition ) ;
626
- edits . push ( { span : ts . createTextSpanFromBounds ( position , position ) , newText : spaces } ) ;
627
- }
628
- else if ( indentPosition < 0 ) {
639
+ // i points to the first non whitespace character
640
+ if ( preferredIndent !== hasIndent ) {
641
+ var firstNoWhiteSpacePosition = lineInfo . offset + i ;
629
642
edits . push ( {
630
- span : ts . createTextSpanFromBounds ( position , position - indentPosition ) ,
631
- newText : ""
643
+ span : ts . createTextSpanFromBounds ( lineInfo . offset , firstNoWhiteSpacePosition ) ,
644
+ newText : generateIndentString ( preferredIndent , editorOptions )
632
645
} ) ;
633
646
}
634
647
}
0 commit comments