Java Code Examples for com.intellij.openapi.fileEditor.FileEditorManager#getSelectedFiles()

The following examples show how to use com.intellij.openapi.fileEditor.FileEditorManager#getSelectedFiles() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProcessAction.java    From JHelper with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
protected void performAction(AnActionEvent e) {
	Project project = e.getProject();
	if (project == null) {
		throw new NotificationException("No project found", "Are you in any project?");
	}
	FileEditorManager manager = FileEditorManager.getInstance(project);
	if (manager == null) {
		throw new NotificationException("This is unexpected", "File editor manager is null");
	}
	VirtualFile[] files = manager.getSelectedFiles();
	if (files.length == 0) {
		throw new NotificationException("No file found", "Do you have opened file?");
	}
	PsiFile file = PsiManager.getInstance(project).findFile(files[0]);
	if (file == null) {
		throw new NotificationException("This is unexpected", "No associated PsiFile");
	}
	if (!FileUtils.isCppFile(file)) {
		throw new NotificationException("Not a cpp file", "Only cpp files are currently supported");
	}
	String result = IncludesProcessor.process(file);
	FileUtils.writeToFile(file, result);
}
 
Example 2
Source File: MacroManager.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static DataContext getCorrectContext(DataContext dataContext) {
  if (dataContext.getData(PlatformDataKeys.FILE_EDITOR) != null) {
    return dataContext;
  }
  Project project = dataContext.getData(CommonDataKeys.PROJECT);
  if (project == null) {
    return dataContext;
  }
  FileEditorManager editorManager = FileEditorManager.getInstance(project);
  VirtualFile[] files = editorManager.getSelectedFiles();
  if (files.length == 0) {
    return dataContext;
  }
  FileEditor fileEditor = editorManager.getSelectedEditor(files[0]);
  return fileEditor == null ? dataContext : DataManager.getInstance().getDataContext(fileEditor.getComponent());
}
 
Example 3
Source File: ProjectViewImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
public void scrollFromSource() {
  final FileEditorManager fileEditorManager = FileEditorManager.getInstance(myProject);
  final Editor selectedTextEditor = fileEditorManager.getSelectedTextEditor();
  if (selectedTextEditor != null) {
    selectElementAtCaret(selectedTextEditor);
    return;
  }
  final FileEditor[] editors = fileEditorManager.getSelectedEditors();
  for (FileEditor fileEditor : editors) {
    if (fileEditor instanceof TextEditor) {
      Editor editor = ((TextEditor)fileEditor).getEditor();
      selectElementAtCaret(editor);
      return;
    }
  }
  final VirtualFile[] selectedFiles = fileEditorManager.getSelectedFiles();
  if (selectedFiles.length > 0) {
    final PsiFile file = PsiManager.getInstance(myProject).findFile(selectedFiles[0]);
    if (file != null) {
      scrollFromFile(file, null);
    }
  }
}
 
Example 4
Source File: SpecFormatter.java    From Intellij-Plugin with Apache License 2.0 5 votes vote down vote up
@Override
public void actionPerformed(AnActionEvent anActionEvent) {
    final Project project = anActionEvent.getData(LangDataKeys.PROJECT);
    if (project == null) {
        return;
    }
    String projectDir = project.getBasePath();
    if (projectDir == null) {
        return;
    }

    FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
    VirtualFile selectedFile = fileEditorManager.getSelectedFiles()[0];
    String fileName = selectedFile.getCanonicalPath();
    Document doc = FileDocumentManager.getInstance().getDocument(selectedFile);
    if (doc != null) {
        FileDocumentManager.getInstance().saveDocument(doc);
    }
    try {
        GaugeSettingsModel settings = getGaugeSettings();
        ProcessBuilder processBuilder = new ProcessBuilder(settings.getGaugePath(), Constants.FORMAT, fileName);
        GaugeUtil.setGaugeEnvironmentsTo(processBuilder, settings);
        processBuilder.directory(new File(projectDir));
        Process process = processBuilder.start();
        int exitCode = process.waitFor();
        if (exitCode != 0) {
            String output = String.format("<pre>%s</pre>", GaugeUtil.getOutput(process.getInputStream(), "\n").replace("<", "&lt;").replace(">", "&gt;"));
            Notifications.Bus.notify(new Notification("Spec Formatting", "Error: Spec Formatting", output, NotificationType.ERROR));
            return;
        }
        VirtualFileManager.getInstance().syncRefresh();
        selectedFile.refresh(false, false);
    } catch (Exception e) {
        e.printStackTrace();
        LOG.debug(e);
        Messages.showErrorDialog("Error on formatting spec", "Format Error");
    }
}