mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-23 14:42:11 +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:
parent
d09a32444e
commit
f37e2ccc7d
2 changed files with 60 additions and 40 deletions
|
@ -302,8 +302,7 @@ public class LanguageManager {
|
|||
* TODO: implement other mapping levels besides project level and content type level
|
||||
*/
|
||||
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)");
|
||||
|
||||
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
|
||||
*/
|
||||
public ILanguage getLanguageForFile(IPath pathToFile, IProject project) throws CoreException {
|
||||
IResource resource = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(pathToFile);
|
||||
|
||||
|
||||
IContentType contentType = CContentTypes.getContentType(project, pathToFile.toString());
|
||||
|
||||
if(contentType == null)
|
||||
{
|
||||
return null;
|
||||
return getLanguageForFile(pathToFile, project, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 4.0
|
||||
* @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.
|
||||
* 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();
|
||||
}
|
||||
|
||||
String contentTypeID = contentType.getId();
|
||||
|
||||
// if we don't have a project but have an IResource then we can infer the project
|
||||
if (project == null && resource != null) {
|
||||
project = ResourcesPlugin.getWorkspace().getRoot().getProject(
|
||||
pathToFile.segment(0));
|
||||
|
||||
if (contentTypeID==null) {
|
||||
IContentType ct= CContentTypes.getContentType(project, pathToFile.toString());
|
||||
if (ct == null) {
|
||||
return null;
|
||||
}
|
||||
contentTypeID= ct.getId();
|
||||
}
|
||||
|
||||
|
||||
// TODO: other mappings would go here
|
||||
|
||||
// Project-level mappings
|
||||
|
@ -383,23 +393,43 @@ public class LanguageManager {
|
|||
* TODO: implement other mapping levels besides project level and content type level
|
||||
*/
|
||||
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();
|
||||
|
||||
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
|
||||
|
||||
// Project-level mappings
|
||||
LanguageMappingConfiguration mappings = getLanguageMappingConfiguration(project);
|
||||
if (mappings != null) {
|
||||
String id = (String) mappings.getProjectMappings().get(contentType.getId());
|
||||
String id = (String) mappings.getProjectMappings().get(contentTypeId);
|
||||
if (id != null) {
|
||||
return getLanguage(id);
|
||||
}
|
||||
}
|
||||
|
||||
// Content type mappings
|
||||
return getLanguageForContentTypeID(contentType.getId());
|
||||
return getLanguageForContentTypeID(contentTypeId);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -59,9 +59,7 @@ import org.eclipse.core.runtime.CoreException;
|
|||
import org.eclipse.core.runtime.IPath;
|
||||
import org.eclipse.core.runtime.IProgressMonitor;
|
||||
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.IContentTypeManager;
|
||||
|
||||
/**
|
||||
* @see ITranslationUnit
|
||||
|
@ -70,7 +68,6 @@ public class TranslationUnit extends Openable implements ITranslationUnit {
|
|||
|
||||
private IPath location = null;
|
||||
private String contentTypeId;
|
||||
private ILanguage language;
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
|
||||
ILanguage language = null;
|
||||
|
||||
language = LanguageManager.getInstance().getLanguageForFile(getLocation(), getCProject().getProject());
|
||||
|
||||
return language;
|
||||
}
|
||||
|
||||
private ILanguage computeLanguage(String contentTypeId) throws CoreException {
|
||||
// Look for the language extension registered against the
|
||||
// content type string
|
||||
IContentTypeManager manager = Platform.getContentTypeManager();
|
||||
IContentType contentType = manager.getContentType(contentTypeId);
|
||||
if (contentType != null) {
|
||||
return LanguageManager.getInstance().getLanguage(contentType);
|
||||
IFile file= getFile();
|
||||
if (file != null) {
|
||||
language= LanguageManager.getInstance().getLanguageForFile(file, contentTypeId);
|
||||
}
|
||||
return null;
|
||||
else {
|
||||
language = LanguageManager.getInstance().getLanguageForFile(getLocation(), getCProject().getProject(), contentTypeId);
|
||||
}
|
||||
return language;
|
||||
}
|
||||
|
||||
public String getContentTypeId() {
|
||||
|
|
Loading…
Add table
Reference in a new issue