From 93af811c6d1dc295c569b5fbddf56ea26f7dda55 Mon Sep 17 00:00:00 2001 From: Nathan Ridge Date: Sat, 30 Jan 2016 01:31:12 -0500 Subject: [PATCH] Bug 486683 - Highlighting for context-sensitive keyword inside macro expansion Change-Id: I28c729dffcffd6a69f4adcc94d22d789ebb00b11 --- .../tests/text/SemanticHighlightingTest.java | 18 +++++++ .../SemanticHighlightingReconciler.java | 52 ++++++++++--------- 2 files changed, 45 insertions(+), 25 deletions(-) diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/SemanticHighlightingTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/SemanticHighlightingTest.java index a505d91f0af..3caf4732889 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/SemanticHighlightingTest.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/SemanticHighlightingTest.java @@ -516,4 +516,22 @@ public class SemanticHighlightingTest extends TestCase { public void testMethodNameInsideMacro_486682() throws Exception { makeAssertions(); } + + // #define WALDO(name) const char* Name() override { return name; } //$macroDefinition + // class S { //$class + // WALDO("name") //$macroSubstitution + // }; + public void testOverrideInMacroExpansion_486683a() throws Exception { + // This tests that the 'override' does not cause the entire invocation + // to be colored with the keyword highlighting. + makeAssertions(); + } + + // #define MIRROR(arg) arg //$macroDefinition + // MIRROR(class S { void foo() override; }) //$macroSubstitution,class,methodDeclaration,c_keyword + public void testOverrideInMacroExpansion_486683b() throws Exception { + // This tests that the 'override' *does* cause the 'override' keyword + // in the argument to be colored with the keyword highlighting. + makeAssertions(); + } } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/SemanticHighlightingReconciler.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/SemanticHighlightingReconciler.java index 8f943861a8d..5dd9ff4a3a5 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/SemanticHighlightingReconciler.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/SemanticHighlightingReconciler.java @@ -47,6 +47,7 @@ import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.core.model.ILanguage; import org.eclipse.cdt.ui.CUIPlugin; +import org.eclipse.cdt.internal.core.dom.parser.ASTNode; import org.eclipse.cdt.internal.core.model.ASTCache; import org.eclipse.cdt.internal.ui.editor.SemanticHighlightingManager.HighlightedPosition; @@ -179,10 +180,9 @@ public class SemanticHighlightingReconciler implements ICReconcilingListener { for (int i= 0, n= fJobSemanticHighlightings.length; i < n; ++i) { SemanticHighlighting semanticHighlighting= fJobSemanticHighlightings[i]; if (fJobHighlightings[i].isEnabled() && semanticHighlighting.consumes(fToken)) { - if (node instanceof IASTName) { - addNameLocation((IASTName) node, fJobHighlightings[i]); - } else { - addNodeLocation(node.getFileLocation(), fJobHighlightings[i]); + IASTNodeLocation location = getLocationToHighlight(node); + if (location != null) { + highlightLocation(location, fJobHighlightings[i]); } consumed= true; break; @@ -193,40 +193,42 @@ public class SemanticHighlightingReconciler implements ICReconcilingListener { } /** - * Add the a location range for the given name. + * Gets the location to highlight for a given node. * - * @param name The name - * @param highlighting The highlighting + * @param node the node */ - private void addNameLocation(IASTName name, HighlightingStyle highlightingStyle) { - IASTImageLocation imageLocation= name.getImageLocation(); + private IASTNodeLocation getLocationToHighlight(IASTNode node) { + IASTImageLocation imageLocation = null; + if (node instanceof IASTName) { + imageLocation = ((IASTName) node).getImageLocation(); + } else if (node instanceof ASTNode) { + // Non-names can still have image locations, they're just not exposed in IASTNode. + imageLocation = ((ASTNode) node).getImageLocation(); + } if (imageLocation != null) { if (imageLocation.getLocationKind() != IASTImageLocation.MACRO_DEFINITION) { - int offset= imageLocation.getNodeOffset(); - int length= imageLocation.getNodeLength(); - if (offset >= 0 && length > 0) { - addPosition(offset, length, highlightingStyle); - } + return imageLocation; } } else { // Fallback in case no image location available. - IASTNodeLocation[] nodeLocations= name.getNodeLocations(); - if (nodeLocations.length == 1 && !(nodeLocations[0] instanceof IASTMacroExpansionLocation)) { - addNodeLocation(nodeLocations[0], highlightingStyle); + IASTNodeLocation[] nodeLocations= node.getNodeLocations(); + if (nodeLocations.length == 1) { + IASTNodeLocation nodeLocation = nodeLocations[0]; + if (!(nodeLocation instanceof IASTMacroExpansionLocation)) { + return nodeLocation; + } } } + return null; } - + /** - * Adds the a location range for the given highlighting. + * Highlights the given node location with the given highlighting. * - * @param nodeLocation The node location - * @param highlighting The highlighting + * @param nodeLocation the node location to highlight + * @param highlightingStyle the highlighting style to apply */ - private void addNodeLocation(IASTNodeLocation nodeLocation, HighlightingStyle highlightingStyle) { - if (nodeLocation == null) { - return; - } + private void highlightLocation(IASTNodeLocation nodeLocation, HighlightingStyle highlightingStyle) { int offset= nodeLocation.getNodeOffset(); int length= nodeLocation.getNodeLength(); if (offset > -1 && length > 0) {