Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorangelozerr2017-12-11 04:38:47 -0500
committerangelozerr2017-12-11 04:39:53 -0500
commitf1942faa93537121ccad39ea194f1b5c04583065 (patch)
treeee8aac2c1d08091e0cdd9393cf0ed9e147b527dd /org.eclipse.jface.text.examples
parentaa8c7fbeb626401b6ffd80dfa3b6331dbd77dc92 (diff)
downloadeclipse.platform.text-f1942faa93537121ccad39ea194f1b5c04583065.tar.gz
eclipse.platform.text-f1942faa93537121ccad39ea194f1b5c04583065.tar.xz
eclipse.platform.text-f1942faa93537121ccad39ea194f1b5c04583065.zip
Bug 527720 - [CodeMining] Provide CodeMining support with
CodeMiningManager Change-Id: I1eff5eeecdefe05598bc695a34e0f4e59fca886b Signed-off-by: angelozerr <angelo.zerr@gmail.com>
Diffstat (limited to 'org.eclipse.jface.text.examples')
-rw-r--r--org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/codemining/AbstractClassCodeMining.java47
-rw-r--r--org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/codemining/ClassImplementationCodeMining.java49
-rw-r--r--org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/codemining/ClassImplementationsCodeMiningProvider.java66
-rw-r--r--org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/codemining/ClassReferenceCodeMining.java71
-rw-r--r--org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/codemining/ClassReferenceCodeMiningProvider.java57
-rw-r--r--org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/codemining/CodeMiningDemo.java116
6 files changed, 406 insertions, 0 deletions
diff --git a/org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/codemining/AbstractClassCodeMining.java b/org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/codemining/AbstractClassCodeMining.java
new file mode 100644
index 000000000..ebfc74bb7
--- /dev/null
+++ b/org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/codemining/AbstractClassCodeMining.java
@@ -0,0 +1,47 @@
+/**
+ * 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.examples.codemining;
+
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.codemining.AbstractCodeMining;
+import org.eclipse.jface.text.codemining.ICodeMiningProvider;
+
+/**
+ * Abstract class for class name mining.
+ *
+ */
+public abstract class AbstractClassCodeMining extends AbstractCodeMining {
+
+ private final String className;
+
+ public AbstractClassCodeMining(String className, int afterLineNumber, IDocument document,
+ ICodeMiningProvider resolver) throws BadLocationException {
+ super(afterLineNumber, document, resolver);
+ this.className = className;
+ }
+
+ public String getClassName() {
+ return className;
+ }
+
+ public static String getLineText(IDocument document, int line) {
+ try {
+ int lo = document.getLineOffset(line);
+ int ll = document.getLineLength(line);
+ return document.get(lo, ll);
+ } catch (Exception e) {
+ e.printStackTrace();
+ return null;
+ }
+ }
+
+}
diff --git a/org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/codemining/ClassImplementationCodeMining.java b/org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/codemining/ClassImplementationCodeMining.java
new file mode 100644
index 000000000..153fa7f5e
--- /dev/null
+++ b/org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/codemining/ClassImplementationCodeMining.java
@@ -0,0 +1,49 @@
+/**
+ * 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.examples.codemining;
+
+import java.util.concurrent.CompletableFuture;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.ITextViewer;
+import org.eclipse.jface.text.codemining.ICodeMiningProvider;
+
+/**
+ * Class implementation mining.
+ *
+ */
+public class ClassImplementationCodeMining extends AbstractClassCodeMining {
+
+ public ClassImplementationCodeMining(String className, int afterLineNumber, IDocument document,
+ ICodeMiningProvider provider) throws BadLocationException {
+ super(className, afterLineNumber, document, provider);
+ }
+
+ @Override
+ protected CompletableFuture<Void> doResolve(ITextViewer viewer, IProgressMonitor monitor) {
+ return CompletableFuture.runAsync(() -> {
+ IDocument document = viewer.getDocument();
+ String className = super.getClassName();
+ int refCount = 0;
+ int lineCount = document.getNumberOfLines();
+ for (int i = 0; i < lineCount; i++) {
+ // check if request was canceled.
+ monitor.isCanceled();
+ String line = getLineText(document, i);
+ refCount += line.contains("implements " + className) ? 1 : 0;
+ }
+ super.setLabel(refCount + " implementation");
+ });
+ }
+
+}
diff --git a/org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/codemining/ClassImplementationsCodeMiningProvider.java b/org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/codemining/ClassImplementationsCodeMiningProvider.java
new file mode 100644
index 000000000..303061fc8
--- /dev/null
+++ b/org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/codemining/ClassImplementationsCodeMiningProvider.java
@@ -0,0 +1,66 @@
+/**
+ * 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.examples.codemining;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.ITextViewer;
+import org.eclipse.jface.text.codemining.AbstractCodeMiningProvider;
+import org.eclipse.jface.text.codemining.ICodeMining;
+
+/**
+ * Class implementation mining provider.
+ *
+ */
+public class ClassImplementationsCodeMiningProvider extends AbstractCodeMiningProvider {
+
+ @Override
+ public CompletableFuture<List<? extends ICodeMining>> provideCodeMinings(ITextViewer viewer,
+ IProgressMonitor monitor) {
+ return CompletableFuture.supplyAsync(() -> {
+ IDocument document = viewer.getDocument();
+ List<ICodeMining> lenses = new ArrayList<>();
+ int lineCount = document.getNumberOfLines();
+ for (int i = 0; i < lineCount; i++) {
+ // check if request was canceled.
+ monitor.isCanceled();
+ updateContentMining(i, document, "class ", lenses);
+ updateContentMining(i, document, "interface ", lenses);
+ }
+ return lenses;
+ });
+ }
+
+ private void updateContentMining(int lineIndex, IDocument document, String token, List<ICodeMining> lenses) {
+ String line = AbstractClassCodeMining.getLineText(document, lineIndex).trim();
+ int index = line.indexOf(token);
+ if (index == 0) {
+ String className = line.substring(index + token.length(), line.length());
+ index = className.indexOf(" ");
+ if (index != -1) {
+ className = className.substring(0, index);
+ }
+ if (className.length() > 0) {
+ try {
+ lenses.add(new ClassImplementationCodeMining(className, lineIndex, document, this));
+ } catch (BadLocationException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ }
+
+}
diff --git a/org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/codemining/ClassReferenceCodeMining.java b/org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/codemining/ClassReferenceCodeMining.java
new file mode 100644
index 000000000..84b988bec
--- /dev/null
+++ b/org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/codemining/ClassReferenceCodeMining.java
@@ -0,0 +1,71 @@
+/**
+ * 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.examples.codemining;
+
+import java.util.concurrent.CancellationException;
+import java.util.concurrent.CompletableFuture;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.ITextViewer;
+import org.eclipse.jface.text.codemining.ICodeMiningProvider;
+
+/**
+ * Class reference mining.
+ *
+ */
+public class ClassReferenceCodeMining extends AbstractClassCodeMining {
+
+ private Object lock = new Object();
+
+ public ClassReferenceCodeMining(String className, int afterLineNumber, IDocument document, ICodeMiningProvider provider)
+ throws BadLocationException {
+ super(className, afterLineNumber, document, provider);
+ }
+
+ @Override
+ protected CompletableFuture<Void> doResolve(ITextViewer viewer, IProgressMonitor monitor) {
+ return CompletableFuture.runAsync(() -> {
+ IDocument document = viewer.getDocument();
+ String className = super.getClassName();
+ try {
+ int wait = Integer.parseInt(className);
+ try {
+ for (int i = 0; i < wait; i++) {
+ monitor.isCanceled();
+ synchronized (lock) {
+ lock.wait(1000);
+ }
+ }
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ }
+ } catch (NumberFormatException e) {
+
+ } catch (CancellationException e) {
+ e.printStackTrace();
+ throw e;
+ }
+
+ int refCount = 0;
+ int lineCount = document.getNumberOfLines();
+ for (int i = 0; i < lineCount; i++) {
+ // check if request was canceled.
+ monitor.isCanceled();
+ String line = getLineText(document, i);
+ refCount += line.contains("new " + className) ? 1 : 0;
+ }
+ super.setLabel(refCount + " references");
+ });
+ }
+
+}
diff --git a/org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/codemining/ClassReferenceCodeMiningProvider.java b/org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/codemining/ClassReferenceCodeMiningProvider.java
new file mode 100644
index 000000000..9c7305a6e
--- /dev/null
+++ b/org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/codemining/ClassReferenceCodeMiningProvider.java
@@ -0,0 +1,57 @@
+/**
+ * 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.examples.codemining;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.ITextViewer;
+import org.eclipse.jface.text.codemining.AbstractCodeMiningProvider;
+import org.eclipse.jface.text.codemining.ICodeMining;
+
+/**
+ * Class reference mining provider.
+ *
+ */
+public class ClassReferenceCodeMiningProvider extends AbstractCodeMiningProvider {
+
+ @Override
+ public CompletableFuture<List<? extends ICodeMining>> provideCodeMinings(ITextViewer viewer,
+ IProgressMonitor monitor) {
+ return CompletableFuture.supplyAsync(() -> {
+ IDocument document = viewer.getDocument();
+ List<ICodeMining> lenses = new ArrayList<>();
+ int lineCount = document.getNumberOfLines();
+ for (int i = 0; i < lineCount; i++) {
+ // check if request was canceled.
+ monitor.isCanceled();
+ String line = AbstractClassCodeMining.getLineText(document, i).trim();
+ int index = line.indexOf("class ");
+ if (index == 0) {
+ String className = line.substring(index + "class ".length(), line.length()).trim();
+ if (className.length() > 0) {
+ try {
+ lenses.add(new ClassReferenceCodeMining(className, i, document, this));
+ } catch (BadLocationException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ }
+ return lenses;
+ });
+ }
+
+}
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
new file mode 100644
index 000000000..6c22ebb0b
--- /dev/null
+++ b/org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/codemining/CodeMiningDemo.java
@@ -0,0 +1,116 @@
+/**
+ * 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.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;
+import org.eclipse.jface.text.ITextViewerExtension2;
+import org.eclipse.jface.text.codemining.ICodeMiningProvider;
+import org.eclipse.jface.text.reconciler.DirtyRegion;
+import org.eclipse.jface.text.reconciler.IReconcilingStrategy;
+import org.eclipse.jface.text.reconciler.MonoReconciler;
+import org.eclipse.jface.text.source.Annotation;
+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.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;
+import org.eclipse.swt.widgets.Shell;
+
+/**
+ * A Code Mining demo with class references and implementations minings.
+ *
+ */
+public class CodeMiningDemo {
+
+ public static void main(String[] args) throws Exception {
+
+ Display display = new Display();
+ Shell shell = new Shell(display);
+ shell.setLayout(new FillLayout());
+ shell.setText("Code Mining demo");
+
+ ISourceViewer sourceViewer = new SourceViewer(shell, null, SWT.V_SCROLL | SWT.BORDER);
+ sourceViewer.setDocument(
+ new Document("// Type class & new keyword and see references CodeMining\n"
+ + "// Name class with a number N to emulate Nms before resolving the references CodeMining \n\n"
+ + "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[] {
+ new ClassReferenceCodeMiningProvider(), new ClassImplementationsCodeMiningProvider() });
+
+ // Execute codemining in a reconciler
+ MonoReconciler reconciler = new MonoReconciler(new IReconcilingStrategy() {
+
+ @Override
+ public void setDocument(IDocument document) {
+ manager.run();
+ }
+
+ @Override
+ public void reconcile(DirtyRegion dirtyRegion, IRegion subRegion) {
+
+ }
+
+ @Override
+ public void reconcile(IRegion partition) {
+ manager.run();
+ }
+ }, false);
+ reconciler.install(sourceViewer);
+
+ shell.open();
+ while (!shell.isDisposed()) {
+ if (!display.readAndDispatch())
+ display.sleep();
+ }
+ manager.uninstall();
+ display.dispose();
+ }
+
+ private static AnnotationPainter createAnnotationPainter(ISourceViewer viewer) {
+ IAnnotationAccess annotationAccess = new IAnnotationAccess() {
+ @Override
+ public Object getType(Annotation annotation) {
+ return annotation.getType();
+ }
+
+ @Override
+ public boolean isMultiLine(Annotation annotation) {
+ return true;
+ }
+
+ @Override
+ public boolean isTemporary(Annotation annotation) {
+ return true;
+ }
+
+ };
+ AnnotationPainter painter = new AnnotationPainter(viewer, annotationAccess);
+ ((ITextViewerExtension2) viewer).addPainter(painter);
+ return painter;
+ }
+
+}

Back to the top