@@ -150,17 +150,21 @@ export abstract class AbstractFormatter implements Formatter {
150
150
protected avoidOverlappingEdits ( textDocument : TextDocument , textEdits : TextEdit [ ] ) : TextEdit [ ] {
151
151
const edits : TextEdit [ ] = [ ] ;
152
152
for ( const edit of textEdits ) {
153
- const last = edits [ edits . length - 1 ] ;
154
- if ( last ) {
153
+ let last = edits [ edits . length - 1 ] ;
154
+ while ( last ) {
155
155
const currentStart = textDocument . offsetAt ( edit . range . start ) ;
156
156
const lastEnd = textDocument . offsetAt ( last . range . end ) ;
157
157
if ( currentStart < lastEnd ) {
158
158
edits . pop ( ) ;
159
+ last = edits [ edits . length - 1 ] ;
160
+ }
161
+ else {
162
+ break ;
159
163
}
160
164
}
161
165
edits . push ( edit ) ;
162
166
}
163
- return edits ;
167
+ return edits . filter ( edit => this . isNecessary ( edit , textDocument ) ) ;
164
168
}
165
169
166
170
protected iterateAstFormatting ( document : LangiumDocument , range ?: Range ) : void {
@@ -200,11 +204,8 @@ export abstract class AbstractFormatter implements Formatter {
200
204
return false ;
201
205
}
202
206
203
- /**
204
- * @deprecated This method has been deprecated with 3.1. It now always returns `true` and is no longer used by the default formatter implementation.
205
- */
206
- protected isNecessary ( _edit : TextEdit , _document : TextDocument ) : boolean {
207
- return true ;
207
+ protected isNecessary ( edit : TextEdit , document : TextDocument ) : boolean {
208
+ return edit . newText !== document . getText ( edit . range ) . replace ( / \r / g, '' ) ;
208
209
}
209
210
210
211
protected iterateCstFormatting ( document : LangiumDocument , formattings : Map < string , FormattingAction > , options : FormattingOptions , range ?: Range ) : TextEdit [ ] {
@@ -382,7 +383,10 @@ export abstract class AbstractFormatter implements Formatter {
382
383
context . indentation += ( tabs ?? 0 ) ;
383
384
const edits : TextEdit [ ] = [ ] ;
384
385
if ( chars !== undefined ) {
385
- edits . push ( this . createSpaceTextEdit ( betweenRange , chars , formatting . options ) ) ;
386
+ // Do not apply formatting on the same line if preceding node is hidden
387
+ if ( ! a ?. hidden ) {
388
+ edits . push ( this . createSpaceTextEdit ( betweenRange , chars , formatting . options ) ) ;
389
+ }
386
390
} else if ( lines !== undefined ) {
387
391
edits . push ( this . createLineTextEdit ( betweenRange , lines , context , formatting . options ) ) ;
388
392
} else if ( tabs !== undefined ) {
0 commit comments