Search code examples
intellij-ideaintellij-plugin

IntelliJ Plugin Get Code From Current Open File


Basically, I want to know how to do this (Eclipse Plugin Get Code from Current Open File) in IntelliJ.


Solution

  • In what context? If you are inside an action, you can simply take everything from the ActionEvent, for example:

    e.getData(LangDataKeys.EDITOR).getDocument().getText();
    

    (When e is AnActionEvent).

    Otherwise, you can get it from the project:

    FileEditorManager.getInstance(project).getSelectedTextEditor().getDocument().getText();