Code cleanup. Adding more tests.
diff --git a/com.google.eclipse.protobuf.test/.project b/com.google.eclipse.protobuf.test/.project
index 55e19bf..f1093ca 100644
--- a/com.google.eclipse.protobuf.test/.project
+++ b/com.google.eclipse.protobuf.test/.project
@@ -20,9 +20,15 @@
<arguments>
</arguments>
</buildCommand>
+ <buildCommand>
+ <name>org.eclipse.xtext.ui.shared.xtextBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
+ <nature>org.eclipse.xtext.ui.shared.xtextNature</nature>
</natures>
</projectDescription>
diff --git a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/grammar/Syntaxes_proto2_Test.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/grammar/Syntaxes_proto2_Test.java
new file mode 100644
index 0000000..87689b5
--- /dev/null
+++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/grammar/Syntaxes_proto2_Test.java
@@ -0,0 +1,26 @@
+/*
+ * 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.grammar;
+
+import static org.hamcrest.core.IsEqual.equalTo;
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+/**
+ * Tests for <code>{@link Syntaxes#proto2()}</code>.
+ *
+ * @author alruiz@google.com (Alex Ruiz)
+ */
+public class Syntaxes_proto2_Test {
+
+ @Test public void should_return_proto2() {
+ assertThat(Syntaxes.proto2(), equalTo("proto2"));
+ }
+}
diff --git a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/model/util/IndexedElements_calculateTagNumberOf_Test.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/model/util/IndexedElements_calculateNewIndexFor_Test.java
similarity index 81%
rename from com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/model/util/IndexedElements_calculateTagNumberOf_Test.java
rename to com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/model/util/IndexedElements_calculateNewIndexFor_Test.java
index a15a702..e63b968 100644
--- a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/model/util/IndexedElements_calculateTagNumberOf_Test.java
+++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/model/util/IndexedElements_calculateNewIndexFor_Test.java
@@ -19,11 +19,11 @@
import com.google.eclipse.protobuf.protobuf.*;
/**
- * Tests for <code>{@link IndexedElements#calculateTagNumberOf(IndexedElement)}</code>.
+ * Tests for <code>{@link IndexedElements#calculateNewIndexFor(IndexedElement)}</code>.
*
* @author alruiz@google.com (Alex Ruiz)
*/
-public class IndexedElements_calculateTagNumberOf_Test {
+public class IndexedElements_calculateNewIndexFor_Test {
@Rule public XtextRule xtext = createWith(unitTestSetup());
@@ -40,7 +40,7 @@
// }
@Test public void should_return_one_for_first_and_only_field() {
MessageField field = xtext.find("name", MessageField.class);
- long index = indexedElements.calculateTagNumberOf(field);
+ long index = indexedElements.calculateNewIndexFor(field);
assertThat(index, equalTo(1L));
}
@@ -50,9 +50,9 @@
// required string name = 6;
// required int32 id = 8;
// }
- @Test public void should_return_max_tag_number_value_plus_one_for_new_field() {
+ @Test public void should_return_max_index_value_plus_one_for_new_field() {
MessageField field = xtext.find("id", MessageField.class);
- long index = indexedElements.calculateTagNumberOf(field);
+ long index = indexedElements.calculateNewIndexFor(field);
assertThat(index, equalTo(7L));
}
}
diff --git a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/model/util/IndexedElements_fieldOptionsOf_Test.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/model/util/IndexedElements_fieldOptionsOf_Test.java
new file mode 100644
index 0000000..7be4bc9
--- /dev/null
+++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/model/util/IndexedElements_fieldOptionsOf_Test.java
@@ -0,0 +1,59 @@
+/*
+ * 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.model.util;
+
+import static com.google.eclipse.protobuf.junit.core.Setups.unitTestSetup;
+import static com.google.eclipse.protobuf.junit.core.XtextRule.createWith;
+import static org.hamcrest.core.IsEqual.equalTo;
+import static org.junit.Assert.assertThat;
+
+import java.util.List;
+
+import org.junit.*;
+
+import com.google.eclipse.protobuf.junit.core.XtextRule;
+import com.google.eclipse.protobuf.protobuf.*;
+
+/**
+ * Tests for <code>{@link IndexedElements#fieldOptionsOf(IndexedElement)}</code>
+ *
+ * @author alruiz@google.com (Alex Ruiz)
+ */
+public class IndexedElements_fieldOptionsOf_Test {
+
+ @Rule public XtextRule xtext = createWith(unitTestSetup());
+
+ private IndexedElements indexedElements;
+
+ @Before public void setUp() {
+ indexedElements = new IndexedElements();
+ }
+
+ // syntax = "proto2";
+ //
+ // message Person {
+ // optional bool active = 1 [default = false, deprecated = true];
+ // }
+ @Test public void should_return_options_of_MessageField() {
+ MessageField field = xtext.find("active", MessageField.class);
+ List<FieldOption> fieldOptions = indexedElements.fieldOptionsOf(field);
+ assertThat(fieldOptions.size(), equalTo(2));
+ }
+
+ // syntax = "proto2";
+ //
+ // message Person {
+ // optional group Names = 8 [deprecated = true] {}
+ // }
+ @Test public void should_return_index_of_Group() {
+ Group group = xtext.find("Names", Group.class);
+ List<FieldOption> fieldOptions = indexedElements.fieldOptionsOf(group);
+ assertThat(fieldOptions.size(), equalTo(1));
+ }
+}
diff --git a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/model/util/IndexedElements_indexOf_Test.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/model/util/IndexedElements_indexOf_Test.java
index 0d2dfde..2973823 100644
--- a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/model/util/IndexedElements_indexOf_Test.java
+++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/model/util/IndexedElements_indexOf_Test.java
@@ -8,12 +8,14 @@
*/
package com.google.eclipse.protobuf.model.util;
+import static com.google.eclipse.protobuf.junit.core.Setups.unitTestSetup;
+import static com.google.eclipse.protobuf.junit.core.XtextRule.createWith;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;
-import static org.mockito.Mockito.*;
import org.junit.*;
+import com.google.eclipse.protobuf.junit.core.XtextRule;
import com.google.eclipse.protobuf.protobuf.*;
/**
@@ -23,24 +25,34 @@
*/
public class IndexedElements_indexOf_Test {
- private static IndexedElements indexedElements;
+ @Rule public XtextRule xtext = createWith(unitTestSetup());
- @BeforeClass public static void setUpOnce() {
+ private IndexedElements indexedElements;
+
+ @Before public void setUp() {
indexedElements = new IndexedElements();
}
- @Test public void should_return_name_of_Property() {
- MessageField field = mock(MessageField.class);
- when(field.getIndex()).thenReturn(6L);
- assertThat(indexedElements.indexOf(field), equalTo(6L));
- verify(field).getIndex();
+ // syntax = "proto2";
+ //
+ // message Person {
+ // optional String firstName = 6;
+ // }
+ @Test public void should_return_index_of_MessageField() {
+ MessageField field = xtext.find("firstName", MessageField.class);
+ long index = indexedElements.indexOf(field);
+ assertThat(index, equalTo(6L));
}
- @Test public void should_return_name_of_Group() {
- Group group = mock(Group.class);
- when(group.getIndex()).thenReturn(8L);
- assertThat(indexedElements.indexOf(group), equalTo(8L));
- verify(group).getIndex();
+ // syntax = "proto2";
+ //
+ // message Person {
+ // optional group Names = 8 {}
+ // }
+ @Test public void should_return_index_of_Group() {
+ Group group = xtext.find("Names", Group.class);
+ long index = indexedElements.indexOf(group);
+ assertThat(index, equalTo(8L));
}
@Test public void should_return_MIN_VALUE_if_IndexedElement_is_null() {
diff --git a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/model/util/IndexedElements_nameOf_Test.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/model/util/IndexedElements_nameOf_Test.java
deleted file mode 100644
index 4c2bf4b..0000000
--- a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/model/util/IndexedElements_nameOf_Test.java
+++ /dev/null
@@ -1,54 +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.model.util;
-
-import static com.google.eclipse.protobuf.junit.core.Setups.unitTestSetup;
-import static com.google.eclipse.protobuf.junit.core.XtextRule.createWith;
-import static org.hamcrest.core.IsEqual.equalTo;
-import static org.junit.Assert.*;
-import static org.mockito.Mockito.*;
-
-import org.junit.*;
-
-import com.google.eclipse.protobuf.junit.core.XtextRule;
-import com.google.eclipse.protobuf.protobuf.*;
-
-/**
- * Tests for <code>{@link IndexedElements#nameOf(IndexedElement)}</code>
- *
- * @author alruiz@google.com (Alex Ruiz)
- */
-public class IndexedElements_nameOf_Test {
-
- @Rule public XtextRule xtext = createWith(unitTestSetup());
-
- private IndexedElements indexedElements;
-
- @Before public void setUp() {
- indexedElements = xtext.getInstanceOf(IndexedElements.class);
- }
-
- @Test public void should_return_name_of_Property() {
- MessageField field = mock(MessageField.class);
- when(field.getName()).thenReturn("foo");
- assertThat(indexedElements.nameOf(field), equalTo("foo"));
- verify(field).getName();
- }
-
- @Test public void should_return_name_of_Group() {
- Group group = mock(Group.class);
- when(group.getName()).thenReturn("foo");
- assertThat(indexedElements.nameOf(group), equalTo("foo"));
- verify(group).getName();
- }
-
- @Test public void should_return_null_if_IndexedElement_is_null() {
- assertNull(indexedElements.nameOf(null));
- }
-}
diff --git a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/model/util/IndexedElements_setIndexOf_Test.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/model/util/IndexedElements_setIndexOf_Test.java
index b636061..6021754 100644
--- a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/model/util/IndexedElements_setIndexOf_Test.java
+++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/model/util/IndexedElements_setIndexOf_Test.java
@@ -8,10 +8,14 @@
*/
package com.google.eclipse.protobuf.model.util;
-import static org.mockito.Mockito.*;
+import static com.google.eclipse.protobuf.junit.core.Setups.unitTestSetup;
+import static com.google.eclipse.protobuf.junit.core.XtextRule.createWith;
+import static org.hamcrest.core.IsEqual.equalTo;
+import static org.junit.Assert.assertThat;
import org.junit.*;
+import com.google.eclipse.protobuf.junit.core.XtextRule;
import com.google.eclipse.protobuf.protobuf.*;
/**
@@ -21,21 +25,33 @@
*/
public class IndexedElements_setIndexOf_Test {
- private static IndexedElements indexedElements;
+ @Rule public XtextRule xtext = createWith(unitTestSetup());
- @BeforeClass public static void setUpOnce() {
+ private IndexedElements indexedElements;
+
+ @Before public void setUp() {
indexedElements = new IndexedElements();
}
- @Test public void should_return_name_of_Property() {
- MessageField field = mock(MessageField.class);
- indexedElements.setIndexTo(field, 6L);
- verify(field).setIndex(6L);
+ // syntax = "proto2";
+ //
+ // message Person {
+ // optional String firstName = 6;
+ // }
+ @Test public void should_set_index_of_MessageField() {
+ MessageField field = xtext.find("firstName", MessageField.class);
+ indexedElements.setIndexTo(field, 1L);
+ assertThat(field.getIndex(), equalTo(1L));
}
- @Test public void should_return_name_of_Group() {
- Group group = mock(Group.class);
- indexedElements.setIndexTo(group, 8L);
- verify(group).setIndex(8L);
+ // syntax = "proto2";
+ //
+ // message Person {
+ // optional group Names = 8 {}
+ // }
+ @Test public void should_set_index_of_Group() {
+ Group group = xtext.find("Names", Group.class);
+ indexedElements.setIndexTo(group, 1L);
+ assertThat(group.getIndex(), equalTo(1L));
}
}
diff --git a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/naming/NameResolver_nameOf_Test.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/naming/NameResolver_nameOf_Test.java
index dc0d184..0da4968 100644
--- a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/naming/NameResolver_nameOf_Test.java
+++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/naming/NameResolver_nameOf_Test.java
@@ -18,6 +18,7 @@
import com.google.eclipse.protobuf.junit.core.XtextRule;
import com.google.eclipse.protobuf.protobuf.*;
+import com.google.eclipse.protobuf.protobuf.Enum;
import com.google.eclipse.protobuf.protobuf.Package;
/**
@@ -37,6 +38,15 @@
// syntax = "proto2";
//
+ // package com.google.proto.test;
+ @Test public void should_return_name_of_Package() {
+ Package aPackage = xtext.find("com.google.proto.test", Package.class);
+ String name = resolver.nameOf(aPackage);
+ assertThat(name, equalTo("com.google.proto.test"));
+ }
+
+ // syntax = "proto2";
+ //
// message Person {}
@Test public void should_return_name_of_Message() {
Message message = xtext.find("Person", Message.class);
@@ -46,10 +56,77 @@
// syntax = "proto2";
//
- // package com.google.proto.test;
- @Test public void should_return_name_of_Package() {
- Package aPackage = xtext.find("com.google.proto.test", Package.class);
- String name = resolver.nameOf(aPackage);
- assertThat(name, equalTo("com.google.proto.test"));
+ // message Person {
+ // optional String firstName = 1;
+ // }
+ @Test public void should_return_name_of_MessageField() {
+ MessageField field = xtext.find("firstName", MessageField.class);
+ String name = resolver.nameOf(field);
+ assertThat(name, equalTo("firstName"));
+ }
+
+ // syntax = "proto2";
+ //
+ // message Person {
+ // optional group Names = 1 {};
+ // }
+ @Test public void should_return_name_of_Group() {
+ Group group = xtext.find("Names", Group.class);
+ String name = resolver.nameOf(group);
+ assertThat(name, equalTo("Names"));
+ }
+
+ // syntax = "proto2";
+ //
+ // enum PhoneType {}
+ @Test public void should_return_name_of_Enum() {
+ Enum anEnum = xtext.find("PhoneType", Enum.class);
+ String name = resolver.nameOf(anEnum);
+ assertThat(name, equalTo("PhoneType"));
+ }
+
+ // syntax = "proto2";
+ //
+ // enum PhoneType {
+ // HOME = 0;
+ // }
+ @Test public void should_return_name_of_Literal() {
+ Literal literal = xtext.find("HOME", Literal.class);
+ String name = resolver.nameOf(literal);
+ assertThat(name, equalTo("HOME"));
+ }
+
+ // syntax = "proto2";
+ //
+ // service CallServer {}
+ @Test public void should_return_name_of_Service() {
+ Service service = xtext.find("CallServer", Service.class);
+ String name = resolver.nameOf(service);
+ assertThat(name, equalTo("CallServer"));
+ }
+
+ // syntax = "proto2";
+ //
+ // message Input {}
+ // message Output {}
+ //
+ // service CallServer {
+ // rpc QuickCall (Input) returns (Output);
+ // }
+ @Test public void should_return_name_of_Rpc() {
+ Rpc rpc = xtext.find("QuickCall", Rpc.class);
+ String name = resolver.nameOf(rpc);
+ assertThat(name, equalTo("QuickCall"));
+ }
+
+ // syntax = "proto2";
+ //
+ // message Person {
+ // optional boolean active = 1 [default = true];
+ // }
+ @Test public void should_return_name_of_default_value_option() {
+ DefaultValueFieldOption option = xtext.find("default", DefaultValueFieldOption.class);
+ String name = resolver.nameOf(option);
+ assertThat(name, equalTo("default"));
}
}
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 286bd9e..3857a40 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
@@ -110,7 +110,7 @@
MessageField field = (MessageField) model;
ContentToInsert content = newContent(field);
if (content.equals(ContentToInsert.TAG_NUMBER_INSERTED)) {
- long index = indexedElements.calculateTagNumberOf(field);
+ long index = indexedElements.calculateNewIndexFor(field);
field.setIndex(index);
updateIndexInCommentOfParent(field, index, document);
}
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 a2ccd53..99e9b0d 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
@@ -315,7 +315,7 @@
@Override public void completeMessageField_Index(EObject model, Assignment assignment, ContentAssistContext context,
ICompletionProposalAcceptor acceptor) {
- long index = indexedElements.calculateTagNumberOf((MessageField) model);
+ long index = indexedElements.calculateNewIndexFor((MessageField) model);
proposeIndex(index, context, acceptor);
}
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
index 964be3f..a36d844 100644
--- 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
@@ -9,7 +9,7 @@
package com.google.eclipse.protobuf.ui.grammar;
import static com.google.eclipse.protobuf.grammar.CommonKeyword.*;
-import static com.google.eclipse.protobuf.grammar.ValidSyntax.proto2;
+import static com.google.eclipse.protobuf.grammar.Syntaxes.proto2;
import static com.google.eclipse.protobuf.util.CommonWords.space;
import com.google.eclipse.protobuf.grammar.CommonKeyword;
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/labeling/Labels.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/labeling/Labels.java
index 4f25dc1..1fb28a4 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/labeling/Labels.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/labeling/Labels.java
@@ -120,10 +120,8 @@
StyledString text = new StyledString(nameResolver.nameOf(field));
String typeName = messageFields.typeNameOf(field);
if (typeName == null) {
- typeName = "<unresolved reference>"; // TODO move to
+ typeName = "<unresolved reference>"; // TODO move to properties file
}
- // properties
- // file
String indexAndType = String.format(" [%d] : %s", field.getIndex(), typeName);
text.append(indexAndType, DECORATIONS_STYLER);
return text;
@@ -135,11 +133,7 @@
StringBuilder b = new StringBuilder();
boolean isCustomOption = option instanceof CustomOption || option instanceof CustomFieldOption;
if (isCustomOption) {
- b.append("(");
- }
- b.append(name);
- if (isCustomOption) {
- b.append(")");
+ b.append(formatCustomOptionName(name));
}
if (option instanceof CustomOption) {
appendFields(b, ((CustomOption) option).getFields());
@@ -150,6 +144,10 @@
return b.toString();
}
+ private String formatCustomOptionName(String name) {
+ return String.format("(%s)", name);
+ }
+
private void appendFields(StringBuilder b, List<OptionField> fields) {
for (OptionField field : fields) {
b.append(".");
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/parser/Proto2OnlyCheckPreferenceStoreInitializer.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/parser/Proto2OnlyCheckPreferenceStoreInitializer.java
new file mode 100644
index 0000000..2ba96d3
--- /dev/null
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/parser/Proto2OnlyCheckPreferenceStoreInitializer.java
@@ -0,0 +1,27 @@
+/*
+ * 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.preferences.parser;
+
+import org.eclipse.jface.preference.IPreferenceStore;
+import org.eclipse.xtext.ui.editor.preferences.*;
+
+/**
+ * Initializes default values for the "Proto2 only check" preferences.
+ *
+ * @author alruiz@google.com (Alex Ruiz)
+ */
+public class Proto2OnlyCheckPreferenceStoreInitializer implements IPreferenceStoreInitializer {
+
+ /** {@inheritDoc} */
+ @Override public void initialize(IPreferenceStoreAccess access) {
+ IPreferenceStore store = access.getWritablePreferenceStore();
+ RawPreferences preferences = new RawPreferences(store);
+ preferences.enableProto2OnlyChecks().defaultValue(false);
+ }
+}
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/parser/RawPreferences.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/parser/RawPreferences.java
new file mode 100644
index 0000000..5ef0403
--- /dev/null
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/parser/RawPreferences.java
@@ -0,0 +1,29 @@
+/*
+ * 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.preferences.parser;
+
+import org.eclipse.jface.preference.IPreferenceStore;
+
+import com.google.eclipse.protobuf.ui.preferences.BooleanPreference;
+
+/**
+ * @author alruiz@google.com (Alex Ruiz)
+ */
+class RawPreferences {
+
+ private final BooleanPreference enableProto2OnlyChecks;
+
+ RawPreferences(IPreferenceStore store) {
+ enableProto2OnlyChecks = new BooleanPreference("compiler.checkProto2", store);
+ }
+
+ BooleanPreference enableProto2OnlyChecks() {
+ return enableProto2OnlyChecks;
+ }
+}
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/quickfix/ProtobufQuickfixProvider.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/quickfix/ProtobufQuickfixProvider.java
index c9f9d93..629d3f6 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/quickfix/ProtobufQuickfixProvider.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/quickfix/ProtobufQuickfixProvider.java
@@ -8,7 +8,7 @@
*/
package com.google.eclipse.protobuf.ui.quickfix;
-import static com.google.eclipse.protobuf.grammar.ValidSyntax.proto2;
+import static com.google.eclipse.protobuf.grammar.Syntaxes.proto2;
import static com.google.eclipse.protobuf.protobuf.BOOL.*;
import static com.google.eclipse.protobuf.ui.quickfix.Messages.*;
import static com.google.eclipse.protobuf.util.Strings.quote;
@@ -62,7 +62,7 @@
ISemanticModification modification = new ISemanticModification() {
@Override public void apply(EObject element, IModificationContext context) throws Exception {
IndexedElement e = (IndexedElement) element;
- long tagNumber = indexedElements.calculateTagNumberOf(e);
+ long tagNumber = indexedElements.calculateNewIndexFor(e);
indexedElements.setIndexTo(e, tagNumber);
}
};
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/grammar/Keywords.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/grammar/Keywords.java
index adf5763..040d060 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/grammar/Keywords.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/grammar/Keywords.java
@@ -25,6 +25,10 @@
private final Set<String> keywords;
+ /**
+ * Creates a new <code>{@link Keywords}</code>.
+ * @param grammarAccess provides access to elements of the protocol buffer language.
+ */
@Inject public Keywords(IGrammarAccess grammarAccess) {
keywords = getAllKeywords(grammarAccess.getGrammar());
}
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/grammar/ValidSyntax.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/grammar/Syntaxes.java
similarity index 71%
rename from com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/grammar/ValidSyntax.java
rename to com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/grammar/Syntaxes.java
index 0b95ed9..3110ed4 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/grammar/ValidSyntax.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/grammar/Syntaxes.java
@@ -11,10 +11,10 @@
/**
* @author alruiz@google.com (Alex Ruiz)
*/
-public final class ValidSyntax {
+public final class Syntaxes {
/**
- * Returns the value to use to specify 'proto2' syntax.
+ * Returns the value to use to in the "syntax" element.
* @return the {@code String} "proto2".
*/
public static String proto2() {
@@ -22,13 +22,13 @@
}
/**
- * Indicates whether the given {@code String} is equal to the one returned by <code>{@link #proto2()}</code>.
+ * Indicates whether the given {@code String} is equal to <code>{@link #proto2()}</code>.
* @param s the {@code String} to check.
* @return {@code true} if the given {@code String} is equal to "proto2," {@code false} otherwise.
*/
- public static boolean isProto2Syntax(String s) {
+ public static boolean isSpecifyingProto2Syntax(String s) {
return proto2().equals(s);
}
- private ValidSyntax() {}
+ private Syntaxes() {}
}
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/model/util/IndexedElements.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/model/util/IndexedElements.java
index 0f38084..b3b314e 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/model/util/IndexedElements.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/model/util/IndexedElements.java
@@ -8,13 +8,14 @@
*/
package com.google.eclipse.protobuf.model.util;
-import static com.google.eclipse.protobuf.protobuf.ProtobufPackage.Literals.*;
import static java.lang.Math.max;
import static java.util.Collections.emptyList;
+import static org.eclipse.xtext.util.SimpleAttributeResolver.newResolver;
import java.util.List;
import org.eclipse.emf.ecore.*;
+import org.eclipse.xtext.util.SimpleAttributeResolver;
import com.google.eclipse.protobuf.protobuf.*;
import com.google.inject.Singleton;
@@ -25,90 +26,11 @@
* @author alruiz@google.com (Alex Ruiz)
*/
@Singleton public class IndexedElements {
+ private final static SimpleAttributeResolver<EObject, Long> INDEX_RESOLVER = newResolver(long.class, "index");
/**
- * Returns the name of the given <code>{@link IndexedElement}</code>.
- * @param e the given {@code IndexedElement}.
- * @return the name of the given {@code IndexedElement}, or {@code null} if the given {@code IndexedElement} is
- * {@code null}.
- */
- public String nameOf(IndexedElement e) {
- if (e instanceof MessageField) {
- MessageField field = (MessageField) e;
- return field.getName();
- }
- if (e instanceof Group) {
- Group group = (Group) e;
- return group.getName();
- }
- return null;
- }
-
- /**
- * Returns the name of the given <code>{@link IndexedElement}</code>.
- * @param e the given {@code IndexedElement}.
- * @return the name of the given {@code IndexedElement}, or {@code Long.MIN_VALUE} if the given {@code IndexedElement}
- * is {@code null}.
- */
- public long indexOf(IndexedElement e) {
- if (e instanceof Group) {
- return ((Group) e).getIndex();
- }
- if (e instanceof MessageField) {
- return ((MessageField) e).getIndex();
- }
- return Long.MIN_VALUE;
- }
-
- /**
- * Returns the "index" feature of the given <code>{@link IndexedElement}</code>.
- * @param e the given {@code IndexedElement}.
- * @return the "index" feature of the given {@code IndexedElement}, or {@code null} if the given
- * {@code IndexedElement} is {@code null}.
- */
- public EStructuralFeature indexFeatureOf(IndexedElement e) {
- if (e instanceof Group) {
- return GROUP__INDEX;
- }
- if (e instanceof MessageField) {
- return MESSAGE_FIELD__INDEX;
- }
- return null;
- }
-
- /**
- * Returns the options of the given <code>{@link IndexedElement}</code>.
- * @param e the given {@code IndexedElement}.
- * @return the options of the given {@code IndexedElement}, or an empty list if the given {@code IndexedElement} is
- * {@code null}.
- */
- public List<FieldOption> fieldOptionsOf(IndexedElement e) {
- if (e instanceof Group) {
- return ((Group) e).getFieldOptions();
- }
- if (e instanceof MessageField) {
- return ((MessageField) e).getFieldOptions();
- }
- return emptyList();
- }
-
- /**
- * Sets the index of the given <code>{@link IndexedElement}</code>.
- * @param e e the given {@code IndexedElement}.
- * @param newIndex the new index to set.
- */
- public void setIndexTo(IndexedElement e, long newIndex) {
- if (e instanceof Group) {
- ((Group) e).setIndex(newIndex);
- }
- if (e instanceof MessageField) {
- ((MessageField) e).setIndex(newIndex);
- }
- }
-
- /**
- * Calculates the tag number value for the given element. The calculated tag number value is the maximum of all the
- * tag number values of the given element's siblings, plus one. The minimum tag number value is 1.
+ * Calculates the index value for the given element. The calculated index value is the maximum of all the
+ * index values of the given element's siblings, plus one. The minimum index value is 1.
* <p>
* For example, in the following message:
*
@@ -119,14 +41,15 @@
* optional PhoneNumber phone =
* </pre>
*
- * The calculated tag number value for the element {@code PhoneNumber} will be 3.
+ * The calculated index value for the element {@code PhoneNumber} will be 3.
* </p>
* @param e the given element.
- * @return the calculated value for the tag number of the given element.
+ * @return the calculated value for the index of the given element.
*/
- public long calculateTagNumberOf(IndexedElement e) {
+ public long calculateNewIndexFor(IndexedElement e) {
long index = 0;
- for (EObject o : e.eContainer().eContents()) {
+ EObject type = e.eContainer();
+ for (EObject o : type.eContents()) {
if (o == e || !(o instanceof IndexedElement)) {
continue;
}
@@ -134,4 +57,61 @@
}
return ++index;
}
+
+ /**
+ * Returns the name of the given <code>{@link IndexedElement}</code>.
+ * @param e the given {@code IndexedElement}.
+ * @return the name of the given {@code IndexedElement}, or {@code Long.MIN_VALUE} if the given {@code IndexedElement}
+ * is {@code null}.
+ */
+ public long indexOf(IndexedElement e) {
+ long index = Long.MIN_VALUE;
+ EStructuralFeature feature = indexFeatureOf(e);
+ if (feature != null) {
+ index = (Long) e.eGet(feature);
+ }
+ return index;
+ }
+
+ /**
+ * Returns the options of the given <code>{@link IndexedElement}</code>.
+ * @param e the given {@code IndexedElement}.
+ * @return the options of the given {@code IndexedElement}, or an empty list if the given {@code IndexedElement} is
+ * {@code null}.
+ */
+ @SuppressWarnings("unchecked")
+ public List<FieldOption> fieldOptionsOf(IndexedElement e) {
+ if (e != null) {
+ EStructuralFeature feature = e.eClass().getEStructuralFeature("fieldOptions");
+ if (feature != null) {
+ return (List<FieldOption>) e.eGet(feature);
+ }
+ }
+ return emptyList();
+ }
+
+ /**
+ * Sets the index of the given <code>{@link IndexedElement}</code>.
+ * @param e e the given {@code IndexedElement}.
+ * @param newIndex the new index to set.
+ */
+ public void setIndexTo(IndexedElement e, long newIndex) {
+ EStructuralFeature feature = indexFeatureOf(e);
+ if (feature != null) {
+ e.eSet(feature, newIndex);
+ }
+ }
+
+ /**
+ * Returns the "index" structural feature of the given <code>{@link IndexedElement}</code>.
+ * @param e the given {@code IndexedElement}.
+ * @return the "index" structural feature of the given {@code IndexedElement}, or {@code null} if the given
+ * {@code IndexedElement} is {@code null}.
+ */
+ public EStructuralFeature indexFeatureOf(IndexedElement e) {
+ if (e != null) {
+ return INDEX_RESOLVER.getAttribute(e);
+ }
+ return null;
+ }
}
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/naming/NameResolver.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/naming/NameResolver.java
index bfbf4e7..4d9f97d 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/naming/NameResolver.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/naming/NameResolver.java
@@ -8,23 +8,29 @@
*/
package com.google.eclipse.protobuf.naming;
+import static com.google.eclipse.protobuf.grammar.CommonKeyword.DEFAULT;
import static org.eclipse.xtext.util.SimpleAttributeResolver.NAME_RESOLVER;
import org.eclipse.emf.ecore.*;
-import com.google.eclipse.protobuf.grammar.CommonKeyword;
import com.google.eclipse.protobuf.protobuf.DefaultValueFieldOption;
import com.google.inject.Singleton;
/**
- * TODO test
+ * Resolves names of elements in the protobuf grammar.
+ *
* @author alruiz@google.com (Alex Ruiz)
*/
@Singleton public class NameResolver {
+ /**
+ * Returns the name of the given element.
+ * @param o the given element.
+ * @return the name of the given element, or {@code null} if the given element does not have support for naming.
+ */
public String nameOf(EObject o) {
if (o instanceof DefaultValueFieldOption) {
- return CommonKeyword.DEFAULT.toString();
+ return DEFAULT.toString();
}
Object value = nameFeatureOf(o);
if (value instanceof String) {
@@ -34,7 +40,7 @@
}
private Object nameFeatureOf(EObject e) {
- EStructuralFeature f = NAME_RESOLVER.getAttribute(e);
- return (f != null) ? e.eGet(f) : null;
+ EStructuralFeature feature = NAME_RESOLVER.getAttribute(e);
+ return (feature != null) ? e.eGet(feature) : null;
}
}
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/ProtobufJavaValidator.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/ProtobufJavaValidator.java
index eeb76d8..0f3a906 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/ProtobufJavaValidator.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/ProtobufJavaValidator.java
@@ -8,7 +8,6 @@
*/
package com.google.eclipse.protobuf.validation;
-import static com.google.eclipse.protobuf.grammar.ValidSyntax.isProto2Syntax;
import static com.google.eclipse.protobuf.protobuf.ProtobufPackage.Literals.*;
import static com.google.eclipse.protobuf.validation.Messages.*;
import static java.lang.String.format;
@@ -20,7 +19,9 @@
import org.eclipse.xtext.scoping.impl.ImportUriResolver;
import org.eclipse.xtext.validation.*;
+import com.google.eclipse.protobuf.grammar.Syntaxes;
import com.google.eclipse.protobuf.model.util.*;
+import com.google.eclipse.protobuf.naming.NameResolver;
import com.google.eclipse.protobuf.protobuf.*;
import com.google.eclipse.protobuf.protobuf.Package;
import com.google.inject.Inject;
@@ -36,6 +37,7 @@
public static final String MORE_THAN_ONE_PACKAGE_ERROR = "moreThanOnePackage";
@Inject private IndexedElements indexedElements;
+ @Inject private NameResolver nameResolver;
@Inject private Protobufs protobufs;
@Inject private IQualifiedNameProvider qualifiedNameProvider;
@Inject private ImportUriResolver uriResolver;
@@ -74,7 +76,7 @@
@Check public void checkSyntaxIsProto2(Syntax syntax) {
String name = syntax.getName();
- if (isProto2Syntax(name)) {
+ if (Syntaxes.proto2().equals(name)) {
return;
}
String msg = (name == null) ? expectedSyntaxIdentifier : format(unrecognizedSyntaxIdentifier, name);
@@ -102,7 +104,7 @@
continue;
}
QualifiedName messageName = qualifiedNameProvider.getFullyQualifiedName(message);
- String msg = format(fieldNumberAlreadyUsed, index, messageName.toString(), indexedElements.nameOf(other));
+ String msg = format(fieldNumberAlreadyUsed, index, messageName.toString(), nameResolver.nameOf(other));
invalidTagNumberError(msg, e);
break;
}
@@ -143,6 +145,6 @@
}
private boolean isNameNull(IndexedElement e) {
- return indexedElements.nameOf(e) == null;
+ return nameResolver.nameOf(e) == null;
}
}