1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-23 17:05:26 +02:00

Tentative fix for 177158: [Editor] Provide highlighting support for references to external SDK

This commit is contained in:
Anton Leherbauer 2007-04-06 12:02:02 +00:00
parent d358ef7a36
commit 774369a532
5 changed files with 218 additions and 6 deletions

View file

@ -121,5 +121,9 @@ label:
FUNCTION_MACRO(0);
if (un.unionField < st->structField) goto label;
problemMethod();
// external SDK
SDKClass sdkClass;
sdkClass.SDKMethod();
SDKFunction();
return 0;
}

View file

@ -10,10 +10,17 @@
*******************************************************************************/
package org.eclipse.cdt.ui.tests.text;
import java.io.File;
import java.net.URI;
import junit.extensions.TestSetup;
import junit.framework.Test;
import junit.framework.TestCase;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.BadPositionCategoryException;
@ -21,11 +28,24 @@ import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.source.SourceViewer;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.IPDOMManager;
import org.eclipse.cdt.core.index.IIndexLocationConverter;
import org.eclipse.cdt.core.index.ResourceContainerRelativeLocationConverter;
import org.eclipse.cdt.core.index.URIRelativeLocationConverter;
import org.eclipse.cdt.core.index.provider.IPDOMDescriptor;
import org.eclipse.cdt.core.index.provider.IReadOnlyPDOMProvider;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
import org.eclipse.cdt.core.testplugin.CProjectHelper;
import org.eclipse.cdt.core.testplugin.util.TestSourceReader;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.PreferenceConstants;
import org.eclipse.cdt.internal.core.CCoreInternals;
import org.eclipse.cdt.internal.core.index.provider.IndexProviderManager;
import org.eclipse.cdt.internal.core.index.provider.ReadOnlyPDOMProviderBridge;
import org.eclipse.cdt.internal.ui.editor.CEditor;
import org.eclipse.cdt.internal.ui.editor.SemanticHighlighting;
import org.eclipse.cdt.internal.ui.editor.SemanticHighlightingManager;
@ -51,14 +71,66 @@ public class AbstractSemanticHighlightingTest extends TestCase {
protected void setUp() throws Exception {
super.setUp();
String sdkCode=
"void SDKFunction();\n"+
"class SDKClass { public: SDKMethod(); };\n\n";
final File sdk= createExternalSDK(sdkCode);
fCProject= EditorTestHelper.createCProject(PROJECT, LINKED_FOLDER);
importExternalSDK(sdk, fCProject);
fEditor= (CEditor) EditorTestHelper.openInEditor(ResourceTestHelper.findFile(fTestFilename), true);
fSourceViewer= EditorTestHelper.getSourceViewer(fEditor);
assertTrue(EditorTestHelper.joinReconciler(fSourceViewer, 500, 10000, 100));
EditorTestHelper.joinBackgroundActivities();
}
private static void importExternalSDK(final File sdk, final ICProject associatedProject) {
final URI baseURI= new File("c:/ExternalSDK/").toURI();
IndexProviderManager ipm= CCoreInternals.getPDOMManager().getIndexProviderManager();
ipm.addIndexProvider(new ReadOnlyPDOMProviderBridge(
new IReadOnlyPDOMProvider() {
public IPDOMDescriptor[] getDescriptors(
ICConfigurationDescription config) {
return new IPDOMDescriptor[] {
new IPDOMDescriptor() {
public IIndexLocationConverter getIndexLocationConverter() {
return new URIRelativeLocationConverter(baseURI);
}
public IPath getLocation() {
return new Path(sdk.getAbsolutePath());
}
}
};
}
public boolean providesFor(ICProject project)
throws CoreException {
return associatedProject.equals(project);
}
}
));
}
private static File createExternalSDK(final String code) throws Exception {
final File sdk= File.createTempFile("foo", "bar");
ICProject cproject= CProjectHelper.createCCProject("foo"+System.currentTimeMillis(), null, IPDOMManager.ID_FAST_INDEXER);
TestSourceReader.createFile(cproject.getProject(), new Path("/this.h"), code);
CCorePlugin.getIndexManager().joinIndexer(5000, new NullProgressMonitor());
ResourceContainerRelativeLocationConverter cvr= new ResourceContainerRelativeLocationConverter(cproject.getProject());
CCoreInternals.getPDOMManager().exportProjectPDOM(cproject, sdk, cvr);
assertTrue(sdk.exists());
CProjectHelper.delete(cproject);
return sdk;
}
protected String getTestFilename() {
return fTestFilename;
}
@ -139,8 +211,6 @@ public class AbstractSemanticHighlightingTest extends TestCase {
protected void setUpSemanticHighlighting(String semanticHighlighting) {
fCurrentHighlighting= semanticHighlighting;
enableSemanticHighlighting(semanticHighlighting);
// EditorTestHelper.forceReconcile(fSourceViewer);
// assertTrue(EditorTestHelper.joinReconciler(fSourceViewer, 500, 10000, 500));
// give enough time to finish updating the highlighting positions
EditorTestHelper.runEventQueue(1000);
}

View file

@ -27,7 +27,7 @@ import org.eclipse.cdt.internal.ui.editor.SemanticHighlightings;
*/
public class SemanticHighlightingTest extends AbstractSemanticHighlightingTest {
private static final boolean PRINT_POSITIONS= false;
private static final boolean PRINT_POSITIONS= true;
private static final Class THIS= SemanticHighlightingTest.class;
@ -119,6 +119,7 @@ public class SemanticHighlightingTest extends AbstractSemanticHighlightingTest {
createPosition(113, 4, 32),
createPosition(114, 23, 9),
createPosition(118, 4, 15),
createPosition(125, 13, 9),
};
Position[] actual= getSemanticHighlightingPositions();
if (PRINT_POSITIONS) System.out.println(toString(actual));
@ -141,6 +142,7 @@ public class SemanticHighlightingTest extends AbstractSemanticHighlightingTest {
createPosition(109, 8, 8),
createPosition(114, 15, 2),
createPosition(116, 13, 2),
createPosition(124, 13, 8),
};
Position[] actual= getSemanticHighlightingPositions();
if (PRINT_POSITIONS) System.out.println(toString(actual));
@ -155,6 +157,7 @@ public class SemanticHighlightingTest extends AbstractSemanticHighlightingTest {
createPosition(117, 4, 2),
createPosition(121, 8, 2),
createPosition(121, 24, 2),
createPosition(125, 4, 8),
};
Position[] actual= getSemanticHighlightingPositions();
if (PRINT_POSITIONS) System.out.println(toString(actual));
@ -241,6 +244,7 @@ public class SemanticHighlightingTest extends AbstractSemanticHighlightingTest {
createPosition(113, 4, 14),
createPosition(114, 4, 9),
createPosition(116, 4, 8),
createPosition(124, 4, 8),
};
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
@ -269,6 +273,7 @@ public class SemanticHighlightingTest extends AbstractSemanticHighlightingTest {
createPosition(30, 8, 10),
createPosition(98, 8, 13),
createPosition(99, 1, 16),
createPosition(126, 4, 11),
};
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
@ -372,4 +377,15 @@ public class SemanticHighlightingTest extends AbstractSemanticHighlightingTest {
assertEqualPositions(expected, actual);
}
public void testExternalSDKHighlighting() throws Exception {
setUpSemanticHighlighting(SemanticHighlightings.EXTERNAL_SDK);
Position[] actual= getSemanticHighlightingPositions();
Position[] expected= new Position[] {
createPosition(125, 13, 9),
createPosition(126, 4, 11),
};
if (PRINT_POSITIONS) System.out.println(toString(actual));
assertEqualPositions(expected, actual);
}
}

View file

@ -165,6 +165,7 @@ SemanticHighlighting_typeDef= Typedefs
SemanticHighlighting_namespace= Namespaces
SemanticHighlighting_label= Labels
SemanticHighlighting_problem= Problems
SemanticHighlighting_externalSDK= External SDK calls
ToggleInsertMode.label=Sma&rt Insert Mode
ToggleInsertMode.tooltip=Toggle Smart Insert Mode

View file

@ -12,6 +12,7 @@
package org.eclipse.cdt.internal.ui.editor;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferenceConverter;
@ -47,6 +48,10 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunctionScope;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespace;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateParameter;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.index.IIndexBinding;
import org.eclipse.cdt.core.index.IIndexFile;
import org.eclipse.cdt.core.index.IIndexName;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.PreferenceConstants;
@ -165,6 +170,11 @@ public class SemanticHighlightings {
*/
public static final String PROBLEM="problem"; //$NON-NLS-1$
/**
* A named preference part that controls the highlighting of external SDK.
*/
public static final String EXTERNAL_SDK="externalSDK"; //$NON-NLS-1$
/** Init debugging mode */
private static final boolean DEBUG= "true".equalsIgnoreCase(Platform.getDebugOption("org.eclipse.cdt.ui/debug/SemanticHighlighting")); //$NON-NLS-1$//$NON-NLS-2$
@ -680,9 +690,12 @@ public class SemanticHighlightings {
public boolean consumes(SemanticToken token) {
IASTNode node= token.getNode();
if (node instanceof IASTName) {
IASTName name= (IASTName)node;
if (name instanceof ICPPASTQualifiedName && name.isReference()) {
return false;
}
IBinding binding= token.getBinding();
if (binding instanceof IFunction
&& !(binding instanceof ICPPMethod)) {
if (binding instanceof IFunction && !(binding instanceof ICPPMethod)) {
return true;
}
}
@ -1610,6 +1623,113 @@ public class SemanticHighlightings {
}
}
/**
* Semantic highlighting for external SDK references.
*/
private static final class ExternalSDKHighlighting extends SemanticHighlighting {
/*
* @see org.eclipse.cdt.internal.ui.editor.SemanticHighlighting#getPreferenceKey()
*/
public String getPreferenceKey() {
return EXTERNAL_SDK;
}
/*
* @see org.eclipse.cdt.internal.ui.editor.SemanticHighlighting#getDefaultTextColor()
*/
public RGB getDefaultTextColor() {
return new RGB(180, 0, 180);
}
/*
* @see org.eclipse.cdt.internal.ui.editor.SemanticHighlighting#getDefaultTextStyleBold()
*/
public boolean isBoldByDefault() {
return true;
}
/*
* @see org.eclipse.cdt.internal.ui.editor.SemanticHighlighting#isStrikethroughByDefault()
*/
public boolean isStrikethroughByDefault() {
return false;
}
/*
* @see org.eclipse.cdt.internal.ui.editor.SemanticHighlighting#isItalicByDefault()
*/
public boolean isItalicByDefault() {
return false;
}
/*
* @see org.eclipse.cdt.internal.ui.editor.SemanticHighlighting#isEnabledByDefault()
*/
public boolean isEnabledByDefault() {
return true;
}
/*
* @see org.eclipse.cdt.internal.ui.editor.SemanticHighlighting#getDisplayName()
*/
public String getDisplayName() {
return CEditorMessages.getString("SemanticHighlighting_externalSDK"); //$NON-NLS-1$
}
/*
* @see org.eclipse.cdt.internal.ui.editor.SemanticHighlighting#consumes(org.eclipse.cdt.internal.ui.editor.SemanticToken)
*/
public boolean consumes(SemanticToken token) {
IASTNode node= token.getNode();
if (node instanceof IASTName) {
IASTName name= (IASTName)node;
if (name instanceof ICPPASTQualifiedName) {
return false;
}
if (name.isReference()) {
IBinding binding= token.getBinding();
IIndex index= token.getRoot().getIndex();
return isExternalSDKReference(binding, index);
}
}
return false;
}
private boolean isExternalSDKReference(IBinding binding, IIndex index) {
if (binding instanceof IIndexBinding && binding instanceof IFunction) {
// unwrap binding from composite binding
// IIndexBinding binding2= (IIndexBinding)binding.getAdapter(IIndexBinding.class);
// if (binding2 != null) {
// binding= binding2;
// }
try {
IIndexName[] defs= index.findDefinitions(binding);
for (int i = 0; i < defs.length; i++) {
IIndexFile indexFile= defs[i].getFile();
if (indexFile != null && indexFile.getLocation().getFullPath() != null) {
return false;
}
}
IIndexName[] decls= index.findDeclarations(binding);
for (int i = 0; i < defs.length; i++) {
IIndexFile indexFile= decls[i].getFile();
if (indexFile != null && indexFile.getLocation().getFullPath() != null) {
return false;
}
}
if (defs.length != 0 || decls.length != 0) {
return true;
}
} catch (CoreException exc) {
CUIPlugin.getDefault().log(exc.getStatus());
return false;
}
}
return false;
}
}
/**
* A named preference that controls the given semantic highlighting's color.
*
@ -1678,6 +1798,7 @@ public class SemanticHighlightings {
fgSemanticHighlightings= new SemanticHighlighting[] {
new MacroReferenceHighlighting(), // before all others!
new ProblemHighlighting(),
new ExternalSDKHighlighting(),
new ClassHighlighting(),
new StaticFieldHighlighting(),
new FieldHighlighting(), // after all other fields