In progress: [Issue 118] Add support for spell checking.
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/commands/SmartSemicolonHandler.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/commands/SmartSemicolonHandler.java
index 3bc3095..c693769 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/commands/SmartSemicolonHandler.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/commands/SmartSemicolonHandler.java
@@ -17,7 +17,6 @@
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.swt.custom.StyledText;
-import org.eclipse.xtext.*;
import org.eclipse.xtext.nodemodel.INode;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.ui.editor.XtextEditor;
@@ -87,14 +86,13 @@
private ContentToInsert newContent(final XtextEditor editor, final StyledText styledText, final String line) {
if (line.endsWith(SEMICOLON)) return INSERT_SEMICOLON_AT_CURRENT_LOCATION;
final IXtextDocument document = editor.getDocument();
- ContentToInsert contentToInsert = ContentToInsert.NONE;
try {
- contentToInsert = document.modify(new IUnitOfWork<ContentToInsert, XtextResource>() {
+ return document.modify(new IUnitOfWork<ContentToInsert, XtextResource>() {
public ContentToInsert exec(XtextResource resource) {
int offset = styledText.getCaretOffset();
ContentAssistContext[] context = contextFactory.create(editor.getInternalSourceViewer(), offset, resource);
for (ContentAssistContext c : context) {
- if (isCommentOrString(c.getCurrentNode())) continue;
+ if (nodes.isCommentOrString(c.getCurrentNode())) continue;
EObject model = c.getCurrentModel();
if (model instanceof Message || model instanceof Enum || model instanceof Protobuf) {
// need to retry, parsing may not be finished yet.
@@ -132,20 +130,6 @@
logger.error("Unable to generate tag number", e);
return INSERT_SEMICOLON_AT_CURRENT_LOCATION;
}
- return contentToInsert;
- }
-
- private boolean isCommentOrString(INode currentNode) {
- return nodes.wasCreatedByAnyComment(currentNode) || wasCreatedByString(currentNode);
- }
-
- private boolean wasCreatedByString(INode node) {
- EObject grammarElement = node.getGrammarElement();
- if (!(grammarElement instanceof RuleCall)) return false;
- AbstractRule rule = ((RuleCall) grammarElement).getRule();
- if (!(rule instanceof TerminalRule)) return false;
- TerminalRule terminalRule = (TerminalRule) rule;
- return "STRING".equals(terminalRule.getName());
}
private ContentToInsert newContent(Literal literal) {
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
new file mode 100644
index 0000000..ab9396c
--- /dev/null
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/editor/spelling/NodeBasedRegion.java
@@ -0,0 +1,61 @@
+/*
+ * 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 25220b7..ba2991a 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
@@ -9,11 +9,27 @@
*/
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.xtext.ui.editor.reconciler.XtextDocumentReconcileStrategy;
+import org.eclipse.xtext.util.concurrent.IUnitOfWork;
+
+import java.util.*;
/**
* @author alruiz@google.com (Alex Ruiz)
@@ -21,11 +37,26 @@
public class ProtobufReconcileStrategy extends XtextDocumentReconcileStrategy {
private SpellingReconcileStrategy spellingStrategy;
+ private XtextDocument document;
+
+ @Inject private ModelNodes nodes;
@Override public void reconcile(DirtyRegion dirtyRegion, IRegion subRegion) {
super.reconcile(dirtyRegion, subRegion);
if (spellingStrategy == null) return;
- spellingStrategy.reconcile(dirtyRegion, subRegion);
+ 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;
+ }
+ });
}
@Override public void reconcile(IRegion subRegion) {
@@ -35,6 +66,7 @@
@Override public void setDocument(IDocument document) {
super.setDocument(document);
+ this.document = (XtextDocument) document;
if (spellingStrategy == null) return;
spellingStrategy.setDocument(document);
initialReconcile();
@@ -42,7 +74,44 @@
private void initialReconcile() {
if (spellingStrategy == null) return;
- spellingStrategy.initialReconcile();
+ 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);
}
void addSpellSupport(ISourceViewer viewer, SpellingService spellingService) {
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 bdd8f79..1fce06a 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
@@ -13,7 +13,7 @@
import com.google.inject.Singleton;
import org.eclipse.emf.ecore.*;
-import org.eclipse.xtext.TerminalRule;
+import org.eclipse.xtext.*;
import org.eclipse.xtext.nodemodel.INode;
import java.util.List;
@@ -40,6 +40,16 @@
if (nodes.isEmpty()) return null;
return nodes.get(0);
}
+
+ /**
+ * 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}
+ * 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.
@@ -50,6 +60,16 @@
return wasCreatedByComment(node, SINGLE_LINE_COMMENT_RULE_NAME, "ML_COMMENT");
}
+ private boolean wasCreatedByString(INode node) {
+ EObject grammarElement = node.getGrammarElement();
+ if (!(grammarElement instanceof RuleCall)) return false;
+ AbstractRule rule = ((RuleCall) grammarElement).getRule();
+ if (!(rule instanceof TerminalRule)) return false;
+ TerminalRule terminalRule = (TerminalRule) rule;
+ return "STRING".equals(terminalRule.getName());
+ }
+
+
/**
* Indicates whether the given node was created by a single-line comment.
* @param node the node to check.