Fixed: [Issue 123] NPE when opening file outside of Eclipse workspace.
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/editor/spelling/ProtobufSpelling.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/editor/spelling/ProtobufSpelling.java
index c714b5c..23cdaa7 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/editor/spelling/ProtobufSpelling.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/editor/spelling/ProtobufSpelling.java
@@ -17,7 +17,6 @@
import org.eclipse.jface.text.source.*;
import org.eclipse.ui.texteditor.spelling.*;
import org.eclipse.xtext.nodemodel.*;
-import org.eclipse.xtext.parser.IParseResult;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.ui.editor.model.XtextDocument;
import org.eclipse.xtext.util.concurrent.IUnitOfWork;
@@ -42,14 +41,16 @@
}
@Override public void reconcile(IRegion region) {
+ IAnnotationModel model = getAnnotationModel();
+ if (model == null) return;
super.reconcile(new Region(0, xtextDocument().getLength()));
- removeUnwantedAnnotations();
+ removeUnwantedAnnotations(model);
}
- private void removeUnwantedAnnotations() {
+ private void removeUnwantedAnnotations(final IAnnotationModel model) {
xtextDocument().readOnly(new IUnitOfWork.Void<XtextResource>() {
@Override public void process(XtextResource resource) throws Exception {
- removeUnwantedAnnotations(resource.getParseResult());
+ removeUnwantedAnnotations(resource.getParseResult().getRootNode(), model);
}
});
}
@@ -58,22 +59,15 @@
return (XtextDocument) super.getDocument();
}
- private void removeUnwantedAnnotations(IParseResult parseResult) {
- IAnnotationModel model = getAnnotationModel();
- ICompositeNode rootNode = parseResult.getRootNode();
- for (Annotation annotation : annotations()) {
+ @SuppressWarnings("unchecked")
+ private void removeUnwantedAnnotations(INode rootNode, IAnnotationModel model) {
+ Iterator<Annotation> iterator = model.getAnnotationIterator();
+ while (iterator.hasNext()) {
+ Annotation annotation = iterator.next();
if (shouldRemoveFromModel(annotation, model, rootNode)) model.removeAnnotation(annotation);
}
}
- private Iterable<Annotation> annotations() {
- return new Iterable<Annotation>() {
- @SuppressWarnings("unchecked") public Iterator<Annotation> iterator() {
- return getAnnotationModel().getAnnotationIterator();
- }
- };
- }
-
private boolean shouldRemoveFromModel(Annotation annotation, IAnnotationModel model, INode rootNode) {
if (!(annotation instanceof SpellingAnnotation)) return false;
SpellingAnnotation spellingAnnotation = (SpellingAnnotation) annotation;