From 9222a14bd922cccd295c3bc444dc07ad6bdb1bf8 Mon Sep 17 00:00:00 2001 From: Doug Schaefer Date: Tue, 18 Apr 2006 02:54:38 +0000 Subject: [PATCH] Got our first entries from ctags. Fixed open declaration to deal with line numbers in the PDOM. So far, just variables and without any scoping information. --- .../indexer/ctags/CtagsBindingFinder.java | 54 ++++ .../ctags/{CtagsName.java => CtagsCName.java} | 19 +- .../core/pdom/indexer/ctags/CtagsCPPName.java | 235 ++++++++++++++++++ .../core/pdom/indexer/ctags/CtagsIndexer.java | 17 +- .../core/pdom/indexer/ctags/CtagsReindex.java | 6 +- .../ui/indexview/OpenDefinitionAction.java | 25 +- 6 files changed, 332 insertions(+), 24 deletions(-) create mode 100644 core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/indexer/ctags/CtagsBindingFinder.java rename core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/indexer/ctags/{CtagsName.java => CtagsCName.java} (90%) create mode 100644 core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/indexer/ctags/CtagsCPPName.java diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/indexer/ctags/CtagsBindingFinder.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/indexer/ctags/CtagsBindingFinder.java new file mode 100644 index 00000000000..d0837f848bb --- /dev/null +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/indexer/ctags/CtagsBindingFinder.java @@ -0,0 +1,54 @@ +/******************************************************************************* + * Copyright (c) 2006 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 + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * QNX - Initial API and implementation + *******************************************************************************/ + +package org.eclipse.cdt.internal.core.pdom.indexer.ctags; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.cdt.core.dom.IPDOMNode; +import org.eclipse.cdt.core.dom.IPDOMVisitor; +import org.eclipse.cdt.internal.core.pdom.dom.PDOMBinding; +import org.eclipse.core.runtime.CoreException; + +/** + * @author Doug Schaefer + * + */ +public class CtagsBindingFinder implements IPDOMVisitor { + + private final String name; + private final int[] types; + private List bindings = new ArrayList(); + + public CtagsBindingFinder(String name, int[] types) { + this.name = name; + this.types = types; + } + + public boolean visit(IPDOMNode node) throws CoreException { + PDOMBinding binding = (PDOMBinding)node; + if (binding.getName().equals(name)) { + int type = binding.getBindingType(); + for (int i = 0; i < types.length; ++i) { + if (type == types[i]) { + bindings.add(binding); + break; + } + } + } + return false; + } + + public PDOMBinding[] getBindings() { + return (PDOMBinding[])bindings.toArray(new PDOMBinding[bindings.size()]); + } +} diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/indexer/ctags/CtagsName.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/indexer/ctags/CtagsCName.java similarity index 90% rename from core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/indexer/ctags/CtagsName.java rename to core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/indexer/ctags/CtagsCName.java index 22795b5d402..48bbb23c2f6 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/indexer/ctags/CtagsName.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/indexer/ctags/CtagsCName.java @@ -30,13 +30,11 @@ import org.eclipse.cdt.internal.core.pdom.dom.PDOMNotImplementedError; import org.eclipse.core.runtime.CoreException; /** - * A fake AST Name derived from a ctags entry. - * * @author Doug Schaefer + * */ -public class CtagsName implements IASTName, IASTFileLocation { - - private final PDOM pdom; +public class CtagsCName implements IASTName, IASTFileLocation { + private final PDOMLinkage linkage; private final String fileName; private final int lineNum; @@ -76,8 +74,8 @@ public class CtagsName implements IASTName, IASTFileLocation { "externvar", //$NON-NLS-1$ }; - public CtagsName(PDOM pdom, String fileName, int lineNum, String elementName, Map fields) throws CoreException { - this.pdom = pdom; + public CtagsCName(PDOMLinkage linkage, String fileName, int lineNum, String elementName, Map fields) throws CoreException { + this.linkage = linkage; this.fileName = fileName; this.lineNum = lineNum; this.elementName = elementName; @@ -94,13 +92,6 @@ public class CtagsName implements IASTName, IASTFileLocation { } } - String languageName = (String)fields.get("language"); - ILanguage language - = (languageName != null && languageName.equals("C++")) - ? (ILanguage)new GPPLanguage() - : (ILanguage)new GCCLanguage(); - - linkage = pdom.getLinkage(language); } public void addToPDOM() throws CoreException { diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/indexer/ctags/CtagsCPPName.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/indexer/ctags/CtagsCPPName.java new file mode 100644 index 00000000000..e889a6d726e --- /dev/null +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/indexer/ctags/CtagsCPPName.java @@ -0,0 +1,235 @@ +/******************************************************************************* + * Copyright (c) 2006 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 + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * QNX - Initial API and implementation + *******************************************************************************/ + +package org.eclipse.cdt.internal.core.pdom.indexer.ctags; + +import java.util.Map; + +import org.eclipse.cdt.core.CCorePlugin; +import org.eclipse.cdt.core.dom.IPDOMNode; +import org.eclipse.cdt.core.dom.ast.ASTNodeProperty; +import org.eclipse.cdt.core.dom.ast.ASTVisitor; +import org.eclipse.cdt.core.dom.ast.IASTFileLocation; +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.IASTTranslationUnit; +import org.eclipse.cdt.core.dom.ast.IBinding; +import org.eclipse.cdt.internal.core.pdom.dom.PDOMBinding; +import org.eclipse.cdt.internal.core.pdom.dom.PDOMLinkage; +import org.eclipse.cdt.internal.core.pdom.dom.PDOMNode; +import org.eclipse.cdt.internal.core.pdom.dom.PDOMNotImplementedError; +import org.eclipse.cdt.internal.core.pdom.dom.cpp.PDOMCPPLinkage; +import org.eclipse.cdt.internal.core.pdom.dom.cpp.PDOMCPPVariable; +import org.eclipse.core.runtime.CoreException; + +/** + * @author Doug Schaefer + * + */ +public class CtagsCPPName implements IASTName, IASTFileLocation { + + private final PDOMLinkage linkage; + private final String fileName; + private final int lineNum; + private final String elementName; + private final Map fields; + private int kind; // Enum from below + + private final static int K_UNKNOWN = 0; + private final static int K_CLASS = 1; + private final static int K_MACRO = 2; + private final static int K_ENUMERATOR = 3; + private final static int K_FUNCTION = 4; + private final static int K_ENUM = 5; + private final static int K_MEMBER = 6; + private final static int K_NAMESPACE = 7; + private final static int K_PROTOTYPE = 8; + private final static int K_STRUCT = 9; + private final static int K_TYPEDEF = 10; + private final static int K_UNION = 11; + private final static int K_VARIABLE = 12; + private final static int K_EXTERNALVAR = 13; + + private final static String[] kinds = { // Order must match value of enum above + null, // unknown kinds + "class", //$NON-NLS-1$ + "macro", //$NON-NLS-1$ + "enumerator", //$NON-NLS-1$ + "function", //$NON-NLS-1$ + "enum", //$NON-NLS-1$ + "member", //$NON-NLS-1$ + "namespace", //$NON-NLS-1$ + "prototype", //$NON-NLS-1$ + "struct", //$NON-NLS-1$ + "typedef", //$NON-NLS-1$ + "union", //$NON-NLS-1$ + "variable", //$NON-NLS-1$ + "externvar", //$NON-NLS-1$ + }; + + public CtagsCPPName(PDOMLinkage linkage, String fileName, int lineNum, String elementName, Map fields) throws CoreException { + this.linkage = linkage; + this.fileName = fileName; + this.lineNum = lineNum; + this.elementName = elementName; + this.fields = fields; + + kind = K_UNKNOWN; + String kindField = (String)fields.get("kind"); //$NON-NLS-1$ + if (kindField != null) { + for (int i = 1; i < kinds.length; ++i) { + if (kindField.equals(kinds[i])) { + kind = i; + break; + } + } + } + } + + public void addToPDOM() throws CoreException { + linkage.addName(this); + } + + public IBinding getBinding() { + throw new PDOMNotImplementedError(); + } + + public boolean isDeclaration() { + throw new PDOMNotImplementedError(); + } + + public boolean isDefinition() { + // Find out how function definitions are done + return true; + } + + public boolean isReference() { + // We're never a reference + return false; + } + + private PDOMBinding makeNewBinding(IPDOMNode scope) throws CoreException { + switch (kind) { + case K_VARIABLE: + return new PDOMCPPVariable(linkage.getPDOM(), (PDOMNode)scope, this); + default: + return null; + } + } + + public IBinding resolveBinding() { + try { + IPDOMNode scope = linkage; + int[] types = null; + switch (kind) { + case K_VARIABLE: + types = new int[] { PDOMCPPLinkage.CPPVARIABLE }; + break; + default: + return null; + } + + CtagsBindingFinder finder = new CtagsBindingFinder(elementName, types); + scope.accept(finder); + PDOMBinding[] bindings = finder.getBindings(); + if (bindings.length == 0) + return makeNewBinding(scope); + else if (bindings.length == 1) + return bindings[0]; + else + // TODO resolve overloads + return bindings[0]; + } catch (CoreException e) { + CCorePlugin.log(e); + return null; + } + } + + public IBinding[] resolvePrefix() { + throw new PDOMNotImplementedError(); + } + + public void setBinding(IBinding binding) { + throw new PDOMNotImplementedError(); + } + + public char[] toCharArray() { + return elementName.toCharArray(); + } + + public boolean accept(ASTVisitor visitor) { + throw new PDOMNotImplementedError(); + } + + public String getContainingFilename() { + return fileName; + } + + public IASTFileLocation getFileLocation() { + return this; + } + + public IASTNodeLocation[] getNodeLocations() { + throw new PDOMNotImplementedError(); + } + + public IASTNode getParent() { + return null; + } + + public ASTNodeProperty getPropertyInParent() { + throw new PDOMNotImplementedError(); + } + + public String getRawSignature() { + throw new PDOMNotImplementedError(); + } + + public IASTTranslationUnit getTranslationUnit() { + throw new PDOMNotImplementedError(); + } + + public void setParent(IASTNode node) { + throw new PDOMNotImplementedError(); + } + + public void setPropertyInParent(ASTNodeProperty property) { + throw new PDOMNotImplementedError(); + } + + public int getEndingLineNumber() { + throw new PDOMNotImplementedError(); + } + + public String getFileName() { + return fileName; + } + + public int getStartingLineNumber() { + return lineNum; + } + + public IASTFileLocation asFileLocation() { + throw new PDOMNotImplementedError(); + } + + public int getNodeLength() { + // -1 means we have a line num as the offset + return -1; + } + + public int getNodeOffset() { + // since node length is -1, we can return the line number here + return lineNum; + } + +} diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/indexer/ctags/CtagsIndexer.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/indexer/ctags/CtagsIndexer.java index edc6995dd5d..82af11d9fde 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/indexer/ctags/CtagsIndexer.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/indexer/ctags/CtagsIndexer.java @@ -21,8 +21,11 @@ import java.util.StringTokenizer; import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.dom.IPDOM; import org.eclipse.cdt.core.dom.IPDOMIndexer; +import org.eclipse.cdt.core.dom.ast.gnu.c.GCCLanguage; +import org.eclipse.cdt.core.dom.ast.gnu.cpp.GPPLanguage; import org.eclipse.cdt.core.model.ICElementDelta; import org.eclipse.cdt.internal.core.pdom.PDOM; +import org.eclipse.cdt.internal.core.pdom.dom.PDOMLinkage; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.ProjectScope; import org.eclipse.core.runtime.CoreException; @@ -127,7 +130,7 @@ public class CtagsIndexer implements IPDOMIndexer { try { token = token.trim(); int i = token.indexOf(';'); - lineNum = Integer.parseInt(token.substring(0, i)); + lineNum = Integer.parseInt(token.substring(0, i)) - 1; // Make it 0 based } catch (NumberFormatException e) { // Not sure what the line number is. lineNum = -1; @@ -145,8 +148,16 @@ public class CtagsIndexer implements IPDOMIndexer { } } - if (elementName != null && fileName != null) - new CtagsName(pdom, fileName, lineNum, elementName, fields).addToPDOM(); + if (elementName != null && fileName != null) { + String languageName = (String)fields.get("language"); //$NON-NLS-1$ + if (languageName.equals("C++")) { //$NON-NLS-1$ + PDOMLinkage linkage = pdom.getLinkage(new GPPLanguage()); + new CtagsCPPName(linkage, fileName, lineNum, elementName, fields).addToPDOM(); + } else { + PDOMLinkage linkage = pdom.getLinkage(new GCCLanguage()); + new CtagsCName(linkage, fileName, lineNum, elementName, fields).addToPDOM(); + } + } } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/indexer/ctags/CtagsReindex.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/indexer/ctags/CtagsReindex.java index 386e97d9c72..dcd61960468 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/indexer/ctags/CtagsReindex.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/indexer/ctags/CtagsReindex.java @@ -11,8 +11,6 @@ package org.eclipse.cdt.internal.core.pdom.indexer.ctags; -import java.util.Arrays; - import org.eclipse.cdt.core.model.ICProject; import org.eclipse.cdt.core.model.IIncludeReference; import org.eclipse.cdt.core.model.ISourceRoot; @@ -25,8 +23,6 @@ import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.jobs.Job; -import com.sun.corba.se.impl.interceptors.PINoOpHandlerImpl; - /** * @author Doug Schaefer * @@ -72,7 +68,7 @@ public class CtagsReindex extends Job { ISourceRoot[] sourceRoots = project.getAllSourceRoots(); - monitor.beginTask("Indexing", sourceRoots.length + includes.length); + monitor.beginTask("Indexing", sourceRoots.length + includes.length + 1); // Clear out the PDOM if (monitor.isCanceled()) diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/indexview/OpenDefinitionAction.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/indexview/OpenDefinitionAction.java index 16d1d0c6c50..fa4d8b5bb20 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/indexview/OpenDefinitionAction.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/indexview/OpenDefinitionAction.java @@ -21,6 +21,8 @@ import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.Path; +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.IDocument; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.TreeViewer; @@ -63,8 +65,27 @@ public class OpenDefinitionAction extends IndexAction { IEditorPart editor = EditorUtility.openInEditor(input); if (editor != null && editor instanceof ITextEditor) { - ((ITextEditor)editor).selectAndReveal(location.getNodeOffset(), location.getNodeLength()); - return; + ITextEditor textEditor = (ITextEditor)editor; + int nodeOffset = location.getNodeOffset(); + int nodeLength = location.getNodeLength(); + int offset; + int length; + if (nodeLength == -1) { + // This means the offset is actually a line number + try { + IDocument document = textEditor.getDocumentProvider().getDocument(editor.getEditorInput()); + offset = document.getLineOffset(nodeOffset); + length = document.getLineLength(nodeOffset); + } catch (BadLocationException e) { + CUIPlugin.getDefault().log(e); + return; + } + } else { + offset = nodeOffset; + length = nodeLength; + } + + textEditor.selectAndReveal(offset, length); } } catch (CoreException e) { CUIPlugin.getDefault().log(e);