From 50906ed31f222055e6822fce5eef425e5e71caa5 Mon Sep 17 00:00:00 2001 From: Chris Wiebe Date: Sat, 21 Aug 2004 01:26:50 +0000 Subject: [PATCH] 2004-08-20 Chris Wiebe make PathUtil visible * browser/org/eclipse/cdt/internal/core/browser/PathUtil.java * browser/org/eclipse/cdt/internal/core/browser/TypeReference.java * browser/org/eclipse/cdt/internal/core/browser/TypeSearchScope.java --- core/org.eclipse.cdt.core/ChangeLog | 7 ++ .../util => core/browser}/PathUtil.java | 89 ++++++++++++++----- .../cdt/core/browser/TypeReference.java | 43 ++------- .../cdt/core/browser/TypeSearchScope.java | 4 + .../browser/cache/IndexerDependenciesJob.java | 2 +- .../core/browser/cache/IndexerTypesJob.java | 2 +- .../core/browser/cache/TypeParser.java | 2 +- 7 files changed, 86 insertions(+), 63 deletions(-) rename core/org.eclipse.cdt.core/browser/org/eclipse/cdt/{internal/core/browser/util => core/browser}/PathUtil.java (51%) diff --git a/core/org.eclipse.cdt.core/ChangeLog b/core/org.eclipse.cdt.core/ChangeLog index bc582b7e526..82b1f721d8c 100644 --- a/core/org.eclipse.cdt.core/ChangeLog +++ b/core/org.eclipse.cdt.core/ChangeLog @@ -1,3 +1,10 @@ +2004-08-20 Chris Wiebe + + make PathUtil visible + * browser/org/eclipse/cdt/internal/core/browser/PathUtil.java + * browser/org/eclipse/cdt/internal/core/browser/TypeReference.java + * browser/org/eclipse/cdt/internal/core/browser/TypeSearchScope.java + 2004-08-18 Alain Magloire Work on the ResolverModel, we make the Core Model aware of the changes. diff --git a/core/org.eclipse.cdt.core/browser/org/eclipse/cdt/internal/core/browser/util/PathUtil.java b/core/org.eclipse.cdt.core/browser/org/eclipse/cdt/core/browser/PathUtil.java similarity index 51% rename from core/org.eclipse.cdt.core/browser/org/eclipse/cdt/internal/core/browser/util/PathUtil.java rename to core/org.eclipse.cdt.core/browser/org/eclipse/cdt/core/browser/PathUtil.java index 717603ad6df..e0649a926bf 100644 --- a/core/org.eclipse.cdt.core/browser/org/eclipse/cdt/internal/core/browser/util/PathUtil.java +++ b/core/org.eclipse.cdt.core/browser/org/eclipse/cdt/core/browser/PathUtil.java @@ -8,12 +8,16 @@ * Contributors: * QNX Software Systems - initial API and implementation *******************************************************************************/ -package org.eclipse.cdt.internal.core.browser.util; +package org.eclipse.cdt.core.browser; import java.io.File; import java.io.IOException; +import org.eclipse.cdt.core.CCorePlugin; +import org.eclipse.cdt.core.parser.IScannerInfo; +import org.eclipse.cdt.core.parser.IScannerInfoProvider; import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IWorkspace; import org.eclipse.core.resources.IWorkspaceRoot; import org.eclipse.core.resources.ResourcesPlugin; @@ -22,18 +26,9 @@ import org.eclipse.core.runtime.Path; public class PathUtil { - private static boolean fGotOS = false; - private static boolean fIsWindows = false; - - public static boolean isWindowsSystem() { - if (!fGotOS) { - String os = System.getProperty("os.name"); //$NON-NLS-1$ - if (os != null && os.startsWith("Win")) { //$NON-NLS-1$ - fIsWindows= true; - } - fGotOS = true; - } - return fIsWindows; + public static boolean isWindowsFileSystem() { + String os = System.getProperty("os.name"); //$NON-NLS-1$ + return (os != null && os.startsWith("Win")); //$NON-NLS-1$ } public static IWorkspaceRoot getWorkspaceRoot() { @@ -44,20 +39,16 @@ public class PathUtil { return null; } - public static IPath getCanonicalPath(String fullPath) { - File file = new File(fullPath); + public static IPath getCanonicalPath(IPath fullPath) { + File file = fullPath.toFile(); try { String canonPath = file.getCanonicalPath(); return new Path(canonPath); } catch (IOException ex) { } - return new Path(fullPath); + return null; } - public static IPath getCanonicalPath(IPath fullPath) { - return getCanonicalPath(fullPath.toString()); - } - public static IPath getWorkspaceRelativePath(IPath fullPath) { IWorkspaceRoot workspaceRoot = getWorkspaceRoot(); if (workspaceRoot != null) { @@ -87,10 +78,6 @@ public class PathUtil { return getWorkspaceRelativePath(new Path(fullPath)); } - public static IPath getProjectRelativePath(String fullPath, IProject project) { - return getProjectRelativePath(new Path(fullPath), project); - } - public static IPath getRawLocation(IPath wsRelativePath) { IWorkspaceRoot workspaceRoot = getWorkspaceRoot(); if (workspaceRoot != null && wsRelativePath != null) { @@ -101,4 +88,58 @@ public class PathUtil { } return wsRelativePath; } + + public static IPath makeRelativePath(IPath path, IPath relativeTo) { + int segments = relativeTo.matchingFirstSegments(path); + if (segments > 0) { + IPath prefix = relativeTo.removeFirstSegments(segments).removeLastSegments(1); + IPath suffix = path.removeFirstSegments(segments); + IPath relativePath = new Path(""); //$NON-NLS-1$ + for (int i = 0; i < prefix.segmentCount(); ++i) { + relativePath = relativePath.append(".." + IPath.SEPARATOR); //$NON-NLS-1$ + } + return relativePath.append(suffix); + } + return null; + } + + public static IPath makeRelativePathToProjectIncludes(IPath fullPath, IProject project) { + IScannerInfoProvider provider = CCorePlugin.getDefault().getScannerInfoProvider(project); + if (provider != null) { + IScannerInfo info = provider.getScannerInformation(project); + if (info != null) { + String[] includePaths = info.getIncludePaths(); + IPath relativePath = null; + int mostSegments = 0; + for (int i = 0; i < includePaths.length; ++i) { + IPath includePath = new Path(includePaths[i]); + if (includePath.isPrefixOf(fullPath)) { + int segments = includePath.matchingFirstSegments(fullPath); + if (segments > mostSegments) { + relativePath = fullPath.removeFirstSegments(segments).setDevice(null); + mostSegments = segments; + } + } + } + if (relativePath != null) + return relativePath; + } + } + return null; + } + + public static IProject getEnclosingProject(IPath fullPath) { + IWorkspaceRoot root = getWorkspaceRoot(); + if (root != null) { + IPath path = getWorkspaceRelativePath(fullPath); + while (!path.isEmpty()) { + IResource res = root.findMember(path); + if (res != null) + return res.getProject(); + + path = path.removeLastSegments(1); + } + } + return null; + } } diff --git a/core/org.eclipse.cdt.core/browser/org/eclipse/cdt/core/browser/TypeReference.java b/core/org.eclipse.cdt.core/browser/org/eclipse/cdt/core/browser/TypeReference.java index 0cfeb9a574b..78e28d9eb00 100644 --- a/core/org.eclipse.cdt.core/browser/org/eclipse/cdt/core/browser/TypeReference.java +++ b/core/org.eclipse.cdt.core/browser/org/eclipse/cdt/core/browser/TypeReference.java @@ -10,19 +10,15 @@ *******************************************************************************/ package org.eclipse.cdt.core.browser; -import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.model.CModelException; import org.eclipse.cdt.core.model.CoreModel; import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.core.model.ICProject; import org.eclipse.cdt.core.model.ITranslationUnit; import org.eclipse.cdt.core.model.IWorkingCopy; -import org.eclipse.cdt.core.parser.IScannerInfo; -import org.eclipse.cdt.core.parser.IScannerInfoProvider; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.IPath; -import org.eclipse.core.runtime.Path; public class TypeReference implements ITypeReference { @@ -195,27 +191,9 @@ public class TypeReference implements ITypeReference { public IPath getRelativeIncludePath(IProject project) { IPath path = getLocation(); if (path != null) { - IScannerInfoProvider provider = CCorePlugin.getDefault().getScannerInfoProvider(project); - if (provider != null) { - IScannerInfo info = provider.getScannerInformation(project); - if (info != null) { - String[] includePaths = info.getIncludePaths(); - IPath relativePath = null; - int mostSegments = 0; - for (int i = 0; i < includePaths.length; ++i) { - IPath includePath = new Path(includePaths[i]); - if (includePath.isPrefixOf(path)) { - int segments = includePath.matchingFirstSegments(path); - if (segments > mostSegments) { - relativePath = path.removeFirstSegments(segments).setDevice(null); - mostSegments = segments; - } - } - } - if (relativePath != null) - path = relativePath; - } - } + IPath relativePath = PathUtil.makeRelativePathToProjectIncludes(path, project); + if (relativePath != null) + return relativePath; } return path; } @@ -223,20 +201,13 @@ public class TypeReference implements ITypeReference { public IPath getRelativePath(IPath relativeToPath) { IPath path = getPath(); if (path != null) { - int segments = relativeToPath.matchingFirstSegments(path); - if (segments > 0) { - IPath prefix = relativeToPath.removeFirstSegments(segments).removeLastSegments(1); - IPath suffix = path.removeFirstSegments(segments); - IPath relativePath = new Path(""); //$NON-NLS-1$ - for (int i = 0; i < prefix.segmentCount(); ++i) { - relativePath = relativePath.append(".." + IPath.SEPARATOR); //$NON-NLS-1$ - } - return relativePath.append(suffix); - } + IPath relativePath = PathUtil.makeRelativePath(path, relativeToPath); + if (relativePath != null) + return relativePath; } return path; } - + public String toString() { IPath path = getLocation(); if (path != null) { diff --git a/core/org.eclipse.cdt.core/browser/org/eclipse/cdt/core/browser/TypeSearchScope.java b/core/org.eclipse.cdt.core/browser/org/eclipse/cdt/core/browser/TypeSearchScope.java index e7385c3f182..3a2122f7a35 100644 --- a/core/org.eclipse.cdt.core/browser/org/eclipse/cdt/core/browser/TypeSearchScope.java +++ b/core/org.eclipse.cdt.core/browser/org/eclipse/cdt/core/browser/TypeSearchScope.java @@ -52,6 +52,10 @@ public class TypeSearchScope implements ITypeSearchScope { add(scope); } + public TypeSearchScope(IProject project) { + add(project); + } + public Collection pathSet() { return fPathSet; } diff --git a/core/org.eclipse.cdt.core/browser/org/eclipse/cdt/internal/core/browser/cache/IndexerDependenciesJob.java b/core/org.eclipse.cdt.core/browser/org/eclipse/cdt/internal/core/browser/cache/IndexerDependenciesJob.java index a6d6e36be83..5ff2bb7e45e 100644 --- a/core/org.eclipse.cdt.core/browser/org/eclipse/cdt/internal/core/browser/cache/IndexerDependenciesJob.java +++ b/core/org.eclipse.cdt.core/browser/org/eclipse/cdt/internal/core/browser/cache/IndexerDependenciesJob.java @@ -13,8 +13,8 @@ package org.eclipse.cdt.internal.core.browser.cache; import java.io.IOException; import org.eclipse.cdt.core.browser.ITypeSearchScope; +import org.eclipse.cdt.core.browser.PathUtil; import org.eclipse.cdt.internal.core.CharOperation; -import org.eclipse.cdt.internal.core.browser.util.PathUtil; import org.eclipse.cdt.internal.core.index.IEntryResult; import org.eclipse.cdt.internal.core.index.IIndex; import org.eclipse.cdt.internal.core.index.impl.BlocksIndexInput; diff --git a/core/org.eclipse.cdt.core/browser/org/eclipse/cdt/internal/core/browser/cache/IndexerTypesJob.java b/core/org.eclipse.cdt.core/browser/org/eclipse/cdt/internal/core/browser/cache/IndexerTypesJob.java index c329bb53f00..b573ec475db 100644 --- a/core/org.eclipse.cdt.core/browser/org/eclipse/cdt/internal/core/browser/cache/IndexerTypesJob.java +++ b/core/org.eclipse.cdt.core/browser/org/eclipse/cdt/internal/core/browser/cache/IndexerTypesJob.java @@ -14,12 +14,12 @@ import java.io.IOException; import org.eclipse.cdt.core.browser.ITypeInfo; import org.eclipse.cdt.core.browser.ITypeSearchScope; +import org.eclipse.cdt.core.browser.PathUtil; import org.eclipse.cdt.core.browser.QualifiedTypeName; import org.eclipse.cdt.core.browser.TypeInfo; import org.eclipse.cdt.core.browser.TypeReference; import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.internal.core.CharOperation; -import org.eclipse.cdt.internal.core.browser.util.PathUtil; import org.eclipse.cdt.internal.core.index.IEntryResult; import org.eclipse.cdt.internal.core.index.IIndex; import org.eclipse.cdt.internal.core.index.impl.BlocksIndexInput; diff --git a/core/org.eclipse.cdt.core/browser/org/eclipse/cdt/internal/core/browser/cache/TypeParser.java b/core/org.eclipse.cdt.core/browser/org/eclipse/cdt/internal/core/browser/cache/TypeParser.java index dbe07346564..e85765787f2 100644 --- a/core/org.eclipse.cdt.core/browser/org/eclipse/cdt/internal/core/browser/cache/TypeParser.java +++ b/core/org.eclipse.cdt.core/browser/org/eclipse/cdt/internal/core/browser/cache/TypeParser.java @@ -25,6 +25,7 @@ import org.eclipse.cdt.core.browser.ITypeInfo; import org.eclipse.cdt.core.browser.ITypeReference; import org.eclipse.cdt.core.browser.ITypeSearchScope; import org.eclipse.cdt.core.browser.IWorkingCopyProvider; +import org.eclipse.cdt.core.browser.PathUtil; import org.eclipse.cdt.core.browser.QualifiedTypeName; import org.eclipse.cdt.core.browser.TypeInfo; import org.eclipse.cdt.core.browser.TypeReference; @@ -92,7 +93,6 @@ import org.eclipse.cdt.core.parser.ast.IASTUsingDeclaration; import org.eclipse.cdt.core.parser.ast.IASTUsingDirective; import org.eclipse.cdt.core.parser.ast.IASTVariable; import org.eclipse.cdt.core.parser.ast.IASTVariableReference; -import org.eclipse.cdt.internal.core.browser.util.PathUtil; import org.eclipse.cdt.internal.core.browser.util.SimpleStack; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject;