@@ -49,31 +49,26 @@ export async function typeFormatting(params: DocumentOnTypeFormattingParams): Pr
4949 }
5050 } ) ;
5151 }
52- } else if ( ( pp = lex . linepos [ line - 1 ] ) !== undefined ) {
53- const range = { start : lex . document . positionAt ( pp ) , end : { line : line - 1 , character : s . length } } ;
54- const newText = lex . beautify ( opts , range ) . trim ( ) ;
55- result = [ { range, newText } ] ;
56- indent_string = opts . indent_string ;
57- if ( linetexts [ 1 ] . substring ( 0 , character ) !== indent_string )
58- result . push ( {
59- newText : indent_string ,
60- range : { start : { line, character : 0 } , end : { line, character } }
61- } ) ;
62- } else if ( ! s ) {
63- if ( linetexts [ 0 ] !== ( linetexts [ 1 ] = linetexts [ 1 ] . substring ( 0 , character ) ) ) {
64- if ( ! linetexts [ 0 ] ) {
65- tk = lex . findToken ( lex . document . offsetAt ( position ) ) ;
66- if ( tk . type === TokenType . String || ( tk . type & TokenType . Comment ) )
67- return ;
68- const b = [ TokenType . EOF , TokenType . BracketStart , TokenType . BlockStart ] ;
69- while ( ( tk = tk . previous_token ! ) ) {
70- if ( b . includes ( tk . type ) )
71- break ;
72- }
73- if ( b . includes ( tk ?. type , 1 ) )
74- return ;
52+ } else if ( s && ! / ^ ( ; | \/ \* ) | [ \t ] ; / . test ( s . trimStart ( ) ) ) {
53+ const start = { line, character : 0 } , offset = lex . document . offsetAt ( start ) ;
54+ const rgs = lex . line_ranges , ll = line - 1 ;
55+ let l = 0 , r = rgs . length - 1 , i ;
56+ while ( l <= r ) {
57+ const [ a , b ] = rgs [ i = ( l + r ) >> 1 ] ;
58+ if ( ll > a )
59+ l = i + 1 ;
60+ else if ( ll < a && offset <= b )
61+ r = i - 1 ;
62+ else if ( lex . findStrOrComment ( offset ) )
63+ break ;
64+ else {
65+ const range = { start : lex . document . positionAt ( b ) , end : { line : ll , character : s . length } } ;
66+ const newText = lex . beautify ( opts , range ) . trim ( ) ;
67+ result = [ { range, newText } ] ;
68+ if ( linetexts [ 1 ] . substring ( 0 , character ) !== ( indent_string = opts . indent_string ) )
69+ result . push ( { newText : indent_string , range : { start, end : position } } ) ;
70+ break ;
7571 }
76- result = [ { newText : linetexts [ 0 ] , range : { start : { line, character : 0 } , end : { line, character } } } ] ;
7772 }
7873 }
7974 return result ;
@@ -93,7 +88,7 @@ export async function typeFormatting(params: DocumentOnTypeFormattingParams): Pr
9388 }
9489
9590 function format_end_with_brace ( pos : Position ) : TextEdit [ ] | undefined {
96- tk = lex . tokens [ lex . document . offsetAt ( { line : pos . line , character : pos . character - 1 } ) ] ;
91+ tk = lex . tokens [ lex . document . offsetAt ( pos ) - 1 ] ;
9792 pp = tk ?. previous_pair_pos ;
9893 if ( pp !== undefined ) {
9994 while ( ( tk = lex . tokens [ pp ] ) ?. previous_pair_pos !== undefined )
0 commit comments