@@ -83,7 +83,7 @@ export class DocumentFormattingEditProvider implements vscode.DocumentFormatting
83
83
range : new vscode . Range ( new vscode . Position ( i , 0 ) , new vscode . Position ( i , space . length ) ) ,
84
84
} ) ;
85
85
}
86
- if ( rest . trimLeft ( ) . length ) {
86
+ if ( rest . trimLeft ( ) . length && ! rest . match ( / ^ \s * \b w h i l e \b / i ) ) {
87
87
const pos = line . text . indexOf ( "}" ) + 1 ;
88
88
edits . push ( {
89
89
newText : "\n" + " " . repeat ( indentSize ) ,
@@ -98,25 +98,44 @@ export class DocumentFormattingEditProvider implements vscode.DocumentFormatting
98
98
const commandsMatch = line . text . match ( / ^ ( \s + [ \s . ] * ) \b ( [ a - z ] + ) \b / i) ;
99
99
if ( commandsMatch ) {
100
100
const indentSize = options . tabSize * indent ;
101
- const [ , space , found ] = commandsMatch ;
101
+ const [ , space ] = commandsMatch ;
102
102
if ( ! space . includes ( "." ) && options . insertSpaces && space . length !== indentSize ) {
103
103
const newText = " " . repeat ( indentSize ) ;
104
104
edits . push ( {
105
105
newText,
106
106
range : new vscode . Range ( new vscode . Position ( i , 0 ) , new vscode . Position ( i , space . length ) ) ,
107
107
} ) ;
108
108
}
109
- const pos = line . text . indexOf ( found ) ;
110
- const range = new vscode . Range ( new vscode . Position ( i , pos ) , new vscode . Position ( i , pos + found . length ) ) ;
111
- const command = commands . find ( el => el . alias . includes ( found . toUpperCase ( ) ) ) ;
112
- if ( command ) {
113
- const expect = this . _formatter . command ( command . label ) ;
114
- if ( expect !== found ) {
115
- edits . push ( {
116
- newText : expect ,
117
- range,
118
- } ) ;
109
+
110
+ // keep strings and comments
111
+ const keepList = [ ] ;
112
+ const restorePattern = [ ] ;
113
+ const toKeep = str => {
114
+ keepList . push ( str ) ;
115
+ restorePattern . push ( String . fromCharCode ( keepList . length ) ) ;
116
+ return String . fromCharCode ( keepList . length ) ;
117
+ } ;
118
+ // restore strings and comments back
119
+ const toRestore = code => keepList [ code . charCodeAt ( 0 ) - 1 ] || code ;
120
+ const formatCommand = ( full , spaces , cmd ) => {
121
+ const command = commands . find ( el => el . alias . includes ( cmd . toUpperCase ( ) ) ) ;
122
+ if ( command ) {
123
+ return spaces + this . _formatter . command ( command . label ) ;
119
124
}
125
+ return full ;
126
+ } ;
127
+ const newText = line . text
128
+ . replace ( / " (?: " " | [ ^ " ] ) * " | \/ \* .* \* \/ | \/ \/ + .* | # # ; .* / g, toKeep )
129
+ . replace ( / (?< = ^ \s | { | } ) ( \s * ) ( \b ( [ a - z ] + ) \b ) / gi, formatCommand )
130
+ . replace ( / ( [ { } ] ) (? ! \s | $ ) / g, "$1 " )
131
+ . replace ( / (?< ! \s ) ( [ { } ] ) / g, " $1" )
132
+ . replace ( new RegExp ( restorePattern . join ( "|" ) , "g" ) , toRestore ) ;
133
+
134
+ if ( newText != line . text ) {
135
+ edits . push ( {
136
+ newText,
137
+ range : line . range ,
138
+ } ) ;
120
139
}
121
140
const setAssignMatch = line . text . match (
122
141
/ ^ \s + (?: \. \s * ) * s e t \s (?: \^ ? % ? (?: [ a - z ] [ a - z 0 - 9 ] * ) (?: \. [ a - z ] [ a - z 0 - 9 ] * ) * ) ( \s * = \s * ) / i
0 commit comments