10

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

Community
  • 1
  • 1
nrubin29
  • 1,522
  • 5
  • 25
  • 53

2 Answers2

16

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...)

rob74
  • 4,939
  • 29
  • 31
  • 1
    I'm kind of trying to do the same, but fail on getting project: `public void actionPerformed(AnActionEvent e) { Project project = e.getProject(); ...` - it is null, somehow. What am I doing wrong? – t1gor Jun 19 '15 at 16:17
12

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();
axelrod
  • 3,782
  • 1
  • 20
  • 25