Bug 577852: [SourceEditor] Fix custom colors of editor overwritten by
CSS styling

  - Add nullable annotations to SourceEditorViewer

Change-Id: Ifd6227da2c4d301d9e6e35f862d666ab8e49766b
diff --git a/ltk/org.eclipse.statet.ltk.ui/src/org/eclipse/statet/ltk/ui/sourceediting/SourceEditorViewer.java b/ltk/org.eclipse.statet.ltk.ui/src/org/eclipse/statet/ltk/ui/sourceediting/SourceEditorViewer.java
index 50f88cb..c005886 100644
--- a/ltk/org.eclipse.statet.ltk.ui/src/org/eclipse/statet/ltk/ui/sourceediting/SourceEditorViewer.java
+++ b/ltk/org.eclipse.statet.ltk.ui/src/org/eclipse/statet/ltk/ui/sourceediting/SourceEditorViewer.java
@@ -14,6 +14,9 @@
 
 package org.eclipse.statet.ltk.ui.sourceediting;
 
+import static org.eclipse.statet.jcommons.lang.ObjectUtils.nonNullAssert;
+import static org.eclipse.statet.jcommons.lang.ObjectUtils.nonNullLateInit;
+
 import org.eclipse.core.runtime.Assert;
 import org.eclipse.jface.text.BadLocationException;
 import org.eclipse.jface.text.BadPositionCategoryException;
@@ -34,12 +37,15 @@
 import org.eclipse.swt.graphics.Point;
 import org.eclipse.swt.widgets.Composite;
 
-import org.eclipse.statet.jcommons.lang.NonNull;
+import org.eclipse.statet.jcommons.lang.NonNullByDefault;
 import org.eclipse.statet.jcommons.lang.Nullable;
 
 import org.eclipse.statet.ecommons.text.core.util.NonDeletingPositionUpdater;
 
+import org.eclipse.statet.ltk.ui.sourceediting.swt.EnhStyledText;
 
+
+@NonNullByDefault
 public class SourceEditorViewer extends ProjectionViewer {
 	
 	
@@ -71,9 +77,9 @@
 	private int lastSentSelectionOffset;
 	private int lastSentSelectionLength;
 	
-	private IInformationPresenter sourceOutlinePresenter;
-	private IInformationPresenter elementOutlinePresenter;
-	private IInformationPresenter elementHierarchyPresenter;
+	private @Nullable IInformationPresenter sourceOutlinePresenter;
+	private @Nullable IInformationPresenter elementOutlinePresenter;
+	private @Nullable IInformationPresenter elementHierarchyPresenter;
 	
 	
 	public SourceEditorViewer(final Composite parent, final IVerticalRuler ruler,
@@ -104,6 +110,12 @@
 	
 	
 	@Override
+	protected StyledText createTextWidget(final Composite parent, final int styles) {
+		return EnhStyledText.forSourceEditor(parent, styles);
+	}
+	
+	
+	@Override
 	protected void fireSelectionChanged(final SelectionChangedEvent event) {
 		final ITextSelection selection= (ITextSelection) event.getSelection();
 		this.lastSentSelectionOffset= selection.getOffset();
@@ -113,7 +125,7 @@
 	}
 	
 	
-	private IInformationPresenter getPresenter(final int operation) {
+	private @Nullable IInformationPresenter getPresenter(final int operation) {
 		switch (operation) {
 		case SHOW_SOURCE_OUTLINE:
 			return this.sourceOutlinePresenter;
@@ -126,7 +138,7 @@
 		}
 	}
 	
-	private void setPresenter(final int operation, final IInformationPresenter presenter) {
+	private void setPresenter(final int operation, final @Nullable IInformationPresenter presenter) {
 		switch (operation) {
 		case SHOW_SOURCE_OUTLINE:
 			this.sourceOutlinePresenter= presenter;
@@ -202,9 +214,9 @@
 		super.unconfigure();
 	}
 	
-	public @NonNull String @Nullable [] getDefaultPrefixes(final String contentType) {
+	public String @Nullable [] getDefaultPrefixes(final String contentType) {
 		return (this.fDefaultPrefixChars != null) ?
-				(String[]) this.fDefaultPrefixChars.get(contentType) :
+				(String[])this.fDefaultPrefixChars.get(contentType) :
 				null;
 	}
 	
@@ -215,19 +227,20 @@
 	private final class ViewerState {
 		
 		/** The position tracking the selection. */
-		private Position selection;
+		private @Nullable Position selectionPosition;
 		
 		/** The position tracking the visually stable line. */
-		private Position stableLine;
+		private @Nullable Position stableLinePosition;
 		/** The pixel offset of the stable line measured from the client area. */
 		private int stablePixel;
 		
-		/** The position updater for {@link #selection} and {@link #stableLine}. */
-		private IPositionUpdater updater;
+		//-- connect(Document) --
+		/** The position updater for {@link #selectionPosition} and {@link #stableLinePosition}. */
+		private IPositionUpdater updater= nonNullLateInit();
 		/** The document that the position updater and the positions are registered with. */
-		private IDocument updaterDocument;
+		private IDocument updaterDocument= nonNullLateInit();
 		/** The position category used by {@link #updater}. */
-		private String updaterCategory;
+		private String updaterCategory= nonNullLateInit();
 		
 		private int topPixel;
 		
@@ -244,17 +257,19 @@
 		
 		
 		public void updateSelection(final int offset, final int length) {
-			if (this.selection == null) {
-				this.selection= new Position(offset, length);
+			var selectionPosition= this.selectionPosition;
+			if (selectionPosition == null) {
+				selectionPosition= new Position(offset, length);
+				this.selectionPosition= selectionPosition;
 				if (isConnected()) {
 					try {
-						this.updaterDocument.addPosition(this.updaterCategory, this.selection);
+						this.updaterDocument.addPosition(this.updaterCategory, selectionPosition);
 					}
 					catch (final BadLocationException | BadPositionCategoryException e) {}
 				}
 			}
 			else {
-				updatePosition(this.selection, offset, length);
+				updatePosition(selectionPosition, offset, length);
 			}
 		}
 		
@@ -264,30 +279,35 @@
 		 * selection has been updated while in redraw(false) mode, the new selection is revealed.
 		 */
 		private void updateViewport() {
-			final StyledText textWidget= getTextWidget();
-			if (this.selection != null) {
-				textWidget.setTopPixel(this.topPixel);
-				revealRange(this.selection.getOffset(), this.selection.getLength());
+			final StyledText textWidget= nonNullAssert(getTextWidget());
+			{	final Position selection= this.selectionPosition;
+				if (selection != null) {
+					textWidget.setTopPixel(this.topPixel);
+					revealRange(selection.getOffset(), selection.getLength());
+					return;
+				}
 			}
-			else if (this.stableLine != null) {
-				int stableLine;
-				try {
-					stableLine= this.updaterDocument.getLineOfOffset(this.stableLine.getOffset());
+			{	final Position stableLinePosition= this.stableLinePosition;
+				if (stableLinePosition != null) {
+					int stableLine;
+					try {
+						stableLine= this.updaterDocument.getLineOfOffset(stableLinePosition.getOffset());
+					}
+					catch (final BadLocationException x) {
+						// ignore and return silently
+						textWidget.setTopPixel(this.topPixel);
+						return;
+					}
+					final int stableWidgetLine= getClosestWidgetLineForModelLine(stableLine);
+					if (stableWidgetLine == -1) {
+						textWidget.setTopPixel(this.topPixel);
+						return;
+					}
+					final int linePixel= textWidget.getLinePixel(stableWidgetLine);
+					final int delta= this.stablePixel - linePixel;
+					final int topPixel= textWidget.getTopPixel();
+					textWidget.setTopPixel(topPixel - delta);
 				}
-				catch (final BadLocationException x) {
-					// ignore and return silently
-					textWidget.setTopPixel(this.topPixel);
-					return;
-				}
-				final int stableWidgetLine= getClosestWidgetLineForModelLine(stableLine);
-				if (stableWidgetLine == -1) {
-					textWidget.setTopPixel(this.topPixel);
-					return;
-				}
-				final int linePixel= textWidget.getLinePixel(stableWidgetLine);
-				final int delta= this.stablePixel - linePixel;
-				final int topPixel= textWidget.getTopPixel();
-				textWidget.setTopPixel(topPixel - delta);
 			}
 		}
 		
@@ -301,7 +321,7 @@
 			Assert.isLegal(!isConnected());
 			this.updaterDocument= document;
 			try {
-				final StyledText textWidget= getTextWidget();
+				final StyledText textWidget= nonNullAssert(getTextWidget());
 				this.updaterCategory= "ViewerState-" + hashCode();
 				this.updater= new NonDeletingPositionUpdater(this.updaterCategory);
 				this.updaterDocument.addPositionCategory(this.updaterCategory);
@@ -311,8 +331,8 @@
 				final int stableWidgetLine= modelLine2WidgetLine(stableLine);
 				this.stablePixel= textWidget.getLinePixel(stableWidgetLine);
 				final IRegion stableLineInfo= this.updaterDocument.getLineInformation(stableLine);
-				this.stableLine= new Position(stableLineInfo.getOffset(), stableLineInfo.getLength());
-				this.updaterDocument.addPosition(this.updaterCategory, this.stableLine);
+				this.updaterDocument.addPosition(this.updaterCategory,
+						this.stableLinePosition= new Position(stableLineInfo.getOffset(), stableLineInfo.getLength()) );
 				
 				this.topPixel= textWidget.getTopPixel();
 			}
@@ -342,7 +362,8 @@
 		 */
 		private int getStableLine() {
 			int stableLine; // the model line that we try to keep stable
-			final int caretLine= getTextWidget().getLineAtOffset(getTextWidget().getCaretOffset());
+			final StyledText textWidget= nonNullAssert(getTextWidget());
+			final int caretLine= textWidget.getLineAtOffset(textWidget.getCaretOffset());
 			if (caretLine < JFaceTextUtil.getPartialTopIndex(getTextWidget()) || caretLine > JFaceTextUtil.getPartialBottomIndex(getTextWidget())) {
 				stableLine= JFaceTextUtil.getPartialTopIndex(SourceEditorViewer.this);
 			}
@@ -365,10 +386,11 @@
 		/**
 		 * Disconnects from the document.
 		 */
+		@SuppressWarnings("null")
 		private void disconnect() {
 			if (isConnected()) {
 				try {
-					this.updaterDocument.removePosition(this.updaterCategory, this.stableLine);
+					this.updaterDocument.removePosition(this.updaterCategory, this.stableLinePosition);
 					this.updaterDocument.removePositionUpdater(this.updater);
 					this.updater= null;
 					this.updaterDocument.removePositionCategory(this.updaterCategory);
@@ -383,7 +405,7 @@
 	}
 	
 	
-	private ViewerState viewerState;
+	private @Nullable ViewerState viewerState;
 	
 	
 	@Override
@@ -399,19 +421,21 @@
 	protected void enabledRedrawing(final int topIndex) {
 		super.enabledRedrawing(topIndex);
 		
-		if (this.viewerState != null) {
-			this.viewerState.disconnect();
-			if (topIndex == -1) {
-				this.viewerState.updateViewport();
-			}
+		final var viewerState= this.viewerState;
+		if (viewerState != null) {
 			this.viewerState= null;
+			viewerState.disconnect();
+			if (topIndex == -1) {
+				viewerState.updateViewport();
+			}
 		}
 	}
 	
 	@Override
 	public void setSelectedRange(final int selectionOffset, final int selectionLength) {
-		if (this.viewerState != null && !redraws()) {
-			this.viewerState.updateSelection(selectionOffset, selectionLength);
+		final var viewerState= this.viewerState;
+		if (viewerState != null && !redraws()) {
+			viewerState.updateSelection(selectionOffset, selectionLength);
 			return;
 		}