Bug 531920 - [CodeMining] CodeMining annotation doesn't appear when text
are added

Change-Id: I4af56cce8ccaec1b0ded4d31bf564610779e528e
Signed-off-by: angelozerr <angelo.zerr@gmail.com>
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 df1e7b9..8bb1e7a 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
@@ -36,7 +36,9 @@
 import org.eclipse.core.runtime.Assert;
 
 import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.DocumentEvent;
 import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.IDocumentListener;
 import org.eclipse.jface.text.IRegion;
 import org.eclipse.jface.text.ISynchronizable;
 import org.eclipse.jface.text.ITextPresentationListener;
@@ -123,17 +125,17 @@
 	 * Class to track start/end offset of visible lines.
 	 *
 	 */
-	private class VisibleLines implements IViewportListener {
+	private class VisibleLines implements IViewportListener, IDocumentListener {
 
 		private int startOffset;
 
-		private int endOffset;
+		private Integer endOffset;
 
 		public VisibleLines() {
 			fViewer.getTextWidget().getDisplay().asyncExec(() -> {
-				startOffset= getInclusiveTopIndexStartOffset();
-				endOffset= getExclusiveBottomIndexEndOffset();
+				compute();
 			});
+			fViewer.getDocument().addDocumentListener(this);
 		}
 
 		@Override
@@ -141,6 +143,17 @@
 			compute();
 		}
 
+		@Override
+		public void documentAboutToBeChanged(DocumentEvent event) {
+			endOffset= null;
+		}
+
+		@Override
+		public void documentChanged(DocumentEvent event) {
+			// Do nothing
+		}
+
+		@SuppressWarnings("boxing")
 		private void compute() {
 			startOffset= getInclusiveTopIndexStartOffset();
 			endOffset= getExclusiveBottomIndexEndOffset();
@@ -194,9 +207,22 @@
 		 * @return <code>true</code> if the given offset is in visible lines and <code>false</code>
 		 *         otherwise.
 		 */
-		public boolean isInVisibleLines(int offset) {
+		@SuppressWarnings("boxing")
+		boolean isInVisibleLines(int offset) {
+			if (endOffset == null) {
+				endOffset= getExclusiveBottomIndexEndOffset();
+			}
 			return offset >= startOffset && offset <= endOffset;
 		}
+
+		/**
+		 * Uninstall visible lines
+		 */
+		void uninstall() {
+			if (fViewer != null && fViewer.getDocument() != null) {
+				fViewer.getDocument().removeDocumentListener(this);
+			}
+		}
 	}
 
 	private class MouseTracker implements MouseTrackListener, MouseMoveListener, MouseListener {
@@ -353,6 +379,10 @@
 			}
 			fViewer.removeViewportListener(visibleLines);
 		}
+		if (visibleLines != null) {
+			visibleLines.uninstall();
+			visibleLines= null;
+		}
 		fViewer= null;
 		fPainter= null;
 	}