@@ -150,17 +150,21 @@ export abstract class AbstractFormatter implements Formatter {
150150 protected avoidOverlappingEdits ( textDocument : TextDocument , textEdits : TextEdit [ ] ) : TextEdit [ ] {
151151 const edits : TextEdit [ ] = [ ] ;
152152 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 ) {
155155 const currentStart = textDocument . offsetAt ( edit . range . start ) ;
156156 const lastEnd = textDocument . offsetAt ( last . range . end ) ;
157157 if ( currentStart < lastEnd ) {
158158 edits . pop ( ) ;
159+ last = edits [ edits . length - 1 ] ;
160+ }
161+ else {
162+ break ;
159163 }
160164 }
161165 edits . push ( edit ) ;
162166 }
163- return edits ;
167+ return edits . filter ( edit => this . isNecessary ( edit , textDocument ) ) ;
164168 }
165169
166170 protected iterateAstFormatting ( document : LangiumDocument , range ?: Range ) : void {
@@ -200,11 +204,8 @@ export abstract class AbstractFormatter implements Formatter {
200204 return false ;
201205 }
202206
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, '' ) ;
208209 }
209210
210211 protected iterateCstFormatting ( document : LangiumDocument , formattings : Map < string , FormattingAction > , options : FormattingOptions , range ?: Range ) : TextEdit [ ] {
@@ -382,7 +383,10 @@ export abstract class AbstractFormatter implements Formatter {
382383 context . indentation += ( tabs ?? 0 ) ;
383384 const edits : TextEdit [ ] = [ ] ;
384385 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+ }
386390 } else if ( lines !== undefined ) {
387391 edits . push ( this . createLineTextEdit ( betweenRange , lines , context , formatting . options ) ) ;
388392 } else if ( tabs !== undefined ) {
0 commit comments