1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-08 02:06:01 +02:00

Language Manager: fixes some warnings, avoids converting file to location and back to file again, avoids recomputation of content-type id cached in ITranslationUnit.

This commit is contained in:
Markus Schorn 2007-02-08 10:16:30 +00:00
parent d09a32444e
commit f37e2ccc7d
2 changed files with 60 additions and 40 deletions

View file

@ -302,8 +302,7 @@ public class LanguageManager {
* TODO: implement other mapping levels besides project level and content type level * TODO: implement other mapping levels besides project level and content type level
*/ */
public ILanguage getLanguageForFile(String fullPathToFile, IProject project) throws CoreException { public ILanguage getLanguageForFile(String fullPathToFile, IProject project) throws CoreException {
if (project == null)
if(project == null)
throw new IllegalArgumentException("project must not be null in call to LanguageManager.getLanguageForFile(String, IProject)"); throw new IllegalArgumentException("project must not be null in call to LanguageManager.getLanguageForFile(String, IProject)");
IContentType contentType = CContentTypes.getContentType(project, fullPathToFile); IContentType contentType = CContentTypes.getContentType(project, fullPathToFile);
@ -341,25 +340,36 @@ public class LanguageManager {
* * TODO: implement other mapping levels besides project level and content type level * * TODO: implement other mapping levels besides project level and content type level
*/ */
public ILanguage getLanguageForFile(IPath pathToFile, IProject project) throws CoreException { public ILanguage getLanguageForFile(IPath pathToFile, IProject project) throws CoreException {
IResource resource = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(pathToFile); return getLanguageForFile(pathToFile, project, null);
}
IContentType contentType = CContentTypes.getContentType(project, pathToFile.toString()); /**
* @since 4.0
if(contentType == null) * @return an ILanguage representing the language to be used for the given file
{ * @param pathToFile the path to the file for which the language is requested.
return null; * The path can be either workspace or project relative.
* @param project the project that this file should be parsed in context of. This field is optional and may
* be set to null. If the project is null then this method tries to determine the project context via workspace APIs.
* @param contentTypeID id of the content type, may be <code>null</code>.
* @throws CoreException
* * TODO: implement other mapping levels besides project level and content type level
*/
public ILanguage getLanguageForFile(IPath pathToFile, IProject project, String contentTypeID) throws CoreException {
if (project == null) {
IResource resource = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(pathToFile);
if (resource == null) {
return null;
}
project= resource.getProject();
} }
if (contentTypeID==null) {
String contentTypeID = contentType.getId(); IContentType ct= CContentTypes.getContentType(project, pathToFile.toString());
if (ct == null) {
// if we don't have a project but have an IResource then we can infer the project return null;
if (project == null && resource != null) { }
project = ResourcesPlugin.getWorkspace().getRoot().getProject( contentTypeID= ct.getId();
pathToFile.segment(0));
} }
// TODO: other mappings would go here // TODO: other mappings would go here
// Project-level mappings // Project-level mappings
@ -383,23 +393,43 @@ public class LanguageManager {
* TODO: implement other mapping levels besides project level and content type level * TODO: implement other mapping levels besides project level and content type level
*/ */
public ILanguage getLanguageForFile(IFile file) throws CoreException { public ILanguage getLanguageForFile(IFile file) throws CoreException {
return getLanguageForFile(file, null);
}
/**
* @since 4.0
* @return an ILanguage representing the language to be used for the given file
* @param file the file for which the language is requested
* @param contentTypeID id of the content type, may be <code>null</code>.
* @throws CoreException
* TODO: implement other mapping levels besides project level and content type level
*/
public ILanguage getLanguageForFile(IFile file, String contentTypeId) throws CoreException {
IProject project = file.getProject(); IProject project = file.getProject();
IContentType contentType = CContentTypes.getContentType(project, file.getLocation().toString()); if (contentTypeId == null) {
IContentType contentType=
CContentTypes.getContentType(project, file.getLocation().toString());
if (contentType == null) {
return null;
}
contentTypeId= contentType.getId();
}
// TODO: other mappings would go here // TODO: other mappings would go here
// Project-level mappings // Project-level mappings
LanguageMappingConfiguration mappings = getLanguageMappingConfiguration(project); LanguageMappingConfiguration mappings = getLanguageMappingConfiguration(project);
if (mappings != null) { if (mappings != null) {
String id = (String) mappings.getProjectMappings().get(contentType.getId()); String id = (String) mappings.getProjectMappings().get(contentTypeId);
if (id != null) { if (id != null) {
return getLanguage(id); return getLanguage(id);
} }
} }
// Content type mappings // Content type mappings
return getLanguageForContentTypeID(contentType.getId()); return getLanguageForContentTypeID(contentTypeId);
} }
} }

View file

@ -59,9 +59,7 @@ import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException; import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.content.IContentType; import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.core.runtime.content.IContentTypeManager;
/** /**
* @see ITranslationUnit * @see ITranslationUnit
@ -70,7 +68,6 @@ public class TranslationUnit extends Openable implements ITranslationUnit {
private IPath location = null; private IPath location = null;
private String contentTypeId; private String contentTypeId;
private ILanguage language;
/** /**
* If set, this is the problem requestor which will be used to notify problems * If set, this is the problem requestor which will be used to notify problems
@ -683,23 +680,16 @@ public class TranslationUnit extends Openable implements ITranslationUnit {
} }
public ILanguage getLanguage() throws CoreException { public ILanguage getLanguage() throws CoreException {
ILanguage language = null; ILanguage language = null;
language = LanguageManager.getInstance().getLanguageForFile(getLocation(), getCProject().getProject());
return language;
}
private ILanguage computeLanguage(String contentTypeId) throws CoreException { IFile file= getFile();
// Look for the language extension registered against the if (file != null) {
// content type string language= LanguageManager.getInstance().getLanguageForFile(file, contentTypeId);
IContentTypeManager manager = Platform.getContentTypeManager();
IContentType contentType = manager.getContentType(contentTypeId);
if (contentType != null) {
return LanguageManager.getInstance().getLanguage(contentType);
} }
return null; else {
language = LanguageManager.getInstance().getLanguageForFile(getLocation(), getCProject().getProject(), contentTypeId);
}
return language;
} }
public String getContentTypeId() { public String getContentTypeId() {