1111 *******************************************************************************/
1212package org .eclipse .lsp4e .test .format ;
1313
14- import static org .eclipse .lsp4e .test .utils .TestUtils .numberOfChangesIs ;
15- import static org .eclipse .lsp4e .test .utils .TestUtils .waitForAndAssertCondition ;
16- import static org .junit .jupiter .api .Assertions .assertEquals ;
17- import static org .junit .jupiter .api .Assertions .assertThrows ;
18- import static org .junit .jupiter .api .Assertions .assertTrue ;
19- import static org .junit .jupiter .api .Assertions .fail ;
14+ import static org .eclipse .lsp4e .test .utils .TestUtils .*;
15+ import static org .junit .jupiter .api .Assertions .*;
2016
2117import java .util .ArrayList ;
2218import java .util .Collections ;
2319import java .util .ConcurrentModificationException ;
20+ import java .util .List ;
2421import java .util .Optional ;
2522import java .util .concurrent .ExecutionException ;
2623
@@ -57,6 +54,29 @@ public void testFormattingInvalidDocument() throws Exception {
5754 assertTrue (edits .isEmpty ());
5855 }
5956
57+ /**
58+ * Verifies that when the language server reports no formatting edits
59+ * (empty edit list), {@link LSPFormatter} returns an empty Optional
60+ * and leaves the document content unchanged.
61+ */
62+ @ Test
63+ public void testFormattingEmptyEditsYieldEmptyOptional () throws Exception {
64+ MockLanguageServer .INSTANCE .setFormattingTextEdits (Collections .emptyList ());
65+
66+ IFile file = TestUtils .createUniqueTestFile (project , "Formatting Other Text" );
67+ IEditorPart editor = TestUtils .openEditor (file );
68+ ITextViewer viewer = LSPEclipseUtils .getTextViewer (editor );
69+
70+ final var formatter = new LSPFormatter ();
71+ ISelection selection = viewer .getSelectionProvider ().getSelection ();
72+
73+ Optional <VersionedEdits > edits = formatter .requestFormatting (viewer .getDocument (), (ITextSelection ) selection ).get ();
74+ assertTrue (edits .isEmpty ());
75+ assertEquals ("Formatting Other Text" , viewer .getDocument ().get ());
76+
77+ TestUtils .closeEditor (editor , false );
78+ }
79+
6080 @ Test
6181 public void testFormattingNoChanges () throws Exception {
6282 MockLanguageServer .INSTANCE .setFormattingTextEdits (Collections .emptyList ());
@@ -69,14 +89,10 @@ public void testFormattingNoChanges() throws Exception {
6989 ISelection selection = viewer .getSelectionProvider ().getSelection ();
7090
7191 Optional <VersionedEdits > edits = formatter .requestFormatting (viewer .getDocument (), (ITextSelection ) selection ).get ();
72- assertTrue (edits .isPresent ());
73- editor .getSite ().getShell ().getDisplay ().syncExec (() -> {
74- try {
75- edits .get ().apply ();
76- } catch (ConcurrentModificationException | BadLocationException e ) {
77- fail (e .getMessage ());
78- }
79- });
92+
93+ // No formatting edits produced -> no VersionedEdits returned
94+ assertTrue (edits .isEmpty ());
95+
8096 final var textEditor = (ITextEditor ) editor ;
8197 textEditor .getDocumentProvider ().getDocument (textEditor .getEditorInput ());
8298 assertEquals ("Formatting Other Text" , viewer .getDocument ().get ());
@@ -243,7 +259,8 @@ public void testSelectiveFormattingWithIncapableServer() throws Exception {
243259 @ Test
244260 public void testOutdatedFormatting ()
245261 throws CoreException , InterruptedException , ExecutionException , BadLocationException {
246- MockLanguageServer .INSTANCE .setFormattingTextEdits (Collections .emptyList ());
262+ // Use a non-empty edit list so that a VersionedEdits is produced
263+ MockLanguageServer .INSTANCE .setFormattingTextEdits (List .of (new TextEdit (new Range (new Position (0 , 0 ), new Position (0 , 0 )), "X" )));
247264
248265 IFile file = TestUtils .createUniqueTestFile (project , "Formatting Other Text" );
249266 IEditorPart editor = TestUtils .openEditor (file );
0 commit comments