如何在java中初始化Project类的对象

时间:2015-03-23 14:58:25

标签: intellij-idea intellij-plugin

我想在intelliJ中获取活动文件,并编写了这段代码:

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

但我不知道如何初始化项目对象。 任何帮助将不胜感激!此致,Erfan

1 个答案:

答案 0 :(得分:1)

如果您想从com.intellij.openapi.actionSystem.AnAction实施中获取项目,可以执行以下操作:

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);
  }
}