简体   繁体   English

在eclipse编辑器中当前所选代码的AST?

[英]AST for current selected code in eclipse editor?

I need to get the AST for the current selection in the java editor fo eclipse. 我需要在eclipse的java编辑器中获取当前选择的AST。 Basically I want to convert the selected java code in to some other form(maybe some other language or XML etc..). 基本上我想将选定的java代码转换为其他形式(可能是其他语言或XML等)。 So I guess, I need to get the AST for the selection. 所以我想,我需要获得选择的AST。 Currently I am able to get the selection as simple text. 目前,我能够将选择作为简单文本。 Is there any way out for such problem? 有没有办法解决这个问题? Thanks already!! 谢谢!

There are a number of handy tools for JDT plugin developers, especially the AST View which does pretty much what you are looking for. JDT插件开发人员有许多方便的工具,尤其是AST View ,它可以满足您的需求。 So, all you need to do is grab the code for AST View and check how it is done. 因此,您需要做的就是获取AST View的代码并检查它是如何完成的。

The plugin can be installed from the following update site: http://www.eclipse.org/jdt/ui/update-site 该插件可以从以下更新站点安装: http//www.eclipse.org/jdt/ui/update-site

JDT ASTView

Use the plugin spy (read more about it in this article ) to start digging into the view classes. 使用插件间谍(在本文中阅读更多相关内容)开始深入研究视图类。

You are traveling into less trivial (and often undocumented) areas of JDT, developing your code digging skills will greatly improve your performance. 您正在前往JDT的不那么琐碎(通常是未记录的)领域,开发您的代码挖掘技能将大大提高您的性能。

The following Code provides you the AST Node of the current selected Code from the CompilationUnitEditor. 以下代码为您提供CompilationUnitEditor中当前所选代码的AST节点。

        ITextEditor editor = (ITextEditor) HandlerUtil.getActiveEditor(event);
        ITextSelection sel  = (ITextSelection) editor.getSelectionProvider().getSelection();
        ITypeRoot typeRoot = JavaUI.getEditorInputTypeRoot(editor.getEditorInput());
        ICompilationUnit icu = (ICompilationUnit) typeRoot.getAdapter(ICompilationUnit.class);
        CompilationUnit cu = parse(icu);
        NodeFinder finder = new NodeFinder(cu, sel.getOffset(), sel.getLength());
        ASTNode node = finder.getCoveringNode();

The JavaUI is the entry point to the JDT UI plugin. JavaUI是JDT UI插件的入口点。

Use the method org.eclipse.jdt.internal.ui.javaeditor.EditorUtility.getActiveEditorJavaInput() . 使用方法org.eclipse.jdt.internal.ui.javaeditor.EditorUtility.getActiveEditorJavaInput() That returns the Java element edited in the current active editor. 它返回在当前活动编辑器中编辑的Java元素。 The return type is org.eclipse.jdt.core.IJavaElement , but if it's a Java file that's being edited, the run time type will be org.eclipse.jdt.core.ICompilationUnit . 返回类型是org.eclipse.jdt.core.IJavaElement ,但如果它是正在编辑的Java文件,则运行时类型将是org.eclipse.jdt.core.ICompilationUnit

To get the AST, ie, the org.eclipse.jdt.core.dom.CompilationUnit , you can use the following code: 要获取AST,即org.eclipse.jdt.core.dom.CompilationUnit ,您可以使用以下代码:

public static CompilationUnit getCompilationUnit(ICompilationUnit icu,
        IProgressMonitor monitor) {
    final ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(icu);
    parser.setResolveBindings(true);
    final CompilationUnit ret = (CompilationUnit) parser.createAST(monitor);
    return ret;
}

Keep in mind that this is for Java >= 5. For earlier versions you'll need to switch the argument to ASTParser.newParser() . 请记住,这是针对Java> = 5.对于早期版本,您需要将参数切换为ASTParser.newParser()

I realize that this question was answered but I wanted to shed light on the EditorUtility class, which is quite useful here. 我意识到这个问题已得到解答,但我想了解一下EditorUtility类,它在这里非常有用。

IIRC, each node in the Eclipse AST contains an offset. IIRC,Eclipse AST中的每个节点都包含一个偏移量。 All you need to do is to compute the offsets for the part of the code you are interested in then walk the AST to select the nodes within those offsets. 您需要做的就是计算您感兴趣的代码部分的偏移量,然后遍历AST以选择这些偏移量内的节点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM