Code cleanup.
diff --git a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/grammar/CompoundElementsTest.java b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/grammar/CompoundElementsTest.java
deleted file mode 100644
index 9116109..0000000
--- a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/grammar/CompoundElementsTest.java
+++ /dev/null
@@ -1,60 +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.grammar;
-
-import static org.hamcrest.core.IsEqual.equalTo;
-import static org.junit.Assert.assertThat;
-
-import org.junit.*;
-
-import com.google.eclipse.protobuf.junit.XtextRule;
-
-/**
- * Tests for <code>{@link CompoundElements}</code>.
- *
- * @author alruiz@google.com (Alex Ruiz)
- */
-public class CompoundElementsTest {
-
- @Rule public XtextRule xtext = new XtextRule();
-
- private CompoundElements elements;
-
- @Before public void setUp() {
- elements = xtext.getInstanceOf(CompoundElements.class);
- }
-
- @Test public void should_return_default() {
- assertThat(elements.defaultValue(), equalTo("default ="));
- }
-
- @Test public void should_return_default_in_brackets() {
- assertThat(elements.defaultValueInBrackets(), equalTo("[default =]"));
- }
-
- @Test public void should_return_default_for_string() {
- assertThat(elements.defaultStringValue(), equalTo("default = \"\""));
- }
-
- @Test public void should_return_default_for_string_in_brackets() {
- assertThat(elements.defaultStringValueInBrackets(), equalTo("[default = \"\"]"));
- }
-
- @Test public void should_return_empty_string() {
- assertThat(elements.emptyString(), equalTo("\"\""));
- }
-
- @Test public void should_return_packed() {
- assertThat(elements.packed(), equalTo("packed = true"));
- }
-
- @Test public void should_return_packed_in_brackets() {
- assertThat(elements.packedInBrackets(), equalTo("[packed = true]"));
- }
-}
diff --git a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/grammar/KeywordsTest.java b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/grammar/KeywordsTest.java
deleted file mode 100644
index 47bbe46..0000000
--- a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/grammar/KeywordsTest.java
+++ /dev/null
@@ -1,76 +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.grammar;
-
-import static com.google.eclipse.protobuf.junit.KeywordHasValueMatcher.hasValue;
-import static org.junit.Assert.assertThat;
-
-import org.junit.*;
-
-import com.google.eclipse.protobuf.junit.XtextRule;
-
-/**
- * Tests for <code>{@link Keywords}</code>.
- *
- * @author alruiz@google.com (Alex Ruiz)
- */
-public class KeywordsTest {
-
- @Rule public XtextRule xtext = new XtextRule();
-
- private Keywords keywords;
-
- @Before public void setUp() {
- keywords = xtext.getInstanceOf(Keywords.class);
- }
-
- @Test public void should_return_bool() {
- assertThat(keywords.bool(), hasValue("bool"));
- }
-
- @Test public void should_return_true() {
- assertThat(keywords.boolTrue(), hasValue("true"));
- }
-
- @Test public void should_return_false() {
- assertThat(keywords.boolFalse(), hasValue("false"));
- }
-
- @Test public void should_return_bytes() {
- assertThat(keywords.bytes(), hasValue("bytes"));
- }
-
- @Test public void should_return_opening_bracket() {
- assertThat(keywords.openingBracket(), hasValue("["));
- }
-
- @Test public void should_return_closing_bracket() {
- assertThat(keywords.closingBracket(), hasValue("]"));
- }
-
- @Test public void should_return_default() {
- assertThat(keywords.defaultValue(), hasValue("default"));
- }
-
- @Test public void should_return_equal_sign() {
- assertThat(keywords.equalSign(), hasValue("="));
- }
-
- @Test public void should_return_packed() {
- assertThat(keywords.packed(), hasValue("packed"));
- }
-
- @Test public void should_return_semicolon() {
- assertThat(keywords.semicolon(), hasValue(";"));
- }
-
- @Test public void should_return_string() {
- assertThat(keywords.string(), hasValue("string"));
- }
-}
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 c7140ae..b34db80 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
@@ -10,6 +10,7 @@
import static com.google.eclipse.protobuf.protobuf.Modifier.REPEATED;
import static com.google.eclipse.protobuf.ui.grammar.CommonKeyword.SEMICOLON;
+import static com.google.eclipse.protobuf.ui.grammar.CompoundElement.PACKED_EQUAL_TRUE_IN_BRACKETS;
import java.util.regex.Pattern;
@@ -21,11 +22,8 @@
import org.eclipse.xtext.ui.editor.contentassist.antlr.ParserBasedContentAssistContextFactory;
import org.eclipse.xtext.util.concurrent.IUnitOfWork;
-import com.google.eclipse.protobuf.protobuf.Literal;
-import com.google.eclipse.protobuf.protobuf.Property;
-import com.google.eclipse.protobuf.ui.grammar.CompoundElements;
-import com.google.eclipse.protobuf.ui.util.Literals;
-import com.google.eclipse.protobuf.ui.util.Properties;
+import com.google.eclipse.protobuf.protobuf.*;
+import com.google.eclipse.protobuf.ui.util.*;
import com.google.inject.Inject;
/**
@@ -42,18 +40,11 @@
private static final Pattern PROPERTY_WITH_INDEX =
Pattern.compile("[\\s]+(.*)[\\s]+(.*)[\\s]+(.*)[\\s]+=[\\s]+[\\d]+(.*)");
- private final CompoundElements compoundElements;
-
@Inject private ParserBasedContentAssistContextFactory contextFactory;
@Inject private Literals literals;
@Inject private Properties properties;
- private final String semicolon;
-
- @Inject public SmartSemicolonHandler(CompoundElements compoundElements) {
- this.compoundElements = compoundElements;
- semicolon = SEMICOLON.value;
- }
+ private final String semicolon = SEMICOLON.toString();
/** {@inheritDoc} */
@Override protected void insertContent(XtextEditor editor) {
@@ -107,14 +98,14 @@
if (hasIndexAlready) {
// we can still insert '[packed = true]' if necessary
if (shouldInsertPackedOption(property)) {
- String content = compoundElements.packedInBrackets() + semicolon;
+ String content = PACKED_EQUAL_TRUE_IN_BRACKETS + semicolon;
return addSpaceAtBeginning(line, content);
}
return semicolon;
}
int index = properties.calculateTagNumberOf(property);
if (shouldInsertPackedOption(property)) {
- String format = "= %d " + compoundElements.packedInBrackets() + "%s";
+ String format = "= %d " + PACKED_EQUAL_TRUE_IN_BRACKETS + "%s";
return indexAndSemicolonToInsert(format, line, index);
}
return defaultIndexAndSemicolonToInsert(line, index);
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/contentassist/ProtobufProposalProvider.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/contentassist/ProtobufProposalProvider.java
index 008f09c..6c8cf04 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/contentassist/ProtobufProposalProvider.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/contentassist/ProtobufProposalProvider.java
@@ -11,6 +11,7 @@
import static com.google.eclipse.protobuf.protobuf.Modifier.*;
import static com.google.eclipse.protobuf.protobuf.ScalarType.STRING;
import static com.google.eclipse.protobuf.ui.grammar.CommonKeyword.*;
+import static com.google.eclipse.protobuf.ui.grammar.CompoundElement.*;
import static java.lang.String.valueOf;
import org.eclipse.emf.ecore.EObject;
@@ -25,8 +26,8 @@
import com.google.eclipse.protobuf.protobuf.*;
import com.google.eclipse.protobuf.protobuf.Enum;
import com.google.eclipse.protobuf.scoping.Globals;
-import com.google.eclipse.protobuf.ui.grammar.CommonKeyword;
-import com.google.eclipse.protobuf.ui.grammar.CompoundElements;
+import com.google.eclipse.protobuf.ui.grammar.*;
+import com.google.eclipse.protobuf.ui.grammar.CompoundElement;
import com.google.eclipse.protobuf.ui.labeling.Images;
import com.google.eclipse.protobuf.ui.util.*;
import com.google.eclipse.protobuf.util.EObjectFinder;
@@ -39,7 +40,6 @@
*/
public class ProtobufProposalProvider extends AbstractProtobufProposalProvider {
- @Inject private CompoundElements compoundElements;
@Inject private EObjectFinder finder;
@Inject private Globals globals;
@Inject private PluginImageHelper imageHelper;
@@ -60,10 +60,10 @@
private void proposeCommonFileOptions(ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
for (Property fileOption : globals.fileOptions()) {
String displayString = fileOption.getName();
- String proposalText = displayString + " " + EQUAL.value + " ";
+ String proposalText = displayString + " " + EQUAL + " ";
boolean isStringOption = properties.isString(fileOption);
if (isStringOption)
- proposalText = proposalText + compoundElements.emptyString() + SEMICOLON.value;
+ proposalText = proposalText + EMPTY_STRING + SEMICOLON;
ICompletionProposal proposal = createCompletionProposal(proposalText, displayString, context);
if (isStringOption && proposal instanceof ConfigurableCompletionProposal) {
// set cursor between the proposal's quotes
@@ -100,8 +100,12 @@
private void proposeAndAccept(CommonKeyword[] keywords, ContentAssistContext context,
ICompletionProposalAcceptor acceptor) {
- for (CommonKeyword keyword : keywords)
- proposeAndAccept(keyword.value, context, acceptor);
+ for (CommonKeyword keyword : keywords) proposeAndAccept(keyword, context, acceptor);
+ }
+
+ private void proposeAndAccept(CommonKeyword keyword, ContentAssistContext context,
+ ICompletionProposalAcceptor acceptor) {
+ proposeAndAccept(keyword.toString(), context, acceptor);
}
@Override public void complete_ID(EObject model, RuleCall ruleCall, ContentAssistContext context,
@@ -120,7 +124,7 @@
}
private void proposeEmptyString(ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
- ICompletionProposal proposal = createCompletionProposal(compoundElements.emptyString(), context);
+ ICompletionProposal proposal = createCompletionProposal(EMPTY_STRING, context);
if (proposal instanceof ConfigurableCompletionProposal) {
ConfigurableCompletionProposal configurable = (ConfigurableCompletionProposal) proposal;
configurable.setCursorPosition(1);
@@ -129,16 +133,15 @@
}
private boolean isProposalForDefaultValue(ContentAssistContext context) {
- return isProposalForAssignment(DEFAULT.value, context);
+ return isProposalForAssignment(DEFAULT, context);
}
- private boolean isProposalForAssignment(String feature, ContentAssistContext context) {
+ private boolean isProposalForAssignment(CommonKeyword feature, ContentAssistContext context) {
ImmutableList<AbstractElement> grammarElements = context.getFirstSetGrammarElements();
for (AbstractElement e : grammarElements) {
if (!(e instanceof Assignment)) continue;
Assignment a = (Assignment) e;
- String equalSign = EQUAL.value;
- if (feature.equals(a.getFeature()) && equalSign.equals(a.getOperator())) return true;
+ if (feature.hasValueEqualTo(a.getFeature()) && EQUAL.hasValueEqualTo(a.getOperator())) return true;
}
return false;
}
@@ -191,10 +194,10 @@
Property p = (Property) model;
Modifier modifier = p.getModifier();
if (OPTIONAL.equals(modifier)) {
- String display = compoundElements.defaultValueInBrackets();
- int cursorPosition = display.indexOf(CLOSING_BRACKET.value);
+ CompoundElement display = DEFAULT_EQUAL_IN_BRACKETS;
+ int cursorPosition = display.indexOf(CLOSING_BRACKET);
if (isStringProperty(p)) {
- display = compoundElements.defaultStringValueInBrackets();
+ display = DEFAULT_EQUAL_STRING_IN_BRACKETS;
cursorPosition++;
}
ICompletionProposal proposal = createCompletionProposal(display, context);
@@ -205,7 +208,7 @@
acceptor.accept(proposal);
}
if (REPEATED.equals(modifier) && properties.isPrimitive(p))
- proposeAndAccept(compoundElements.packedInBrackets(), context, acceptor);
+ proposeAndAccept(PACKED_EQUAL_TRUE_IN_BRACKETS, context, acceptor);
return true;
}
@@ -214,16 +217,21 @@
if (p == null) return;
Modifier modifier = p.getModifier();
if (!REPEATED.equals(modifier) || !properties.isPrimitive(p)) return;
- proposeAndAccept(compoundElements.packed(), context, acceptor);
+ proposeAndAccept(PACKED_EQUAL_TRUE, context, acceptor);
+ }
+
+ private void proposeAndAccept(CompoundElement proposalText, ContentAssistContext context,
+ ICompletionProposalAcceptor acceptor) {
+ proposeAndAccept(proposalText.toString(), context, acceptor);
}
private void proposeDefaultValue(ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
Modifier modifier = extractModifierFromModel(context);
if (!OPTIONAL.equals(modifier)) return;
- String display = compoundElements.defaultValue();
- int cursorPosition = display.length();
+ CompoundElement display = DEFAULT_EQUAL;
+ int cursorPosition = display.charCount();
if (isStringProperty((Property) context.getCurrentModel())) {
- display = compoundElements.defaultStringValue();
+ display = DEFAULT_EQUAL_STRING;
cursorPosition++;
}
ICompletionProposal proposal = createCompletionProposal(display, context);
@@ -251,6 +259,10 @@
return STRING.equals(finder.scalarTypeOf(p));
}
+ private ICompletionProposal createCompletionProposal(CompoundElement proposal, ContentAssistContext context) {
+ return createCompletionProposal(proposal.toString(), context);
+ }
+
@Override public void completeLiteral_Index(EObject model, Assignment assignment, ContentAssistContext context,
ICompletionProposalAcceptor acceptor) {
int index = literals.calculateIndexOf((Literal) model);
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/grammar/CommonKeyword.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/grammar/CommonKeyword.java
index f156559..24efb42 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/grammar/CommonKeyword.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/grammar/CommonKeyword.java
@@ -11,16 +11,19 @@
import org.eclipse.xtext.Keyword;
/**
- * Set of common keywords (it may or may not include all the keywords in the grammar.)
+ * A commonly used keyword.
*
* @author alruiz@google.com (Alex Ruiz)
*/
public enum CommonKeyword {
- BOOL("bool"), TRUE("true"), FALSE("false"), BYTES("bytes"), OPENING_BRACKET("["), CLOSING_BRACKET("]"),
- DEFAULT("default"), EQUAL("="), PACKED("packet"), SEMICOLON(":"), STRING("string");
+ // we used to get keywords from IGrammarAccess. The problem was that we still had to hardcode the keyword we were
+ // looking for. The code was too complicated and we had to change our implementation anyway.
- public final String value;
+ BOOL("bool"), TRUE("true"), FALSE("false"), BYTES("bytes"), OPENING_BRACKET("["), CLOSING_BRACKET("]"),
+ DEFAULT("default"), EQUAL("="), PACKED("packet"), SEMICOLON(";"), STRING("string");
+
+ private final String value;
private CommonKeyword(String value) {
this.value = value;
@@ -33,4 +36,8 @@
public boolean hasValueEqualTo(String s) {
return value.equals(s);
}
+
+ @Override public String toString() {
+ return value;
+ }
}
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/grammar/CompoundElement.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/grammar/CompoundElement.java
new file mode 100644
index 0000000..5fe9ed8
--- /dev/null
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/grammar/CompoundElement.java
@@ -0,0 +1,69 @@
+/*
+ * 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.grammar;
+
+import static com.google.eclipse.protobuf.ui.grammar.CommonKeyword.*;
+
+/**
+ * Element composed of one or more keywords.
+ *
+ * @author alruiz@google.com (Alex Ruiz)
+ */
+public enum CompoundElement {
+
+ DEFAULT_EQUAL(join(DEFAULT, EQUAL)),
+ DEFAULT_EQUAL_IN_BRACKETS(inBrackets(DEFAULT_EQUAL)),
+ EMPTY_STRING("\"\""),
+ DEFAULT_EQUAL_STRING(join(DEFAULT_EQUAL, EMPTY_STRING)),
+ DEFAULT_EQUAL_STRING_IN_BRACKETS(inBrackets(DEFAULT_EQUAL_STRING)),
+ PACKED_EQUAL_TRUE(join(PACKED, EQUAL, TRUE)),
+ PACKED_EQUAL_TRUE_IN_BRACKETS(inBrackets(PACKED_EQUAL_TRUE));
+
+ private final String value;
+
+ private static String join(CommonKeyword...keywords) {
+ StringBuilder buffer = new StringBuilder();
+ int count = keywords.length;
+ for (int i = 0; i < count; i++) {
+ buffer.append(keywords[i].toString());
+ if (i < count - 1) buffer.append(" ");
+ }
+ return buffer.toString();
+ }
+
+ private static String join(CompoundElement...elements) {
+ StringBuilder buffer = new StringBuilder();
+ int count = elements.length;
+ for (int i = 0; i < count; i++) {
+ buffer.append(elements[i].value);
+ if (i < count - 1) buffer.append(" ");
+ }
+ return buffer.toString();
+ }
+
+ private static String inBrackets(CompoundElement element) {
+ return String.format("[%s]", element.value);
+ }
+
+ private CompoundElement(String value) {
+ this.value = value;
+ }
+
+ public int charCount() {
+ return value.length();
+ }
+
+ public int indexOf(CommonKeyword keyword) {
+ return value.indexOf(keyword.toString());
+ }
+
+ @Override public String toString() {
+ return value;
+ }
+}
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/grammar/CompoundElements.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/grammar/CompoundElements.java
deleted file mode 100644
index a619471..0000000
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/grammar/CompoundElements.java
+++ /dev/null
@@ -1,118 +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.grammar;
-
-import org.eclipse.xtext.Keyword;
-
-import com.google.inject.Inject;
-import com.google.inject.Singleton;
-
-/**
- * Common grammar elements composed of one or more keywords.
- *
- * @author alruiz@google.com (Alex Ruiz)
- */
-@Singleton
-public class CompoundElements {
-
- private static final String EMPTY_STRING = "\"\"";
-
- private final String inBracketsFormat;
-
- private final String defaultValue;
- private final String defaultValueInBrackets;
- private final String defaultStringValue;
- private final String defaultStringValueInBrackets;
- private final String packed;
- private final String packedInBrackets;
-
- /**
- * Creates a new </code>{@link CompoundElements}</code>.
- * @param keywords the keywords in our grammar.
- */
- @Inject public CompoundElements(Keywords keywords) {
- inBracketsFormat = keywords.openingBracket().getValue() + "%s" + keywords.closingBracket().getValue();
- defaultValue = format("%s %s", keywords.defaultValue(), keywords.equalSign());
- defaultValueInBrackets = inBrackets(defaultValue);
- defaultStringValue = format("%s %s %s", keywords.defaultValue(), keywords.equalSign(), EMPTY_STRING);
- defaultStringValueInBrackets = inBrackets(defaultStringValue);
- packed = format("%s %s %s", keywords.packed(), keywords.equalSign(), keywords.boolTrue());
- packedInBrackets = inBrackets(packed);
- }
-
- private static String format(String format, Object...values) {
- int count = values.length;
- Object[] cleanValues = new Object[count];
- for (int i = 0; i < count; i++) {
- Object value = values[i];
- cleanValues[i] = (value instanceof Keyword) ? ((Keyword) value).getValue() : value;
- }
- return String.format(format, cleanValues);
- }
-
- private String inBrackets(String element) {
- return String.format(inBracketsFormat, element);
- }
-
- /**
- * Returns 'default ='.
- * @return 'default ='.
- */
- public String defaultValue() {
- return defaultValue;
- }
-
- /**
- * Returns '[default =]'.
- * @return '[default =]'.
- */
- public String defaultValueInBrackets() {
- return defaultValueInBrackets;
- }
-
- /**
- * Returns 'default = ""'.
- * @return 'default = ""'.
- */
- public String defaultStringValue() {
- return defaultStringValue;
- }
-
- /**
- * Returns '[default = ""]'.
- * @return '[default = ""]'.
- */
- public String defaultStringValueInBrackets() {
- return defaultStringValueInBrackets;
- }
-
- /**
- * Returns '""'.
- * @return '""'.
- */
- public String emptyString() {
- return EMPTY_STRING;
- }
-
- /**
- * Returns 'packed = true'.
- * @return 'packed = true'.
- */
- public String packed() {
- return packed;
- }
-
- /**
- * Returns '[packed = true]'.
- * @return '[packed = true]'.
- */
- public String packedInBrackets() {
- return packedInBrackets;
- }
-}
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/grammar/Keywords.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/grammar/Keywords.java
deleted file mode 100644
index a8695c2..0000000
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/grammar/Keywords.java
+++ /dev/null
@@ -1,219 +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.grammar;
-
-import static org.eclipse.xtext.GrammarUtil.containedKeywords;
-
-import java.util.List;
-
-import org.eclipse.xtext.IGrammarAccess;
-import org.eclipse.xtext.Keyword;
-
-import com.google.inject.Inject;
-import com.google.inject.Singleton;
-
-/**
- * Common keywords used in the code base.
- *
- * @author alruiz@google.com (Alex Ruiz)
- */
-@Singleton
-public class Keywords {
-
- private Keyword bool;
- private Keyword boolTrue;
- private Keyword boolFalse;
- private Keyword bytes;
- private Keyword openingBracket;
- private Keyword closingBracket;
- private Keyword defaultValue;
- private Keyword equalSign;
- private Keyword packed;
- private Keyword semicolon;
- private Keyword string;
-
- /**
- * Creates a new </code>{@link Keywords}</code>.
- * @param grammarAccess contains the grammar whose keywords we'll be extracting.
- */
- @Inject public Keywords(IGrammarAccess grammarAccess) {
- List<Keyword> allKeywords = containedKeywords(grammarAccess.getGrammar());
- for (Keyword k : allKeywords) {
- if (assignIfIsBool(k)) continue;
- if (assignIfIsBoolTrue(k)) continue;
- if (assignIfIsBoolFalse(k)) continue;
- if (assignIfIsBytes(k)) continue;
- if (assignIfIsOpeningBracket(k)) continue;
- if (assignIfIsClosingBracket(k)) continue;
- if (assignIfIsDefaultValue(k)) continue;
- if (assignIfIsEqualSign(k)) continue;
- if (assignIfIsPacked(k)) continue;
- if (assignIfIsSemicolon(k)) continue;
- if (assignIfIsString(k)) continue;
- }
- }
-
- private boolean assignIfIsBool(Keyword k) {
- if (!isKeywordValue(k, "bool")) return false;
- bool = k;
- return true;
- }
-
- private boolean assignIfIsBoolTrue(Keyword k) {
- if (!isKeywordValue(k, "true")) return false;
- boolTrue = k;
- return true;
- }
-
- private boolean assignIfIsBoolFalse(Keyword k) {
- if (!isKeywordValue(k, "false")) return false;
- boolFalse = k;
- return true;
- }
-
- private boolean assignIfIsBytes(Keyword k) {
- if (!isKeywordValue(k, "bytes")) return false;
- bytes = k;
- return true;
- }
-
- private boolean assignIfIsOpeningBracket(Keyword k) {
- if (!isKeywordValue(k, "[")) return false;
- openingBracket = k;
- return true;
- }
-
- private boolean assignIfIsClosingBracket(Keyword k) {
- if (!isKeywordValue(k, "]")) return false;
- closingBracket = k;
- return true;
- }
-
- private boolean assignIfIsDefaultValue(Keyword k) {
- if (!isKeywordValue(k, "default")) return false;
- defaultValue = k;
- return true;
- }
-
- private boolean assignIfIsEqualSign(Keyword k) {
- if (!isKeywordValue(k, "=")) return false;
- equalSign = k;
- return true;
- }
-
- private boolean assignIfIsPacked(Keyword k) {
- if (!isKeywordValue(k, "packed")) return false;
- packed = k;
- return true;
- }
-
- private boolean assignIfIsSemicolon(Keyword k) {
- if (!isKeywordValue(k, ";")) return false;
- semicolon = k;
- return true;
- }
-
- private boolean assignIfIsString(Keyword k) {
- if (!isKeywordValue(k, "string")) return false;
- string = k;
- return true;
- }
-
- private static boolean isKeywordValue(Keyword k, String expectedValue) {
- return expectedValue.equals(k.getValue());
- }
-
- /**
- * Returns the representation of the keyword "bool".
- * @return the representation of the keyword "bool".
- */
- public Keyword bool() {
- return bool;
- }
-
- /**
- * Returns the representation of the keyword "true".
- * @return the representation of the keyword "true".
- */
- public Keyword boolTrue() {
- return boolTrue;
- }
-
- /**
- * Returns the representation of the keyword "false".
- * @return the representation of the keyword "false".
- */
- public Keyword boolFalse() {
- return boolFalse;
- }
-
- /**
- * Returns the representation of the keyword "bytes".
- * @return the representation of the keyword "bytes".
- */
- public Keyword bytes() {
- return bytes;
- }
-
- /**
- * Returns the representation of the keyword "[".
- * @return the representation of the keyword "[".
- */
- public Keyword openingBracket() {
- return openingBracket;
- }
-
- /**
- * Returns the representation of the keyword "]".
- * @return the representation of the keyword "]".
- */
- public Keyword closingBracket() {
- return closingBracket;
- }
-
- /**
- * Returns the representation of the keyword "default".
- * @return the representation of the keyword "default".
- */
- public Keyword defaultValue() {
- return defaultValue;
- }
-
- /**
- * Returns the representation of the keyword "=".
- * @return the representation of the keyword "=".
- */
- public Keyword equalSign() {
- return equalSign;
- }
-
- /**
- * Returns the representation of the keyword "packed".
- * @return the representation of the keyword "packed".
- */
- public Keyword packed() {
- return packed;
- }
-
- /**
- * Returns the representation of the keyword ";".
- * @return the representation of the keyword ";".
- */
- public Keyword semicolon() {
- return semicolon;
- }
-
- /**
- * Returns the representation of the keyword "string".
- * @return the representation of the keyword "string".
- */
- public Keyword string() {
- return string;
- }
-}