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

initial checkin as a part of work for 151850 (allow user to specify which parser/language to parse a given content type with)

This commit is contained in:
Chris Recoskie 2007-02-06 21:57:39 +00:00
parent 167425f50a
commit cc7e0a31f7
23 changed files with 828 additions and 37 deletions

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2006 Symbian Software Systems and others.
* Copyright (c) 2006, 2007 Symbian Software Systems and others.
* 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
@ -7,6 +7,7 @@
*
* Contributors:
* Andrew Ferguson (Symbian) - Initial implementation
* IBM Corporation
*******************************************************************************/
package org.eclipse.cdt.internal.pdom.tests;
@ -27,12 +28,15 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.ILanguage;
import org.eclipse.cdt.core.model.LanguageManager;
import org.eclipse.cdt.core.testplugin.CTestPlugin;
import org.eclipse.cdt.core.testplugin.util.TestSourceReader;
import org.eclipse.cdt.internal.core.CCoreInternals;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPVisitor;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.osgi.framework.Bundle;
@ -82,7 +86,18 @@ public abstract class IndexBindingResolutionTestBase extends PDOMTestBase {
}
protected IBinding getBindingFromASTName(String section, int len) {
IASTName[] names= ast.getLanguage().getSelectedNames(ast, testData[1].indexOf(section), len);
// get the language from the language manager
ILanguage language = null;
try {
language = LanguageManager.getInstance().getLanguageForFile(ast.getFilePath(), cproject.getProject());
} catch (CoreException e) {
fail("Unexpected exception while getting language for file.");
}
assertNotNull("No language for file " + ast.getFilePath().toString(), language);
IASTName[] names= language.getSelectedNames(ast, testData[1].indexOf(section), len);
assertEquals("<>1 name found for \""+section+"\"", 1, names.length);
IBinding binding = names[0].resolveBinding();
assertNotNull("No binding for "+names[0].getRawSignature(), binding);
@ -91,7 +106,17 @@ public abstract class IndexBindingResolutionTestBase extends PDOMTestBase {
}
protected IBinding getProblemFromASTName(String section, int len) {
IASTName[] names= ast.getLanguage().getSelectedNames(ast, testData[1].indexOf(section), len);
// get the language from the language manager
ILanguage language = null;
try {
language = LanguageManager.getInstance().getLanguageForFile(ast.getFilePath(), cproject.getProject());
} catch (CoreException e) {
fail("Unexpected exception while getting language for file.");
}
assertNotNull("No language for file " + ast.getFilePath().toString(), language);
IASTName[] names= language.getSelectedNames(ast, testData[1].indexOf(section), len);
assertEquals("<>1 name found for \""+section+"\"", 1, names.length);
IBinding binding = names[0].resolveBinding();
assertNotNull("No binding for "+names[0].getRawSignature(), binding);

View file

@ -32,6 +32,7 @@ Export-Package: org.eclipse.cdt.core,
org.eclipse.cdt.internal.core.dom.parser.c;x-friends:="org.eclipse.cdt.refactoring",
org.eclipse.cdt.internal.core.dom.parser.cpp;x-friends:="org.eclipse.cdt.ui,org.eclipse.cdt.refactoring",
org.eclipse.cdt.internal.core.index;x-internal:=true,
org.eclipse.cdt.internal.core.language;x-friends:="org.eclipse.cdt.ui",
org.eclipse.cdt.internal.core.model;x-friends:="org.eclipse.cdt.ui,org.eclipse.cdt.refactoring",
org.eclipse.cdt.internal.core.model.ext;x-friends:="org.eclipse.cdt.ui",
org.eclipse.cdt.internal.core.parser;x-internal:=true,

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2005, 2006 QNX Software Systems and others.
* Copyright (c) 2005, 2007 QNX Software Systems and others.
* 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
@ -62,6 +62,12 @@ public interface ILanguage extends IAdaptable {
*/
public String getId();
/**
* @return the human readable name corresponding to this language, suitable for display.
* @since 4.0
*/
public String getName();
/**
* @deprecated use {@link ITranslationUnit#getAST()}.
*/

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2005, 2006 QNX Software Systems and others.
* Copyright (c) 2005, 2007 QNX Software Systems and others.
* 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
@ -15,15 +15,24 @@ package org.eclipse.cdt.core.model;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.ILinkage;
import org.eclipse.cdt.internal.core.CContentTypes;
import org.eclipse.cdt.internal.core.language.LanguageMappingConfiguration;
import org.eclipse.cdt.internal.core.language.LanguageMappingStore;
import org.eclipse.cdt.internal.core.model.TranslationUnit;
import org.eclipse.cdt.internal.core.pdom.dom.IPDOMLinkageFactory;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.ISafeRunnable;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.SafeRunner;
@ -47,6 +56,8 @@ public class LanguageManager {
private Map fLanguageCache = new HashMap();
private Map fPDOMLinkageFactoryCache= new HashMap();
private Map fContentTypeToLanguageCache= new HashMap();
private Map fLanguageConfigurationCache = new HashMap();
private boolean fIsFullyCached;
public static LanguageManager getInstance() {
if (instance == null)
@ -95,6 +106,8 @@ public class LanguageManager {
}
private ILanguage getLanguageForContentTypeID(String contentTypeID) {
cacheAllLanguages();
ILanguage language = (ILanguage)fContentTypeToLanguageCache.get(contentTypeID);
if (language != null || fContentTypeToLanguageCache.containsKey(contentTypeID))
return language;
@ -224,4 +237,169 @@ public class LanguageManager {
}
return result[0];
}
public ILanguage[] getRegisteredLanguages() {
cacheAllLanguages();
ILanguage[] languages = new ILanguage[fLanguageCache.size()];
Iterator values = fLanguageCache.values().iterator();
for (int i = 0; values.hasNext(); i++) {
languages[i] = (ILanguage) values.next();
}
return languages;
}
private void cacheAllLanguages() {
if (fIsFullyCached) {
return;
}
IConfigurationElement[] configs= Platform.getExtensionRegistry().getConfigurationElementsFor(LANGUAGE_EXTENSION_POINT_ID);
for (int j = 0; j < configs.length; ++j) {
final IConfigurationElement languageElem = configs[j];
if (ELEMENT_LANGUAGE.equals(languageElem.getName())) {
String langId = getLanguageID(languageElem);
final ILanguage[] result= new ILanguage[]{null};
SafeRunner.run(new ISafeRunnable(){
public void handleException(Throwable exception) {
CCorePlugin.log(exception);
}
public void run() throws Exception {
result[0]= (ILanguage)languageElem.createExecutableExtension(ATTRIBUTE_CLASS);
}
});
if (result[0] != null) {
fLanguageCache.put(langId, result[0]);
}
}
}
fIsFullyCached = true;
}
public LanguageMappingConfiguration getLanguageMappingConfiguration(IProject project) throws CoreException {
LanguageMappingConfiguration mappings = (LanguageMappingConfiguration) fLanguageConfigurationCache.get(project);
if (mappings != null) {
return mappings;
}
LanguageMappingStore store = new LanguageMappingStore(project);
mappings = store.decodeMappings();
fLanguageConfigurationCache.put(project, mappings);
return mappings;
}
public void storeLanguageMappingConfiguration(IProject project) throws CoreException {
LanguageMappingConfiguration mappings = (LanguageMappingConfiguration) fLanguageConfigurationCache.get(project);
LanguageMappingStore store = new LanguageMappingStore(project);
store.storeMappings(mappings);
}
/**
* @since 4.0
* @return an ILanguage representing the language to be used for the given file
* @param fullPathToFile the full path to the file for which the language is requested
* @param project the IProject that this file is in the context of. This field cannot be null.
* @throws CoreException
* TODO: implement other mapping levels besides project level and content type level
*/
public ILanguage getLanguageForFile(String fullPathToFile, IProject project) throws CoreException {
if(project == null)
throw new IllegalArgumentException("project must not be null in call to LanguageManager.getLanguageForFile(String, IProject)");
IContentType contentType = CContentTypes.getContentType(project, fullPathToFile);
if(contentType == null)
{
return null;
}
String 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);
if (id != null) {
return getLanguage(id);
}
}
// Content type mappings
return getLanguageForContentTypeID(contentTypeID);
}
/**
* @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.
* @throws CoreException
* * 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;
}
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));
}
// TODO: other mappings would go here
// Project-level mappings
LanguageMappingConfiguration mappings = getLanguageMappingConfiguration(project);
if (mappings != null) {
String id = (String) mappings.getProjectMappings().get(contentType);
if (id != null) {
return getLanguage(id);
}
}
// Content type mappings
return getLanguageForContentTypeID(contentTypeID);
}
/**
* @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
* @throws CoreException
* TODO: implement other mapping levels besides project level and content type level
*/
public ILanguage getLanguageForFile(IFile file) throws CoreException {
IProject project = file.getProject();
IContentType contentType = CContentTypes.getContentType(project, file.getLocation().toString());
// TODO: other mappings would go here
// Project-level mappings
LanguageMappingConfiguration mappings = getLanguageMappingConfiguration(project);
if (mappings != null) {
String id = (String) mappings.getProjectMappings().get(contentType.getId());
if (id != null) {
return getLanguage(id);
}
}
// Content type mappings
return getLanguageForContentTypeID(contentType.getId());
}
}

View file

@ -0,0 +1,41 @@
/*******************************************************************************
* Copyright (c) 2007 IBM Corporation and others.
* 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
*
* Contributors:
* IBM Corporation - Initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.core.language;
import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;
public class LanguageMappingConfiguration {
private Map fProjectMappings;
public LanguageMappingConfiguration() {
fProjectMappings = new TreeMap();
}
public Map getProjectMappings() {
return Collections.unmodifiableMap(fProjectMappings);
}
public void setProjectMappings(Map projectMappings) {
fProjectMappings = projectMappings;
}
public void addProjectMapping(String contentType, String language) {
fProjectMappings.put(contentType, language);
}
public void removeProjectMapping(String contentType) {
fProjectMappings.remove(contentType);
}
}

View file

@ -0,0 +1,104 @@
/*******************************************************************************
* Copyright (c) 2007 IBM Corporation and others.
* 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
*
* Contributors:
* IBM Corporation - Initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.core.language;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import java.util.Map.Entry;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.ICDescriptor;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class LanguageMappingStore {
private static final String LANGUAGE_MAPPING_ID = "org.eclipse.cdt.core.language.mapping";
private static final String PROJECT_MAPPINGS = "project-mappings";
private static final String PROJECT_MAPPING = "project-mapping";
private static final String ATTRIBUTE_CONTENT_TYPE = "content-type";
private static final String ATTRIBUTE_LANGUAGE = "language";
private IProject fProject;
public LanguageMappingStore(IProject project) {
fProject = project;
}
public LanguageMappingConfiguration decodeMappings() throws CoreException {
LanguageMappingConfiguration config = new LanguageMappingConfiguration();
ICDescriptor descriptor = getProjectDescription();
Element rootElement = descriptor.getProjectData(LANGUAGE_MAPPING_ID);
if (rootElement == null) {
return config;
}
config.setProjectMappings(decodeProjectMappings(rootElement));
return config;
}
protected ICDescriptor getProjectDescription() throws CoreException {
return CCorePlugin.getDefault().getCProjectDescription(fProject, true);
}
private Map decodeProjectMappings(Element rootElement) throws CoreException {
Map decodedMappings = new TreeMap();
NodeList elements = rootElement.getElementsByTagName(PROJECT_MAPPINGS);
for (int i = 0; i < elements.getLength(); i++) {
Element projectMappings = (Element) elements.item(i);
NodeList mappingElements = projectMappings.getElementsByTagName(PROJECT_MAPPING);
for (int j = 0; j < mappingElements.getLength(); j++) {
Element mapping = (Element) mappingElements.item(j);
String contentType = mapping.getAttribute(ATTRIBUTE_CONTENT_TYPE);
String language = mapping.getAttribute(ATTRIBUTE_LANGUAGE);
decodedMappings.put(contentType, language);
}
}
return decodedMappings;
}
public void storeMappings(LanguageMappingConfiguration config) throws CoreException {
ICDescriptor descriptor = getProjectDescription();
Element rootElement = descriptor.getProjectData(LANGUAGE_MAPPING_ID);
clearChildren(rootElement);
addProjectMappings(config.getProjectMappings(), rootElement);
descriptor.saveProjectData();
}
private void clearChildren(Element element) {
Node child = element.getFirstChild();
while (child != null) {
element.removeChild(child);
child = element.getFirstChild();
}
}
private void addProjectMappings(Map mappings, Element rootElement) {
Document document = rootElement.getOwnerDocument();
Element projectMappings = document.createElement(PROJECT_MAPPINGS);
Iterator entries = mappings.entrySet().iterator();
while (entries.hasNext()) {
Entry entry = (Entry) entries.next();
Element mapping = document.createElement(PROJECT_MAPPING);
mapping.setAttribute(ATTRIBUTE_CONTENT_TYPE, (String) entry.getKey());
mapping.setAttribute(ATTRIBUTE_LANGUAGE, (String) entry.getValue());
projectMappings.appendChild(mapping);
}
rootElement.appendChild(projectMappings);
}
}

View file

@ -683,10 +683,7 @@ public class TranslationUnit extends Openable implements ITranslationUnit {
}
public ILanguage getLanguage() throws CoreException {
if (language == null) {
language = computeLanguage(contentTypeId);
}
ILanguage language = LanguageManager.getInstance().getLanguageForFile(getFile());
return language;
}

View file

@ -235,12 +235,5 @@ public interface IASTTranslationUnit extends IASTNode {
* @param index
*/
public void setIndex(IIndex index);
/**
* Returns the language for this translation unit.
*
* @return language for this translation unit
*/
public ILanguage getLanguage();
}

View file

@ -0,0 +1,22 @@
package org.eclipse.cdt.core.dom.ast.gnu;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
public class Messages {
private static final String BUNDLE_NAME = "org.eclipse.cdt.core.dom.ast.gnu.messages"; //$NON-NLS-1$
private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle
.getBundle(BUNDLE_NAME);
private Messages() {
}
public static String getString(String key) {
try {
return RESOURCE_BUNDLE.getString(key);
} catch (MissingResourceException e) {
return '!' + key + '!';
}
}
}

View file

@ -23,6 +23,7 @@ import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.c.CASTVisitor;
import org.eclipse.cdt.core.dom.ast.gnu.Messages;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.model.AbstractLanguage;
import org.eclipse.cdt.core.model.IContributedModelBuilder;
@ -65,6 +66,10 @@ public class GCCLanguage extends AbstractLanguage {
return ID;
}
public String getName() {
return new String(Messages.getString("GCCLanguage.name")); //$NON-NLS-1$
}
public Object getAdapter(Class adapter) {
if (adapter == IPDOMLinkageFactory.class)
return new PDOMCLinkageFactory();

View file

@ -23,6 +23,7 @@ import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor;
import org.eclipse.cdt.core.dom.ast.gnu.Messages;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.model.AbstractLanguage;
import org.eclipse.cdt.core.model.IContributedModelBuilder;
@ -64,6 +65,10 @@ public class GPPLanguage extends AbstractLanguage {
return ID;
}
public String getName() {
return new String(Messages.getString("GPPLanguage.name")); //$NON-NLS-1$
}
public Object getAdapter(Class adapter) {
if (adapter == IPDOMLinkageFactory.class)
return new PDOMCPPLinkageFactory();

View file

@ -0,0 +1,2 @@
GPPLanguage.name=C++ for G++ compiler
GCCLanguage.name=C for GCC compiler

View file

@ -43,6 +43,7 @@ import org.eclipse.cdt.core.dom.ast.c.ICASTDesignator;
import org.eclipse.cdt.core.dom.ast.gnu.c.GCCLanguage;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.model.ILanguage;
import org.eclipse.cdt.core.model.LanguageManager;
import org.eclipse.cdt.core.parser.ParserLanguage;
import org.eclipse.cdt.core.parser.ast.IASTEnumerator;
import org.eclipse.cdt.core.parser.util.ArrayUtil;
@ -573,13 +574,9 @@ public class CASTTranslationUnit extends CASTNode implements
}
public ParserLanguage getParserLanguage() {
return ParserLanguage.C;
return ParserLanguage.C;
}
public ILanguage getLanguage() {
// Assuming gnu C for now.
return new GCCLanguage();
}
public IIndex getIndex() {
return index;

View file

@ -57,6 +57,7 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPScope;
import org.eclipse.cdt.core.dom.ast.gnu.cpp.GPPLanguage;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.model.ILanguage;
import org.eclipse.cdt.core.model.LanguageManager;
import org.eclipse.cdt.core.parser.ParserLanguage;
import org.eclipse.cdt.core.parser.ast.IASTEnumerator;
import org.eclipse.cdt.core.parser.util.ArrayUtil;
@ -620,11 +621,6 @@ public class CPPASTTranslationUnit extends CPPASTNode implements
return ParserLanguage.CPP;
}
public ILanguage getLanguage() {
// Assuming gnu C++ for now.
return new GPPLanguage();
}
public IIndex getIndex() {
return index;
}

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2006 QNX Software Systems and others.
* Copyright (c) 2006, 2007 QNX Software Systems and others.
* 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
@ -76,10 +76,6 @@ class PDOMCLinkage extends PDOMLinkage {
public static final int CPARAMETER = PDOMLinkage.LAST_NODE_TYPE + 8;
public static final int CBASICTYPE = PDOMLinkage.LAST_NODE_TYPE + 9;
public ILanguage getLanguage() {
return new GCCLanguage();
}
public PDOMBinding addBinding(IBinding binding) throws CoreException {
PDOMBinding pdomBinding = adaptBinding(binding);
try {

View file

@ -96,10 +96,6 @@ class PDOMCPPLinkage extends PDOMLinkage {
public static final int CPP_CONSTRUCTOR= PDOMLinkage.LAST_NODE_TYPE + 14;
public static final int CPP_REFERENCE_TYPE= PDOMLinkage.LAST_NODE_TYPE + 15;
public ILanguage getLanguage() {
return new GPPLanguage();
}
public PDOMBinding addBinding(IASTName name) throws CoreException {
if (name == null || name instanceof ICPPASTQualifiedName)
return null;

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2006 Wind River Systems, Inc. and others.
* Copyright (c) 2006, 2007 Wind River Systems, Inc. and others.
* 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
@ -7,6 +7,7 @@
*
* Contributors:
* Markus Schorn - initial API and implementation
* IBM Corporation
*******************************************************************************/
package org.eclipse.cdt.ui.tests.text.selection;
@ -15,6 +16,7 @@ import junit.framework.Test;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.cdt.core.CCorePlugin;
@ -27,6 +29,8 @@ import org.eclipse.cdt.core.dom.ast.IVariable;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.ILanguage;
import org.eclipse.cdt.core.model.LanguageManager;
import org.eclipse.cdt.core.testplugin.CProjectHelper;
import org.eclipse.cdt.ui.tests.BaseUITestCase;
@ -58,7 +62,17 @@ public class ResolveBindingTests extends BaseUITestCase {
}
private IASTName getSelectedName(IASTTranslationUnit astTU, int offset, int len) {
IASTName[] names= astTU.getLanguage().getSelectedNames(astTU, offset, len);
// get the language from the language manager
ILanguage language = null;
try {
language = LanguageManager.getInstance().getLanguageForFile(astTU.getFilePath(), fCProject.getProject());
} catch (CoreException e) {
fail("Unexpected exception while getting language for file.");
}
assertNotNull("No language for file " + astTU.getFilePath().toString(), language);
IASTName[] names= language.getSelectedNames(astTU, offset, len);
assertEquals(1, names.length);
return names[0];
}

View file

@ -257,6 +257,8 @@ CDTHelpProperty.name=C/C++ Documentation
CDTFileTypesProperty.name=C/C++ File Types
CDTLanguagesProperty.name=Language Mappings
cDocumentFactory=C Document Factory
cDocumentSetupParticipant=C Document Setup Participant

View file

@ -1388,6 +1388,17 @@
</adapt>
</enabledWhen>
</page>
<page
adaptable="true"
class="org.eclipse.cdt.internal.ui.language.ProjectLanguageMappingPropertyPage"
id="org.eclipse.cdt.ui.projectLanguageMappings"
name="%CDTLanguagesProperty.name"
objectClass="org.eclipse.core.resources.IProject">
<filter
name="nature"
value="org.eclipse.cdt.core.cnature">
</filter>
</page>
</extension>
<extension
point="org.eclipse.cdt.ui.PathContainerPage">

View file

@ -0,0 +1,172 @@
/*******************************************************************************
* Copyright (c) 2007 IBM Corporation and others.
* 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
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.ui.language;
import java.util.Collections;
import java.util.HashMap;
import java.util.Set;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.content.IContentTypeManager;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.cdt.core.model.ILanguage;
import org.eclipse.cdt.core.model.LanguageManager;
import org.eclipse.cdt.internal.ui.preferences.PreferencesMessages;
public class ContentTypeMappingDialog extends Dialog {
Combo fContentType;
Combo fLanguage;
String fSelectedContentTypeName;
String fSelectedContentTypeID;
String fSelectedLanguageName;
String fSelectedLanguageID;
private Set fFilteredContentTypes;
private HashMap fContentTypeNamesToIDsMap;
private HashMap fLanguageNamesToIDsMap;
public ContentTypeMappingDialog(Shell parentShell) {
super(parentShell);
fFilteredContentTypes = Collections.EMPTY_SET;
fContentTypeNamesToIDsMap = new HashMap();
fLanguageNamesToIDsMap = new HashMap();
}
public String getSelectedContentTypeName() {
return fSelectedContentTypeName;
}
public String getContentTypeID() {
return fSelectedContentTypeID;
}
public String getSelectedLanguageName() {
return fSelectedLanguageName;
}
public String getLanguageID() {
return fSelectedLanguageID;
}
protected void configureShell(Shell newShell) {
super.configureShell(newShell);
newShell.setText(PreferencesMessages.ContentTypeMappingsDialog_title);
}
protected void createButtonsForButtonBar(Composite parent) {
createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
true);
createButton(parent, IDialogConstants.CANCEL_ID,
IDialogConstants.CANCEL_LABEL, false);
getButton(IDialogConstants.OK_ID).setEnabled(false);
}
private boolean isValidSelection() {
return fContentType.getSelectionIndex() != -1
&& fLanguage.getSelectionIndex() != -1;
}
protected Control createDialogArea(Composite parent) {
Composite area = new Composite(parent, SWT.NONE);
area.setLayout(new GridLayout(2, false));
Label contentTypeLabel = new Label(area, SWT.TRAIL);
contentTypeLabel
.setText(PreferencesMessages.ContentTypeMappingsDialog_contentType);
fContentType = new Combo(area, SWT.DROP_DOWN | SWT.READ_ONLY);
fContentType
.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
configureContentTypes(fContentType);
fContentType.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
fSelectedContentTypeName = fContentType.getText();
fSelectedContentTypeID = (String) fContentTypeNamesToIDsMap
.get(fSelectedContentTypeName);
getButton(IDialogConstants.OK_ID)
.setEnabled(isValidSelection());
}
});
Label languageLabel = new Label(area, SWT.TRAIL);
languageLabel
.setText(PreferencesMessages.ContentTypeMappingsDialog_language);
fLanguage = new Combo(area, SWT.DROP_DOWN | SWT.READ_ONLY);
fLanguage.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
fLanguage.setItems(getLanguages());
fLanguage.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
fSelectedLanguageName = fLanguage.getText();
fSelectedLanguageID = (String) fLanguageNamesToIDsMap
.get(fSelectedLanguageName);
getButton(IDialogConstants.OK_ID)
.setEnabled(isValidSelection());
}
});
return area;
}
private void configureContentTypes(Combo combo) {
combo.removeAll();
String[] contentTypesIDs = LanguageManager.getInstance()
.getRegisteredContentTypeIds();
IContentTypeManager contentTypeManager = Platform
.getContentTypeManager();
for (int i = 0; i < contentTypesIDs.length; i++) {
if (!fFilteredContentTypes.contains(contentTypesIDs[i])) {
String name = contentTypeManager.getContentType(
contentTypesIDs[i]).getName();
combo.add(name);
// keep track of what ID this name corresponds to so that when
// we setup the mapping
// later based upon user selection, we'll know what ID to use
fContentTypeNamesToIDsMap.put(name, contentTypesIDs[i]);
}
}
}
private String[] getLanguages() {
ILanguage[] languages = LanguageManager.getInstance()
.getRegisteredLanguages();
String[] descriptions = new String[languages.length];
for (int i = 0; i < descriptions.length; i++) {
descriptions[i] = languages[i].getName();
fLanguageNamesToIDsMap.put(descriptions[i], languages[i].getId());
}
return descriptions;
}
public void setContentTypeFilter(Set contentTypes) {
fFilteredContentTypes = contentTypes;
}
}

View file

@ -0,0 +1,209 @@
/*******************************************************************************
* Copyright (c) 2007 IBM Corporation and others.
* 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
*
* Contributors:
* IBM Corporation - Initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.ui.language;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.content.IContentTypeManager;
import org.eclipse.jface.layout.TableColumnAdapter;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.dialogs.PropertyPage;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.model.LanguageManager;
import org.eclipse.cdt.internal.core.language.LanguageMappingConfiguration;
import org.eclipse.cdt.internal.ui.preferences.PreferencesMessages;
public class ProjectLanguageMappingPropertyPage extends PropertyPage {
private static final int MINIMUM_COLUMN_WIDTH = 150;
private LanguageMappingConfiguration fMappings;
private Table fTable;
private HashMap fContentTypeNamesToIDsMap;
public ProjectLanguageMappingPropertyPage() {
super();
// keep a mapping of all registered content types and their names
fContentTypeNamesToIDsMap = new HashMap();
String[] contentTypesIDs = LanguageManager.getInstance()
.getRegisteredContentTypeIds();
IContentTypeManager contentTypeManager = Platform
.getContentTypeManager();
for (int i = 0; i < contentTypesIDs.length; i++) {
String name = contentTypeManager.getContentType(contentTypesIDs[i])
.getName();
// keep track of what ID this name corresponds to so that when we
// setup the mapping
// later based upon user selection, we'll know what ID to use
fContentTypeNamesToIDsMap.put(name, contentTypesIDs[i]);
}
}
/**
* @see PreferencePage#createContents(Composite)
*/
protected Control createContents(Composite parent) {
fetchMappings();
Composite composite = new Composite(parent, SWT.NONE);
composite
.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
composite.setLayout(new GridLayout(2, false));
Composite tableParent = new Composite(composite, SWT.NONE);
tableParent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
tableParent.setLayout(new FillLayout());
fTable = new Table(tableParent, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL);
fTable.setHeaderVisible(true);
fTable.setLinesVisible(true);
TableColumn contentTypeColumn = new TableColumn(fTable, SWT.LEAD);
contentTypeColumn
.setText(PreferencesMessages.ProjectLanguagesPropertyPage_contentTypeColumn);
TableColumn languageColumn = new TableColumn(fTable, SWT.LEAD);
languageColumn
.setText(PreferencesMessages.ProjectLanguagesPropertyPage_languageColumn);
TableColumnAdapter columnAdapter = new TableColumnAdapter(fTable);
columnAdapter.addColumnData(new ColumnWeightData(1,
MINIMUM_COLUMN_WIDTH, true));
columnAdapter.addColumnData(new ColumnWeightData(1,
MINIMUM_COLUMN_WIDTH, true));
tableParent.addControlListener(columnAdapter);
Composite buttons = new Composite(composite, SWT.NONE);
buttons.setLayout(new GridLayout());
buttons.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, false,
false));
Button addButton = new Button(buttons, SWT.PUSH);
addButton
.setText(PreferencesMessages.ProjectLanguagesPropertyPage_addMappingButton);
addButton.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
ContentTypeMappingDialog dialog = new ContentTypeMappingDialog(
getShell());
dialog.setContentTypeFilter(fMappings.getProjectMappings()
.keySet());
dialog.setBlockOnOpen(true);
if (dialog.open() == Window.OK) {
String contentType = dialog.getContentTypeID();
String language = dialog.getLanguageID();
fMappings.addProjectMapping(contentType, language);
refreshMappings();
}
}
});
Button removeButton = new Button(buttons, SWT.PUSH);
removeButton
.setText(PreferencesMessages.ProjectLanguagesPropertyPage_removeMappingButton);
removeButton.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
TableItem[] selection = fTable.getSelection();
for (int i = 0; i < selection.length; i++) {
fMappings
.removeProjectMapping((String) fContentTypeNamesToIDsMap
.get(selection[i].getText(0)));
}
refreshMappings();
}
});
refreshMappings();
return composite;
}
private void refreshMappings() {
fTable.removeAll();
Iterator mappings = fMappings.getProjectMappings().entrySet()
.iterator();
IContentTypeManager contentTypeManager = Platform
.getContentTypeManager();
while (mappings.hasNext()) {
Entry entry = (Entry) mappings.next();
TableItem item = new TableItem(fTable, SWT.NONE);
String contentTypeName = contentTypeManager.getContentType(
(String) entry.getKey()).getName();
String languageName = LanguageManager.getInstance().getLanguage(
(String) entry.getValue()).getName();
item.setText(0, contentTypeName);
item.setText(1, languageName);
}
}
private void fetchMappings() {
try {
fMappings = LanguageManager.getInstance()
.getLanguageMappingConfiguration(getProject());
} catch (CoreException e) {
CCorePlugin.log(e);
}
}
protected void performDefaults() {
fMappings = new LanguageMappingConfiguration();
}
public boolean performOk() {
try {
LanguageManager.getInstance().storeLanguageMappingConfiguration(
getProject());
return true;
} catch (CoreException e) {
CCorePlugin.log(e);
return false;
}
}
private IProject getProject() {
return (IProject) getElement().getAdapter(IProject.class);
}
}

View file

@ -193,6 +193,15 @@ public final class PreferencesMessages extends NLS {
public static String CodeFormatterPreferencePage_title;
public static String CodeFormatterPreferencePage_description;
public static String ProjectLanguagesPropertyPage_contentTypeColumn;
public static String ProjectLanguagesPropertyPage_languageColumn;
public static String ProjectLanguagesPropertyPage_addMappingButton;
public static String ProjectLanguagesPropertyPage_removeMappingButton;
public static String ContentTypeMappingsDialog_title;
public static String ContentTypeMappingsDialog_contentType;
public static String ContentTypeMappingsDialog_language;
static {
NLS.initializeMessages(BUNDLE_NAME, PreferencesMessages.class);
}

View file

@ -227,5 +227,15 @@ PathEntryVariablesBlock_removeVariableButton = &Remove
#Indexer
IndexerPrefs_description=Sets default Indexer Options for new Projects
# Language settings
ProjectLanguagesPropertyPage_contentTypeColumn = Content Type
ProjectLanguagesPropertyPage_languageColumn = Language
ProjectLanguagesPropertyPage_addMappingButton = &Add...
ProjectLanguagesPropertyPage_removeMappingButton = &Remove
ContentTypeMappingsDialog_title = Add Mapping
ContentTypeMappingsDialog_contentType = Content type
ContentTypeMappingsDialog_language = Language
# Others
ProposalFilterPreferencesUtil_defaultFilterName=<Default Filter>