Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 586
nrubin29 Avatar asked Jul 29 '13 03:07

nrubin29


Video Answer


2 Answers

Just in case somebody is looking for this - if you want the file name of the currently open file, you have to jump through some additional hoops:

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

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

like image 191
rob74 Avatar answered Jan 21 '23 00:01

rob74


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();
like image 23
axelrod Avatar answered Jan 21 '23 00:01

axelrod