Erfan Farhangy
Erfan Farhangy

Reputation: 459

How to initialize object of Project class in java

I want to get the active file in intelliJ and have written this code:

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

but I don't know how initialize the project object. Any help would be greatly appreciated! Regards, Erfan

Upvotes: 0

Views: 271

Answers (1)

Bas Leijdekkers
Bas Leijdekkers

Reputation: 26482

If you want to get the project from an com.intellij.openapi.actionSystem.AnAction implementation, you can do something like this:

public class MyAction extends AnAction {
  public void actionPerformed(AnActionEvent e) {
    Project project = e.getData(PlatformDataKeys.PROJECT);

    // or get the current virtual file directly:
    e.getData(PlatformDataKeys.VIRTUAL_FILE);
  }
}

Upvotes: 1

Related Questions