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

The following examples show how to use com.intellij.openapi.fileEditor.FileEditorManager#openFile() . 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: CreateFileFix.java    From consulo with Apache License 2.0 6 votes vote down vote up
protected void openFile(@Nonnull Project project, PsiDirectory directory, PsiFile newFile, String text) {
  final FileEditorManager editorManager = FileEditorManager.getInstance(directory.getProject());
  final FileEditor[] fileEditors = editorManager.openFile(newFile.getVirtualFile(), true);

  if (text != null) {
    for (FileEditor fileEditor : fileEditors) {
      if (fileEditor instanceof TextEditor) { // JSP is not safe to edit via Psi
        final Document document = ((TextEditor)fileEditor).getEditor().getDocument();
        document.setText(text);

        if (ApplicationManager.getApplication().isUnitTestMode()) {
          FileDocumentManager.getInstance().saveDocument(document);
        }
        PsiDocumentManager.getInstance(project).commitDocument(document);
        break;
      }
    }
  }
}
 
Example 2
Source File: ClassHelper.java    From MVPManager with MIT License 5 votes vote down vote up
/**
 * open mvp's java file.
 *
 * @param project
 */
private static void openFiles(Project project, PsiClass... psiClasses) {
    FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
    for (PsiClass psiClass :
            psiClasses) {
        fileEditorManager.openFile(psiClass.getContainingFile().getVirtualFile(), true, true);
    }
}
 
Example 3
Source File: JSGraphQLQueryContextHighlightVisitor.java    From js-graphql-intellij-plugin with MIT License 5 votes vote down vote up
/**
 * Locates the first operation in the specified editor and places the caret inside it
 */
private static void placeCaretInsideFirstOperation(Editor editor, PsiFile psiFile) {
    if (editor.isDisposed()) {
        return;
    }
    if (psiFile.isValid() && psiFile.getVirtualFile() != null && psiFile.getVirtualFile().isValid()) {
        for (PsiElement psiElement : psiFile.getChildren()) {
            if (psiElement instanceof PsiWhiteSpace) {
                continue;
            }
            GraphQLOperationDefinition operationOrNull = asOperationOrNull(psiElement);
            if (operationOrNull != null) {
                PsiElement navigationTarget = operationOrNull;
                final Project project = editor.getProject();
                if (project != null) {
                    // try to find the name of the operation
                    if (operationOrNull instanceof GraphQLSelectionSetOperationDefinition) {
                        // unnamed query
                        final GraphQLSelectionSet selectionSet = ((GraphQLSelectionSetOperationDefinition) operationOrNull).getSelectionSet();
                        if (selectionSet != null) {
                            navigationTarget = selectionSet;
                        }
                    } else if (operationOrNull.getNameIdentifier() != null) {
                        navigationTarget = operationOrNull.getNameIdentifier();
                    }
                    final FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
                    fileEditorManager.openFile(psiFile.getVirtualFile(), true, true);
                    editor.getSelectionModel().removeSelection();
                    editor.getCaretModel().moveToOffset(navigationTarget.getTextOffset());
                    editor.getScrollingModel().scrollToCaret(ScrollType.CENTER);
                }
                return;
            }
        }
    }
}
 
Example 4
Source File: DocImpl.java    From floobits-intellij with Apache License 2.0 5 votes vote down vote up
@Override
public void applyHighlight(HighlightContext highlight) {
    final FileEditorManager manager = FileEditorManager.getInstance(context.project);
    final VirtualFile virtualFile = FileDocumentManager.getInstance().getFile(document);

    if ((highlight.force || highlight.following) && virtualFile != null && virtualFile.isValid()) {
        boolean spam = false;
        if (!manager.isFileOpen(virtualFile) || !Arrays.asList(manager.getSelectedFiles()).contains(virtualFile)) {
            spam = true;
        }
        if (spam && highlight.username.length() > 0 && highlight.force) {
            context.statusMessage(String.format("%s has summoned you!", highlight.username));
        }
        manager.openFile(virtualFile, false, true);
    }

    highlight.textLength = document.getTextLength();
    if (highlight.textLength == 0) {
        return;
    }
    synchronized (context) {
        try {
            context.setListener(false);
            highlight.force = highlight.force || highlight.following;
            highlight.context = context;
            applyHighlight_(highlight);
        } catch (Throwable e) {
            Flog.error(e);
        } finally {
            context.setListener(true);
        }
    }
}
 
Example 5
Source File: NavigationUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static boolean activatePsiElementIfOpen(@Nonnull PsiElement elt, boolean searchForOpen, boolean requestFocus) {
  if (!elt.isValid()) return false;
  elt = elt.getNavigationElement();
  final PsiFile file = elt.getContainingFile();
  if (file == null || !file.isValid()) return false;

  VirtualFile vFile = file.getVirtualFile();
  if (vFile == null) return false;

  if (!EditorHistoryManager.getInstance(elt.getProject()).hasBeenOpen(vFile)) return false;

  final FileEditorManager fem = FileEditorManager.getInstance(elt.getProject());
  if (!fem.isFileOpen(vFile)) {
    fem.openFile(vFile, requestFocus, searchForOpen);
  }

  final TextRange range = elt.getTextRange();
  if (range == null) return false;

  final FileEditor[] editors = fem.getEditors(vFile);
  for (FileEditor editor : editors) {
    if (editor instanceof TextEditor) {
      final Editor text = ((TextEditor)editor).getEditor();
      final int offset = text.getCaretModel().getOffset();

      if (range.containsOffset(offset)) {
        // select the file
        fem.openFile(vFile, requestFocus, searchForOpen);
        return true;
      }
    }
  }

  return false;
}
 
Example 6
Source File: BlazeModuleSystem.java    From intellij with Apache License 2.0 4 votes vote down vote up
@Override
public void registerDependency(GradleCoordinate coordinate, DependencyType type) {
  if (type != DependencyType.IMPLEMENTATION) {
    throw new UnsupportedOperationException("Unsupported dependency type in Blaze: " + type);
  }
  BlazeProjectData blazeProjectData =
      BlazeProjectDataManager.getInstance(project).getBlazeProjectData();
  if (blazeProjectData == null) {
    return;
  }
  AndroidResourceModuleRegistry registry = AndroidResourceModuleRegistry.getInstance(project);
  TargetIdeInfo targetIdeInfo =
      blazeProjectData.getTargetMap().get(registry.getTargetKey(module));
  if (targetIdeInfo == null || targetIdeInfo.getBuildFile() == null) {
    return;
  }

  // TODO: automagically edit deps instead of just opening the BUILD file?
  // Need to translate Gradle coordinates into blaze targets.
  // Will probably need to hardcode for each dependency.
  FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
  PsiElement buildTargetPsi =
      BuildReferenceManager.getInstance(project).resolveLabel(targetIdeInfo.getKey().getLabel());
  if (buildTargetPsi != null) {
    // If we can find a PSI for the target,
    // then we can jump straight to the target in the build file.
    fileEditorManager.openTextEditor(
        new OpenFileDescriptor(
            project,
            buildTargetPsi.getContainingFile().getVirtualFile(),
            buildTargetPsi.getTextOffset()),
        true);
  } else {
    // If not, just the build file is good enough.
    ArtifactLocation buildFile = targetIdeInfo.getBuildFile();
    File buildIoFile =
        Preconditions.checkNotNull(
            OutputArtifactResolver.resolve(
                project, blazeProjectData.getArtifactLocationDecoder(), buildFile),
            "Fail to find file %s",
            buildFile.getRelativePath());
    VirtualFile buildVirtualFile =
        VfsUtils.resolveVirtualFile(buildIoFile, /* refreshIfNeeded= */ true);
    if (buildVirtualFile != null) {
      fileEditorManager.openFile(buildVirtualFile, true);
    }
  }
}
 
Example 7
Source File: NoSqlDatabaseFileSystem.java    From nosql4idea with Apache License 2.0 4 votes vote down vote up
public void openEditor(final NoSqlDatabaseObjectFile databaseObjectFile) {
    FileEditorManager fileEditorManager = FileEditorManager.getInstance(databaseObjectFile.getProject());
    fileEditorManager.openFile(databaseObjectFile, true);
}