AST for current selected code in eclipse editor?

前端 未结 4 597
隐瞒了意图╮
隐瞒了意图╮ 2021-01-19 03:59

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 lang

4条回答
  •  北荒
    北荒 (楼主)
    2021-01-19 04:55

    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.

提交回复
热议问题