mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-23 22:52:11 +02:00
Bug 486683 - Highlighting for context-sensitive keyword inside macro expansion
Change-Id: I28c729dffcffd6a69f4adcc94d22d789ebb00b11
This commit is contained in:
parent
a201468432
commit
93af811c6d
2 changed files with 45 additions and 25 deletions
|
@ -516,4 +516,22 @@ public class SemanticHighlightingTest extends TestCase {
|
||||||
public void testMethodNameInsideMacro_486682() throws Exception {
|
public void testMethodNameInsideMacro_486682() throws Exception {
|
||||||
makeAssertions();
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,6 +47,7 @@ import org.eclipse.cdt.core.model.ICElement;
|
||||||
import org.eclipse.cdt.core.model.ILanguage;
|
import org.eclipse.cdt.core.model.ILanguage;
|
||||||
import org.eclipse.cdt.ui.CUIPlugin;
|
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.core.model.ASTCache;
|
||||||
|
|
||||||
import org.eclipse.cdt.internal.ui.editor.SemanticHighlightingManager.HighlightedPosition;
|
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) {
|
for (int i= 0, n= fJobSemanticHighlightings.length; i < n; ++i) {
|
||||||
SemanticHighlighting semanticHighlighting= fJobSemanticHighlightings[i];
|
SemanticHighlighting semanticHighlighting= fJobSemanticHighlightings[i];
|
||||||
if (fJobHighlightings[i].isEnabled() && semanticHighlighting.consumes(fToken)) {
|
if (fJobHighlightings[i].isEnabled() && semanticHighlighting.consumes(fToken)) {
|
||||||
if (node instanceof IASTName) {
|
IASTNodeLocation location = getLocationToHighlight(node);
|
||||||
addNameLocation((IASTName) node, fJobHighlightings[i]);
|
if (location != null) {
|
||||||
} else {
|
highlightLocation(location, fJobHighlightings[i]);
|
||||||
addNodeLocation(node.getFileLocation(), fJobHighlightings[i]);
|
|
||||||
}
|
}
|
||||||
consumed= true;
|
consumed= true;
|
||||||
break;
|
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 node the node
|
||||||
* @param highlighting The highlighting
|
|
||||||
*/
|
*/
|
||||||
private void addNameLocation(IASTName name, HighlightingStyle highlightingStyle) {
|
private IASTNodeLocation getLocationToHighlight(IASTNode node) {
|
||||||
IASTImageLocation imageLocation= name.getImageLocation();
|
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 != null) {
|
||||||
if (imageLocation.getLocationKind() != IASTImageLocation.MACRO_DEFINITION) {
|
if (imageLocation.getLocationKind() != IASTImageLocation.MACRO_DEFINITION) {
|
||||||
int offset= imageLocation.getNodeOffset();
|
return imageLocation;
|
||||||
int length= imageLocation.getNodeLength();
|
|
||||||
if (offset >= 0 && length > 0) {
|
|
||||||
addPosition(offset, length, highlightingStyle);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Fallback in case no image location available.
|
// Fallback in case no image location available.
|
||||||
IASTNodeLocation[] nodeLocations= name.getNodeLocations();
|
IASTNodeLocation[] nodeLocations= node.getNodeLocations();
|
||||||
if (nodeLocations.length == 1 && !(nodeLocations[0] instanceof IASTMacroExpansionLocation)) {
|
if (nodeLocations.length == 1) {
|
||||||
addNodeLocation(nodeLocations[0], highlightingStyle);
|
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 nodeLocation the node location to highlight
|
||||||
* @param highlighting The highlighting
|
* @param highlightingStyle the highlighting style to apply
|
||||||
*/
|
*/
|
||||||
private void addNodeLocation(IASTNodeLocation nodeLocation, HighlightingStyle highlightingStyle) {
|
private void highlightLocation(IASTNodeLocation nodeLocation, HighlightingStyle highlightingStyle) {
|
||||||
if (nodeLocation == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
int offset= nodeLocation.getNodeOffset();
|
int offset= nodeLocation.getNodeOffset();
|
||||||
int length= nodeLocation.getNodeLength();
|
int length= nodeLocation.getNodeLength();
|
||||||
if (offset > -1 && length > 0) {
|
if (offset > -1 && length > 0) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue