-
Couldn't load subscription status.
- Fork 226
Fix #3405 Fix incorrect reuse of code mining annotations with mismatched types #3416
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
tobiasmelcher
merged 1 commit into
eclipse-platform:master
from
travkin79:feature/patch-3405-code-minings
Oct 28, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
||
|
|
@@ -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. | ||
| * | ||
|
|
@@ -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(); | ||
|
|
@@ -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); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
||
|
|
@@ -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; | ||
|
|
@@ -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()); | ||
|
|
@@ -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" // | ||
|
|
@@ -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 | ||
|
|
@@ -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(); | ||
|
|
||
88 changes: 88 additions & 0 deletions
88
....examples/src/org/eclipse/jface/text/examples/codemining/ReferenceCodeMiningProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
|
|
||
| } |
30 changes: 30 additions & 0 deletions
30
...xt.examples/src/org/eclipse/jface/text/examples/codemining/ReferenceInLineCodeMining.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
|
|
||
| } |
39 changes: 39 additions & 0 deletions
39
...xamples/src/org/eclipse/jface/text/examples/codemining/ReferenceLineHeaderCodeMining.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.