Code cleanup.
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 1633d1b..c7140ae 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
@@ -9,6 +9,7 @@ package com.google.eclipse.protobuf.ui.commands; import static com.google.eclipse.protobuf.protobuf.Modifier.REPEATED; +import static com.google.eclipse.protobuf.ui.grammar.CommonKeyword.SEMICOLON; import java.util.regex.Pattern; @@ -23,7 +24,6 @@ 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.grammar.Keywords; import com.google.eclipse.protobuf.ui.util.Literals; import com.google.eclipse.protobuf.ui.util.Properties; import com.google.inject.Inject; @@ -50,9 +50,9 @@ private final String semicolon; - @Inject public SmartSemicolonHandler(CompoundElements compoundElements, Keywords keywords) { + @Inject public SmartSemicolonHandler(CompoundElements compoundElements) { this.compoundElements = compoundElements; - semicolon = keywords.semicolon().getValue(); + semicolon = SEMICOLON.value; } /** {@inheritDoc} */
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 53bcd64..008f09c 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
@@ -10,6 +10,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 java.lang.String.valueOf; import org.eclipse.emf.ecore.EObject; @@ -24,8 +25,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.Keywords; import com.google.eclipse.protobuf.ui.labeling.Images; import com.google.eclipse.protobuf.ui.util.*; import com.google.eclipse.protobuf.util.EObjectFinder; @@ -43,7 +44,6 @@ @Inject private Globals globals; @Inject private PluginImageHelper imageHelper; @Inject private Images imageRegistry; - @Inject private Keywords keywords; @Inject private Literals literals; @Inject private Properties properties; @Inject private Strings strings; @@ -60,10 +60,10 @@ private void proposeCommonFileOptions(ContentAssistContext context, ICompletionProposalAcceptor acceptor) { for (Property fileOption : globals.fileOptions()) { String displayString = fileOption.getName(); - String proposalText = displayString + " " + keywords.equalSign().getValue() + " "; + String proposalText = displayString + " " + EQUAL.value + " "; boolean isStringOption = properties.isString(fileOption); if (isStringOption) - proposalText = proposalText + compoundElements.emptyString() + keywords.semicolon().getValue(); + proposalText = proposalText + compoundElements.emptyString() + SEMICOLON.value; ICompletionProposal proposal = createCompletionProposal(proposalText, displayString, context); if (isStringOption && proposal instanceof ConfigurableCompletionProposal) { // set cursor between the proposal's quotes @@ -94,8 +94,14 @@ } private void proposeBooleanValues(ContentAssistContext context, ICompletionProposalAcceptor acceptor) { - proposeAndAccept(keywords.boolFalse().getValue(), context, acceptor); - proposeAndAccept(keywords.boolTrue().getValue(), context, acceptor); + CommonKeyword[] keywords = { FALSE, TRUE }; + proposeAndAccept(keywords, context, acceptor); + } + + private void proposeAndAccept(CommonKeyword[] keywords, ContentAssistContext context, + ICompletionProposalAcceptor acceptor) { + for (CommonKeyword keyword : keywords) + proposeAndAccept(keyword.value, context, acceptor); } @Override public void complete_ID(EObject model, RuleCall ruleCall, ContentAssistContext context, @@ -123,7 +129,7 @@ } private boolean isProposalForDefaultValue(ContentAssistContext context) { - return isProposalForAssignment(keywords.defaultValue().getValue(), context); + return isProposalForAssignment(DEFAULT.value, context); } private boolean isProposalForAssignment(String feature, ContentAssistContext context) { @@ -131,7 +137,7 @@ for (AbstractElement e : grammarElements) { if (!(e instanceof Assignment)) continue; Assignment a = (Assignment) e; - String equalSign = keywords.equalSign().getValue(); + String equalSign = EQUAL.value; if (feature.equals(a.getFeature()) && equalSign.equals(a.getOperator())) return true; } return false; @@ -141,18 +147,18 @@ ICompletionProposalAcceptor acceptor) { if (keyword == null) return; if (isKeywordEqualToPreviousWordInEditor(keyword, context)) return; - if (keyword.equals(keywords.boolTrue()) || keyword.equals(keywords.boolFalse())) { + if (TRUE.hasValueEqualTo(keyword) || FALSE.hasValueEqualTo(keyword)) { if (!isBoolProposalValid(context)) return; } - if (keyword.equals(keywords.openingBracket())) { + if (OPENING_BRACKET.hasValueEqualTo(keyword)) { boolean proposalWasHandledAlready = proposeOpenBracket(context, acceptor); if (proposalWasHandledAlready) return; } - if (keyword.equals(keywords.packed())) { + if (PACKED.hasValueEqualTo(keyword)) { proposePackedOption(context, acceptor); return; } - if (keyword.equals(keywords.defaultValue())) { + if (DEFAULT.hasValueEqualTo(keyword)) { proposeDefaultValue(context, acceptor); return; } @@ -186,7 +192,7 @@ Modifier modifier = p.getModifier(); if (OPTIONAL.equals(modifier)) { String display = compoundElements.defaultValueInBrackets(); - int cursorPosition = display.indexOf(keywords.closingBracket().getValue()); + int cursorPosition = display.indexOf(CLOSING_BRACKET.value); if (isStringProperty(p)) { display = compoundElements.defaultStringValueInBrackets(); cursorPosition++;
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 new file mode 100644 index 0000000..f156559 --- /dev/null +++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/grammar/CommonKeyword.java
@@ -0,0 +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.grammar; + +import org.eclipse.xtext.Keyword; + +/** + * Set of common keywords (it may or may not include all the keywords in the grammar.) + * + * @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"); + + public final String value; + + private CommonKeyword(String value) { + this.value = value; + } + + public boolean hasValueEqualTo(Keyword k) { + return hasValueEqualTo(k.getValue()); + } + + public boolean hasValueEqualTo(String s) { + return value.equals(s); + } +}
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/Properties.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/Properties.java index a484024..ebd00e4 100644 --- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/Properties.java +++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/Properties.java
@@ -8,16 +8,14 @@ */ package com.google.eclipse.protobuf.ui.util; +import static com.google.eclipse.protobuf.ui.grammar.CommonKeyword.*; import static java.lang.Math.max; import static org.eclipse.xtext.EcoreUtil2.getAllContentsOfType; import java.util.List; -import org.eclipse.xtext.Keyword; - import com.google.eclipse.protobuf.protobuf.*; -import com.google.eclipse.protobuf.ui.grammar.Keywords; -import com.google.inject.Inject; +import com.google.eclipse.protobuf.ui.grammar.CommonKeyword; import com.google.inject.Singleton; /** @@ -28,10 +26,8 @@ @Singleton public class Properties { - @Inject private Keywords keywords; - /** - * Indicates whether the type of the given property is primitive. Primitive types include: {@code double}, + * Indicates whether the type of the given property is primitive. Primitive types include: {@code double}, * {@code float}, {@code int32}, {@code int64}, {@code uint32}, {@code uint64}, {@code sint32}, {@code sint64}, * {@code fixed32}, {@code fixed64}, {@code sfixed32}, {@code sfixed64} and {@code bool}. * @param p the given property. @@ -41,7 +37,7 @@ AbstractTypeReference r = p.getType(); if (!(r instanceof ScalarTypeReference)) return false; String typeName = ((ScalarTypeReference) r).getScalar().getName(); - return !keywords.string().getValue().equals(typeName) && !keywords.bytes().getValue().equals(typeName); + return !STRING.hasValueEqualTo(typeName) && !BYTES.hasValueEqualTo(typeName); } /** @@ -50,7 +46,7 @@ * @return {@code true} if the given property is of type {@code bool}, {@code false} otherwise. */ public boolean isBool(Property p) { - return isScalarType(p, keywords.bool()); + return isScalarType(p, BOOL); } /** @@ -59,11 +55,11 @@ * @return {@code true} if the given property is of type {@code string}, {@code false} otherwise. */ public boolean isString(Property p) { - return isScalarType(p, keywords.string()); + return isScalarType(p, STRING); } - - private boolean isScalarType(Property p, Keyword typeKeyword) { - return typeKeyword.getValue().equals(typeNameOf(p)); + + private boolean isScalarType(Property p, CommonKeyword typeKeyword) { + return typeKeyword.hasValueEqualTo(typeNameOf(p)); } /** @@ -82,18 +78,18 @@ } /** - * Calculates the tag number value for the given property. The calculated tag number value is the maximum of all the + * Calculates the tag number value for the given property. The calculated tag number value is the maximum of all the * tag number values of the given property's siblings, plus one. The minimum tag number value is 1. * <p> * For example, in the following message: - * + * * <pre> * message Person { * required string name = 1; * optional string email = 2; * optional PhoneNumber phone = * </pre> - * + * * The calculated tag number value for the property {@code PhoneNumber} will be 3. * </p> * @param p the given property.