1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-09-10 12:03:16 +02:00

Bug 259015 - Ctrl-click and F3 (Navigate) should have the same semantics

This commit is contained in:
Anton Leherbauer 2009-02-04 13:50:41 +00:00
parent 0abeb03657
commit 38bde2611c

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2008 QNX Software Systems and others.
* Copyright (c) 2000, 2009 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
@ -12,6 +12,8 @@
*******************************************************************************/
package org.eclipse.cdt.internal.ui.editor;
import java.util.Arrays;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.action.IAction;
@ -31,6 +33,7 @@ import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTNodeSelector;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIncludeStatement;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.model.ICLanguageKeywords;
import org.eclipse.cdt.core.model.ILanguage;
import org.eclipse.cdt.core.model.IWorkingCopy;
import org.eclipse.cdt.ui.CUIPlugin;
@ -38,12 +41,14 @@ import org.eclipse.cdt.ui.text.ICPartitions;
import org.eclipse.cdt.internal.core.model.ASTCache.ASTRunnable;
import org.eclipse.cdt.internal.ui.text.CWordFinder;
public class CElementHyperlinkDetector extends AbstractHyperlinkDetector {
public CElementHyperlinkDetector() {
}
public IHyperlink[] detectHyperlinks(ITextViewer textViewer, final IRegion region, boolean canShowMultipleHyperlinks) {
public IHyperlink[] detectHyperlinks(final ITextViewer textViewer, final IRegion region, boolean canShowMultipleHyperlinks) {
ITextEditor textEditor= (ITextEditor)getAdapter(ITextEditor.class);
if (region == null || !(textEditor instanceof CEditor))
return null;
@ -95,6 +100,21 @@ public class CElementHyperlinkDetector extends AbstractHyperlinkDetector {
if (linkLocation != null) {
result[0]= new CElementHyperlink(
new Region(linkLocation.getNodeOffset(), linkLocation.getNodeLength()), openAction);
} else {
// consider fallback navigation
final IDocument document= textViewer.getDocument();
IRegion wordRegion= CWordFinder.findWord(document, offset);
if (wordRegion != null) {
try {
String word= document.get(wordRegion.getOffset(), wordRegion.getLength());
if (!isLanguageKeyword(lang, word)) {
result[0]= new CElementHyperlink(
new Region(wordRegion.getOffset(), wordRegion.getLength()), openAction);
}
} catch (BadLocationException exc) {
// ingore
}
}
}
}
return Status.OK_STATUS;
@ -106,4 +126,20 @@ public class CElementHyperlinkDetector extends AbstractHyperlinkDetector {
return result[0] == null ? null : result;
}
private static boolean isLanguageKeyword(ILanguage lang, String word) {
ICLanguageKeywords keywords= (ICLanguageKeywords) lang.getAdapter(ICLanguageKeywords.class);
if (keywords != null) {
if (Arrays.asList(keywords.getKeywords()).contains(word)) {
return true;
}
if (Arrays.asList(keywords.getBuiltinTypes()).contains(word)) {
return true;
}
if (Arrays.asList(keywords.getPreprocessorKeywords()).contains('#'+word)) {
return true;
}
}
return false;
}
}