Skip to content

Commit 73307cb

Browse files
mx990mickaelistria
authored andcommitted
Code Mining: Example for redraw of end-of-line annotation #1070
This change modifies the code mining example to show the redraw of end-of-line annotations. A text field is added at the top to change the label of end-of-line code minings through user input. Signed-off-by: Martin Jobst <[email protected]>
1 parent a71582a commit 73307cb

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

examples/org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/codemining/CodeMiningDemo.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
*/
1414
package org.eclipse.jface.text.examples.codemining;
1515

16+
import java.util.concurrent.atomic.AtomicReference;
17+
18+
import org.eclipse.jface.layout.GridDataFactory;
1619
import org.eclipse.jface.text.Document;
1720
import org.eclipse.jface.text.IDocument;
1821
import org.eclipse.jface.text.IRegion;
@@ -29,9 +32,10 @@
2932
import org.eclipse.jface.text.source.ISourceViewerExtension5;
3033
import org.eclipse.jface.text.source.SourceViewer;
3134
import org.eclipse.swt.SWT;
32-
import org.eclipse.swt.layout.FillLayout;
35+
import org.eclipse.swt.layout.GridLayout;
3336
import org.eclipse.swt.widgets.Display;
3437
import org.eclipse.swt.widgets.Shell;
38+
import org.eclipse.swt.widgets.Text;
3539

3640
/**
3741
* A Code Mining demo with class references and implementations minings.
@@ -43,9 +47,14 @@ public static void main(String[] args) throws Exception {
4347

4448
Display display = new Display();
4549
Shell shell = new Shell(display);
46-
shell.setLayout(new FillLayout());
50+
shell.setLayout(new GridLayout());
4751
shell.setText("Code Mining demo");
4852

53+
AtomicReference<String> endOfLineString = new AtomicReference<>("End of line");
54+
Text endOfLineText = new Text(shell, SWT.NONE);
55+
endOfLineText.setText(endOfLineString.get());
56+
GridDataFactory.fillDefaults().grab(true, false).applyTo(endOfLineText);
57+
4958
ISourceViewer sourceViewer = new SourceViewer(shell, null, SWT.V_SCROLL | SWT.BORDER);
5059
sourceViewer.setDocument(
5160
new Document("// Type class & new keyword and see references CodeMining\n"
@@ -63,6 +72,7 @@ public static void main(String[] args) throws Exception {
6372
+ "new 5\n" //
6473
+ "new 5\n"),
6574
new AnnotationModel());
75+
GridDataFactory.fillDefaults().grab(true, true).applyTo(sourceViewer.getTextWidget());
6676
// Add AnnotationPainter (required by CodeMining)
6777
addAnnotationPainter(sourceViewer);
6878
// Initialize codemining providers
@@ -71,7 +81,7 @@ public static void main(String[] args) throws Exception {
7181
new ClassImplementationsCodeMiningProvider(), //
7282
new ToEchoWithHeaderAndInlineCodeMiningProvider("echo"), //
7383
new EmptyLineCodeMiningProvider(), //
74-
new EchoAtEndOfLineCodeMiningProvider()});
84+
new EchoAtEndOfLineCodeMiningProvider(endOfLineString) });
7585
// Execute codemining in a reconciler
7686
MonoReconciler reconciler = new MonoReconciler(new IReconcilingStrategy() {
7787

@@ -92,6 +102,11 @@ public void reconcile(IRegion partition) {
92102
}, false);
93103
reconciler.install(sourceViewer);
94104

105+
endOfLineText.addModifyListener(event -> {
106+
endOfLineString.set(endOfLineText.getText());
107+
((ISourceViewerExtension5) sourceViewer).updateCodeMinings();
108+
});
109+
95110
shell.open();
96111
while (!shell.isDisposed()) {
97112
if (!display.readAndDispatch())

examples/org.eclipse.jface.text.examples/src/org/eclipse/jface/text/examples/codemining/EchoAtEndOfLineCodeMiningProvider.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.util.ArrayList;
1414
import java.util.List;
1515
import java.util.concurrent.CompletableFuture;
16+
import java.util.concurrent.atomic.AtomicReference;
1617
import java.util.function.Consumer;
1718

1819
import org.eclipse.core.runtime.IProgressMonitor;
@@ -26,6 +27,12 @@
2627

2728
public class EchoAtEndOfLineCodeMiningProvider extends AbstractCodeMiningProvider {
2829

30+
private final AtomicReference<String> text;
31+
32+
public EchoAtEndOfLineCodeMiningProvider(AtomicReference<String> text) {
33+
this.text = text;
34+
}
35+
2936
@Override
3037
public CompletableFuture<List<? extends ICodeMining>> provideCodeMinings(ITextViewer viewer,
3138
IProgressMonitor monitor) {
@@ -37,7 +44,7 @@ public CompletableFuture<List<? extends ICodeMining>> provideCodeMinings(ITextVi
3744
res.add(new LineEndCodeMining(document, i, this) {
3845
@Override
3946
public String getLabel() {
40-
return "End of line";
47+
return text.get();
4148
}
4249
@Override
4350
public boolean isResolved() {

0 commit comments

Comments
 (0)