Bug 540448 - [code mining] How to create code minings at the end of the line
Summary: [code mining] How to create code minings at the end of the line
Status: RESOLVED FIXED
Alias: None
Product: Platform
Classification: Eclipse Project
Component: Text (show other bugs)
Version: 4.10   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: 4.23 M3   Edit
Assignee: Mickael Istria CLA
QA Contact:
URL:
Whiteboard:
Keywords: noteworthy
Depends on:
Blocks: 541344
  Show dependency tree
 
Reported: 2018-10-24 17:34 EDT by Christian Dietrich CLA
Modified: 2022-11-29 09:49 EST (History)
5 users (show)

See Also:


Attachments
Screenshot naive approach (19.73 KB, image/png)
2018-10-24 17:34 EDT, Christian Dietrich CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Christian Dietrich CLA 2018-10-24 17:34:07 EDT
Created attachment 276369 [details]
Screenshot naive approach

How to create code minings at the end of the line

With eclipse 2018-09+ its no longer possible to have code minings with position length 0.

i want to create minings at the end of a line.
here is a very naive impl

public class LineEndCodeMiningProvider extends AbstractCodeMiningProvider {

	@Override
	public CompletableFuture<List<? extends ICodeMining>> provideCodeMinings(ITextViewer viewer, IProgressMonitor monitor) {
		return CompletableFuture.supplyAsync(() -> {
			System.err.println("mimimi");
			IDocument document = viewer.getDocument();
			int numberOfLines = document.getNumberOfLines();
			List<ICodeMining> result = new ArrayList<>();
			for (int i=0; i<numberOfLines;i++) {
				try {
					IRegion lineInformation = document.getLineInformation(i);
					Position position = new Position(lineInformation.getOffset() + lineInformation.getLength(), 1);
					LineContentCodeMining e = new LineContentCodeMining(position, this) {
						@Override
						protected CompletableFuture<Void> doResolve(ITextViewer viewer, IProgressMonitor monitor) {
							return CompletableFuture.runAsync(() -> {
								this.setLabel("error");
							});
						}
					
					};
					result.add(e);
				} catch (BadLocationException e) {
					e.printStackTrace();
				}
			}
			return result;
		});
	}

}

but this does not work since the result looks like attached.

If i move the mining one position to the left it does not work neither cause then it would be drawn in the middle of the text.

Did i overlooks something or is this a bug.
Thanks
Comment 1 Angelo ZERR CLA 2018-10-25 08:16:13 EDT
@Christian,

I have manage this case in JDT CodeMinjing too (to draw end statement, see at https://github.com/angelozerr/jdt-codemining/blob/master/org.eclipse.jdt.codemining/src/org/eclipse/jdt/experimental/ui/javaeditor/codemining/endstatement/EndStatementCodeMining.java#L49)

The only NOT cleaned solution that I have found is to add a space before the label. In your case:

-----------------------------
this.setLabel(" error");
-----------------------------

should work.

To be honnest with you, I wanted this feature, but I have not worked a lot to improve codemining. Any patch are welcome if you find a better solution.
Comment 2 Eclipse Genie CLA 2018-11-19 10:21:03 EST
New Gerrit change created: https://git.eclipse.org/r/132691
Comment 3 Angelo ZERR CLA 2018-11-19 10:25:59 EST
@Mickael, please review my gerrit patch https://git.eclipse.org/r/#/c/132691/

This gerrit patch avoid using GlyphMetrics#width when line content annotation is drawn at the end of the line. It fixes 2 issues:

 * you don't need to add an extra space to draw teh mining (original issue)
 * when you click on the right of the last character of the line, it doesn't execute the mining. 

You can test my gerrit patch with JDT CodeMining by activating "Show End Statement" an dwrite in your Java Editor a code like

---------------------------------
if (true) {




}
---------------------------------

It should show the following mining:

---------------------------------
if (true) {




}  [// -> true]
---------------------------------

and if you click on the right of '}' you could set the cursor without executing the mining (which set the cursor on the if statement).
Comment 5 Mickael Istria CLA 2018-11-19 10:45:00 EST
Thanks Angelo!
Comment 6 Angelo ZERR CLA 2018-11-19 11:06:36 EST
Thanks @Mickael!
Comment 7 Angelo ZERR CLA 2018-11-19 11:48:06 EST
I reopen it because I have introduced a bug -(
Comment 8 Eclipse Genie CLA 2018-11-19 12:09:17 EST
New Gerrit change created: https://git.eclipse.org/r/132702
Comment 9 Angelo ZERR CLA 2018-11-19 12:12:04 EST
Please review my gerrit patch please. I have forgotten the case where the editor doesn't update the region where mining must be drawn (clipping check). As StyleRange is not set in this case, we must redraw the regions if gc clipping doesn't contains the mining to draw.

Sorry @Mickael for the introduced bug -(
Comment 10 Angelo ZERR CLA 2018-11-19 12:12:47 EST
It was an error. Please review my gerrit patch.
Comment 12 Angelo ZERR CLA 2018-11-19 13:16:46 EST
Find a new bug again when there are 2 en statement to draw.there are infinite redraw.clipping check is a bad idea. I must fix That.
Comment 13 Mickael Istria CLA 2018-11-19 14:12:05 EST
(In reply to Angelo ZERR from comment #12)
> Find a new bug again when there are 2 en statement to draw.there are
> infinite redraw.clipping check is a bad idea. I must fix That.

Would be great if you manage to add automated tests for those cases by the way.
Comment 14 Mickael Istria CLA 2018-11-19 15:58:56 EST
This is very likely to miss 4.10.M3 and isn't too critical for further work regarding code-mining. Retargetting to 4.10.RC1.
Comment 15 Eclipse Genie CLA 2018-11-20 04:00:39 EST
New Gerrit change created: https://git.eclipse.org/r/132732
Comment 16 Eclipse Genie CLA 2018-11-20 04:08:53 EST
New Gerrit change created: https://git.eclipse.org/r/132733
Comment 17 Angelo ZERR CLA 2018-11-20 04:10:43 EST
@Mickael please review https://git.eclipse.org/r/132733 which fixes freeze of code mining when it is at the end of line. IMHO I think this fix is very important.
 
> Would be great if you manage to add automated tests for those cases by the way.

I agree with you, we need tests for code mining, but it's so hard to test it.
Comment 18 Mickael Istria CLA 2018-11-20 04:13:51 EST
(In reply to Angelo ZERR from comment #17)
> @Mickael please review https://git.eclipse.org/r/132733 which fixes freeze
> of code mining when it is at the end of line. IMHO I think this fix is very
> important.

I'll have a look right now, but we won't be able to merge it before Friday since we're in a Freeze period for 4.10.M3.
Comment 19 Mickael Istria CLA 2018-11-20 04:21:47 EST
(In reply to Angelo ZERR from comment #17)
> I agree with you, we need tests for code mining, but it's so hard to test it.

Please at least make sure one example covers those tricky cases.
Comment 20 Lars Vogel CLA 2019-02-19 03:31:46 EST
Mass change, please reset target if you still planning to fix this for 4.11.
Comment 21 Mickael Istria CLA 2019-06-04 15:19:32 EDT
I'll try that for 4.13, as I think we have some interesting cases for Code Minings that require it.
Comment 22 Dani Megert CLA 2019-10-19 09:11:51 EDT
Mickael 4.12 is long gone. What's the status here?
Comment 23 Dani Megert CLA 2019-10-19 09:12:27 EDT
(In reply to Dani Megert from comment #22)
> Mickael 4.12 is long gone. What's the status here?
I've removed the target as I do not want to see open 4.12 bugs.
Comment 24 Eclipse Genie CLA 2022-02-06 15:31:04 EST
New Gerrit change created: https://git.eclipse.org/r/c/platform/eclipse.platform.text/+/190503
Comment 25 Eclipse Genie CLA 2022-02-06 15:51:01 EST
New Gerrit change created: https://git.eclipse.org/r/c/www.eclipse.org/eclipse/news/+/190504
Comment 28 Eclipse Genie CLA 2022-02-11 05:11:53 EST
New Gerrit change created: https://git.eclipse.org/r/c/platform/eclipse.platform.text/+/190710
Comment 30 Mickael Istria CLA 2022-02-14 03:36:10 EST
Doesn't work for \r\n line endings.
Comment 31 Andrey Loskutov CLA 2022-02-14 12:09:42 EST
(In reply to Mickael Istria from comment #30)
> Doesn't work for \r\n line endings.

I see it is reopened, but looking into the code I can't understand how that is supposed to work, it doesn't look like a new line issue.

org.eclipse.jface.text.source.inlined.InlinedAnnotationDrawingStrategy.drawAfterLine(LineContentAnnotation, GC, StyledText, int, int, Color) is always called with gc == null from the related code and isn't doing anything because of that:

	at org.eclipse.jface.text.source.inlined.InlinedAnnotationDrawingStrategy.drawAfterLine(InlinedAnnotationDrawingStrategy.java:140)
	at org.eclipse.jface.text.source.inlined.InlinedAnnotationDrawingStrategy.draw(InlinedAnnotationDrawingStrategy.java:130)
	at org.eclipse.jface.text.source.inlined.InlinedAnnotationDrawingStrategy.draw(InlinedAnnotationDrawingStrategy.java:61)
	at org.eclipse.jface.text.source.inlined.AbstractInlinedAnnotation.lambda$0(AbstractInlinedAnnotation.java:131)
	at org.eclipse.jface.text.source.inlined.InlinedAnnotationSupport.lambda$0(InlinedAnnotationSupport.java:573)
	at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:40)
	at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:185)
	at org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:4035)
Comment 32 Mickael Istria CLA 2022-02-14 13:55:39 EST
(In reply to Andrey Loskutov from comment #31)
> I see it is reopened, but looking into the code I can't understand how that
> is supposed to work, it doesn't look like a new line issue.

The line endings have an impact on AnnotationPainter, on the line
  if (paintLength >= 0 && regionsTouchOrOverlap(paintStart, paintLength, clippingOffset, clippingLength)) {
which, at the moment, always return false (paintLength < 0) and skips drawing for \r\n line endings. I believe the problem is not that code itself, but rather how the CodeMiningManager turns the LineEndCodeMining into an InlinedContentAnnotation for that case.
Comment 33 Eclipse Genie CLA 2022-02-14 16:02:19 EST
New Gerrit change created: https://git.eclipse.org/r/c/platform/eclipse.platform.text/+/190801
Comment 34 Mickael Istria CLA 2022-02-15 05:02:41 EST
I'm closing this one as the feature is available in I-Build and can already work with projects that already provide an ITextSelection->IVariable adapter.
Further issues, improvements or integration of eg JDT, should now be tracked in separate bugs.
Comment 36 Tamas Miklossy CLA 2022-11-29 09:49:30 EST
Recently I have also encountered some problem with the line ending code mining, see https://github.com/eclipse/xtext-eclipse/issues/1947