Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*
* Contributors:
* Angelo Zerr <[email protected]> - [CodeMining] Provide CodeMining support with CodeMiningManager - Bug 527720
* Dietrich Travkin <[email protected]> - Fix code mining redrawing - Issue 3405
*/
package org.eclipse.jface.internal.text.codemining;

Expand Down Expand Up @@ -230,6 +231,41 @@ private static Map<Position, List<ICodeMining>> groupByLines(List<? extends ICod
Collectors.mapping(Function.identity(), Collectors.toList())));
}

private static enum CodeMiningMode {
InLine(LineContentCodeMining.class, CodeMiningLineContentAnnotation.class),
HeaderLine(LineHeaderCodeMining.class, CodeMiningLineHeaderAnnotation.class),
FooterLine(DocumentFooterCodeMining.class, CodeMiningDocumentFooterAnnotation.class);

public final Class<? extends ICodeMining> codeMiningType;
public final Class<? extends AbstractInlinedAnnotation> annotationType;

CodeMiningMode(Class<? extends ICodeMining> codeMiningType,
Class<? extends AbstractInlinedAnnotation> annotationType) {
this.codeMiningType= codeMiningType;
this.annotationType= annotationType;
}

public static CodeMiningMode createFor(List<ICodeMining> minings) {
Assert.isNotNull(minings);

CodeMiningMode mode= CodeMiningMode.HeaderLine;
if (!minings.isEmpty()) {
ICodeMining first= minings.get(0);

if (CodeMiningMode.InLine.codeMiningType.isInstance(first)) {
mode= CodeMiningMode.InLine;
} else if (CodeMiningMode.HeaderLine.codeMiningType.isInstance(first)) {
mode= CodeMiningMode.HeaderLine;
} else if (CodeMiningMode.FooterLine.codeMiningType.isInstance(first)) {
mode= CodeMiningMode.FooterLine;
} else {
mode= CodeMiningMode.InLine;
}
}
return mode;
}
}

/**
* Render the codemining grouped by line position.
*
Expand Down Expand Up @@ -257,11 +293,13 @@ private void renderCodeMinings(Map<Position, List<ICodeMining>> groups, ISourceV
Position pos= new Position(g.getKey().offset, g.getKey().length);
List<ICodeMining> minings= g.getValue();
ICodeMining first= minings.get(0);
boolean inLineHeader= !minings.isEmpty() ? (first instanceof LineHeaderCodeMining) : true;

CodeMiningMode mode= CodeMiningMode.createFor(minings);

// Try to find existing annotation
AbstractInlinedAnnotation ann= fInlinedAnnotationSupport.findExistingAnnotation(pos);
if (ann == null) {
// The annotation doesn't exists, create it.
if (ann == null || !mode.annotationType.isInstance(ann)) {
// The annotation doesn't exists or has wrong type => create a new one.
boolean afterPosition= false;
if (first instanceof LineContentCodeMining m) {
afterPosition= m.isAfterPosition();
Expand All @@ -274,16 +312,14 @@ private void renderCodeMinings(Map<Position, List<ICodeMining>> groups, ISourceV
mouseOut= first.getMouseOut();
mouseMove= first.getMouseMove();
}
if (inLineHeader) {
ann= new CodeMiningLineHeaderAnnotation(pos, viewer, mouseHover, mouseOut, mouseMove);
} else {
boolean inFooter= !minings.isEmpty() ? (first instanceof DocumentFooterCodeMining) : false;
if (inFooter) {
ann= new CodeMiningDocumentFooterAnnotation(pos, viewer, mouseHover, mouseOut, mouseMove);
} else {
ann= new CodeMiningLineContentAnnotation(pos, viewer, afterPosition, mouseHover, mouseOut, mouseMove);
}
}

ann= switch (mode) {
case InLine -> new CodeMiningLineContentAnnotation(pos, viewer, afterPosition, mouseHover, mouseOut, mouseMove);
case HeaderLine -> new CodeMiningLineHeaderAnnotation(pos, viewer, mouseHover, mouseOut, mouseMove);
case FooterLine -> new CodeMiningDocumentFooterAnnotation(pos, viewer, mouseHover, mouseOut, mouseMove);

default -> throw new IllegalStateException("Found unexpected code mining display mode: " + mode); //$NON-NLS-1$
};
} else if (ann instanceof ICodeMiningAnnotation && ((ICodeMiningAnnotation) ann).isInVisibleLines()) {
// annotation is in visible lines
annotationsToRedraw.add((ICodeMiningAnnotation) ann);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*
* Contributors:
* Angelo Zerr <[email protected]> - [CodeMining] Add CodeMining support in SourceViewer - Bug 527515
* Dietrich Travkin <[email protected]> - Fix code mining redrawing - Issue 3405
*/
package org.eclipse.jface.text.examples.codemining;

Expand All @@ -34,7 +35,9 @@
import org.eclipse.jface.text.source.ISourceViewerExtension5;
import org.eclipse.jface.text.source.SourceViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
Expand All @@ -45,14 +48,22 @@
public class CodeMiningDemo {

private static boolean showWhitespaces = false;
private static AtomicReference<Boolean> useInLineCodeMinings = new AtomicReference<>(false);

private static String LINE_HEADER = "Line header";
private static String IN_LINE = "In-line";

public static void main(String[] args) throws Exception {

Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
shell.setLayout(new GridLayout(2, false));
shell.setText("Code Mining demo");

Button toggleInLineButton = new Button(shell, SWT.PUSH);
toggleInLineButton.setText(LINE_HEADER);
GridDataFactory.fillDefaults().align(SWT.BEGINNING, SWT.FILL).grab(false, false).applyTo(toggleInLineButton);

AtomicReference<String> endOfLineString = new AtomicReference<>("End of line");
Text endOfLineText = new Text(shell, SWT.NONE);
endOfLineText.setText(endOfLineString.get());
Expand All @@ -70,7 +81,9 @@ public static void main(String[] args) throws Exception {
+ "// Name class with a number N to emulate Nms before resolving the references CodeMining\n"
+ "// Empty lines show a header annotating they're empty.\n"
+ "// The word `echo` is echoed.\n"
+ "// Lines containing `end` get an annotation at their end\n\n"
+ "// Lines containing `end` get an annotation at their end\n"
+ "// Press the toggle button in the upper left corner to switch between\n"
+ "// showing reference titles in-line and showing them in additional lines.\n\n"
+ "class A\n" //
+ "new A\n" //
+ "new A\n\n" //
Expand All @@ -79,29 +92,39 @@ public static void main(String[] args) throws Exception {
+ "class 5\n" //
+ "new 5\n" //
+ "new 5\n" //
+ "new 5\n" //
+ "new 5\n\n" //
+ "Text with some references like [REF-X]\n" + "and [REF-Y] in it.\n\n"
+ "multiline \n" //
+ "multiline \n\n" //
+ "suffix \n"),
new AnnotationModel());
GridDataFactory.fillDefaults().grab(true, true).applyTo(sourceViewer.getTextWidget());
GridDataFactory.fillDefaults().span(2, 1).grab(true, true).applyTo(sourceViewer.getTextWidget());
// Add AnnotationPainter (required by CodeMining)
addAnnotationPainter(sourceViewer);

toggleInLineButton.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> {
useInLineCodeMinings.set(!useInLineCodeMinings.get());
toggleInLineButton.setText(useInLineCodeMinings.get() ? IN_LINE : LINE_HEADER);
sourceViewer.updateCodeMinings();
}));

// Initialize codemining providers
((ISourceViewerExtension5) sourceViewer).setCodeMiningProviders(new ICodeMiningProvider[] {
sourceViewer.setCodeMiningProviders(new ICodeMiningProvider[] {
new ClassReferenceCodeMiningProvider(), //
new ClassImplementationsCodeMiningProvider(), //
new ToEchoWithHeaderAndInlineCodeMiningProvider("echo"), //
new MultilineCodeMiningProvider(), //
new EmptyLineCodeMiningProvider(), //
new EchoAtEndOfLineCodeMiningProvider(endOfLineString), //
new LineContentCodeMiningAfterPositionProvider() });
new LineContentCodeMiningAfterPositionProvider(), //
new ReferenceCodeMiningProvider(useInLineCodeMinings) });

// Execute codemining in a reconciler
MonoReconciler reconciler = new MonoReconciler(new IReconcilingStrategy() {

@Override
public void setDocument(IDocument document) {
((ISourceViewerExtension5) sourceViewer).updateCodeMinings();
sourceViewer.updateCodeMinings();
}

@Override
Expand All @@ -111,14 +134,14 @@ public void reconcile(DirtyRegion dirtyRegion, IRegion subRegion) {

@Override
public void reconcile(IRegion partition) {
((ISourceViewerExtension5) sourceViewer).updateCodeMinings();
sourceViewer.updateCodeMinings();
}
}, false);
reconciler.install(sourceViewer);

endOfLineText.addModifyListener(event -> {
endOfLineString.set(endOfLineText.getText());
((ISourceViewerExtension5) sourceViewer).updateCodeMinings();
sourceViewer.updateCodeMinings();
});

shell.open();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*******************************************************************************
* Copyright (c) 2025, Advantest Europe GmbH
*
* 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
*
* Contributors:
* Dietrich Travkin <[email protected]> - Fix code mining redrawing - Issue 3405
*
*******************************************************************************/
package org.eclipse.jface.text.examples.codemining;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

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;

public class ReferenceCodeMiningProvider extends AbstractCodeMiningProvider {

private static final String REGEX_REF = "\\[REF-X\\]|\\[REF-Y\\]";
private static final Pattern REGEX_PATTERN = Pattern.compile(REGEX_REF);

private AtomicReference<Boolean> useInLineCodeMinings;

public ReferenceCodeMiningProvider(AtomicReference<Boolean> useInLineCodeMinings) {
this.useInLineCodeMinings = useInLineCodeMinings;
}

@Override
public CompletableFuture<List<? extends ICodeMining>> provideCodeMinings(ITextViewer viewer,
IProgressMonitor monitor) {
return CompletableFuture.supplyAsync(() -> {
IDocument document = viewer.getDocument();

if (document == null) {
return Collections.emptyList();
}

return createCodeMiningsFor(document);
});
}

List<ICodeMining> createCodeMiningsFor(IDocument document) {
String documentContent = document.get();
List<ICodeMining> minings = new ArrayList<>();

Matcher regexMatcher = REGEX_PATTERN.matcher(documentContent);
while (regexMatcher.find()) {
String matchedText = regexMatcher.group();
int startIndex = regexMatcher.start();

String title = matchedText.endsWith("X]") ? "Plugging into Eclipse"
: "Building commercial quality plug-ins";

if (useInLineCodeMinings.get()) {
minings.add(new ReferenceInLineCodeMining(title + ": ", startIndex, document, this));
} else {
try {
int offset = startIndex;
int line = document.getLineOfOffset(offset);
int lineOffset = document.getLineOffset(line);

minings.add(new ReferenceLineHeaderCodeMining(title, line, offset - lineOffset, title.length(),
document, this));
} catch (BadLocationException e) {
e.printStackTrace();
}
}
}

return minings;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*******************************************************************************
* Copyright (c) 2025, Advantest Europe GmbH
*
* 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
*
* Contributors:
* Dietrich Travkin <[email protected]> - Fix code mining redrawing - Issue 3405
*
*******************************************************************************/
package org.eclipse.jface.text.examples.codemining;

import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.codemining.ICodeMiningProvider;
import org.eclipse.jface.text.codemining.LineContentCodeMining;

public class ReferenceInLineCodeMining extends LineContentCodeMining {

public ReferenceInLineCodeMining(String label, int positionOffset, IDocument document,
ICodeMiningProvider provider) {
super(new Position(positionOffset, 1), true, provider);
this.setLabel(label);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*******************************************************************************
* Copyright (c) 2025, Advantest Europe GmbH
*
* 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
*
* Contributors:
* Dietrich Travkin <[email protected]> - Fix code mining redrawing - Issue 3405
*
*******************************************************************************/
package org.eclipse.jface.text.examples.codemining;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.codemining.ICodeMiningProvider;
import org.eclipse.jface.text.codemining.LineHeaderCodeMining;
import org.eclipse.jface.text.source.inlined.Positions;

public class ReferenceLineHeaderCodeMining extends LineHeaderCodeMining {

public ReferenceLineHeaderCodeMining(String label, int beforeLineNumber, int columnInLine, int length,
IDocument document, ICodeMiningProvider provider) throws BadLocationException {
super(calculatePosition(beforeLineNumber, columnInLine, document), provider, null);
this.setLabel(label);
}

private static Position calculatePosition(int beforeLineNumber, int columnInLine, IDocument document)
throws BadLocationException {
Position pos = Positions.of(beforeLineNumber, document, true);
pos.setOffset(pos.offset + columnInLine);
return pos;
}

}
Loading
Loading