From 343844689b954e869652bcfb29639dd2479ee788 Mon Sep 17 00:00:00 2001 From: Marc-Andre Laperle Date: Wed, 4 May 2011 05:45:38 +0000 Subject: [PATCH] Bug 344604 - Hyperlink on arguments inside macro function call underlines all the call --- .../cdt/ui/tests/text/HyperlinkTest.java | 9 +++++- .../ui/editor/CElementHyperlinkDetector.java | 31 ++++++++++++++----- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/HyperlinkTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/HyperlinkTest.java index de95ef4015a..5b8ee47441d 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/HyperlinkTest.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/HyperlinkTest.java @@ -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 * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -65,9 +65,12 @@ public class HyperlinkTest extends TestCase { " private: \n" + " int x, y; \n" + "}; \n" + + "#define SOMEMACROFUNCTION(a) function(a) \n" + "int test(Point p) { \n" + " char* str = \"STRING LITERAL\"; \n" + " p + p;" + + " int arg;" + + " SOMEMACROFUNCTION(arg);" + "} \n"; 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 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()); + // see bug 344604 + assertHyperlink(CPP_CODE.indexOf("arg);"), CPP_CODE.indexOf("arg);"), "arg".length()); // no hyperlinks for comments assertNotHyperlink(CPP_CODE.indexOf("//") + 1); @@ -216,6 +221,8 @@ public class HyperlinkTest extends TestCase { // see bug 259015 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()); + // see bug 344604 + assertHyperlink(CPP_CODE.indexOf("arg);"), CPP_CODE.indexOf("arg);"), "arg".length()); // no hyperlinks for comments assertNotHyperlink(CPP_CODE.indexOf("//") + 1); diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CElementHyperlinkDetector.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CElementHyperlinkDetector.java index ea4d0efbd6f..fe353422d33 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CElementHyperlinkDetector.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CElementHyperlinkDetector.java @@ -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 * are made available under the terms of the Eclipse Public License v1.0 * 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.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.IASTNode; +import org.eclipse.cdt.core.dom.ast.IASTNodeLocation; import org.eclipse.cdt.core.dom.ast.IASTNodeSelector; import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIncludeStatement; import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; @@ -75,27 +76,43 @@ public class CElementHyperlinkDetector extends AbstractHyperlinkDetector { final int offset= region.getOffset(); final int length= Math.max(1, region.getLength()); final IASTNodeSelector nodeSelector= ast.getNodeSelector(null); + IASTNode linkASTNode = null; + IASTNodeLocation linkLocation = null; + IASTName selectedName= nodeSelector.findEnclosingName(offset, length); - IASTFileLocation linkLocation= null; if (selectedName != null) { // found a name // Prefer include statement over the include name if (selectedName.getParent() instanceof IASTPreprocessorIncludeStatement) { - linkLocation= selectedName.getParent().getFileLocation(); + linkASTNode = selectedName.getParent(); } else { - linkLocation= selectedName.getFileLocation(); + linkASTNode = selectedName; } } else { final IASTNode implicit = nodeSelector.findEnclosingImplicitName(offset, length); if (implicit != null) { - linkLocation = implicit.getFileLocation(); + linkASTNode = implicit; } else { // Search for include statement final IASTNode cand= nodeSelector.findEnclosingNode(offset, length); 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) { // Consider a fallback way of finding the hyperlink // (see http://bugs.eclipse.org/bugs/show_bug.cgi?id=333050).