Working on supporting pluggable descriptor.proto definitions.
diff --git a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/scoping/Descriptor_fileOptions_Test.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/scoping/Descriptor_fileOptions_Test.java index 3afe1bb..43ee6b3 100644 --- a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/scoping/Descriptor_fileOptions_Test.java +++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/scoping/Descriptor_fileOptions_Test.java
@@ -28,7 +28,7 @@ @Rule public XtextRule xtext = new XtextRule(); - private IProtoDescriptor descriptor; + private ProtoDescriptor descriptor; @Before public void setUp() { descriptor = xtext.getInstanceOf(ProtoDescriptor.class);
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 845fa54..ed93126 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
@@ -106,7 +106,7 @@ @Override public void completeOption_Value(EObject model, Assignment assignment, ContentAssistContext context, ICompletionProposalAcceptor acceptor) { Option option = (Option) model; - IProtoDescriptor descriptor = descriptorProvider.get(); + ProtoDescriptor descriptor = descriptorProvider.get(); Enum enumType = descriptor.enumTypeOf(option); if (enumType != null) { proposeAndAccept(enumType, context, acceptor); @@ -399,7 +399,7 @@ @Override public void completeFieldOption_Value(EObject model, Assignment assignment, ContentAssistContext context, ICompletionProposalAcceptor acceptor) { FieldOption option = (FieldOption) model; - IProtoDescriptor descriptor = descriptorProvider.get(); + ProtoDescriptor descriptor = descriptorProvider.get(); Enum enumType = descriptor.enumTypeOf(option); if (enumType != null) { proposeAndAccept(enumType, context, acceptor);
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/IProtoDescriptor.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/IProtoDescriptor.java deleted file mode 100644 index 4560359..0000000 --- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/IProtoDescriptor.java +++ /dev/null
@@ -1,83 +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.scoping; - -import java.util.Collection; - -import com.google.eclipse.protobuf.protobuf.*; -import com.google.eclipse.protobuf.protobuf.Enum; - -/** - * Contains the elements from descriptor.proto (provided with protobuf's library.) - * - * @author alruiz@google.com (Alex Ruiz) - */ -public interface IProtoDescriptor { - - /** - * Returns all the file-level options available. These are the options defined in - * {@code google/protobuf/descriptor.proto} (more details can be found - * <a href=http://code.google.com/apis/protocolbuffers/docs/proto.html#options" target="_blank">here</a>.) - * @return all the file-level options available. - */ - public abstract Collection<Property> fileOptions(); - - /** - * Looks up an option per name, as defined in {@code google/protobuf/descriptor.proto} - * (more details can be found <a - * href=http://code.google.com/apis/protocolbuffers/docs/proto.html#options" target="_blank">here</a>.) - * @param name the name of the option to look for. - * @return the option whose name matches the given one or {@code null} if a matching option is not found. - */ - public abstract Property lookupOption(String name); - - /** - * Returns all the message-level options available. These are the options defined in - * {@code google/protobuf/descriptor.proto} (more details can be found - * <a href=http://code.google.com/apis/protocolbuffers/docs/proto.html#options" target="_blank">here</a>.) - * @return all the message-level options available. - */ - public abstract Collection<Property> messageOptions(); - - /** - * Returns all the field-level options available. These are the options defined in - * {@code google/protobuf/descriptor.proto} (more details can be found - * <a href=http://code.google.com/apis/protocolbuffers/docs/proto.html#options" target="_blank">here</a>.) - * @return all the field-level options available. - */ - public abstract Collection<Property> fieldOptions(); - - /** - * Looks up a field-level option per name. Field-level options are defined in {@code google/protobuf/descriptor.proto} - * (more details can be found <a - * href=http://code.google.com/apis/protocolbuffers/docs/proto.html#options" target="_blank">here</a>.) - * @param name the name of the option to look for. - * @return the option whose name matches the given one or {@code null} if a matching option is not found. - */ - public abstract Property lookupFieldOption(String name); - - /** - * Returns the enum type of the given option, only if the given option is defined in - * {@code google/protobuf/descriptor.proto} and its type is enum (more details can be found <a - * href=http://code.google.com/apis/protocolbuffers/docs/proto.html#options" target="_blank">here</a>.) - * @param option the given option. - * @return the enum type of the given option or {@code null} if the type of the given option is not enum. - */ - public abstract Enum enumTypeOf(Option option); - - /** - * Returns the enum type of the given option, only if the given option is defined in - * {@code google/protobuf/descriptor.proto} and its type is enum (more details can be found <a - * href=http://code.google.com/apis/protocolbuffers/docs/proto.html#options" target="_blank">here</a>.) - * @param option the given option. - * @return the enum type of the given option or {@code null} if the type of the given option is not enum. - */ - public abstract Enum enumTypeOf(FieldOption option); - -}
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/IProtoDescriptorSource.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/IProtoDescriptorSource.java new file mode 100644 index 0000000..319e45b --- /dev/null +++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/IProtoDescriptorSource.java
@@ -0,0 +1,37 @@ +/* + * 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.scoping; + +import com.google.inject.ImplementedBy; + +import org.eclipse.emf.common.util.URI; + +import java.io.*; + +/** + * Provides the contents of <code>{@link ProtoDescriptor}</code>. + * + * @author alruiz@google.com (Alex Ruiz) + */ +@ImplementedBy(OssProtoDescriptorSource.class) +public interface IProtoDescriptorSource { + + /** + * Returns the URI of the resource containing the descriptor. + * @return the URI of the resource containing the descriptor. + */ + URI uri(); + + /** + * Returns the contents of the descriptor. + * @return the contents of the descriptor. + * @throws IOException if something goes wrong. + */ + InputStream contents() throws IOException; +}
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/OssProtoDescriptorSource.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/OssProtoDescriptorSource.java new file mode 100644 index 0000000..01492b4 --- /dev/null +++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/OssProtoDescriptorSource.java
@@ -0,0 +1,37 @@ +/* + * 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.scoping; + +import com.google.inject.Singleton; + +import org.eclipse.emf.common.util.URI; + +import java.io.*; +import java.net.URL; + +/** + * @author alruiz@google.com (Alex Ruiz) + */ +@Singleton +class OssProtoDescriptorSource implements IProtoDescriptorSource { + + private static final String DESCRIPTOR_URL = "platform:/plugin/com.google.eclipse.protobuf/descriptor.proto"; + + private static final URI DESCRIPTOR_URI = URI.createURI(DESCRIPTOR_URL); + + public URI uri() { + return DESCRIPTOR_URI; + } + + public InputStream contents() throws IOException { + URL url = new URL(DESCRIPTOR_URL); + return url.openConnection().getInputStream(); + } + +}
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtoDescriptor.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtoDescriptor.java index 618b2d4..31c4cf5 100644 --- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtoDescriptor.java +++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtoDescriptor.java
@@ -12,7 +12,6 @@ import static com.google.eclipse.protobuf.scoping.OptionType.*; import static com.google.eclipse.protobuf.util.Closeables.close; import static java.util.Collections.unmodifiableCollection; -import static org.eclipse.emf.common.util.URI.createURI; import static org.eclipse.xtext.EcoreUtil2.*; import static org.eclipse.xtext.util.CancelIndicator.NullImpl; import static org.eclipse.xtext.util.Strings.isEmpty; @@ -26,8 +25,7 @@ import org.eclipse.xtext.parser.*; import org.eclipse.xtext.resource.XtextResource; -import java.io.*; -import java.net.URL; +import java.io.InputStreamReader; import java.util.*; /** @@ -35,9 +33,8 @@ * * @author alruiz@google.com (Alex Ruiz) */ -public class ProtoDescriptor implements IProtoDescriptor { +public class ProtoDescriptor { - private static final String DESCRIPTOR_URI = "platform:/plugin/com.google.eclipse.protobuf/descriptor.proto"; private static final Map<String, OptionType> OPTION_DEFINITION_BY_NAME = new HashMap<String, OptionType>(); static { @@ -54,13 +51,13 @@ private final ModelNodes nodes; - @Inject public ProtoDescriptor(IParser parser, ModelNodes nodes) { + @Inject public ProtoDescriptor(IParser parser, IProtoDescriptorSource source, ModelNodes nodes) { this.nodes = nodes; addOptionTypes(); InputStreamReader reader = null; try { - XtextResource resource = new XtextResource(createURI(DESCRIPTOR_URI)); - reader = new InputStreamReader(globalScopeContents(), "UTF-8"); + XtextResource resource = new XtextResource(source.uri()); + reader = new InputStreamReader(source.contents(), "UTF-8"); IParseResult result = parser.parse(reader); root = (Protobuf) result.getRootASTElement(); resource.getContents().add(root); @@ -79,11 +76,6 @@ optionsByType.put(type, new LinkedHashMap<String, Property>()); } - private static InputStream globalScopeContents() throws IOException { - URL url = new URL(DESCRIPTOR_URI); - return url.openConnection().getInputStream(); - } - private void initContents() { for (Message m : getAllContentsOfType(root, Message.class)) { OptionType type = OPTION_DEFINITION_BY_NAME.get(m.getName()); @@ -114,17 +106,28 @@ return "uninterpreted_option".equals(property.getName()); } - /** {@inheritDoc} */ + /** + * Returns all the file-level options available. These are the options defined in + * {@code google/protobuf/descriptor.proto} (more details can be found + * <a href=http://code.google.com/apis/protocolbuffers/docs/proto.html#options" target="_blank">here</a>.) + * @return all the file-level options available. + */ public Collection<Property> fileOptions() { return optionsOfType(FILE); } - /** {@inheritDoc} */ + /** + * Looks up an option per name, as defined in {@code google/protobuf/descriptor.proto} + * (more details can be found <a + * href=http://code.google.com/apis/protocolbuffers/docs/proto.html#options" target="_blank">here</a>.) + * @param name the name of the option to look for. + * @return the option whose name matches the given one or {@code null} if a matching option is not found. + */ public Property lookupOption(String name) { return lookupOption(name, FILE, MESSAGE, METHOD); } - public Property lookupOption(String name, OptionType...types) { + private Property lookupOption(String name, OptionType...types) { for (OptionType type : types) { Property p = lookupOption(name, type); if (p != null) return p; @@ -132,7 +135,13 @@ return null; } - /** {@inheritDoc} */ + /** + * Looks up a field-level option per name. Field-level options are defined in {@code google/protobuf/descriptor.proto} + * (more details can be found <a + * href=http://code.google.com/apis/protocolbuffers/docs/proto.html#options" target="_blank">here</a>.) + * @param name the name of the option to look for. + * @return the option whose name matches the given one or {@code null} if a matching option is not found. + */ public Property lookupFieldOption(String name) { return lookupOption(name, FIELD); } @@ -141,12 +150,22 @@ return optionsByType.get(type).get(name); } - /** {@inheritDoc} */ + /** + * Returns all the message-level options available. These are the options defined in + * {@code google/protobuf/descriptor.proto} (more details can be found + * <a href=http://code.google.com/apis/protocolbuffers/docs/proto.html#options" target="_blank">here</a>.) + * @return all the message-level options available. + */ public Collection<Property> messageOptions() { return optionsOfType(MESSAGE); } - /** {@inheritDoc} */ + /** + * Returns all the field-level options available. These are the options defined in + * {@code google/protobuf/descriptor.proto} (more details can be found + * <a href=http://code.google.com/apis/protocolbuffers/docs/proto.html#options" target="_blank">here</a>.) + * @return all the field-level options available. + */ public Collection<Property> fieldOptions() { return optionsOfType(FIELD); } @@ -155,13 +174,25 @@ return unmodifiableCollection(optionsByType.get(type).values()); } - /** {@inheritDoc} */ + /** + * Returns the enum type of the given option, only if the given option is defined in + * {@code google/protobuf/descriptor.proto} and its type is enum (more details can be found <a + * href=http://code.google.com/apis/protocolbuffers/docs/proto.html#options" target="_blank">here</a>.) + * @param option the given option. + * @return the enum type of the given option or {@code null} if the type of the given option is not enum. + */ public Enum enumTypeOf(Option option) { String name = option.getName(); return enumTypeOf(lookupOption(name)); } - /** {@inheritDoc} */ + /** + * Returns the enum type of the given option, only if the given option is defined in + * {@code google/protobuf/descriptor.proto} and its type is enum (more details can be found <a + * href=http://code.google.com/apis/protocolbuffers/docs/proto.html#options" target="_blank">here</a>.) + * @param option the given option. + * @return the enum type of the given option or {@code null} if the type of the given option is not enum. + */ public Enum enumTypeOf(FieldOption option) { String name = option.getName(); return enumTypeOf(lookupFieldOption(name));
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtoDescriptorProvider.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtoDescriptorProvider.java index 82aef7f..b235efb 100644 --- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtoDescriptorProvider.java +++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtoDescriptorProvider.java
@@ -19,15 +19,16 @@ * @author Alex Ruiz */ @Singleton -public class ProtoDescriptorProvider implements Provider<IProtoDescriptor> { +public class ProtoDescriptorProvider implements Provider<ProtoDescriptor> { @Inject private IParser parser; + @Inject private IProtoDescriptorSource source; @Inject private ModelNodes nodes; - private IProtoDescriptor descriptor; + private ProtoDescriptor descriptor; - public synchronized IProtoDescriptor get() { - if (descriptor == null) descriptor = new ProtoDescriptor(parser, nodes); + public synchronized ProtoDescriptor get() { + if (descriptor == null) descriptor = new ProtoDescriptor(parser, source, nodes); return descriptor; } }
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtobufScopeProvider.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtobufScopeProvider.java index 461b2a5..562a552 100644 --- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtobufScopeProvider.java +++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtobufScopeProvider.java
@@ -170,7 +170,7 @@ } private Enum enumTypeOfOption(EObject mayBeOption) { - IProtoDescriptor descriptor = descriptorProvider.get(); + ProtoDescriptor descriptor = descriptorProvider.get(); if (mayBeOption instanceof Option) return descriptor.enumTypeOf((Option) mayBeOption); if (mayBeOption instanceof FieldOption) return descriptor.enumTypeOf((FieldOption) mayBeOption); return null;