@@ -21,6 +21,21 @@ module ts.server {
2121 }
2222 return spaceCache [ n ] ;
2323 }
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+ }
2439
2540 interface FileStart {
2641 file : string ;
@@ -541,31 +556,29 @@ module ts.server {
541556 var editorOptions : ts . EditorOptions = {
542557 IndentSize : formatOptions . IndentSize ,
543558 TabSize : formatOptions . TabSize ,
544- NewLineCharacter : "\n" ,
559+ NewLineCharacter : formatOptions . NewLineCharacter ,
545560 ConvertTabsToSpaces : formatOptions . ConvertTabsToSpaces ,
546561 } ;
547- var indentPosition =
548- compilerService . languageService . getIndentationAtPosition ( file , position , editorOptions ) ;
562+ var preferredIndent = compilerService . languageService . getIndentationAtPosition ( file , position , editorOptions ) ;
563+ var hasIndent = 0 ;
549564 for ( var i = 0 , len = lineText . length ; i < len ; i ++ ) {
550565 if ( lineText . charAt ( i ) == " " ) {
551- indentPosition -- ;
566+ hasIndent ++ ;
552567 }
553568 else if ( lineText . charAt ( i ) == "\t" ) {
554- indentPosition - = editorOptions . IndentSize ;
569+ hasIndent + = editorOptions . TabSize ;
555570 }
556571 else {
557572 break ;
558573 }
559574 }
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+ } ) ;
569582 }
570583 }
571584 }
0 commit comments