IntelliJ plugin Get code from current open file

Basically, I want to know how to do this ( Eclipse Plugin Get code from the current open file ) in IntelliJ.

+8
intellij-idea intellij-plugin
source share
2 answers

In what context? If you are inside an action, you can just take everything from an 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(); 
+9
source share

Just in case, someone is looking for this - if you want the file name of an open file, you need to jump over the extra hoops:

 Document currentDoc = FileEditorManager.getInstance(project).getSelectedTextEditor().getDocument(); VirtualFile currentFile = FileDocumentManager.getInstance().getFile(currentDoc); String fileName = currentFile.getPath() 

(found this by entering the name "getSelectedTextEditor filename" in a Github search - sometimes all you need is a pointer in the right direction ...)

+9
source share

All Articles