In progress: [Issue 199] Add ability to navigate to proto element from
generated C++ code.
Added support for navigation to message fields.
diff --git a/com.google.eclipse.protobuf.cdt/src/com/google/eclipse/protobuf/cdt/actions/AstBasedCppToProtobufMapper.java b/com.google.eclipse.protobuf.cdt/src/com/google/eclipse/protobuf/cdt/actions/AstBasedCppToProtobufMapper.java
index 231fda8..ab73569 100644
--- a/com.google.eclipse.protobuf.cdt/src/com/google/eclipse/protobuf/cdt/actions/AstBasedCppToProtobufMapper.java
+++ b/com.google.eclipse.protobuf.cdt/src/com/google/eclipse/protobuf/cdt/actions/AstBasedCppToProtobufMapper.java
@@ -50,11 +50,9 @@
return CANCEL_STATUS;
}
IASTNodeSelector nodeSelector= ast.getNodeSelector(null);
- IASTName selectedName = nodeSelector.findEnclosingName(offset, 1);
- if (selectedName == null) {
- return CANCEL_STATUS;
- }
- if (selectedName.isDefinition()) {
+ IASTNode selectedNode = nodeSelector.findEnclosingNode(offset, 1);
+ if (selectedNode instanceof IASTName) {
+ IASTName selectedName = (IASTName) selectedNode;
IBinding binding = selectedName.resolveBinding();
CppToProtobufMapping info = delegate.createMappingFrom(binding);
mappingReference.set(info);
diff --git a/com.google.eclipse.protobuf.cdt/src/com/google/eclipse/protobuf/cdt/mapping/CppToProtobufMapper.java b/com.google.eclipse.protobuf.cdt/src/com/google/eclipse/protobuf/cdt/mapping/CppToProtobufMapper.java
index 8f5f919..969cef3 100644
--- a/com.google.eclipse.protobuf.cdt/src/com/google/eclipse/protobuf/cdt/mapping/CppToProtobufMapper.java
+++ b/com.google.eclipse.protobuf.cdt/src/com/google/eclipse/protobuf/cdt/mapping/CppToProtobufMapper.java
@@ -13,18 +13,21 @@
import java.util.Map;
import org.eclipse.cdt.core.dom.ast.IBinding;
+import org.eclipse.cdt.internal.core.dom.parser.ProblemBinding;
import com.google.inject.Singleton;
/**
* @author alruiz@google.com (Alex Ruiz)
*/
+@SuppressWarnings("restriction")
@Singleton public class CppToProtobufMapper {
private final Map<Class<? extends IBinding>, IBindingMappingStrategy<?>> strategies = newHashMap();
public CppToProtobufMapper() {
register(new ClassMappingStrategy());
register(new EnumMappingStrategy());
+ register(new MethodMappingStrategy());
}
private void register(IBindingMappingStrategy<?> strategy) {
@@ -39,7 +42,18 @@
* element that can be traced back to a protocol buffer element.
*/
public CppToProtobufMapping createMappingFrom(IBinding binding) {
- IBindingMappingStrategy<?> strategy = strategies.get(binding.getClass());
- return (strategy != null) ? strategy.createMappingFrom(binding) : null;
+ IBinding bindingToUse = binding;
+ if (binding instanceof ProblemBinding) {
+ ProblemBinding problemBinding = (ProblemBinding) binding;
+ IBinding[] candidates = problemBinding.getCandidateBindings();
+ if (candidates != null && candidates.length == 1) {
+ bindingToUse = candidates[0];
+ }
+ }
+ IBindingMappingStrategy<?> strategy = strategies.get(bindingToUse.getClass());
+ if (strategy != null) {
+ return strategy.createMappingFrom(bindingToUse);
+ }
+ return null;
}
}
diff --git a/com.google.eclipse.protobuf.cdt/src/com/google/eclipse/protobuf/cdt/mapping/MethodMappingStrategy.java b/com.google.eclipse.protobuf.cdt/src/com/google/eclipse/protobuf/cdt/mapping/MethodMappingStrategy.java
new file mode 100644
index 0000000..db5150a
--- /dev/null
+++ b/com.google.eclipse.protobuf.cdt/src/com/google/eclipse/protobuf/cdt/mapping/MethodMappingStrategy.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2012 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.cdt.mapping;
+
+import org.eclipse.cdt.core.dom.ast.*;
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunctionType;
+import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPMethod;
+import org.eclipse.xtext.naming.QualifiedName;
+
+import com.google.eclipse.protobuf.protobuf.MessageField;
+
+/**
+ * @author alruiz@google.com (Alex Ruiz)
+ */
+@SuppressWarnings("restriction")
+class MethodMappingStrategy implements IBindingMappingStrategy<CPPMethod> {
+
+ @Override public CppToProtobufMapping createMappingFrom(IBinding binding) {
+ CPPMethod method = typeOfSupportedBinding().cast(binding);
+ ICPPFunctionType type = method.getType();
+ if (!type.isConst()) {
+ return null;
+ }
+ IType[] types = type.getParameterTypes();
+ if (types != null && types.length > 0) {
+ return null;
+ }
+ QualifiedName qualifiedName = QualifiedName.create(method.getQualifiedName());
+ return new CppToProtobufMapping(qualifiedName, MessageField.class);
+ }
+
+ @Override public Class<CPPMethod> typeOfSupportedBinding() {
+ return CPPMethod.class;
+ }
+}
diff --git a/com.google.eclipse.protobuf.cdt/src/com/google/eclipse/protobuf/cdt/matching/ComplexTypeMatcherStrategy.java b/com.google.eclipse.protobuf.cdt/src/com/google/eclipse/protobuf/cdt/matching/ComplexTypeMatcherStrategy.java
deleted file mode 100644
index 6302468..0000000
--- a/com.google.eclipse.protobuf.cdt/src/com/google/eclipse/protobuf/cdt/matching/ComplexTypeMatcherStrategy.java
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- * Copyright (c) 2012 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.cdt.matching;
-
-import java.util.List;
-import java.util.regex.Pattern;
-
-import org.eclipse.emf.common.util.URI;
-import org.eclipse.emf.ecore.EObject;
-import org.eclipse.xtext.naming.IQualifiedNameConverter;
-import org.eclipse.xtext.resource.*;
-
-import com.google.eclipse.protobuf.cdt.mapping.CppToProtobufMapping;
-import com.google.eclipse.protobuf.protobuf.*;
-import com.google.eclipse.protobuf.protobuf.Enum;
-import com.google.eclipse.protobuf.resource.ResourceDescriptions;
-import com.google.inject.Inject;
-
-/**
- * @author alruiz@google.com (Alex Ruiz)
- */
-class ComplexTypeMatcherStrategy implements ProtobufElementMatcherStrategy {
- @Inject private IQualifiedNameConverter converter;
- @Inject private ResourceDescriptions descriptions;
-
- @Override public URI findUriOfMatchingProtobufElement(IResourceDescription resource, CppToProtobufMapping mapping) {
- String qualifiedNameAsText = converter.toString(mapping.qualifiedName());
- String regex = Pattern.quote(qualifiedNameAsText.replaceAll("_", "."));
- Pattern pattern = Pattern.compile(regex);
- List<IEObjectDescription> matches = descriptions.matchingQualifiedNames(resource, pattern);
- if (matches.size() == 1) {
- return matches.get(0).getEObjectURI();
- }
- return null;
- }
-
- @Override public boolean canHandle(Class<? extends EObject> protobufElementType) {
- return protobufElementType.equals(Message.class) || protobufElementType.equals(Enum.class);
- }
-}
diff --git a/com.google.eclipse.protobuf.cdt/src/com/google/eclipse/protobuf/cdt/matching/ProtobufElementMatcher.java b/com.google.eclipse.protobuf.cdt/src/com/google/eclipse/protobuf/cdt/matching/ProtobufElementMatcher.java
index 48e0e7c..43a0d2c 100644
--- a/com.google.eclipse.protobuf.cdt/src/com/google/eclipse/protobuf/cdt/matching/ProtobufElementMatcher.java
+++ b/com.google.eclipse.protobuf.cdt/src/com/google/eclipse/protobuf/cdt/matching/ProtobufElementMatcher.java
@@ -8,17 +8,23 @@
*/
package com.google.eclipse.protobuf.cdt.matching;
+import java.util.List;
+import java.util.regex.Pattern;
+
import org.eclipse.emf.common.util.URI;
-import org.eclipse.xtext.resource.IResourceDescription;
+import org.eclipse.xtext.naming.*;
+import org.eclipse.xtext.resource.*;
import com.google.eclipse.protobuf.cdt.mapping.CppToProtobufMapping;
+import com.google.eclipse.protobuf.resource.ResourceDescriptions;
import com.google.inject.Inject;
/**
* @author alruiz@google.com (Alex Ruiz)
*/
public class ProtobufElementMatcher {
- @Inject private ComplexTypeMatcherStrategy complexTypeMatcherStrategy;
+ @Inject private IQualifiedNameConverter converter;
+ @Inject private ResourceDescriptions descriptions;
/**
* Returns the URI of the protocol buffer element in the given resource, whose qualified name matches the one in the
@@ -28,9 +34,18 @@
* @return the found URI, or {@code null} if it was not possible to find a matching protocol buffer element.
*/
public URI findUriOfMatchingProtobufElement(IResourceDescription resource, CppToProtobufMapping mapping) {
- if (complexTypeMatcherStrategy.canHandle(mapping.type())) {
- return complexTypeMatcherStrategy.findUriOfMatchingProtobufElement(resource, mapping);
+ QualifiedName qualifiedName = mapping.qualifiedName();
+ Pattern pattern = patternToMatchFrom(qualifiedName);
+ List<IEObjectDescription> matches = descriptions.matchingQualifiedNames(resource, pattern);
+ if (matches.size() == 1) {
+ return matches.get(0).getEObjectURI();
}
return null;
}
+
+ Pattern patternToMatchFrom(QualifiedName qualifiedName) {
+ String qualifiedNameAsText = converter.toString(qualifiedName);
+ String regex = Pattern.quote(qualifiedNameAsText.replaceAll("_", "."));
+ return Pattern.compile(regex);
+ }
}
diff --git a/com.google.eclipse.protobuf.cdt/src/com/google/eclipse/protobuf/cdt/matching/ProtobufElementMatcherStrategy.java b/com.google.eclipse.protobuf.cdt/src/com/google/eclipse/protobuf/cdt/matching/ProtobufElementMatcherStrategy.java
deleted file mode 100644
index 95326a8..0000000
--- a/com.google.eclipse.protobuf.cdt/src/com/google/eclipse/protobuf/cdt/matching/ProtobufElementMatcherStrategy.java
+++ /dev/null
@@ -1,24 +0,0 @@
-/*
- * Copyright (c) 2012 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.cdt.matching;
-
-import org.eclipse.emf.common.util.URI;
-import org.eclipse.emf.ecore.EObject;
-import org.eclipse.xtext.resource.IResourceDescription;
-
-import com.google.eclipse.protobuf.cdt.mapping.CppToProtobufMapping;
-
-/**
- * @author alruiz@google.com (Alex Ruiz)
- */
-interface ProtobufElementMatcherStrategy {
- URI findUriOfMatchingProtobufElement(IResourceDescription resource, CppToProtobufMapping mapping);
-
- boolean canHandle(Class<? extends EObject> protobufElementType);
-}