Skip to content

Commit c40d73b

Browse files
committed
test: add Fold All / Unfold All command test cases
1 parent 412b3e3 commit c40d73b

3 files changed

Lines changed: 131 additions & 2 deletions

File tree

org.eclipse.lsp4e.test/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: Tests for language server bundle (Incubation)
44
Bundle-SymbolicName: org.eclipse.lsp4e.test;singleton:=true
5-
Bundle-Version: 0.15.29.qualifier
5+
Bundle-Version: 0.15.30.qualifier
66
Fragment-Host: org.eclipse.lsp4e
77
Bundle-Vendor: Eclipse LSP4E
88
Bundle-RequiredExecutionEnvironment: JavaSE-17

org.eclipse.lsp4e.test/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</parent>
99
<artifactId>org.eclipse.lsp4e.test</artifactId>
1010
<packaging>eclipse-test-plugin</packaging>
11-
<version>0.15.29-SNAPSHOT</version>
11+
<version>0.15.30-SNAPSHOT</version>
1212

1313
<properties>
1414
<os-jvm-flags /> <!-- for the default case -->
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Vegard IT GmbH and others.
3+
* This program and the accompanying materials are made
4+
* available under the terms of the Eclipse Public License 2.0
5+
* which is available at https://www.eclipse.org/legal/epl-2.0/
6+
*
7+
* SPDX-License-Identifier: EPL-2.0
8+
*
9+
* Contributors:
10+
* Sebastian Thomschke (Vegard IT GmbH) - initial implementation
11+
*******************************************************************************/
12+
package org.eclipse.lsp4e.test.folding;
13+
14+
import static org.junit.Assert.*;
15+
16+
import java.util.List;
17+
18+
import org.eclipse.jface.preference.IPreferenceStore;
19+
import org.eclipse.jface.text.ITextViewer;
20+
import org.eclipse.jface.text.source.Annotation;
21+
import org.eclipse.jface.text.source.projection.ProjectionAnnotation;
22+
import org.eclipse.jface.text.source.projection.ProjectionAnnotationModel;
23+
import org.eclipse.jface.text.source.projection.ProjectionViewer;
24+
import org.eclipse.lsp4e.LSPEclipseUtils;
25+
import org.eclipse.lsp4e.LanguageServerPlugin;
26+
import org.eclipse.lsp4e.test.utils.AbstractTest;
27+
import org.eclipse.lsp4e.test.utils.TestUtils;
28+
import org.eclipse.lsp4e.tests.mock.MockLanguageServer;
29+
import org.eclipse.lsp4e.ui.FoldingPreferencePage;
30+
import org.eclipse.lsp4j.FoldingRange;
31+
import org.eclipse.lsp4j.FoldingRangeKind;
32+
import org.eclipse.ui.PlatformUI;
33+
import org.eclipse.ui.handlers.IHandlerService;
34+
import org.junit.Test;
35+
36+
public class FoldingCommandsTest extends AbstractTest {
37+
38+
private static final int MAX_WAIT_MS = 5_000;
39+
40+
private static final String CONTENT = """
41+
/**
42+
* SPDX-License-Identifier: EPL-2.0
43+
*/
44+
import
45+
import
46+
import
47+
/**
48+
* Some comment
49+
*/
50+
visible
51+
""";
52+
53+
@Test
54+
public void foldAndUnfoldAllCommands() throws Exception {
55+
// Ensure no auto-folding interferes with the command behavior
56+
configureAutoFolding(false);
57+
58+
// Provide folding ranges from the Mock LS: license header and imports
59+
final var foldingRangeLicense = new FoldingRange(0, 2);
60+
foldingRangeLicense.setKind(FoldingRangeKind.Comment);
61+
final var foldingRangeImport = new FoldingRange(3, 5);
62+
foldingRangeImport.setKind(FoldingRangeKind.Imports);
63+
MockLanguageServer.INSTANCE.setFoldingRanges(List.of(foldingRangeLicense, foldingRangeImport));
64+
65+
// Open editor and wait until folding annotations are present
66+
final var editor = TestUtils.openEditor(TestUtils.createUniqueTestFile(null, CONTENT));
67+
final ITextViewer viewer = LSPEclipseUtils.getTextViewer(editor);
68+
assertTrue(viewer instanceof ProjectionViewer);
69+
70+
final var pViewer = (ProjectionViewer) viewer;
71+
TestUtils.waitForAndAssertCondition(MAX_WAIT_MS, () -> getAnnotationModel(pViewer) != null);
72+
final ProjectionAnnotationModel model = getAnnotationModel(pViewer);
73+
74+
// Ensure folding annotations are populated after projection model exists
75+
// by toggling the folding-enabled preference to trigger a reconcile.
76+
IPreferenceStore store = LanguageServerPlugin.getDefault().getPreferenceStore();
77+
store.setValue(FoldingPreferencePage.PREF_FOLDING_ENABLED, false);
78+
store.setValue(FoldingPreferencePage.PREF_FOLDING_ENABLED, true);
79+
80+
TestUtils.waitForAndAssertCondition(MAX_WAIT_MS, () -> countAnnotations(model) == 2);
81+
assertEquals(2, countAnnotations(model));
82+
83+
// Execute "Fold All"
84+
IHandlerService handlerService = PlatformUI.getWorkbench().getService(IHandlerService.class);
85+
handlerService.executeCommand("org.eclipse.lsp4e.folding.collapseAll", null);
86+
87+
TestUtils.waitForAndAssertCondition(MAX_WAIT_MS, () -> countCollapsed(model) == 2);
88+
assertEquals(2, countCollapsed(model));
89+
90+
// Execute "Unfold All"
91+
handlerService.executeCommand("org.eclipse.lsp4e.folding.expandAll", null);
92+
TestUtils.waitForAndAssertCondition(MAX_WAIT_MS, () -> countCollapsed(model) == 0);
93+
assertEquals(0, countCollapsed(model));
94+
}
95+
96+
private static ProjectionAnnotationModel getAnnotationModel(ProjectionViewer viewer) {
97+
return viewer.getProjectionAnnotationModel();
98+
}
99+
100+
private static int countAnnotations(ProjectionAnnotationModel model) {
101+
int count = 0;
102+
for (var it = model.getAnnotationIterator(); it != null && it.hasNext();) {
103+
if (it.next() instanceof ProjectionAnnotation) {
104+
count++;
105+
}
106+
}
107+
return count;
108+
}
109+
110+
private static int countCollapsed(ProjectionAnnotationModel model) {
111+
int count = 0;
112+
for (var it = model.getAnnotationIterator(); it != null && it.hasNext();) {
113+
Annotation a = it.next();
114+
if (a instanceof ProjectionAnnotation pa && pa.isCollapsed()) {
115+
count++;
116+
}
117+
}
118+
return count;
119+
}
120+
121+
private static void configureAutoFolding(boolean enabled) {
122+
IPreferenceStore store = LanguageServerPlugin.getDefault().getPreferenceStore();
123+
store.setValue(FoldingPreferencePage.PREF_FOLDING_ENABLED, true);
124+
store.setValue(FoldingPreferencePage.PREF_AUTOFOLD_COMMENTS, enabled);
125+
store.setValue(FoldingPreferencePage.PREF_AUTOFOLD_LICENSE_HEADERS_COMMENTS, enabled);
126+
store.setValue(FoldingPreferencePage.PREF_AUTOFOLD_REGIONS, enabled);
127+
store.setValue(FoldingPreferencePage.PREF_AUTOFOLD_IMPORT_STATEMENTS, enabled);
128+
}
129+
}

0 commit comments

Comments
 (0)