Skip to content

Commit 92a9730

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 92a9730

2 files changed

Lines changed: 73 additions & 2 deletions

File tree

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

Lines changed: 32 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,8 @@ public class CNFOutlinePage implements IContentOutlinePage, ILabelProviderListen
7575

7676
private final LanguageServerWrapper wrapper;
7777

78+
private static Boolean canUseCommonViewerComparator;
79+
7880
public CNFOutlinePage(LanguageServerWrapper wrapper, @Nullable ITextEditor textEditor) {
7981
preferences = InstanceScope.INSTANCE.getNode(LanguageServerPlugin.PLUGIN_ID);
8082
preferences.addPreferenceChangeListener(this);
@@ -90,7 +92,7 @@ public void createControl(final Composite parent) {
9092
if (document != null) {
9193
outlineViewer.setInput(new OutlineViewerInput(document, wrapper, textEditor));
9294
}
93-
outlineViewer.setComparator(new CommonViewerComparator());
95+
outlineViewer.setComparator(createComparator());
9496
outlineViewer.getLabelProvider().addListener(this);
9597
final var textEditor = this.textEditor;
9698
if (textEditor != null) {
@@ -124,6 +126,34 @@ public void createControl(final Composite parent) {
124126
}
125127
}
126128

129+
/**
130+
* Try to be compatible with older Eclipse versions (before 4.39) where
131+
* CommonViewerComparator is not available.
132+
*
133+
* @return comparator for the outline
134+
*/
135+
private static ViewerComparator createComparator() {
136+
if (checkIfCommonViewerComparatorAvailable()) {
137+
return Comparators.createCommonViewerComparator();
138+
}
139+
// Can be removed if Eclipse 4.38 is no longer supported
140+
return Comparators.createCommonViewerSorter();
141+
}
142+
143+
private static boolean checkIfCommonViewerComparatorAvailable() {
144+
if(canUseCommonViewerComparator != null) {
145+
return canUseCommonViewerComparator.booleanValue();
146+
}
147+
try {
148+
Class.forName("org.eclipse.ui.navigator.CommonViewerComparator"); //$NON-NLS-1$
149+
canUseCommonViewerComparator = Boolean.TRUE;
150+
return true;
151+
} catch (ClassNotFoundException e) {
152+
canUseCommonViewerComparator = Boolean.FALSE;
153+
return false;
154+
}
155+
}
156+
127157
/**
128158
* Returns the range of the given selection and null otherwise.
129159
*
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)