use of com.intellij.openapi.fileEditor.FileDocumentManager in project intellij-plugins by StepicOrg.
the class InsertStepikDirectives method actionPerformed.
@Override
public void actionPerformed(AnActionEvent e) {
Project project = e.getProject();
if (project == null) {
return;
}
StudyNode selectedNode = StepikProjectManager.getSelected(project);
if (!(selectedNode instanceof StepNode) || ((StepNode) selectedNode).getType() != CODE) {
return;
}
StepNode targetStepNode = (StepNode) selectedNode;
FileDocumentManager documentManager = FileDocumentManager.getInstance();
for (VirtualFile file : FileEditorManager.getInstance(project).getOpenFiles()) {
Document document = documentManager.getDocument(file);
if (document != null)
documentManager.saveDocument(document);
}
SupportedLanguages currentLang = targetStepNode.getCurrentLang();
VirtualFile src = getOrCreateSrcDirectory(project, targetStepNode, true);
if (src == null) {
return;
}
VirtualFile file = src.findChild(currentLang.getMainFileName());
if (file == null) {
return;
}
String text = getFileText(file);
StepikProjectManager projectManager = StepikProjectManager.getInstance(project);
boolean showHint = projectManager != null && projectManager.getShowHint();
boolean needInsert = !containsDirectives(text, currentLang);
if (needInsert) {
text = insertAmbientCode(text, currentLang, showHint);
Metrics.insertAmbientCodeAction(project, targetStepNode, SUCCESSFUL);
} else {
text = removeAmbientCode(text, showHint, currentLang, true);
Metrics.removeAmbientCodeAction(project, targetStepNode, SUCCESSFUL);
}
writeInToFile(text, file, project);
if (needInsert) {
final Document document = FileDocumentManager.getInstance().getDocument(file);
if (document != null) {
ReformatUtils.reformatSelectedEditor(project, document);
}
}
}
Aggregations