Bug 579267 - Caret not vertically aligned with line content code minings

Change-Id: Ic6cb63fc75b40cf4baff3e73dffcc00c64bc439f
Signed-off-by: Mickael Istria <mistria@redhat.com>
Reviewed-on: https://git.eclipse.org/r/c/platform/eclipse.platform.text/+/191916
Tested-by: Platform Bot <platform-bot@eclipse.org>
diff --git a/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/InlinedAnnotationDrawingStrategy.java b/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/InlinedAnnotationDrawingStrategy.java
index 7a4ba95..28bc891 100644
--- a/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/InlinedAnnotationDrawingStrategy.java
+++ b/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/InlinedAnnotationDrawingStrategy.java
@@ -202,7 +202,7 @@
 					// END TO REMOVE
 
 					// Annotation takes place, add GlyphMetrics width to the style
-					StyleRange newStyle= annotation.updateStyle(style);
+					StyleRange newStyle= annotation.updateStyle(style, gc.getFontMetrics());
 					if (newStyle != null) {
 						textWidget.setStyleRange(newStyle);
 						return;
@@ -285,7 +285,7 @@
 				// END TO REMOVE
 
 				// Annotation takes place, add GlyphMetrics width to the style
-				StyleRange newStyle= annotation.updateStyle(style);
+				StyleRange newStyle= annotation.updateStyle(style, gc.getFontMetrics());
 				if (newStyle != null) {
 					textWidget.setStyleRange(newStyle);
 					return;
diff --git a/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/InlinedAnnotationSupport.java b/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/InlinedAnnotationSupport.java
index 6682ad7..0282beb 100644
--- a/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/InlinedAnnotationSupport.java
+++ b/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/InlinedAnnotationSupport.java
@@ -35,6 +35,8 @@
 import org.eclipse.swt.graphics.Device;
 import org.eclipse.swt.graphics.Font;
 import org.eclipse.swt.graphics.FontData;
+import org.eclipse.swt.graphics.FontMetrics;
+import org.eclipse.swt.graphics.GC;
 import org.eclipse.swt.graphics.GlyphMetrics;
 import org.eclipse.swt.widgets.Display;
 
@@ -112,7 +114,7 @@
 					.forEachRemaining(annotation -> {
 						if (annotation instanceof LineContentAnnotation) {
 							LineContentAnnotation ann= (LineContentAnnotation) annotation;
-							StyleRange style= ann.updateStyle(null);
+							StyleRange style= ann.updateStyle(null, fFontMetrics);
 							if (style != null) {
 								if (fViewer instanceof ITextViewerExtension5) {
 									ITextViewerExtension5 projectionViewer= (ITextViewerExtension5) fViewer;
@@ -340,6 +342,8 @@
 	 */
 	private final MouseTracker fMouseTracker= new MouseTracker();
 
+	private FontMetrics fFontMetrics;
+
 	/**
 	 * Install the inlined annotation support for the given viewer.
 	 *
@@ -364,6 +368,10 @@
 		text.addMouseListener(fMouseTracker);
 		text.addMouseMoveListener(fMouseTracker);
 		setColor(text.getDisplay().getSystemColor(SWT.COLOR_DARK_GRAY));
+		GC gc= new GC(viewer.getTextWidget());
+		gc.setFont(viewer.getTextWidget().getFont());
+		fFontMetrics= gc.getFontMetrics();
+		gc.dispose();
 	}
 
 	/**
diff --git a/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/LineContentAnnotation.java b/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/LineContentAnnotation.java
index 0c10b8a..fff0731 100644
--- a/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/LineContentAnnotation.java
+++ b/org.eclipse.jface.text/src/org/eclipse/jface/text/source/inlined/LineContentAnnotation.java
@@ -16,6 +16,7 @@
 import org.eclipse.swt.custom.StyleRange;
 import org.eclipse.swt.custom.StyledText;
 import org.eclipse.swt.graphics.Color;
+import org.eclipse.swt.graphics.FontMetrics;
 import org.eclipse.swt.graphics.GC;
 import org.eclipse.swt.graphics.GlyphMetrics;
 
@@ -110,10 +111,11 @@
 	 * model position before passing it to a {@link TextPresentation}.
 	 *
 	 * @param style the current style and null otherwise.
+	 * @param fontMetrics font metrics
 	 * @return the style to apply with GlyphMetrics width only if needed. It uses widget position,
 	 *         not model position.
 	 */
-	StyleRange updateStyle(StyleRange style) {
+	StyleRange updateStyle(StyleRange style, FontMetrics fontMetrics) {
 		Position widgetPosition= computeWidgetPosition();
 		if (widgetPosition == null) {
 			return null;
@@ -134,7 +136,7 @@
 		GlyphMetrics metrics= style.metrics;
 		if (!isMarkedDeleted()) {
 			if (metrics == null) {
-				metrics= new GlyphMetrics(0, 0, fullWidth);
+				metrics= new GlyphMetrics(fontMetrics.getAscent(), fontMetrics.getDescent(), fullWidth);
 			} else {
 				if (metrics.width == fullWidth) {
 					return null;
@@ -144,7 +146,7 @@
 				 * later in StyledText#setStyleRange will compare the same (modified) and won't
 				 * realize an update happened.
 				 */
-				metrics= new GlyphMetrics(0, 0, fullWidth);
+				metrics= new GlyphMetrics(fontMetrics.getAscent(), fontMetrics.getDescent(), fullWidth);
 			}
 		} else {
 			metrics= null;