Bug 521382 -[generic editor]Tests for Default Highlighter

Test for highlight extension point

Change-Id: I9250cc635f8e897cedc9af142833f506ae880d9e
Signed-off-by: Lucas Bullen <lbullen@redhat.com>
diff --git a/org.eclipse.ui.genericeditor.tests/plugin.xml b/org.eclipse.ui.genericeditor.tests/plugin.xml
index bc3a6bb..a9c5904 100644
--- a/org.eclipse.ui.genericeditor.tests/plugin.xml
+++ b/org.eclipse.ui.genericeditor.tests/plugin.xml
@@ -10,6 +10,7 @@
 <!-- Contributors:                                                          -->
 <!--     Sopot Cela & Mickael Istria (Red Hat Inc). -initial implementation -->
 <!--     Lucas Bullen (Red Hat Inc.) - Bug 508829 custom reconciler support -->
+<!--                                 - Bug 521382 default highlighter       -->
 <!-- ====================================================================== -->
 <plugin>
 	<extension
@@ -135,4 +136,11 @@
 			contentType="org.eclipse.ui.genericeditor.tests.sub-specialized-content-type">
 		</autoEditStrategy>
 	</extension>
+	<extension
+			point="org.eclipse.ui.genericeditor.highlightReconcilers">
+		<highlightReconciler
+			class="org.eclipse.ui.genericeditor.tests.contributions.HighlightReconciler"
+			contentType="org.eclipse.ui.genericeditor.tests.content-type">
+		</highlightReconciler>
+ </extension>
 </plugin>
diff --git a/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/GenericEditorTestSuite.java b/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/GenericEditorTestSuite.java
index 76c9c1c..ee4c194 100644
--- a/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/GenericEditorTestSuite.java
+++ b/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/GenericEditorTestSuite.java
@@ -7,6 +7,7 @@
  *
  * Contributors:
  *     Mickael Istria, Sopot Cela (Red Hat Inc.)
+ *     Lucas Bullen (Red Hat Inc.)
  *******************************************************************************/
 package org.eclipse.ui.genericeditor.tests;
 
@@ -22,9 +23,10 @@
 		HoverTest.class,
 		EditorTest.class,
 		AutoEditTest.class,
-		ReconcilerTest.class
+		ReconcilerTest.class,
+		HighlightTest.class
 })
 public class GenericEditorTestSuite {
 	// see @SuiteClasses
-	
+
 }
diff --git a/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/HighlightTest.java b/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/HighlightTest.java
new file mode 100644
index 0000000..e259ffe
--- /dev/null
+++ b/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/HighlightTest.java
@@ -0,0 +1,55 @@
+/*******************************************************************************
+ * Copyright (c) 2017 Red Hat Inc. and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * - Lucas Bullen (Red Hat Inc.)
+ *******************************************************************************/
+package org.eclipse.ui.genericeditor.tests;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import org.eclipse.swt.widgets.Display;
+
+import org.eclipse.jface.text.source.Annotation;
+import org.eclipse.jface.text.source.IAnnotationModel;
+import org.eclipse.jface.text.tests.util.DisplayHelper;
+
+import org.eclipse.ui.texteditor.IDocumentProvider;
+
+public class HighlightTest extends AbstratGenericEditorTest {
+
+	private static final String ANNOTATION_TYPE = "org.eclipse.ui.genericeditor.text"; //$NON-NLS-1$
+
+	@Test
+	public void testHighlightReconciler() {
+		IDocumentProvider dp = editor.getDocumentProvider();
+		IAnnotationModel am = dp.getAnnotationModel(editor.getEditorInput());
+		new DisplayHelper() {
+			@Override
+			protected boolean condition() {
+				return getAnnotationsFromAnnotationModel(am).size() == 1;
+			}
+		}.waitForCondition(Display.getDefault().getActiveShell().getDisplay(), 2000);
+		Assert.assertTrue("file does not have highlighting", getAnnotationsFromAnnotationModel(am).size() == 1);
+	}
+	private List<Annotation> getAnnotationsFromAnnotationModel(IAnnotationModel annotationModel) {
+		List<Annotation> annotationList = new ArrayList<>();
+		Iterator<Annotation> annotationIterator=annotationModel.getAnnotationIterator();
+		while (annotationIterator.hasNext()) {
+			Annotation ann = annotationIterator.next();
+			if (ann.getType().indexOf(ANNOTATION_TYPE) > -1) {
+				annotationList.add(ann);
+			}
+		}
+		return annotationList;
+	}
+}
diff --git a/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/contributions/HighlightReconciler.java b/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/contributions/HighlightReconciler.java
new file mode 100644
index 0000000..ce2e307
--- /dev/null
+++ b/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/contributions/HighlightReconciler.java
@@ -0,0 +1,37 @@
+/*******************************************************************************
+ * Copyright (c) 2017 Red Hat Inc. and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * - Lucas Bullen (Red Hat Inc.)
+ *******************************************************************************/
+package org.eclipse.ui.genericeditor.tests.contributions;
+
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.ITextViewer;
+import org.eclipse.jface.text.reconciler.Reconciler;
+
+public class HighlightReconciler extends Reconciler {
+
+	private HighlightStrategy fStrategy;
+
+	public HighlightReconciler() {
+        fStrategy = new HighlightStrategy();
+        this.setReconcilingStrategy(fStrategy, IDocument.DEFAULT_CONTENT_TYPE);
+    }
+
+	@Override
+	public void install(ITextViewer textViewer) {
+		super.install(textViewer);
+		fStrategy.install(textViewer);
+	}
+
+	@Override
+	public void uninstall() {
+		super.uninstall();
+		fStrategy.uninstall();
+	}
+}
diff --git a/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/contributions/HighlightStrategy.java b/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/contributions/HighlightStrategy.java
new file mode 100644
index 0000000..0785d80
--- /dev/null
+++ b/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/contributions/HighlightStrategy.java
@@ -0,0 +1,164 @@
+/*******************************************************************************
+ * Copyright (c) 2017 Red Hat Inc. and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * - Lucas Bullen (Red Hat Inc.)
+ *******************************************************************************/
+package org.eclipse.ui.genericeditor.tests.contributions;
+
+import org.eclipse.swt.custom.CaretEvent;
+import org.eclipse.swt.custom.CaretListener;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.preferences.IEclipsePreferences;
+import org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener;
+import org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent;
+import org.eclipse.core.runtime.preferences.InstanceScope;
+
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.IRegion;
+import org.eclipse.jface.text.ISynchronizable;
+import org.eclipse.jface.text.ITextViewer;
+import org.eclipse.jface.text.Position;
+import org.eclipse.jface.text.reconciler.DirtyRegion;
+import org.eclipse.jface.text.reconciler.IReconcilingStrategy;
+import org.eclipse.jface.text.reconciler.IReconcilingStrategyExtension;
+import org.eclipse.jface.text.source.Annotation;
+import org.eclipse.jface.text.source.IAnnotationModel;
+import org.eclipse.jface.text.source.IAnnotationModelExtension;
+import org.eclipse.jface.text.source.ISourceViewer;
+
+/**
+*
+* This Reconciler Strategy is an example for how to override the default highlight strategy.
+* It will highlight closing and opening tag names that match the current word the user is on.
+*
+*/
+public class HighlightStrategy implements IReconcilingStrategy, IReconcilingStrategyExtension, CaretListener, IPreferenceChangeListener {
+
+	private static final String ANNOTATION_TYPE = "org.eclipse.ui.genericeditor.text"; //$NON-NLS-1$
+	public static final String TOGGLE_HIGHLIGHT_PREFERENCE = "org.eclipse.ui.genericeditor.togglehighlight"; //$NON-NLS-1$
+	public static final String GENERIC_EDITOR_BUNDLE_ID = "org.eclipse.ui.genericeditor"; //$NON-NLS-1$
+
+	private boolean enabled;
+	private ISourceViewer sourceViewer;
+	private IDocument document;
+
+	public static final String SEARCH_TERM = "'bar'";
+
+	private Annotation[] fOccurrenceAnnotations = null;
+
+	private void applyHighlights() {
+		if (sourceViewer == null || !enabled) {
+			return;
+		}
+
+		removeOccurrenceAnnotations();
+		int searchOffset = document.get().indexOf(SEARCH_TERM);
+		if(searchOffset == -1) {
+			return;
+		}
+		sourceViewer.getAnnotationModel().addAnnotation(new Annotation(ANNOTATION_TYPE, false, null),
+				new Position(searchOffset, 5));
+
+	}
+
+	public void install(ITextViewer viewer) {
+		if (!(viewer instanceof ISourceViewer)) {
+			return;
+		}
+		IEclipsePreferences preferences = InstanceScope.INSTANCE.getNode(GENERIC_EDITOR_BUNDLE_ID);
+		preferences.addPreferenceChangeListener(this);
+		this.enabled = preferences.getBoolean(TOGGLE_HIGHLIGHT_PREFERENCE, true);
+		this.sourceViewer = (ISourceViewer) viewer;
+		this.sourceViewer.getTextWidget().addCaretListener(this);
+	}
+
+	public void uninstall() {
+		if (sourceViewer != null) {
+			sourceViewer.getTextWidget().removeCaretListener(this);
+		}
+		IEclipsePreferences preferences = InstanceScope.INSTANCE.getNode(GENERIC_EDITOR_BUNDLE_ID);
+		preferences.removePreferenceChangeListener(this);
+	}
+
+	@Override
+	public void preferenceChange(PreferenceChangeEvent event) {
+		if (event.getKey().equals(TOGGLE_HIGHLIGHT_PREFERENCE)) {
+			this.enabled = Boolean.parseBoolean(event.getNewValue().toString());
+			if (enabled) {
+				initialReconcile();
+			} else {
+				removeOccurrenceAnnotations();
+			}
+		}
+	}
+
+	@Override
+	public void caretMoved(CaretEvent event) {
+		applyHighlights();
+	}
+
+	@Override
+	public void initialReconcile() {
+		if (sourceViewer != null) {
+			sourceViewer.getTextWidget().getDisplay().asyncExec(() -> {
+				if (sourceViewer != null) {
+					applyHighlights();
+				}
+			});
+		}
+	}
+
+	void removeOccurrenceAnnotations() {
+		IAnnotationModel annotationModel = sourceViewer.getAnnotationModel();
+		if (annotationModel == null || fOccurrenceAnnotations == null) {
+			return;
+		}
+
+		synchronized (getLockObject(annotationModel)) {
+			if (annotationModel instanceof IAnnotationModelExtension) {
+				((IAnnotationModelExtension) annotationModel).replaceAnnotations(fOccurrenceAnnotations, null);
+			} else {
+				for (Annotation fOccurrenceAnnotation : fOccurrenceAnnotations) {
+					annotationModel.removeAnnotation(fOccurrenceAnnotation);
+				}
+			}
+			fOccurrenceAnnotations = null;
+		}
+	}
+
+	private static Object getLockObject(IAnnotationModel annotationModel) {
+		if (annotationModel instanceof ISynchronizable) {
+			Object lock = ((ISynchronizable) annotationModel).getLockObject();
+			if (lock != null) {
+				return lock;
+			}
+		}
+		return annotationModel;
+	}
+
+	@Override
+	public void setDocument(IDocument document) {
+		this.document = document;
+	}
+
+	@Override
+	public void reconcile(DirtyRegion dirtyRegion, IRegion subRegion) {
+		// Do nothing
+	}
+
+	@Override
+	public void reconcile(IRegion partition) {
+		// Do nothing
+	}
+
+	@Override
+	public void setProgressMonitor(IProgressMonitor monitor) {
+		// Not used
+	}
+}