Code cleanup.
diff --git a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/ProjectVariable_useProjectName_Test.java b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/preferences/paths/ProjectVariable_useProjectName_Test.java similarity index 92% rename from com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/ProjectVariable_useProjectName_Test.java rename to com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/preferences/paths/ProjectVariable_useProjectName_Test.java index 417f81a..d03094f 100644 --- a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/ProjectVariable_useProjectName_Test.java +++ b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/preferences/paths/ProjectVariable_useProjectName_Test.java
@@ -6,12 +6,14 @@ * * http://www.eclipse.org/legal/epl-v10.html */ -package com.google.eclipse.protobuf.ui.util; +package com.google.eclipse.protobuf.ui.preferences.paths; import static org.hamcrest.core.IsEqual.equalTo; import static org.junit.Assert.assertThat; import static org.mockito.Mockito.*; +import com.google.eclipse.protobuf.ui.preferences.paths.ProjectVariable; + import org.eclipse.core.resources.IProject; import org.junit.*;
diff --git a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/ProjectVariable_useProjectVariable_Test.java b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/preferences/paths/ProjectVariable_useProjectVariable_Test.java similarity index 92% rename from com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/ProjectVariable_useProjectVariable_Test.java rename to com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/preferences/paths/ProjectVariable_useProjectVariable_Test.java index 4cab0c5..36fed91 100644 --- a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/ProjectVariable_useProjectVariable_Test.java +++ b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/preferences/paths/ProjectVariable_useProjectVariable_Test.java
@@ -6,12 +6,14 @@ * * http://www.eclipse.org/legal/epl-v10.html */ -package com.google.eclipse.protobuf.ui.util; +package com.google.eclipse.protobuf.ui.preferences.paths; import static org.hamcrest.core.IsEqual.equalTo; import static org.junit.Assert.assertThat; import static org.mockito.Mockito.*; +import com.google.eclipse.protobuf.ui.preferences.paths.ProjectVariable; + import org.eclipse.core.resources.IProject; import org.eclipse.core.runtime.*; import org.junit.*;
diff --git a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/scoping/SingleDirectoryFileResolver_resolveUri_Test.java b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/scoping/SingleDirectoryFileResolver_resolveUri_Test.java index ae823a7..6ec8b87 100644 --- a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/scoping/SingleDirectoryFileResolver_resolveUri_Test.java +++ b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/scoping/SingleDirectoryFileResolver_resolveUri_Test.java
@@ -14,7 +14,6 @@ import static org.mockito.Matchers.any; import static org.mockito.Mockito.*; -import org.eclipse.core.resources.IProject; import org.eclipse.emf.common.util.URI; import org.junit.*; @@ -22,7 +21,7 @@ import com.google.eclipse.protobuf.ui.util.Resources; /** - * Tests for <code>{@link SingleDirectoryFileResolver#resolveUri(String, URI, IProject, PathsPreferences)}</code>. + * Tests for <code>{@link SingleDirectoryFileResolver#resolveUri(String, URI, PathsPreferences)}</code>. * * @author alruiz@google.com (Alex Ruiz) */ @@ -46,19 +45,19 @@ @Test public void should_resolve_import_URI_if_missing_scheme() { when(resources.fileExists(any(URI.class))).thenReturn(true); - String resolved = resolver.resolveUri("folder1/address.proto", resourceUri, null, preferences); + String resolved = resolver.resolveUri("folder1/address.proto", resourceUri, preferences); assertThat(resolved, equalTo("platform:/resource/src/proto/folder1/address.proto")); } @Test public void should_resolve_import_URI_even_if_overlapping_folders_with_resource_URI() { when(resources.fileExists(any(URI.class))).thenReturn(true); - String resolved = resolver.resolveUri("src/proto/folder1/address.proto", resourceUri, null, preferences); + String resolved = resolver.resolveUri("src/proto/folder1/address.proto", resourceUri, preferences); assertThat(resolved, equalTo("platform:/resource/src/proto/folder1/address.proto")); } @Test public void should_resolve_import_URI_even_if_overlapping_one_folder_only_with_resource_URI() { when(resources.fileExists(any(URI.class))).thenReturn(true); - String resolved = resolver.resolveUri("src/proto/read-only/address.proto", resourceUri, null, preferences); + String resolved = resolver.resolveUri("src/proto/read-only/address.proto", resourceUri, preferences); assertThat(resolved, equalTo("platform:/resource/src/proto/read-only/address.proto")); } }
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/AddDirectoryDialog.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/AddDirectoryDialog.java index 5679b03..da8704c 100644 --- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/AddDirectoryDialog.java +++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/AddDirectoryDialog.java
@@ -10,7 +10,7 @@ import static com.google.eclipse.protobuf.ui.preferences.paths.DirectorySelectionDialogs.*; import static com.google.eclipse.protobuf.ui.preferences.paths.Messages.*; -import static com.google.eclipse.protobuf.ui.util.ProjectVariable.useProjectVariable; +import static com.google.eclipse.protobuf.ui.preferences.paths.ProjectVariable.useProjectVariable; import static org.eclipse.jface.dialogs.IDialogConstants.OK_ID; import static org.eclipse.xtext.util.Strings.isEmpty;
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/DirectoryPath.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/DirectoryPath.java index 611426d..5e25ed4 100644 --- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/DirectoryPath.java +++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/DirectoryPath.java
@@ -8,7 +8,9 @@ */ package com.google.eclipse.protobuf.ui.preferences.paths; -import static com.google.eclipse.protobuf.ui.util.ProjectVariable.containsProjectVariable; +import static com.google.eclipse.protobuf.ui.preferences.paths.ProjectVariable.useProjectName; + +import org.eclipse.core.resources.IProject; import java.util.regex.*; @@ -23,9 +25,16 @@ private final boolean isWorkspacePath; static DirectoryPath parse(String path) { + return parse(path, null); + } + + static DirectoryPath parse(String path, IProject project) { Matcher matcher = WORKSPACE_PATH_PATTERN.matcher(path); - if (matcher.matches()) return new DirectoryPath(matcher.group(1), true); - if (containsProjectVariable(path)) return new DirectoryPath(path, true); + if (matcher.matches()) { + String actualPath = matcher.group(1); + if (project != null) actualPath = useProjectName(actualPath, project); + return new DirectoryPath(actualPath, true); + } return new DirectoryPath(path, false); } @@ -36,7 +45,7 @@ /** {@inheritDoc} */ @Override public String toString() { - if (!isWorkspacePath || containsProjectVariable(value)) return value; + if (!isWorkspacePath) return value; return "${workspace_loc:" + value + "}"; }
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/PathsPreferences.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/PathsPreferences.java index f276046..8d25a04 100644 --- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/PathsPreferences.java +++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/PathsPreferences.java
@@ -13,11 +13,11 @@ import static com.google.eclipse.protobuf.ui.util.Strings.CSV_PATTERN; import static java.util.Collections.*; -import java.util.ArrayList; -import java.util.List; - +import org.eclipse.core.resources.IProject; import org.eclipse.jface.preference.IPreferenceStore; +import java.util.*; + /** * Paths preferences, retrieved from an <code>{@link IPreferenceStore}</code>. To create a new instance invoke * <code>{@link PathsPreferencesProvider#getPreferences(org.eclipse.core.resources.IProject)}</code> @@ -29,16 +29,16 @@ private final PathResolutionType pathResolutionType; private final List<DirectoryPath> importRoots; - PathsPreferences(IPreferenceStore store) { + PathsPreferences(IPreferenceStore store, IProject project) { pathResolutionType = PathResolutionType.readFrom(store); - importRoots = importRoots(pathResolutionType, store); + importRoots = importRoots(pathResolutionType, store, project); } - private static List<DirectoryPath> importRoots(PathResolutionType types, IPreferenceStore store) { + private static List<DirectoryPath> importRoots(PathResolutionType types, IPreferenceStore store, IProject project) { if (types.equals(SINGLE_DIRECTORY)) return emptyList(); List<DirectoryPath> roots = new ArrayList<DirectoryPath>(); for (String root : store.getString(IMPORT_ROOTS).split(CSV_PATTERN)) { - roots.add(DirectoryPath.parse(root)); + roots.add(DirectoryPath.parse(root, project)); } return unmodifiableList(roots); }
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/PathsPreferencesProvider.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/PathsPreferencesProvider.java index 436d1a4..96e0675 100644 --- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/PathsPreferencesProvider.java +++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/PathsPreferencesProvider.java
@@ -25,6 +25,6 @@ public PathsPreferences getPreferences(IProject project) { IPreferenceStore store = storeAccess.getWritablePreferenceStore(project); - return new PathsPreferences(store); + return new PathsPreferences(store, project); } }
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/ProjectVariable.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/ProjectVariable.java new file mode 100644 index 0000000..269b4d1 --- /dev/null +++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/paths/ProjectVariable.java
@@ -0,0 +1,39 @@ +/* + * 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.paths; + +import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.*; + +/** + * ${project} variable. + * + * @author alruiz@google.com (Alex Ruiz) + */ +class ProjectVariable { + + private static final String VARIABLE_VALUE = "${project}"; + + static IPath useProjectVariable(IPath path, IProject project) { + return switchProjectNames(path, project.getName(), VARIABLE_VALUE); + } + + static String useProjectName(String path, IProject project) { + IPath newPath = switchProjectNames(new Path(path), VARIABLE_VALUE, project.getName()); + return newPath.toString(); + } + + private static IPath switchProjectNames(IPath path, String originalName, String newName) { + if (!originalName.equals(path.segment(0))) return path; + IPath newPath = new Path(newName); + newPath = newPath.append(path.removeFirstSegments(1)); + if (path.isAbsolute()) newPath = newPath.makeAbsolute(); + return newPath; + } +}
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/FileResolverStrategy.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/FileResolverStrategy.java index 261d16b..e7101a9 100644 --- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/FileResolverStrategy.java +++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/FileResolverStrategy.java
@@ -8,7 +8,6 @@ */ package com.google.eclipse.protobuf.ui.scoping; -import org.eclipse.core.resources.IProject; import org.eclipse.core.runtime.IPath; import org.eclipse.emf.common.util.URI; @@ -19,8 +18,8 @@ */ interface FileResolverStrategy { - String PREFIX = "platform:/resource"; + String PLATFORM_RESOURCE_PREFIX = "platform:/resource"; char SEPARATOR = IPath.SEPARATOR; - String resolveUri(String importUri, URI declaringResourceUri, IProject project, PathsPreferences preferences); + String resolveUri(String importUri, URI declaringResourceUri, PathsPreferences preferences); }
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/FileUriResolver.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/FileUriResolver.java index 9000aa8..05acd5c 100644 --- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/FileUriResolver.java +++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/FileUriResolver.java
@@ -61,7 +61,7 @@ if (project == null) project = resources.activeProject(); if (project == null) throw new IllegalStateException("Unable to find current project"); PathsPreferences preferences = preferenceProvider.getPreferences(project); - return resolver(preferences).resolveUri(importUri, resourceUri, project, preferences); + return resolver(preferences).resolveUri(importUri, resourceUri, preferences); } private FileResolverStrategy resolver(PathsPreferences preferences) {
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/MultipleDirectoriesFileResolver.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/MultipleDirectoriesFileResolver.java index b39adbc..597c712 100644 --- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/MultipleDirectoriesFileResolver.java +++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/MultipleDirectoriesFileResolver.java
@@ -8,19 +8,15 @@ */ package com.google.eclipse.protobuf.ui.scoping; -import static com.google.eclipse.protobuf.ui.util.ProjectVariable.useProjectName; - -import java.util.List; +import com.google.eclipse.protobuf.ui.preferences.paths.*; +import com.google.eclipse.protobuf.ui.util.Resources; import org.eclipse.core.filesystem.*; -import org.eclipse.core.resources.IProject; -import org.eclipse.core.runtime.IPath; -import org.eclipse.core.runtime.Path; +import org.eclipse.core.filesystem.URIUtil; +import org.eclipse.core.runtime.*; import org.eclipse.emf.common.util.URI; -import com.google.eclipse.protobuf.ui.preferences.paths.DirectoryPath; -import com.google.eclipse.protobuf.ui.preferences.paths.PathsPreferences; -import com.google.eclipse.protobuf.ui.util.*; +import java.util.List; /** * @author alruiz@google.com (Alex Ruiz) @@ -36,10 +32,10 @@ } /** {@inheritDoc} */ - public String resolveUri(String importUri, URI declaringResourceUri, IProject project, PathsPreferences preferences) { + public String resolveUri(String importUri, URI declaringResourceUri, PathsPreferences preferences) { List<DirectoryPath> importRoots = preferences.importRoots(); for (DirectoryPath root : importRoots) { - String resolved = resolveUri(importUri, root, project); + String resolved = resolveUri(importUri, root); if (resolved != null) return resolved; } for (DirectoryPath root : importRoots) { @@ -50,14 +46,14 @@ return null; } - private String resolveUri(String importUri, DirectoryPath importRoot, IProject project) { + private String resolveUri(String importUri, DirectoryPath importRoot) { String root = importRoot.value(); - if (importRoot.isWorkspacePath()) return resolveUriInWorkspace(importUri, root, project); + if (importRoot.isWorkspacePath()) return resolveUriInWorkspace(importUri, root); return resolveUriInFileSystem(importUri, root); } - private String resolveUriInWorkspace(String importUri, String importRoot, IProject project) { - String path = PREFIX + useProjectName(importRoot, project) + SEPARATOR + importUri; + private String resolveUriInWorkspace(String importUri, String importRoot) { + String path = PLATFORM_RESOURCE_PREFIX + importRoot + SEPARATOR + importUri; boolean exists = resources.fileExists(URI.createURI(path)); return (exists) ? path : null; }
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/SingleDirectoryFileResolver.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/SingleDirectoryFileResolver.java index c863a21..1841e83 100644 --- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/SingleDirectoryFileResolver.java +++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/SingleDirectoryFileResolver.java
@@ -13,7 +13,6 @@ import java.util.*; -import org.eclipse.core.resources.IProject; import org.eclipse.emf.common.util.URI; import org.eclipse.xtext.util.Pair; @@ -31,7 +30,7 @@ this.resources = resources; } - public String resolveUri(String importUri, URI declaringResourceUri, IProject project, PathsPreferences preferences) { + public String resolveUri(String importUri, URI declaringResourceUri, PathsPreferences preferences) { List<String> resourceUriSegments = removeFirstAndLast(declaringResourceUri.segmentsList()); Pair<String, List<String>> importUriPair = pair(importUri, createURI(importUri).segmentsList()); return resolveUri(importUriPair, resourceUriSegments); @@ -53,7 +52,7 @@ if (segment.equals(firstSegment)) break; pathBuilder.append(segment).append(SEPARATOR); } - String resolved = PREFIX + SEPARATOR + pathBuilder.toString() + importUri.getFirst(); + String resolved = PLATFORM_RESOURCE_PREFIX + SEPARATOR + pathBuilder.toString() + importUri.getFirst(); return (resources.fileExists(createURI(resolved))) ? resolved : null; }
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/ProjectVariable.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/ProjectVariable.java deleted file mode 100644 index 787c78b..0000000 --- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/ProjectVariable.java +++ /dev/null
@@ -1,43 +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.ui.util; - -import org.eclipse.core.resources.IProject; -import org.eclipse.core.runtime.*; - -/** - * ${project} variable. - * - * @author alruiz@google.com (Alex Ruiz) - */ -public class ProjectVariable { - - private static final String VARIABLE_VALUE = "${project}"; - - public static IPath useProjectVariable(IPath path, IProject project) { - return switchNames(path, project.getName(), VARIABLE_VALUE); - } - - public static String useProjectName(String path, IProject project) { - IPath newPath = switchNames(new Path(path), VARIABLE_VALUE, project.getName()); - return newPath.toString(); - } - - private static IPath switchNames(IPath path, String originalName, String newName) { - if (!originalName.equals(path.segment(0))) return path; - IPath newPath = new Path(newName); - newPath = newPath.append(path.removeFirstSegments(1)); - if (path.isAbsolute()) newPath = newPath.makeAbsolute(); - return newPath; - } - - public static boolean containsProjectVariable(String path) { - return path != null && path.contains(VARIABLE_VALUE); - } -}