AST for current selected code in eclipse editor?

梦想的初衷 提交于 2019-12-19 23:21:53

问题


I need to get the AST for the current selection in the java editor fo eclipse. Basically I want to convert the selected java code in to some other form(maybe some other language or XML etc..). So I guess, I need to get the AST for the selection. Currently I am able to get the selection as simple text. Is there any way out for such problem? Thanks already!!


回答1:


There are a number of handy tools for JDT plugin developers, especially the AST View which does pretty much what you are looking for. So, all you need to do is grab the code for AST View and check how it is done.

The plugin can be installed from the following update site: http://www.eclipse.org/jdt/ui/update-site

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.




回答2:


The following Code provides you the AST Node of the current selected Code from the CompilationUnitEditor.

        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.




回答3:


Use the method org.eclipse.jdt.internal.ui.javaeditor.EditorUtility.getActiveEditorJavaInput(). That returns the Java element edited in the current active editor. 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.

To get the AST, i.e., the org.eclipse.jdt.core.dom.CompilationUnit, you can use the following code:

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().

I realize that this question was answered but I wanted to shed light on the EditorUtility class, which is quite useful here.




回答4:


IIRC, each node in the Eclipse AST contains an offset. 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.



来源:https://stackoverflow.com/questions/1636131/ast-for-current-selected-code-in-eclipse-editor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!