diff options
5 files changed, 163 insertions, 19 deletions
diff --git a/org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/codemining/CodeMiningDemo.java b/org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/codemining/CodeMiningDemo.java index 6c22ebb0b66..64fb47d11d4 100644 --- a/org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/codemining/CodeMiningDemo.java +++ b/org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/codemining/CodeMiningDemo.java @@ -10,7 +10,6 @@ */
package org.eclipse.jface.text.examples.codemining;
-import org.eclipse.jface.internal.text.codemining.CodeMiningManager;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
@@ -24,9 +23,8 @@ import org.eclipse.jface.text.source.AnnotationModel; import org.eclipse.jface.text.source.AnnotationPainter;
import org.eclipse.jface.text.source.IAnnotationAccess;
import org.eclipse.jface.text.source.ISourceViewer;
-//import org.eclipse.jface.text.source.ISourceViewerExtension5;
+import org.eclipse.jface.text.source.ISourceViewerExtension5;
import org.eclipse.jface.text.source.SourceViewer;
-import org.eclipse.jface.text.source.inlined.InlinedAnnotationSupport;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
@@ -52,21 +50,16 @@ public class CodeMiningDemo { + "class A\n" + "new A\n" + "new A\n\n" + "class 5\n" + "new 5\n" + "new 5\n" + "new 5"),
new AnnotationModel());
// Add AnnotationPainter (required by CodeMining)
- AnnotationPainter painter = createAnnotationPainter(sourceViewer);
- // Install Inlined annotation support
- InlinedAnnotationSupport support = new InlinedAnnotationSupport();
- support.install(sourceViewer, painter);
-
- // Create manager
- CodeMiningManager manager = new CodeMiningManager(sourceViewer, support, new ICodeMiningProvider[] {
+ addAnnotationPainter(sourceViewer);
+ // Initialize codemining providers
+ ((ISourceViewerExtension5) sourceViewer).setCodeMiningProviders(new ICodeMiningProvider[] {
new ClassReferenceCodeMiningProvider(), new ClassImplementationsCodeMiningProvider() });
-
// Execute codemining in a reconciler
MonoReconciler reconciler = new MonoReconciler(new IReconcilingStrategy() {
@Override
public void setDocument(IDocument document) {
- manager.run();
+ ((ISourceViewerExtension5) sourceViewer).updateCodeMinings();
}
@Override
@@ -76,7 +69,7 @@ public class CodeMiningDemo { @Override
public void reconcile(IRegion partition) {
- manager.run();
+ ((ISourceViewerExtension5) sourceViewer).updateCodeMinings();
}
}, false);
reconciler.install(sourceViewer);
@@ -86,11 +79,10 @@ public class CodeMiningDemo { if (!display.readAndDispatch())
display.sleep();
}
- manager.uninstall();
display.dispose();
}
- private static AnnotationPainter createAnnotationPainter(ISourceViewer viewer) {
+ private static void addAnnotationPainter(ISourceViewer viewer) {
IAnnotationAccess annotationAccess = new IAnnotationAccess() {
@Override
public Object getType(Annotation annotation) {
@@ -110,7 +102,6 @@ public class CodeMiningDemo { };
AnnotationPainter painter = new AnnotationPainter(viewer, annotationAccess);
((ITextViewerExtension2) viewer).addPainter(painter);
- return painter;
}
}
diff --git a/org.eclipse.jface.text/src/org/eclipse/jface/text/source/ISourceViewer.java b/org.eclipse.jface.text/src/org/eclipse/jface/text/source/ISourceViewer.java index d8f4c2373b4..ab0eaa3d8f9 100644 --- a/org.eclipse.jface.text/src/org/eclipse/jface/text/source/ISourceViewer.java +++ b/org.eclipse.jface.text/src/org/eclipse/jface/text/source/ISourceViewer.java @@ -7,6 +7,7 @@ * * Contributors: * IBM Corporation - initial API and implementation + * Angelo Zerr <angelo.zerr@gmail.com> - [CodeMining] Add CodeMining support in SourceViewer - Bug 527515 *******************************************************************************/ package org.eclipse.jface.text.source; @@ -46,6 +47,8 @@ import org.eclipse.jface.text.ITextViewer; * to the quick assist invocation context as well as the current annotation hover.</li> * <li>{@link org.eclipse.jface.text.source.ISourceViewerExtension4} since version 3.4 * introducing API to access a minimal set of content assistant APIs.</li> + * <li>{@link org.eclipse.jface.text.source.ISourceViewerExtension5} since version 3.13 + * introducing API to access minimal set of code mining APIs.</li> * </ul></p> * <p> * Clients may implement this interface and its extension interfaces or use the diff --git a/org.eclipse.jface.text/src/org/eclipse/jface/text/source/ISourceViewerExtension5.java b/org.eclipse.jface.text/src/org/eclipse/jface/text/source/ISourceViewerExtension5.java new file mode 100644 index 00000000000..6cfa66cdeb6 --- /dev/null +++ b/org.eclipse.jface.text/src/org/eclipse/jface/text/source/ISourceViewerExtension5.java @@ -0,0 +1,43 @@ +/** + * Copyright (c) 2017 Angelo ZERR. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Angelo Zerr <angelo.zerr@gmail.com> - [CodeMining] Add CodeMining support in SourceViewer - Bug 527515 + */ +package org.eclipse.jface.text.source; + +import org.eclipse.jface.text.codemining.ICodeMining; +import org.eclipse.jface.text.codemining.ICodeMiningProvider; + +/** + * Extension interface for {@link org.eclipse.jface.text.source.ISourceViewer}. + * <p> + * It introduces API to access a minimal set of code mining APIs.</li> + * </p> + * + * @see ICodeMining + * @see ICodeMiningProvider + * @since 3.13 + */ +public interface ISourceViewerExtension5 { + + /** + * Update the code minings. + * + * Clients and implementors are responsible of calling this method when needed. A typical + * use-case can be to run it upon completion of a reconcilier and after a job that would compute + * all the necessary pre-requisites to insert code mining annotations. + */ + void updateCodeMinings(); + + /** + * Register the code mining providers. + * + * @param codeMiningProviders the code mining providers to register. + */ + void setCodeMiningProviders(ICodeMiningProvider[] codeMiningProviders); +} diff --git a/org.eclipse.jface.text/src/org/eclipse/jface/text/source/SourceViewer.java b/org.eclipse.jface.text/src/org/eclipse/jface/text/source/SourceViewer.java index c1eda300c50..0876ffd5783 100644 --- a/org.eclipse.jface.text/src/org/eclipse/jface/text/source/SourceViewer.java +++ b/org.eclipse.jface.text/src/org/eclipse/jface/text/source/SourceViewer.java @@ -10,6 +10,7 @@ * Tom Eicher (Avaloq Evolution AG) - block selection mode * Tom Hofmann (Perspectix AG) - bug 297572 * Sergey Prigogin (Google) - bug 441448 + * Angelo Zerr <angelo.zerr@gmail.com> - [CodeMining] Add CodeMining support in SourceViewer - Bug 527515 *******************************************************************************/ package org.eclipse.jface.text.source; @@ -28,6 +29,7 @@ import org.eclipse.swt.widgets.ScrollBar; import org.eclipse.jface.internal.text.NonDeletingPositionUpdater; import org.eclipse.jface.internal.text.StickyHoverManager; +import org.eclipse.jface.internal.text.codemining.CodeMiningManager; import org.eclipse.jface.text.AbstractHoverInformationControlManager; import org.eclipse.jface.text.BadLocationException; @@ -38,6 +40,7 @@ import org.eclipse.jface.text.DocumentRewriteSessionType; import org.eclipse.jface.text.IBlockTextSelection; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.IDocumentExtension4; +import org.eclipse.jface.text.IPainter; import org.eclipse.jface.text.IPositionUpdater; import org.eclipse.jface.text.IRegion; import org.eclipse.jface.text.IRewriteTarget; @@ -49,6 +52,7 @@ import org.eclipse.jface.text.ITextViewerExtension2; import org.eclipse.jface.text.Position; import org.eclipse.jface.text.Region; import org.eclipse.jface.text.TextViewer; +import org.eclipse.jface.text.codemining.ICodeMiningProvider; import org.eclipse.jface.text.contentassist.IContentAssistant; import org.eclipse.jface.text.contentassist.IContentAssistantExtension2; import org.eclipse.jface.text.contentassist.IContentAssistantExtension4; @@ -64,6 +68,7 @@ import org.eclipse.jface.text.projection.ChildDocument; import org.eclipse.jface.text.quickassist.IQuickAssistAssistant; import org.eclipse.jface.text.quickassist.IQuickAssistInvocationContext; import org.eclipse.jface.text.reconciler.IReconciler; +import org.eclipse.jface.text.source.inlined.InlinedAnnotationSupport; /** * SWT based implementation of @@ -82,7 +87,7 @@ import org.eclipse.jface.text.reconciler.IReconciler; * <p> * Clients may subclass this class but should expect some breakage by future releases.</p> */ -public class SourceViewer extends TextViewer implements ISourceViewer, ISourceViewerExtension, ISourceViewerExtension2, ISourceViewerExtension3, ISourceViewerExtension4 { +public class SourceViewer extends TextViewer implements ISourceViewer, ISourceViewerExtension, ISourceViewerExtension2, ISourceViewerExtension3, ISourceViewerExtension4, ISourceViewerExtension5 { /** @@ -348,6 +353,7 @@ public class SourceViewer extends TextViewer implements ISourceViewer, ISourceVi /** * The overview ruler. + * * @since 2.1 */ private IOverviewRuler fOverviewRuler; @@ -356,7 +362,30 @@ public class SourceViewer extends TextViewer implements ISourceViewer, ISourceVi * @since 2.1 */ private boolean fIsOverviewRulerVisible; - + /** + * The inlined annotation support used by code mining manager. + * + * @since 3.13 + */ + private InlinedAnnotationSupport fInlinedAnnotationSupport; + /** + * The text viewer's code mining providers. + * + * @since 3.13 + */ + private ICodeMiningProvider[] fCodeMiningProviders; + /** + * The text viewer's annotation painter to draw code mining annotations. + * + * @since 3.13 + */ + private AnnotationPainter fAnnotationPainter; + /** + * The text viewer's code mining manager. + * + * @since 3.13 + */ + private CodeMiningManager fCodeMiningManager; /** * Constructs a new source viewer. The vertical ruler is initially visible. @@ -524,6 +553,7 @@ public class SourceViewer extends TextViewer implements ISourceViewer, ISourceVi if (prefixes != null && prefixes.length > 0) setDefaultPrefixes(prefixes, t); } + setCodeMiningProviders(configuration.getCodeMiningProviders(this)); activatePlugins(); } @@ -731,6 +761,15 @@ public class SourceViewer extends TextViewer implements ISourceViewer, ISourceVi } setHyperlinkDetectors(null, SWT.NONE); + + if (fCodeMiningManager != null) { + fCodeMiningManager.uninstall(); + fCodeMiningManager= null; + } + setCodeMiningProviders(null); + if (fInlinedAnnotationSupport != null) { + fInlinedAnnotationSupport.uninstall(); + } } @Override @@ -1223,4 +1262,57 @@ public class SourceViewer extends TextViewer implements ISourceViewer, ISourceVi return fVerticalRulerHoveringController.getCurrentAnnotationHover(); } + @Override + public void setCodeMiningProviders(ICodeMiningProvider[] codeMiningProviders) { + if (fCodeMiningProviders != null) { + for (ICodeMiningProvider contentMiningProvider : fCodeMiningProviders) { + contentMiningProvider.dispose(); + } + } + boolean enable= codeMiningProviders != null && codeMiningProviders.length > 0; + fCodeMiningProviders= codeMiningProviders; + if (enable) { + if (fCodeMiningManager != null) { + fCodeMiningManager.setCodeMiningProviders(fCodeMiningProviders); + } + ensureCodeMiningManagerInstalled(); + } else { + if (fCodeMiningManager != null) + fCodeMiningManager.uninstall(); + fCodeMiningManager= null; + } + } + + /** + * Ensures that the code mining manager has been + * installed if a content mining provider is available. + * + * @since 3.13 + */ + private void ensureCodeMiningManagerInstalled() { + if (fCodeMiningProviders != null && fCodeMiningProviders.length > 0 && fAnnotationPainter != null) { + if (fInlinedAnnotationSupport == null) { + fInlinedAnnotationSupport= new InlinedAnnotationSupport(); + fInlinedAnnotationSupport.install(this, fAnnotationPainter); + } + fCodeMiningManager= new CodeMiningManager(this, fInlinedAnnotationSupport, fCodeMiningProviders); + } + } + + @Override + public void updateCodeMinings() { + if (fCodeMiningManager != null) { + fCodeMiningManager.run(); + } + } + + @Override + public void addPainter(IPainter painter) { + if (painter instanceof AnnotationPainter) { + fAnnotationPainter= (AnnotationPainter) painter; + ensureCodeMiningManagerInstalled(); + } + super.addPainter(painter); + } + } diff --git a/org.eclipse.jface.text/src/org/eclipse/jface/text/source/SourceViewerConfiguration.java b/org.eclipse.jface.text/src/org/eclipse/jface/text/source/SourceViewerConfiguration.java index 54e96502d42..f4e82d829a3 100644 --- a/org.eclipse.jface.text/src/org/eclipse/jface/text/source/SourceViewerConfiguration.java +++ b/org.eclipse.jface.text/src/org/eclipse/jface/text/source/SourceViewerConfiguration.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2008 IBM Corporation and others. + * Copyright (c) 2000, 2017 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -7,6 +7,7 @@ * * Contributors: * IBM Corporation - initial API and implementation + * Angelo Zerr <angelo.zerr@gmail.com> - [CodeMining] Add CodeMining support in SourceViewer - Bug 527515 *******************************************************************************/ package org.eclipse.jface.text.source; @@ -29,6 +30,7 @@ import org.eclipse.jface.text.ITextHover; import org.eclipse.jface.text.ITextViewerExtension2; import org.eclipse.jface.text.IUndoManager; import org.eclipse.jface.text.TextViewerUndoManager; +import org.eclipse.jface.text.codemining.ICodeMiningProvider; import org.eclipse.jface.text.contentassist.IContentAssistant; import org.eclipse.jface.text.formatter.IContentFormatter; import org.eclipse.jface.text.hyperlink.DefaultHyperlinkPresenter; @@ -417,4 +419,17 @@ public class SourceViewerConfiguration { public int getHyperlinkStateMask(ISourceViewer sourceViewer) { return SWT.MOD1; } + + /** + * Returns the code mining providers which be used to draw code minings in the given source + * viewer. This implementation always returns <code>null</code>. + * + * @param sourceViewer the source viewer to be configured by this configuration + * @return an array with code mining providers or <code>null</code> if no code mining support + * should be installed + * @since 3.13 + */ + public ICodeMiningProvider[] getCodeMiningProviders(ISourceViewer sourceViewer) { + return null; + } } |