2121import java .util .ArrayList ;
2222import java .util .Collections ;
2323import java .util .ConcurrentModificationException ;
24+ import java .util .List ;
2425import java .util .Optional ;
2526import java .util .concurrent .ExecutionException ;
2627
@@ -57,6 +58,29 @@ public void testFormattingInvalidDocument() throws Exception {
5758 assertTrue (edits .isEmpty ());
5859 }
5960
61+ /**
62+ * Verifies that when the language server reports no formatting edits
63+ * (empty edit list), {@link LSPFormatter} returns an empty Optional
64+ * and leaves the document content unchanged.
65+ */
66+ @ Test
67+ public void testFormattingEmptyEditsYieldEmptyOptional () throws Exception {
68+ MockLanguageServer .INSTANCE .setFormattingTextEdits (Collections .emptyList ());
69+
70+ IFile file = TestUtils .createUniqueTestFile (project , "Formatting Other Text" );
71+ IEditorPart editor = TestUtils .openEditor (file );
72+ ITextViewer viewer = LSPEclipseUtils .getTextViewer (editor );
73+
74+ final var formatter = new LSPFormatter ();
75+ ISelection selection = viewer .getSelectionProvider ().getSelection ();
76+
77+ Optional <VersionedEdits > edits = formatter .requestFormatting (viewer .getDocument (), (ITextSelection ) selection ).get ();
78+ assertTrue (edits .isEmpty ());
79+ assertEquals ("Formatting Other Text" , viewer .getDocument ().get ());
80+
81+ TestUtils .closeEditor (editor , false );
82+ }
83+
6084 @ Test
6185 public void testFormattingNoChanges () throws Exception {
6286 MockLanguageServer .INSTANCE .setFormattingTextEdits (Collections .emptyList ());
@@ -69,14 +93,10 @@ public void testFormattingNoChanges() throws Exception {
6993 ISelection selection = viewer .getSelectionProvider ().getSelection ();
7094
7195 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- });
96+
97+ // No formatting edits produced -> no VersionedEdits returned
98+ assertTrue (edits .isEmpty ());
99+
80100 final var textEditor = (ITextEditor ) editor ;
81101 textEditor .getDocumentProvider ().getDocument (textEditor .getEditorInput ());
82102 assertEquals ("Formatting Other Text" , viewer .getDocument ().get ());
@@ -243,7 +263,8 @@ public void testSelectiveFormattingWithIncapableServer() throws Exception {
243263 @ Test
244264 public void testOutdatedFormatting ()
245265 throws CoreException , InterruptedException , ExecutionException , BadLocationException {
246- MockLanguageServer .INSTANCE .setFormattingTextEdits (Collections .emptyList ());
266+ // Use a non-empty edit list so that a VersionedEdits is produced
267+ MockLanguageServer .INSTANCE .setFormattingTextEdits (List .of (new TextEdit (new Range (new Position (0 , 0 ), new Position (0 , 0 )), "X" )));
247268
248269 IFile file = TestUtils .createUniqueTestFile (project , "Formatting Other Text" );
249270 IEditorPart editor = TestUtils .openEditor (file );
0 commit comments