FixedL [Issue 118] Add support for spell checking
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/editor/spelling/NodeBasedRegion.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/editor/spelling/NodeBasedRegion.java
deleted file mode 100644
index ab9396c..0000000
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/editor/spelling/NodeBasedRegion.java
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * Copyright (c) 2011 Google Inc.
- * 
- * 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
- */
-package com.google.eclipse.protobuf.ui.editor.spelling;
-
-import org.eclipse.jface.text.IRegion;
-import org.eclipse.xtext.nodemodel.INode;
-
-class NodeBasedRegion implements IRegion {
-  
-  private final int length;
-  private final int offset;
-  private final String text;
-
-  NodeBasedRegion(INode node) {
-    length = node.getTotalLength();
-    offset = node.getTotalOffset();
-    text = node.getText();
-  }
-
-  public int getLength() {
-    return length;
-  }
-
-  public int getOffset() {
-    return offset;
-  }
-
-  @Override public boolean equals(Object obj) {
-    if (this == obj) return true;
-    if (obj == null) return false;
-    if (getClass() != obj.getClass()) return false;
-    NodeBasedRegion other = (NodeBasedRegion) obj;
-    if (length != other.length) return false;
-    return offset == other.offset;
-  }
-
-  @Override public int hashCode() {
-    final int prime = 31;
-    int result = 1;
-    result = prime * result + length;
-    result = prime * result + offset;
-    return result;
-  }
-
-  @Override public String toString() {
-    String format = getClass().getSimpleName() + " [length=%s, offset=%s, text=%s]";
-    return String.format(format, length, offset, quote(text));
-  }
-  
-  private String quote(String s) {
-    if (s == null) return s;
-    return "'" + s + "'";
-  }
-}
\ No newline at end of file
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/editor/spelling/ProtobufReconcileStrategy.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/editor/spelling/ProtobufReconcileStrategy.java
index ba2991a..eedeb01 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/editor/spelling/ProtobufReconcileStrategy.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/editor/spelling/ProtobufReconcileStrategy.java
@@ -1,62 +1,36 @@
 /*
  * Copyright (c) 2011 Google Inc.
- * 
+ *
  * 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
  */
 package com.google.eclipse.protobuf.ui.editor.spelling;
 
-import static java.util.Collections.emptyList;
-import static org.eclipse.xtext.nodemodel.util.NodeModelUtils.findLeafNodeAtOffset;
-import static org.eclipse.xtext.util.Strings.isEmpty;
-
-import com.google.eclipse.protobuf.protobuf.Import;
-import com.google.eclipse.protobuf.util.ModelNodes;
-import com.google.inject.Inject;
-
 import org.eclipse.jface.text.*;
 import org.eclipse.jface.text.reconciler.DirtyRegion;
 import org.eclipse.jface.text.source.ISourceViewer;
-import org.eclipse.ui.texteditor.spelling.*;
-import org.eclipse.xtext.nodemodel.*;
-import org.eclipse.xtext.nodemodel.impl.AbstractNode;
-import org.eclipse.xtext.parser.IParseResult;
-import org.eclipse.xtext.resource.XtextResource;
-import org.eclipse.xtext.ui.editor.model.XtextDocument;
+import org.eclipse.ui.texteditor.spelling.SpellingService;
 import org.eclipse.xtext.ui.editor.reconciler.XtextDocumentReconcileStrategy;
-import org.eclipse.xtext.util.concurrent.IUnitOfWork;
 
-import java.util.*;
+import com.google.eclipse.protobuf.util.ModelNodes;
+import com.google.inject.Inject;
 
 /**
  * @author alruiz@google.com (Alex Ruiz)
  */
 public class ProtobufReconcileStrategy extends XtextDocumentReconcileStrategy {
 
-  private SpellingReconcileStrategy spellingStrategy;
-  private XtextDocument document;
+  private ProtobufSpelling spellingStrategy;
 
   @Inject private ModelNodes nodes;
 
   @Override public void reconcile(DirtyRegion dirtyRegion, IRegion subRegion) {
     super.reconcile(dirtyRegion, subRegion);
     if (spellingStrategy == null) return;
-    INode node = nodeToSpellCheck(subRegion);
-    if (node != null) spellingStrategy.reconcile(dirtyRegion, new NodeBasedRegion(node));
-  }
-
-  private INode nodeToSpellCheck(final IRegion subRegion) {
-    if (document == null) return null;
-    return document.readOnly(new IUnitOfWork<INode, XtextResource>() {
-      public INode exec(XtextResource resource) throws Exception {
-        IParseResult parseResult = resource.getParseResult();
-        ILeafNode node = findLeafNodeAtOffset(parseResult.getRootNode(), subRegion.getOffset());
-        return shouldSpellCheck(node) ? node : null;
-      }
-    });
+    spellingStrategy.reconcile(dirtyRegion, subRegion);
   }
 
   @Override public void reconcile(IRegion subRegion) {
@@ -66,7 +40,6 @@
 
   @Override public void setDocument(IDocument document) {
     super.setDocument(document);
-    this.document = (XtextDocument) document;
     if (spellingStrategy == null) return;
     spellingStrategy.setDocument(document);
     initialReconcile();
@@ -74,48 +47,11 @@
 
   private void initialReconcile() {
     if (spellingStrategy == null) return;
-    for (IRegion region : regionsToSpellCheck()) {
-      spellingStrategy.reconcile(region);
-    }
-  }
-
-  private Collection<IRegion> regionsToSpellCheck() {
-    if (document == null) return emptyList();
-    return document.readOnly(new IUnitOfWork<Collection<IRegion>, XtextResource>() {
-      public Collection<IRegion> exec(XtextResource resource) throws Exception {
-        return regionsToSpellCheck(resource.getParseResult().getRootNode());
-      }
-    });
-  }
-  
-  private Collection<IRegion> regionsToSpellCheck(INode root) {
-    if (!(root instanceof AbstractNode)) {
-      System.out.println("is not instanceof AbstractNode: " + root);
-      return emptyList();
-    }
-    Set<IRegion> regions = new HashSet<IRegion>();
-    BidiTreeIterator<AbstractNode> iterator = ((AbstractNode) root).basicIterator();
-    while (iterator.hasNext()) {
-      AbstractNode next = iterator.next();
-      if (next == root) continue;
-      if (next instanceof ILeafNode) {
-        if (shouldSpellCheck(next)) regions.add(new NodeBasedRegion(next));
-        continue;
-      }
-      if (next.getSemanticElement() instanceof Import) continue;
-      regions.addAll(regionsToSpellCheck(next));
-    }
-    return regions;
-  }
-  
-  private boolean shouldSpellCheck(INode node) {
-    String text = node.getText();
-    if (isEmpty(text) || isEmpty(text.trim())) return false;
-    return nodes.isCommentOrString(node);
+    spellingStrategy.initialReconcile();
   }
 
   void addSpellSupport(ISourceViewer viewer, SpellingService spellingService) {
     if (spellingStrategy != null) return;
-    spellingStrategy = new SpellingReconcileStrategy(viewer, spellingService);
+    spellingStrategy = new ProtobufSpelling(viewer, spellingService, nodes);
   }
 }
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
new file mode 100644
index 0000000..2b482dd
--- /dev/null
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/editor/spelling/ProtobufSpelling.java
@@ -0,0 +1,90 @@
+/*
+ * Copyright (c) 2011 Google Inc.
+ *
+ * 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
+ */
+package com.google.eclipse.protobuf.ui.editor.spelling;
+
+import static org.eclipse.xtext.nodemodel.util.NodeModelUtils.findLeafNodeAtOffset;
+import static org.eclipse.xtext.util.Strings.isEmpty;
+
+import java.util.Iterator;
+
+import org.eclipse.jface.text.*;
+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;
+
+import com.google.eclipse.protobuf.util.ModelNodes;
+
+/**
+ * @author alruiz@google.com (Alex Ruiz)
+ */
+class ProtobufSpelling extends SpellingReconcileStrategy {
+
+  private final ModelNodes nodes;
+
+  ProtobufSpelling(ISourceViewer viewer, SpellingService spellingService, ModelNodes nodes) {
+    super(viewer, spellingService);
+    this.nodes = nodes;
+  }
+
+  @Override public void setDocument(IDocument document) {
+    super.setDocument(document);
+  }
+
+  public void reconcile(IRegion region) {
+    super.reconcile(new Region(0, xtextDocument().getLength()));
+    removeUnwantedAnnotations();
+  }
+
+  private void removeUnwantedAnnotations() {
+    xtextDocument().readOnly(new IUnitOfWork.Void<XtextResource>() {
+      @Override public void process(XtextResource resource) throws Exception {
+        removeUnwantedAnnotations(resource.getParseResult());
+      }
+    });
+  }
+
+  private XtextDocument xtextDocument() {
+    return (XtextDocument) super.getDocument();
+  }
+
+  private void removeUnwantedAnnotations(IParseResult parseResult) {
+    IAnnotationModel model = getAnnotationModel();
+    ICompositeNode rootNode = parseResult.getRootNode();
+    for (Annotation annotation : annotations()) {
+      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;
+    Position position = model.getPosition(spellingAnnotation);
+    ILeafNode node = findLeafNodeAtOffset(rootNode, position.getOffset());
+    return !shouldSpellCheck(node);
+  }
+
+  private boolean shouldSpellCheck(INode node) {
+    if (node == null) return false;
+    String text = node.getText();
+    if (isEmpty(text) || isEmpty(text.trim())) return false;
+    return nodes.isCommentOrString(node);
+  }
+}
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/util/ModelNodes.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/util/ModelNodes.java
index 1fce06a..e8c5d31 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/util/ModelNodes.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/util/ModelNodes.java
@@ -10,17 +10,17 @@
 
 import static org.eclipse.xtext.nodemodel.util.NodeModelUtils.findNodesForFeature;
 
-import com.google.inject.Singleton;
+import java.util.List;
 
 import org.eclipse.emf.ecore.*;
 import org.eclipse.xtext.*;
 import org.eclipse.xtext.nodemodel.INode;
 
-import java.util.List;
+import com.google.inject.Singleton;
 
 /**
  * Utility methods related to <code>{@link INode}</code>s.
- * 
+ *
  * @author alruiz@google.com (Alex Ruiz)
  */
 @Singleton
@@ -44,20 +44,20 @@
   /**
    * Indicates whether the given node was created by a string, or a single- or multi-line comment.
    * @param node the node to check.
-   * @return {@code true} if the given node was created by a string, or a single- or multi-line comment; {@code false} 
+   * @return {@code true} if the given node was created by a string, or a single- or multi-line comment; {@code false}
    * otherwise.
    */
   public boolean isCommentOrString(INode node) {
     return wasCreatedByAnyComment(node) || wasCreatedByString(node);
   }
-  
+
   /**
    * Indicates whether the given node was created by a single- or multi-line comment.
    * @param node the node to check.
    * @return {@code true} if the given node was created by a single- or multi-line comment; {@code false} otherwise.
    */
   public boolean wasCreatedByAnyComment(INode node) {
-    return wasCreatedByComment(node, SINGLE_LINE_COMMENT_RULE_NAME, "ML_COMMENT");
+    return wasCreatedByRule(node, SINGLE_LINE_COMMENT_RULE_NAME, "ML_COMMENT");
   }
 
   private boolean wasCreatedByString(INode node) {
@@ -69,22 +69,21 @@
     return "STRING".equals(terminalRule.getName());
   }
 
-  
   /**
    * Indicates whether the given node was created by a single-line comment.
    * @param node the node to check.
    * @return {@code true} if the given node was created by a single-line comment; {@code false} otherwise.
    */
   public boolean wasCreatedBySingleLineComment(INode node) {
-    return wasCreatedByComment(node, SINGLE_LINE_COMMENT_RULE_NAME);
+    return wasCreatedByRule(node, SINGLE_LINE_COMMENT_RULE_NAME);
   }
-  
-  private boolean wasCreatedByComment(INode node, String...commentRuleNames) {
+
+  private boolean wasCreatedByRule(INode node, String...ruleNames) {
     EObject o = node.getGrammarElement();
     if (!(o instanceof TerminalRule)) return false;
     TerminalRule rule = (TerminalRule) o;
     String actualName = rule.getName();
-    for (String name : commentRuleNames) {
+    for (String name : ruleNames) {
       if (name.equalsIgnoreCase(actualName)) return true;
     }
     return false;