我想在intelliJ中获取活动文件,并编写了这段代码:
Document currentDoc= FileEditorManager.getInstance(project)
.getSelectedTextEditor().getDocument();
VirtualFile currentFile = FileDocumentManager.getInstance().getFile(currentDoc);
String fileName = currentFile.getPath();
System.out.println(fileName);
但我不知道如何初始化项目对象。 任何帮助将不胜感激!此致,Erfan
答案 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);
}
}