Skip to content

Commit cc2c0ab

Browse files
committed
fix: add ViewerComparator compatibility workaround for Outline view
CommonViewerComparator was introduced in Eclipse 4.39 and so Outline can't use this class on older Eclipse versions. Let use old CommonViewerSorter for compatibility with older Eclipse releases. See eclipse-platform/eclipse.platform.ui#3621 Fixes #1515
1 parent 2380ada commit cc2c0ab

2 files changed

Lines changed: 74 additions & 2 deletions

File tree

org.eclipse.lsp4e/src/org/eclipse/lsp4e/outline/CNFOutlinePage.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.eclipse.jface.viewers.TreePath;
4141
import org.eclipse.jface.viewers.TreeSelection;
4242
import org.eclipse.jface.viewers.TreeViewer;
43+
import org.eclipse.jface.viewers.ViewerComparator;
4344
import org.eclipse.lsp4e.LSPEclipseUtils;
4445
import org.eclipse.lsp4e.LanguageServerPlugin;
4546
import org.eclipse.lsp4e.LanguageServerWrapper;
@@ -53,7 +54,6 @@
5354
import org.eclipse.swt.widgets.Control;
5455
import org.eclipse.ui.IActionBars;
5556
import org.eclipse.ui.navigator.CommonViewer;
56-
import org.eclipse.ui.navigator.CommonViewerComparator;
5757
import org.eclipse.ui.texteditor.ITextEditor;
5858
import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
5959

@@ -75,6 +75,9 @@ public class CNFOutlinePage implements IContentOutlinePage, ILabelProviderListen
7575

7676
private final LanguageServerWrapper wrapper;
7777

78+
@Nullable
79+
private static Boolean canUseCommonViewerComparator;
80+
7881
public CNFOutlinePage(LanguageServerWrapper wrapper, @Nullable ITextEditor textEditor) {
7982
preferences = InstanceScope.INSTANCE.getNode(LanguageServerPlugin.PLUGIN_ID);
8083
preferences.addPreferenceChangeListener(this);
@@ -90,7 +93,7 @@ public void createControl(final Composite parent) {
9093
if (document != null) {
9194
outlineViewer.setInput(new OutlineViewerInput(document, wrapper, textEditor));
9295
}
93-
outlineViewer.setComparator(new CommonViewerComparator());
96+
outlineViewer.setComparator(createComparator());
9497
outlineViewer.getLabelProvider().addListener(this);
9598
final var textEditor = this.textEditor;
9699
if (textEditor != null) {
@@ -124,6 +127,34 @@ public void createControl(final Composite parent) {
124127
}
125128
}
126129

130+
/**
131+
* Try to be compatible with older Eclipse versions (before 4.39) where
132+
* CommonViewerComparator is not available.
133+
*
134+
* @return comparator for the outline
135+
*/
136+
private static ViewerComparator createComparator() {
137+
if (checkIfCommonViewerComparatorAvailable()) {
138+
return Comparators.createCommonViewerComparator();
139+
}
140+
// Can be removed if Eclipse 4.38 is no longer supported
141+
return Comparators.createCommonViewerSorter();
142+
}
143+
144+
private static boolean checkIfCommonViewerComparatorAvailable() {
145+
if(canUseCommonViewerComparator != null) {
146+
return canUseCommonViewerComparator.booleanValue();
147+
}
148+
try {
149+
Class.forName("org.eclipse.ui.navigator.CommonViewerComparator"); //$NON-NLS-1$
150+
canUseCommonViewerComparator = Boolean.TRUE;
151+
return true;
152+
} catch (ClassNotFoundException e) {
153+
canUseCommonViewerComparator = Boolean.FALSE;
154+
return false;
155+
}
156+
}
157+
127158
/**
128159
* Returns the range of the given selection and null otherwise.
129160
*
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2026 Andrey Loskutov <loskutov@gmx.de>.
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+
* Andrey Loskutov <loskutov@gmx.de> - initial implementation
11+
*******************************************************************************/
12+
package org.eclipse.lsp4e.outline;
13+
14+
import org.eclipse.jface.viewers.ViewerComparator;
15+
import org.eclipse.ui.navigator.CommonViewerComparator;
16+
import org.eclipse.ui.navigator.CommonViewerSorter;
17+
18+
/**
19+
* Support different Eclipse platform versions providing different ViewerComparator implementations
20+
*/
21+
public class Comparators {
22+
/**
23+
* Compatible with modern Eclipse versions (4.39 and later) where
24+
* CommonViewerComparator is available.
25+
*
26+
* @return comparator for the outline
27+
*/
28+
public static ViewerComparator createCommonViewerComparator() {
29+
return new CommonViewerComparator();
30+
}
31+
32+
/**
33+
* Compatible with older Eclipse versions (before 4.39) where
34+
* CommonViewerComparator is not available.
35+
*
36+
* @return comparator for the outline
37+
*/
38+
public static ViewerComparator createCommonViewerSorter() {
39+
return new CommonViewerSorter();
40+
}
41+
}

0 commit comments

Comments
 (0)