Skip to content

Commit

Permalink
Add internal API to CEditor to allow CDT-LSP to contribute a "try it"…
Browse files Browse the repository at this point in the history
… banner

Part of eclipse-cdt#968
  • Loading branch information
jonahgraham committed Feb 16, 2025
1 parent ca5dabc commit be7896c
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
7 changes: 6 additions & 1 deletion core/org.eclipse.cdt.ui/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ Export-Package: org.eclipse.cdt.internal.corext;x-internal:=true,
org.eclipse.cdt.internal.ui.dialogs;x-internal:=true,
org.eclipse.cdt.internal.ui.dialogs.cpaths;x-internal:=true,
org.eclipse.cdt.internal.ui.dnd;x-internal:=true,
org.eclipse.cdt.internal.ui.editor;x-friends:="org.eclipse.cdt.codan.ui,org.eclipse.cdt.codan.ui.cxx,org.eclipse.cdt.dsf.ui",
org.eclipse.cdt.internal.ui.editor;
x-friends:="org.eclipse.cdt.codan.ui,
org.eclipse.cdt.codan.ui.cxx,
org.eclipse.cdt.dsf.ui,
org.eclipse.cdt.lsp",
org.eclipse.cdt.internal.ui.editor.asm;x-internal:=true,
org.eclipse.cdt.internal.ui.filters;x-internal:=true,
org.eclipse.cdt.internal.ui.help;x-internal:=true,
Expand Down Expand Up @@ -51,6 +55,7 @@ Export-Package: org.eclipse.cdt.internal.corext;x-internal:=true,
org.eclipse.cdt.internal.ui.resources,
org.eclipse.cdt.internal.ui.search;x-internal:=true,
org.eclipse.cdt.internal.ui.search.actions;x-internal:=true,
org.eclipse.cdt.internal.ui.switchtolsp;x-friends:="org.eclipse.cdt.lsp",
org.eclipse.cdt.internal.ui.text;x-friends:="org.eclipse.cdt.codan.ui.cxx,org.eclipse.cdt.dsf.ui",
org.eclipse.cdt.internal.ui.text.asm;x-internal:=true,
org.eclipse.cdt.internal.ui.text.c.hover;x-internal:=true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
import org.eclipse.cdt.internal.ui.search.IOccurrencesFinder.OccurrenceLocation;
import org.eclipse.cdt.internal.ui.search.OccurrencesFinder;
import org.eclipse.cdt.internal.ui.search.actions.SelectionSearchGroup;
import org.eclipse.cdt.internal.ui.switchtolsp.ISwitchToLsp;
import org.eclipse.cdt.internal.ui.text.CHeuristicScanner;
import org.eclipse.cdt.internal.ui.text.CPairMatcher;
import org.eclipse.cdt.internal.ui.text.CSourceViewerScalableConfiguration;
Expand Down Expand Up @@ -111,6 +112,7 @@
import org.eclipse.core.runtime.ListenerList;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.SafeRunner;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.core.runtime.jobs.Job;
Expand Down Expand Up @@ -2786,8 +2788,10 @@ public final ISourceViewer getViewer() {

@Override
protected ISourceViewer createSourceViewer(Composite parent, IVerticalRuler ruler, int styles) {
Composite editorComposite = createTryLspBanner(parent);

IPreferenceStore store = getPreferenceStore();
AdaptedSourceViewer cSourceViewer = new AdaptedSourceViewer(parent, ruler, getOverviewRuler(),
AdaptedSourceViewer cSourceViewer = new AdaptedSourceViewer(editorComposite, ruler, getOverviewRuler(),
isOverviewRulerVisible(), styles, store);

/*
Expand Down Expand Up @@ -2818,6 +2822,29 @@ protected ISourceViewer createSourceViewer(Composite parent, IVerticalRuler rule
return cSourceViewer;
}

/**
* Wraps {@link ISwitchToLsp#createTryLspEditor(org.eclipse.ui.texteditor.ITextEditor, Composite)}
* with the needed service access + fallback checks.
*
* If the {@link ISwitchToLsp} service doesn't exist, or fails, this method
* is a no-op that simply returns its input.
*
* @see ISwitchToLsp#createTryLspEditor(org.eclipse.ui.texteditor.ITextEditor, Composite)
*/
private Composite createTryLspBanner(Composite parent) {
Composite editorComposite = SafeRunner.run(() -> {
ISwitchToLsp switchToLsp = PlatformUI.getWorkbench().getService(ISwitchToLsp.class);
if (switchToLsp != null) {
return switchToLsp.createTryLspEditor(CEditor.this, parent);
}
return null;
});
if (editorComposite == null) {
editorComposite = parent;
}
return editorComposite;
}

/** Outliner context menu Id */
protected String fOutlinerContextMenuId;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*******************************************************************************
* Copyright (c) 2025 Kichwa Coders Canada Inc. and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/

package org.eclipse.cdt.internal.ui.switchtolsp;

import org.eclipse.cdt.internal.ui.editor.CEditor;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.texteditor.ITextEditor;

/**
* This interface can be implemented by CDT-LSP to display a banner in the {@link CEditor}
* to encourage users to try the new C/C++ editing experience based on CDT/LSP
*
* See org.eclipse.cdt.lsp/OSGI-INF/org.eclipse.cdt.lsp.internal.switchtolsp.SwitchToLsp.xml
* for the use.
*
* This interface is not public API, it is provided as a hook for CDT LSP and may
* be changed at any point.
*/
public interface ISwitchToLsp {

/**
* Create the banner controls for the "try new experience"
*
* @param part the editor part that the banner is added on
* @param parent the parent control
* @return the new parent control the editor should use
*/
Composite createTryLspEditor(ITextEditor part, Composite parent);

}

0 comments on commit be7896c

Please sign in to comment.