Eclipse Plugin Get Code from Current Open File

前端 未结 1 1175
囚心锁ツ
囚心锁ツ 2020-12-18 09:11

How can I get the code from the current open file in Eclipse returned in a String or String[]? I need this for a plugin I\'m making.

Let\'s

相关标签:
1条回答
  • 2020-12-18 09:44

    To get the currently edited file's content you can do something like that:

    IWorkbenchPart workbenchPart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart(); 
    IFile file = (IFile) workbenchPart.getSite().getPage().getActiveEditor().getEditorInput().getAdapter(IFile.class);
    if (file == null) throw new FileNotFoundException();
    String content = IOUtils.toString(file.getContents(), file.getCharset());
    
    0 讨论(0)
提交回复
热议问题