Skip to content

Commit

Permalink
Editor.getMarkers returns invisible markers #1839
Browse files Browse the repository at this point in the history
Signed-off-by: Andrej Podhradsky <apodhrad@redhat.com>
  • Loading branch information
apodhrad authored and rawagner committed Jan 8, 2018
1 parent 5e4c730 commit 5561f99
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 35 deletions.
Expand Up @@ -98,6 +98,13 @@ public interface Editor extends WorkbenchPart {
*/
List<Marker> getMarkers();

/**
* Returns editor validation AYT (as you type) markers.
*
* @return editor validation AYT (as you type) markers
*/
List<Marker> getAYTMarkers();

/**
* Gets content assistant opened automatically by instructions defined within run method of execute parameter or
* null in case Content Assistant shell was not opened.
Expand Down
Expand Up @@ -138,33 +138,50 @@ public void run() {

}

/**
* Returns validation markers.
* @param editor to extract validation markers from
* @return list of validation markers
*/
public List<Marker> getMarkers(final IEditorPart editor) {
List<Marker> markers = new ArrayList<Marker>();
ITextEditor textEditor = (ITextEditor) editor.getAdapter(ITextEditor.class);
if (textEditor == null) {
return markers;
}
final IDocumentProvider documentProvider = textEditor.getDocumentProvider();
if (documentProvider == null) {
return markers;
}
IAnnotationModel model = documentProvider.getAnnotationModel(textEditor.getEditorInput());
Iterator<?> it = model.getAnnotationIterator();
while (it.hasNext()) {
Object o = it.next();
if (o instanceof Annotation && !(o instanceof ILineDiffInfo)) {
Annotation annotation = (Annotation) o;
markers.add(new Marker(annotation, editor));
}
}
return markers;
}

/**
* Returns persistent validation markers.
*
* @param editor
* to extract validation markers from
* @return list of persistent validation markers
*/
public List<Marker> getMarkers(final IEditorPart editor) {
return getMarkers(editor, true);
}

/**
* Returns validation markers.
*
* @param editor
* editor to extract validation markers from
* @param isPersistent
* common markers are persistent, AYT markers are not persistent
* @return validation markers
*/
public List<Marker> getMarkers(final IEditorPart editor, boolean isPersistent) {
List<Marker> markers = new ArrayList<Marker>();
ITextEditor textEditor = (ITextEditor) editor.getAdapter(ITextEditor.class);
if (textEditor == null) {
return markers;
}
final IDocumentProvider documentProvider = textEditor.getDocumentProvider();
if (documentProvider == null) {
return markers;
}
IAnnotationModel model = documentProvider.getAnnotationModel(textEditor.getEditorInput());
Iterator<?> it = model.getAnnotationIterator();
while (it.hasNext()) {
Object o = it.next();
if (o instanceof Annotation && !(o instanceof ILineDiffInfo)) {
Annotation annotation = (Annotation) o;
if (annotation.isPersistent() == isPersistent) {
markers.add(new Marker(annotation, editor));
}
}
}
return markers;
}

/**
* Closes all editors.
* @param save performs save operation before closing if true
Expand Down
Expand Up @@ -300,6 +300,12 @@ public List<Marker> getMarkers() {
activate();
return EditorHandler.getInstance().getMarkers(editorPart);
}

@Override
public List<Marker> getAYTMarkers() {
activate();
return EditorHandler.getInstance().getMarkers(editorPart, false);
}

@Override
public ContentAssistant getAutoContentAssistant(Runnable execute) {
Expand Down
Expand Up @@ -20,6 +20,7 @@
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.URL;
import java.util.List;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Platform;
Expand Down Expand Up @@ -51,6 +52,7 @@
import org.eclipse.reddeer.workbench.handler.EditorHandler;
import org.eclipse.reddeer.workbench.handler.WorkbenchShellHandler;
import org.eclipse.reddeer.workbench.impl.editor.AbstractEditor.ContentAssistantEnum;
import org.eclipse.reddeer.workbench.impl.editor.Marker;
import org.eclipse.reddeer.workbench.impl.editor.TextEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
Expand Down Expand Up @@ -330,22 +332,31 @@ public void getLineOfTextIndex() {
@Test
public void getMarkers() {
TextEditor textEditor = TextEditorTest.openJavaFile();
textEditor.setText(textEditor.getText().replace("System", "Systemx"));
textEditor.setText(textEditor.getText().replace("System", "SystemX"));
// in case if the editor has been changed by the test getAYTMarkers()
textEditor.setText(textEditor.getText().replace("SystemY", "SystemX"));
textEditor.save();
AbstractWait.sleep(TimePeriod.SHORT);
assertEquals(2, textEditor.getMarkers().size());
assertEquals("Systemx cannot be resolved", textEditor.getMarkers().get(0).getText());
List<Marker> markers = textEditor.getMarkers();
assertEquals(2, markers.size());
assertEquals("SystemX cannot be resolved", markers.get(0).getText());
assertEquals(15, markers.get(0).getLineNumber());
}

@Test
public void getAYTMarkers() {
TextEditor textEditor = TextEditorTest.openJavaFile();
textEditor.setText(textEditor.getText().replace("System", "Systemx"));
// textEditor.save();
// in case if the editor has been changed by the test getMarkers()
textEditor.setText(textEditor.getText().replace("SystemX", "System"));
textEditor.save();
AbstractWait.sleep(TimePeriod.SHORT);
textEditor.setText(textEditor.getText().replace("System", "SystemY"));
// Wait and do not save the editor
AbstractWait.sleep(TimePeriod.SHORT);
assertEquals(1, textEditor.getMarkers().size());
assertEquals("Systemx cannot be resolved", textEditor.getMarkers().get(0).getText());
assertEquals(15, textEditor.getMarkers().get(0).getLineNumber());
List<Marker> markers = textEditor.getAYTMarkers();
assertEquals(1, markers.size());
assertEquals("SystemY cannot be resolved", markers.get(0).getText());
assertEquals(15, markers.get(0).getLineNumber());
}

@Test
Expand Down

0 comments on commit 5561f99

Please sign in to comment.