1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-09-09 19:43:27 +02:00

Bug 344604 - Hyperlink on arguments inside macro function call underlines all the call

This commit is contained in:
Marc-Andre Laperle 2011-05-04 05:45:38 +00:00
parent 1b82834478
commit 343844689b
2 changed files with 32 additions and 8 deletions

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2000, 2010 IBM Corporation and others. * Copyright (c) 2000, 2011 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -65,9 +65,12 @@ public class HyperlinkTest extends TestCase {
" private: \n" + " private: \n" +
" int x, y; \n" + " int x, y; \n" +
"}; \n" + "}; \n" +
"#define SOMEMACROFUNCTION(a) function(a) \n" +
"int test(Point p) { \n" + "int test(Point p) { \n" +
" char* str = \"STRING LITERAL\"; \n" + " char* str = \"STRING LITERAL\"; \n" +
" p + p;" + " p + p;" +
" int arg;" +
" SOMEMACROFUNCTION(arg);" +
"} \n"; "} \n";
private static final String C_FILE_NAME_1 = "hyperlink_test_c_1.c"; private static final String C_FILE_NAME_1 = "hyperlink_test_c_1.c";
@ -162,6 +165,8 @@ public class HyperlinkTest extends TestCase {
// see bug 259015 // see bug 259015
assertHyperlink(CPP_CODE.indexOf("macro_token1"), CPP_CODE.indexOf("macro_token1"), "macro_token1".length()); assertHyperlink(CPP_CODE.indexOf("macro_token1"), CPP_CODE.indexOf("macro_token1"), "macro_token1".length());
assertHyperlink(CPP_CODE.indexOf("macro_token2"), CPP_CODE.indexOf("macro_token2"), "macro_token2".length()); assertHyperlink(CPP_CODE.indexOf("macro_token2"), CPP_CODE.indexOf("macro_token2"), "macro_token2".length());
// see bug 344604
assertHyperlink(CPP_CODE.indexOf("arg);"), CPP_CODE.indexOf("arg);"), "arg".length());
// no hyperlinks for comments // no hyperlinks for comments
assertNotHyperlink(CPP_CODE.indexOf("//") + 1); assertNotHyperlink(CPP_CODE.indexOf("//") + 1);
@ -216,6 +221,8 @@ public class HyperlinkTest extends TestCase {
// see bug 259015 // see bug 259015
assertHyperlink(CPP_CODE.indexOf("macro_token1"), CPP_CODE.indexOf("macro_token1"), "macro_token1".length()); assertHyperlink(CPP_CODE.indexOf("macro_token1"), CPP_CODE.indexOf("macro_token1"), "macro_token1".length());
assertHyperlink(CPP_CODE.indexOf("macro_token2"), CPP_CODE.indexOf("macro_token2"), "macro_token2".length()); assertHyperlink(CPP_CODE.indexOf("macro_token2"), CPP_CODE.indexOf("macro_token2"), "macro_token2".length());
// see bug 344604
assertHyperlink(CPP_CODE.indexOf("arg);"), CPP_CODE.indexOf("arg);"), "arg".length());
// no hyperlinks for comments // no hyperlinks for comments
assertNotHyperlink(CPP_CODE.indexOf("//") + 1); assertNotHyperlink(CPP_CODE.indexOf("//") + 1);

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2000, 2010 QNX Software Systems and others. * Copyright (c) 2000, 2011 QNX Software Systems and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -27,9 +27,10 @@ import org.eclipse.jface.text.hyperlink.AbstractHyperlinkDetector;
import org.eclipse.jface.text.hyperlink.IHyperlink; import org.eclipse.jface.text.hyperlink.IHyperlink;
import org.eclipse.ui.texteditor.ITextEditor; import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation; import org.eclipse.cdt.core.dom.ast.IASTImageLocation;
import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTNodeLocation;
import org.eclipse.cdt.core.dom.ast.IASTNodeSelector; import org.eclipse.cdt.core.dom.ast.IASTNodeSelector;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIncludeStatement; import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIncludeStatement;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
@ -75,27 +76,43 @@ public class CElementHyperlinkDetector extends AbstractHyperlinkDetector {
final int offset= region.getOffset(); final int offset= region.getOffset();
final int length= Math.max(1, region.getLength()); final int length= Math.max(1, region.getLength());
final IASTNodeSelector nodeSelector= ast.getNodeSelector(null); final IASTNodeSelector nodeSelector= ast.getNodeSelector(null);
IASTNode linkASTNode = null;
IASTNodeLocation linkLocation = null;
IASTName selectedName= nodeSelector.findEnclosingName(offset, length); IASTName selectedName= nodeSelector.findEnclosingName(offset, length);
IASTFileLocation linkLocation= null;
if (selectedName != null) { // found a name if (selectedName != null) { // found a name
// Prefer include statement over the include name // Prefer include statement over the include name
if (selectedName.getParent() instanceof IASTPreprocessorIncludeStatement) { if (selectedName.getParent() instanceof IASTPreprocessorIncludeStatement) {
linkLocation= selectedName.getParent().getFileLocation(); linkASTNode = selectedName.getParent();
} else { } else {
linkLocation= selectedName.getFileLocation(); linkASTNode = selectedName;
} }
} else { } else {
final IASTNode implicit = nodeSelector.findEnclosingImplicitName(offset, length); final IASTNode implicit = nodeSelector.findEnclosingImplicitName(offset, length);
if (implicit != null) { if (implicit != null) {
linkLocation = implicit.getFileLocation(); linkASTNode = implicit;
} else { } else {
// Search for include statement // Search for include statement
final IASTNode cand= nodeSelector.findEnclosingNode(offset, length); final IASTNode cand= nodeSelector.findEnclosingNode(offset, length);
if (cand instanceof IASTPreprocessorIncludeStatement) { if (cand instanceof IASTPreprocessorIncludeStatement) {
linkLocation= cand.getFileLocation(); linkASTNode = cand;
} }
} }
} }
if (linkASTNode != null) {
if (linkASTNode instanceof IASTName) {
IASTName astName = (IASTName) linkASTNode;
IASTImageLocation imageLocation = astName.getImageLocation();
if (imageLocation != null) {
linkLocation = imageLocation;
}
}
if (linkLocation == null) {
linkLocation = linkASTNode.getFileLocation();
}
}
if (linkLocation == null) { if (linkLocation == null) {
// Consider a fallback way of finding the hyperlink // Consider a fallback way of finding the hyperlink
// (see http://bugs.eclipse.org/bugs/show_bug.cgi?id=333050). // (see http://bugs.eclipse.org/bugs/show_bug.cgi?id=333050).