Skip to content

Commit 47b1978

Browse files
authored
Add internal API to CEditor to allow CDT-LSP to contribute a "try it" banner (#1088)
Part of #968
1 parent 07fcd8f commit 47b1978

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

core/org.eclipse.cdt.ui/META-INF/MANIFEST.MF

+6-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ Export-Package: org.eclipse.cdt.internal.corext;x-internal:=true,
2222
org.eclipse.cdt.internal.ui.dialogs;x-internal:=true,
2323
org.eclipse.cdt.internal.ui.dialogs.cpaths;x-internal:=true,
2424
org.eclipse.cdt.internal.ui.dnd;x-internal:=true,
25-
org.eclipse.cdt.internal.ui.editor;x-friends:="org.eclipse.cdt.codan.ui,org.eclipse.cdt.codan.ui.cxx,org.eclipse.cdt.dsf.ui",
25+
org.eclipse.cdt.internal.ui.editor;
26+
x-friends:="org.eclipse.cdt.codan.ui,
27+
org.eclipse.cdt.codan.ui.cxx,
28+
org.eclipse.cdt.dsf.ui,
29+
org.eclipse.cdt.lsp",
2630
org.eclipse.cdt.internal.ui.editor.asm;x-internal:=true,
2731
org.eclipse.cdt.internal.ui.filters;x-internal:=true,
2832
org.eclipse.cdt.internal.ui.help;x-internal:=true,
@@ -51,6 +55,7 @@ Export-Package: org.eclipse.cdt.internal.corext;x-internal:=true,
5155
org.eclipse.cdt.internal.ui.resources,
5256
org.eclipse.cdt.internal.ui.search;x-internal:=true,
5357
org.eclipse.cdt.internal.ui.search.actions;x-internal:=true,
58+
org.eclipse.cdt.internal.ui.switchtolsp;x-friends:="org.eclipse.cdt.lsp",
5459
org.eclipse.cdt.internal.ui.text;x-friends:="org.eclipse.cdt.codan.ui.cxx,org.eclipse.cdt.dsf.ui",
5560
org.eclipse.cdt.internal.ui.text.asm;x-internal:=true,
5661
org.eclipse.cdt.internal.ui.text.c.hover;x-internal:=true,

core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditor.java

+28-1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
import org.eclipse.cdt.internal.ui.search.IOccurrencesFinder.OccurrenceLocation;
7575
import org.eclipse.cdt.internal.ui.search.OccurrencesFinder;
7676
import org.eclipse.cdt.internal.ui.search.actions.SelectionSearchGroup;
77+
import org.eclipse.cdt.internal.ui.switchtolsp.ISwitchToLsp;
7778
import org.eclipse.cdt.internal.ui.text.CHeuristicScanner;
7879
import org.eclipse.cdt.internal.ui.text.CPairMatcher;
7980
import org.eclipse.cdt.internal.ui.text.CSourceViewerScalableConfiguration;
@@ -111,6 +112,7 @@
111112
import org.eclipse.core.runtime.ListenerList;
112113
import org.eclipse.core.runtime.NullProgressMonitor;
113114
import org.eclipse.core.runtime.Platform;
115+
import org.eclipse.core.runtime.SafeRunner;
114116
import org.eclipse.core.runtime.Status;
115117
import org.eclipse.core.runtime.content.IContentType;
116118
import org.eclipse.core.runtime.jobs.Job;
@@ -2786,8 +2788,10 @@ public final ISourceViewer getViewer() {
27862788

27872789
@Override
27882790
protected ISourceViewer createSourceViewer(Composite parent, IVerticalRuler ruler, int styles) {
2791+
Composite editorComposite = createTryLspBanner(parent);
2792+
27892793
IPreferenceStore store = getPreferenceStore();
2790-
AdaptedSourceViewer cSourceViewer = new AdaptedSourceViewer(parent, ruler, getOverviewRuler(),
2794+
AdaptedSourceViewer cSourceViewer = new AdaptedSourceViewer(editorComposite, ruler, getOverviewRuler(),
27912795
isOverviewRulerVisible(), styles, store);
27922796

27932797
/*
@@ -2818,6 +2822,29 @@ protected ISourceViewer createSourceViewer(Composite parent, IVerticalRuler rule
28182822
return cSourceViewer;
28192823
}
28202824

2825+
/**
2826+
* Wraps {@link ISwitchToLsp#createTryLspEditor(org.eclipse.ui.texteditor.ITextEditor, Composite)}
2827+
* with the needed service access + fallback checks.
2828+
*
2829+
* If the {@link ISwitchToLsp} service doesn't exist, or fails, this method
2830+
* is a no-op that simply returns its input.
2831+
*
2832+
* @see ISwitchToLsp#createTryLspEditor(org.eclipse.ui.texteditor.ITextEditor, Composite)
2833+
*/
2834+
private Composite createTryLspBanner(Composite parent) {
2835+
Composite editorComposite = SafeRunner.run(() -> {
2836+
ISwitchToLsp switchToLsp = PlatformUI.getWorkbench().getService(ISwitchToLsp.class);
2837+
if (switchToLsp != null) {
2838+
return switchToLsp.createTryLspEditor(CEditor.this, parent);
2839+
}
2840+
return null;
2841+
});
2842+
if (editorComposite == null) {
2843+
editorComposite = parent;
2844+
}
2845+
return editorComposite;
2846+
}
2847+
28212848
/** Outliner context menu Id */
28222849
protected String fOutlinerContextMenuId;
28232850

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Kichwa Coders Canada Inc. and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*******************************************************************************/
11+
12+
package org.eclipse.cdt.internal.ui.switchtolsp;
13+
14+
import org.eclipse.cdt.internal.ui.editor.CEditor;
15+
import org.eclipse.swt.widgets.Composite;
16+
import org.eclipse.ui.texteditor.ITextEditor;
17+
18+
/**
19+
* This interface can be implemented by CDT-LSP to display a banner in the {@link CEditor}
20+
* to encourage users to try the new C/C++ editing experience based on CDT/LSP
21+
*
22+
* See org.eclipse.cdt.lsp/OSGI-INF/org.eclipse.cdt.lsp.internal.switchtolsp.SwitchToLsp.xml
23+
* for the use.
24+
*
25+
* This interface is not public API, it is provided as a hook for CDT LSP and may
26+
* be changed at any point.
27+
*/
28+
public interface ISwitchToLsp {
29+
30+
/**
31+
* Create the banner controls for the "try new experience"
32+
*
33+
* @param part the editor part that the banner is added on
34+
* @param parent the parent control
35+
* @return the new parent control the editor should use
36+
*/
37+
Composite createTryLspEditor(ITextEditor part, Composite parent);
38+
39+
}

0 commit comments

Comments
 (0)