Code cleanup.
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 ab73569..621f55b 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
@@ -21,8 +21,8 @@
import org.eclipse.core.runtime.*;
import org.eclipse.ui.IEditorPart;
-import com.google.eclipse.protobuf.cdt.editor.Editors;
import com.google.eclipse.protobuf.cdt.mapping.*;
+import com.google.eclipse.protobuf.cdt.util.Editors;
import com.google.inject.*;
/**
diff --git a/com.google.eclipse.protobuf.cdt/src/com/google/eclipse/protobuf/cdt/actions/ProtoFilePathFinder.java b/com.google.eclipse.protobuf.cdt/src/com/google/eclipse/protobuf/cdt/actions/ProtoFilePathFinder.java
new file mode 100644
index 0000000..38d640c
--- /dev/null
+++ b/com.google.eclipse.protobuf.cdt/src/com/google/eclipse/protobuf/cdt/actions/ProtoFilePathFinder.java
@@ -0,0 +1,73 @@
+/*
+ * 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.actions;
+
+import static org.eclipse.xtext.util.Strings.isEmpty;
+
+import java.io.*;
+import java.util.regex.*;
+
+import org.eclipse.cdt.core.model.*;
+import org.eclipse.cdt.internal.ui.editor.CEditor;
+import org.eclipse.core.runtime.*;
+import org.eclipse.ui.IEditorPart;
+
+import com.google.inject.Singleton;
+
+/**
+ * @author alruiz@google.com (Alex Ruiz)
+ */
+@SuppressWarnings("restriction")
+@Singleton class ProtoFilePathFinder {
+ private static final Pattern PROTO_PATH_PATTERN = Pattern.compile("// source: (.*)");
+
+ IPath findProtoFilePathIn(IEditorPart editor) {
+ CEditor cEditor = (CEditor) editor;
+ String contents = contentsOf(cEditor);
+ if (isEmpty(contents)) {
+ return null;
+ }
+ String line = null;
+ int lineCounter = 0;
+ BufferedReader reader = new BufferedReader(new StringReader(contents));
+ try {
+ while ((line = reader.readLine()) != null) {
+ switch (lineCounter) {
+ case 0:
+ if (!"// Generated by the protocol buffer compiler. DO NOT EDIT!".equals(line)) {
+ return null;
+ }
+ lineCounter++;
+ break;
+ case 1:
+ return pathFrom(line);
+ }
+ }
+ } catch (IOException e) {}
+ return null;
+ }
+
+ private String contentsOf(CEditor cEditor) {
+ ICElement input = cEditor.getInputCElement();
+ if (input instanceof ITranslationUnit) {
+ ITranslationUnit translationUnit = (ITranslationUnit) input;
+ return new String(translationUnit.getContents());
+ }
+ return null;
+ }
+
+ private IPath pathFrom(String line) {
+ Matcher matcher = PROTO_PATH_PATTERN.matcher(line);
+ if (!matcher.matches()) {
+ return null;
+ }
+ String path = matcher.group(1);
+ return (!isEmpty(path)) ? new Path(path) : null;
+ }
+}
diff --git a/com.google.eclipse.protobuf.cdt/src/com/google/eclipse/protobuf/cdt/actions/ProtobufElementUriFinder.java b/com.google.eclipse.protobuf.cdt/src/com/google/eclipse/protobuf/cdt/actions/ProtobufElementUriFinder.java
index 8b46ce4..badec44 100644
--- a/com.google.eclipse.protobuf.cdt/src/com/google/eclipse/protobuf/cdt/actions/ProtobufElementUriFinder.java
+++ b/com.google.eclipse.protobuf.cdt/src/com/google/eclipse/protobuf/cdt/actions/ProtobufElementUriFinder.java
@@ -8,17 +8,14 @@
*/
package com.google.eclipse.protobuf.cdt.actions;
-import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IPath;
import org.eclipse.emf.common.util.URI;
import org.eclipse.ui.IEditorPart;
import org.eclipse.xtext.naming.QualifiedName;
import org.eclipse.xtext.resource.IResourceDescription;
-import com.google.eclipse.protobuf.cdt.editor.Editors;
import com.google.eclipse.protobuf.cdt.mapping.CppToProtobufMapping;
import com.google.eclipse.protobuf.cdt.matching.ProtobufElementMatcher;
-import com.google.eclipse.protobuf.cdt.path.ProtoFilePaths;
import com.google.eclipse.protobuf.resource.*;
import com.google.inject.Inject;
@@ -28,14 +25,12 @@
class ProtobufElementUriFinder {
@Inject private ProtobufElementMatcher matcher;
@Inject private ResourceDescriptions descriptions;
- @Inject private Editors editors;
@Inject private IndexLookup indexLookup;
@Inject private AstBasedCppToProtobufMapper mapper;
- @Inject private ProtoFilePaths paths;
+ @Inject private ProtoFilePathFinder pathFinder;
URI findProtobufElementUriFromSelectionOf(IEditorPart editor) {
- IFile file = editors.fileDisplayedIn(editor);
- IPath protoFilePath = paths.protoFilePath(file);
+ IPath protoFilePath = pathFinder.findProtoFilePathIn(editor);
if (protoFilePath != null) {
CppToProtobufMapping mapping = mapper.createMappingFromSelectionOf(editor);
if (mapping != null) {
diff --git a/com.google.eclipse.protobuf.cdt/src/com/google/eclipse/protobuf/cdt/path/ProtoFilePaths.java b/com.google.eclipse.protobuf.cdt/src/com/google/eclipse/protobuf/cdt/path/ProtoFilePaths.java
deleted file mode 100644
index 3f69641..0000000
--- a/com.google.eclipse.protobuf.cdt/src/com/google/eclipse/protobuf/cdt/path/ProtoFilePaths.java
+++ /dev/null
@@ -1,86 +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.path;
-
-import static com.google.common.collect.Lists.newArrayList;
-import static com.google.eclipse.protobuf.ui.preferences.compiler.core.CompilerPreferences.compilerPreferences;
-import static com.google.eclipse.protobuf.ui.util.Paths.segmentsOf;
-import static org.eclipse.core.runtime.IPath.SEPARATOR;
-import static org.eclipse.xtext.util.Strings.*;
-
-import java.util.List;
-
-import org.eclipse.core.resources.*;
-import org.eclipse.core.runtime.*;
-import org.eclipse.xtext.ui.editor.preferences.IPreferenceStoreAccess;
-
-import com.google.eclipse.protobuf.ui.preferences.compiler.core.CompilerPreferences;
-import com.google.inject.Inject;
-
-/**
- * Utility methods related to paths of .proto files.
- *
- * @author alruiz@google.com (Alex Ruiz)
- */
-public class ProtoFilePaths {
- private static final String PATH_SEPARATOR = new String(new char[] { SEPARATOR });
-
- @Inject private IPreferenceStoreAccess storeAccess;
-
- /**
- * Returns the path of the .proto file used as source of the generated C++ header file.
- * @param cppHeaderFile the generated C++ header file.
- * @return the path of the .proto file used as source of the generated C++ header file, or {@code null} if the given
- * file is not a C++ header file or if C++ code generation is not enabled in the proto editor.
- */
- public IPath protoFilePath(IFile cppHeaderFile) {
- IPath cppOutputDirectory = cppOutputDirectory(cppHeaderFile.getProject());
- if (cppOutputDirectory == null) {
- return null;
- }
- IPath headerFilePath = cppHeaderFile.getFullPath();
- List<String> newPathSegments = newArrayList(headerFilePath.segments());
- for (int i = 0; i < headerFilePath.segmentCount() - 1; i++) {
- newPathSegments.remove(0);
- if (headerFilePath.segment(i).equals(cppOutputDirectory.lastSegment())) {
- break;
- }
- }
- int fileNameIndex = newPathSegments.size() - 1;
- String fileName = newPathSegments.get(fileNameIndex);
- newPathSegments.set(fileNameIndex, fileName.replace(".pb.h", ".proto"));
- return new Path(concat(PATH_SEPARATOR, newPathSegments));
- }
-
- private IPath cppOutputDirectory(IProject project) {
- CompilerPreferences preferences = compilerPreferences(storeAccess, project);
- if (!preferences.compileProtoFiles().getValue() && !preferences.cppCodeGenerationEnabled().getValue()) {
- return null;
- }
- String directoryName = preferences.cppOutputDirectory().getValue();
- if (isEmpty(directoryName)) {
- return null;
- }
- IFolder directory = null;
- String[] segments = segmentsOf(directoryName);
- StringBuilder pathBuilder = new StringBuilder();
- for (String segment : segments) {
- pathBuilder.append(segment);
- directory = project.getFolder(pathBuilder.toString());
- if (!directory.exists()) {
- return null;
- }
- pathBuilder.append(SEPARATOR);
- }
- if (directory == null) {
- return null;
- }
- return directory.getFullPath();
- }
-}
diff --git a/com.google.eclipse.protobuf.cdt/src/com/google/eclipse/protobuf/cdt/editor/Editors.java b/com.google.eclipse.protobuf.cdt/src/com/google/eclipse/protobuf/cdt/util/Editors.java
similarity index 96%
rename from com.google.eclipse.protobuf.cdt/src/com/google/eclipse/protobuf/cdt/editor/Editors.java
rename to com.google.eclipse.protobuf.cdt/src/com/google/eclipse/protobuf/cdt/util/Editors.java
index 2c088b4..8a006dd 100644
--- a/com.google.eclipse.protobuf.cdt/src/com/google/eclipse/protobuf/cdt/editor/Editors.java
+++ b/com.google.eclipse.protobuf.cdt/src/com/google/eclipse/protobuf/cdt/util/Editors.java
@@ -6,7 +6,7 @@
*
* http://www.eclipse.org/legal/epl-v10.html
*/
-package com.google.eclipse.protobuf.cdt.editor;
+package com.google.eclipse.protobuf.cdt.util;
import org.eclipse.core.resources.IFile;
import org.eclipse.jface.text.ITextSelection;
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/resource/ResourceDescriptions.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/resource/ResourceDescriptions.java
index b28ffe3..ae13715 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/resource/ResourceDescriptions.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/resource/ResourceDescriptions.java
@@ -64,7 +64,7 @@
for (IEObjectDescription exported : resource.getExportedObjects()) {
QualifiedName qualifiedName = exported.getQualifiedName();
Matcher matcher = pattern.matcher(converter.toString(qualifiedName));
- if (matcher.matches() && haveMatchingNames(exported.getEClass(), type)) {
+ if (haveMatchingNames(exported.getEClass(), type) && matcher.matches()) {
descriptions.add(exported);
}
}