com.intellij.openapi.fileEditor.OpenFileDescriptor Java Examples

The following examples show how to use com.intellij.openapi.fileEditor.OpenFileDescriptor. 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: SMTRunnerConsoleProperties.java    From consulo with Apache License 2.0 6 votes vote down vote up
@javax.annotation.Nullable
@Deprecated
protected Navigatable findSuitableNavigatableForLine(@Nonnull Project project, @Nonnull VirtualFile file, int line) {
  // lets find first non-ws psi element

  final Document doc = FileDocumentManager.getInstance().getDocument(file);
  final PsiFile psi = doc == null ? null : PsiDocumentManager.getInstance(project).getPsiFile(doc);
  if (psi == null) {
    return null;
  }

  int offset = doc.getLineStartOffset(line);
  int endOffset = doc.getLineEndOffset(line);
  for (int i = offset + 1; i < endOffset; i++) {
    PsiElement el = psi.findElementAt(i);
    if (el != null && !(el instanceof PsiWhiteSpace)) {
      offset = el.getTextOffset();
      break;
    }
  }

  return new OpenFileDescriptor(project, file, offset);
}
 
Example #2
Source File: PsiUtils.java    From intellij-reference-diagram with Apache License 2.0 6 votes vote down vote up
public static void navigate(PsiElement psiElement, Project project) {
    ApplicationManager.getApplication().invokeLater(
            () -> {
                ApplicationManager.getApplication().assertIsDispatchThread();
                Navigatable n = (Navigatable) psiElement;
                //this is for better cursor position
                if (psiElement instanceof PsiFile) {
                    VirtualFile file = ((PsiFile) psiElement).getVirtualFile();
                    if (file == null) return;
                    OpenFileDescriptor descriptor = new OpenFileDescriptor(project, file, 0, 0);
                    n = descriptor.setUseCurrentWindow(true);
                }

                if (n.canNavigate()) {
                    n.navigate(true);
                }
            }
    );
}
 
Example #3
Source File: InjectedLanguageUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static Editor openEditorFor(@Nonnull PsiFile file, @Nonnull Project project) {
  Document document = PsiDocumentManager.getInstance(project).getDocument(file);
  // may return editor injected in current selection in the host editor, not for the file passed as argument
  VirtualFile virtualFile = file.getVirtualFile();
  if (virtualFile == null) {
    return null;
  }
  if (virtualFile instanceof VirtualFileWindow) {
    virtualFile = ((VirtualFileWindow)virtualFile).getDelegate();
  }
  Editor editor = FileEditorManager.getInstance(project).openTextEditor(new OpenFileDescriptor(project, virtualFile, -1), false);
  if (editor == null || editor instanceof EditorWindow || editor.isDisposed()) return editor;
  if (document instanceof DocumentWindowImpl) {
    return EditorWindowImpl.create((DocumentWindowImpl)document, (DesktopEditorImpl)editor, file);
  }
  return editor;
}
 
Example #4
Source File: FileSearchEverywhereContributor.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public boolean processSelectedItem(@Nonnull Object selected, int modifiers, @Nonnull String searchText) {
  if (selected instanceof PsiFile) {
    VirtualFile file = ((PsiFile)selected).getVirtualFile();
    if (file != null && myProject != null) {
      Pair<Integer, Integer> pos = getLineAndColumn(searchText);
      OpenFileDescriptor descriptor = new OpenFileDescriptor(myProject, file, pos.first, pos.second);
      descriptor.setUseCurrentWindow(openInCurrentWindow(modifiers));
      if (descriptor.canNavigate()) {
        descriptor.navigate(true);
        return true;
      }
    }
  }

  return super.processSelectedItem(selected, modifiers, searchText);
}
 
Example #5
Source File: GoToBuckFile.java    From buck with Apache License 2.0 6 votes vote down vote up
@Override
public void actionPerformed(AnActionEvent e) {
  Project project = e.getProject();
  if (project == null) {
    return;
  }
  VirtualFile buckFile = findBuckFile(project);
  if (buckFile != null) {
    final OpenFileDescriptor descriptor = new OpenFileDescriptor(e.getProject(), buckFile);
    // This is for better cursor position.
    final Navigatable n = descriptor.setUseCurrentWindow(false);
    if (!n.canNavigate()) {
      return;
    }
    n.navigate(true);
  }
}
 
Example #6
Source File: BuckTreeViewPanelImpl.java    From buck with Apache License 2.0 6 votes vote down vote up
private static void handleClickOnError(BuckErrorItemNode node) {
  TreeNode parentNode = node.getParent();
  if (parentNode instanceof BuckFileErrorNode) {
    BuckFileErrorNode buckParentNode = (BuckFileErrorNode) parentNode;
    DataContext dataContext = DataManager.getInstance().getDataContext();
    Project project = DataKeys.PROJECT.getData(dataContext);

    String relativePath = buckParentNode.getText().replace(project.getBasePath(), "");

    VirtualFile virtualFile = project.getBaseDir().findFileByRelativePath(relativePath);
    if (virtualFile != null) {
      OpenFileDescriptor openFileDescriptor =
          new OpenFileDescriptor(project, virtualFile, node.getLine() - 1, node.getColumn() - 1);
      openFileDescriptor.navigate(true);
    }
  }
}
 
Example #7
Source File: LSPPsiElement.java    From lsp4intellij with Apache License 2.0 5 votes vote down vote up
public void navigate(boolean requestFocus) {
    Editor editor = FileUtils.editorFromPsiFile(getContainingFile());
    if (editor == null) {
        OpenFileDescriptor descriptor = new OpenFileDescriptor(getProject(), getContainingFile().getVirtualFile(),
                getTextOffset());
        ApplicationUtils.invokeLater(() -> ApplicationUtils
                .writeAction(() -> FileEditorManager.getInstance(getProject()).openTextEditor(descriptor, false)));
    }
}
 
Example #8
Source File: OutputFileUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public Result applyFilter(String line, int entireLength) {
  if (line.startsWith(CONSOLE_OUTPUT_FILE_MESSAGE)) {
    final String filePath = StringUtil.trimEnd(line.substring(CONSOLE_OUTPUT_FILE_MESSAGE.length()), "\n");

    return new Result(entireLength - filePath.length() - 1, entireLength, new HyperlinkInfo() {
      @Override
      public void navigate(final Project project) {
        final VirtualFile file =
          ApplicationManager.getApplication().runWriteAction(new Computable<VirtualFile>() {
            @Nullable
            @Override
            public VirtualFile compute() {
              return LocalFileSystem.getInstance().refreshAndFindFileByPath(FileUtil.toSystemIndependentName(filePath));
            }
          });

        if (file != null) {
          file.refresh(false, false);
          ApplicationManager.getApplication().runReadAction(new Runnable() {
            @Override
            public void run() {
              FileEditorManager.getInstance(project).openTextEditor(new OpenFileDescriptor(project, file), true);
            }
          });
        }
      }
    });
  }
  return null;
}
 
Example #9
Source File: FileStructureDialog.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public Object getData(@Nonnull Key dataId) {
  Object selectedElement = myCommanderPanel.getSelectedValue();

  if (selectedElement instanceof TreeElement) selectedElement = ((StructureViewTreeElement)selectedElement).getValue();

  if (PlatformDataKeys.NAVIGATABLE == dataId) {
    return selectedElement instanceof Navigatable ? selectedElement : myNavigatable;
  }

  if (OpenFileDescriptor.NAVIGATE_IN_EDITOR == dataId) return myEditor;

  return getDataImpl(dataId);
}
 
Example #10
Source File: UsageViewUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void navigateTo(@Nonnull UsageInfo info, boolean requestFocus) {
  int offset = info.getNavigationOffset();
  VirtualFile file = info.getVirtualFile();
  Project project = info.getProject();
  if (file != null) {
    FileEditorManager.getInstance(project).openTextEditor(new OpenFileDescriptor(project, file, offset), requestFocus);
  }
}
 
Example #11
Source File: CompilerTask.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static TextRange getTextRange(final CompilerMessage message) {
  Navigatable navigatable = message.getNavigatable();
  if (navigatable instanceof OpenFileDescriptor) {
    int offset = ((OpenFileDescriptor)navigatable).getOffset();
    return new TextRange(offset, offset);
  }
  return TextRange.EMPTY_RANGE;
}
 
Example #12
Source File: CompilerTask.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static VirtualFile getVirtualFile(final CompilerMessage message) {
  VirtualFile virtualFile = message.getVirtualFile();
  if (virtualFile == null) {
    Navigatable navigatable = message.getNavigatable();
    if (navigatable instanceof OpenFileDescriptor) {
      virtualFile = ((OpenFileDescriptor)navigatable).getFile();
    }
  }
  return virtualFile;
}
 
Example #13
Source File: CompilerMessageImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public Navigatable getNavigatable() {
  if (myNavigatable != null) {
    return myNavigatable;
  }
  final VirtualFile virtualFile = getVirtualFile();
  if (virtualFile != null && virtualFile.isValid()) {
    final int line = getLine() - 1; // editor lines are zero-based
    if (line >= 0) {
      return myNavigatable = new OpenFileDescriptor(myProject, virtualFile, line, Math.max(0, getColumn()-1));
    }
  }
  return null;
}
 
Example #14
Source File: ImplementationViewComponent.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void actionPerformed(AnActionEvent e) {
  PsiElement element = myElements[myIndex];
  PsiElement navigationElement = element.getNavigationElement();
  PsiFile file = getContainingFile(navigationElement);
  if (file == null) return;
  VirtualFile virtualFile = file.getVirtualFile();
  if (virtualFile == null) return;
  Project project = element.getProject();
  FileEditorManagerEx fileEditorManager = FileEditorManagerEx.getInstanceEx(project);
  OpenFileDescriptor descriptor = new OpenFileDescriptor(project, virtualFile, navigationElement.getTextOffset());
  fileEditorManager.openTextEditor(descriptor, myFocusEditor);
}
 
Example #15
Source File: SelectInEditorManagerImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
private Editor openEditor(VirtualFile file, int textOffset){
  if (file == null || !file.isValid()){
    return null;
  }
  OpenFileDescriptor descriptor = new OpenFileDescriptor(myProject, file, textOffset);
  return FileEditorManager.getInstance(myProject).openTextEditor(descriptor, false);
}
 
Example #16
Source File: InspectionResultsView.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
private Navigatable getSelectedNavigatable(final CommonProblemDescriptor descriptor, final PsiElement psiElement) {
  if (descriptor instanceof ProblemDescriptorBase) {
    Navigatable navigatable = ((ProblemDescriptorBase)descriptor).getNavigatable();
    if (navigatable != null) {
      return navigatable;
    }
  }
  if (psiElement == null || !psiElement.isValid()) return null;
  PsiFile containingFile = psiElement.getContainingFile();
  VirtualFile virtualFile = containingFile == null ? null : containingFile.getVirtualFile();

  if (virtualFile != null) {
    int startOffset = psiElement.getTextOffset();
    if (descriptor instanceof ProblemDescriptorBase) {
      final TextRange textRange = ((ProblemDescriptorBase)descriptor).getTextRangeForNavigation();
      if (textRange != null) {
        if (virtualFile instanceof VirtualFileWindow) {
          virtualFile = ((VirtualFileWindow)virtualFile).getDelegate();
        }
        startOffset = textRange.getStartOffset();
      }
    }
    return new OpenFileDescriptor(myProject, virtualFile, startOffset);
  }
  return null;
}
 
Example #17
Source File: TestsUIUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static Navigatable getOpenFileDescriptor(final AbstractTestProxy proxy, final TestConsoleProperties testConsoleProperties, final boolean openFailureLine) {
  final Project project = testConsoleProperties.getProject();

  if (proxy != null) {
    final Location location = proxy.getLocation(project, testConsoleProperties.getScope());
    if (openFailureLine) {
      return proxy.getDescriptor(location, testConsoleProperties);
    }
    final OpenFileDescriptor openFileDescriptor = location == null ? null : location.getOpenFileDescriptor();
    if (openFileDescriptor != null && openFileDescriptor.getFile().isValid()) {
      return openFileDescriptor;
    }
  }
  return null;
}
 
Example #18
Source File: FoldingTestCase.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
private String getFoldingDescription(@Nonnull String content, @Nonnull String fileName, boolean doCheckCollapseStatus) {
  FileType fileTypeByFileName = FileTypeManager.getInstance().getFileTypeByFileName(fileName);

  PsiFile file = createFile(fileName, fileTypeByFileName, content);

  final FileEditorManager fileEditorManager = FileEditorManager.getInstance(myProject);

  Editor editor = fileEditorManager.openTextEditor(new OpenFileDescriptor(myProject, file.getVirtualFile()), false);

  CodeFoldingManager.getInstance(myProject).buildInitialFoldings(editor);

  final FoldingModel model = editor.getFoldingModel();
  final FoldRegion[] foldingRegions = model.getAllFoldRegions();
  final List<Border> borders = new LinkedList<Border>();

  for (FoldRegion region : foldingRegions) {
    borders.add(new Border(Border.LEFT, region.getStartOffset(), region.getPlaceholderText(), region.isExpanded()));
    borders.add(new Border(Border.RIGHT, region.getEndOffset(), "", region.isExpanded()));
  }
  Collections.sort(borders);

  StringBuilder result = new StringBuilder(editor.getDocument().getText());
  for (Border border : borders) {
    result.insert(border.getOffset(), border.isSide() == Border.LEFT ? "<fold text=\'" + border.getText() + "\'" +
                                                                       (doCheckCollapseStatus ? " expand=\'" +
                                                                                                border.isExpanded() +
                                                                                                "\'" : "") +
                                                                       ">" : END_FOLD);
  }

  return result.toString();
}
 
Example #19
Source File: ChangesUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static Navigatable[] getNavigatableArray(@Nonnull Project project, @Nonnull Stream<VirtualFile> files) {
  return files
          .filter(file -> !file.isDirectory())
          .map(file -> new OpenFileDescriptor(project, file))
          .toArray(Navigatable[]::new);
}
 
Example #20
Source File: FileDocumentContentImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public Navigatable getNavigatable(@Nonnull LineCol position) {
  Project project = getProject();
  if (project == null || project.isDefault() || !myFile.isValid()) return null;
  return new OpenFileDescriptor(project, myFile, position.line, position.column);
}
 
Example #21
Source File: FileContentImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public Navigatable getNavigatable() {
  if (myProject == null || myProject.isDefault()) return null;
  if (myHighlightFile == null || !myHighlightFile.isValid()) return null;
  return new OpenFileDescriptor(myProject, myHighlightFile);
}
 
Example #22
Source File: CommandLineProcessor.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static void openFile(@Nonnull UIAccess uiAccess, @Nonnull Project project, @Nonnull VirtualFile virtualFile, int line) {
  uiAccess.give(() -> {
    if (line == -1) {
      new OpenFileDescriptor(project, virtualFile).navigate(true);
    }
    else {
      new OpenFileDescriptor(project, virtualFile, line - 1, 0).navigate(true);
    }
  });
}
 
Example #23
Source File: MigrationsToolWindow.java    From yiistorm with MIT License 5 votes vote down vote up
public void openMigrationFile(final String name) {
    ApplicationManager.getApplication().invokeLater(new Runnable() {
        @Override
        public void run() {
            String migrationPath = yiiProtected.replace(_project.getBasePath(), "").replace("\\", "/");
            VirtualFile baseDir = _project.getBaseDir();
            if (baseDir != null) {
                VirtualFile migrationsFolder = baseDir.findFileByRelativePath(migrationPath + "migrations/");
                if (migrationsFolder != null) {
                    migrationsFolder.refresh(false, true);
                    VirtualFile migrationFile = migrationsFolder.findFileByRelativePath(name + ".php"); //migrationPath + "migrations/" +
                    if (migrationFile != null) {
                        OpenFileDescriptor of = new OpenFileDescriptor(_project, migrationFile);
                        if (of.canNavigate()) {
                            of.navigate(true);
                        }
                    } else {
                        PluginManager.getLogger().error("Migrations file not founded");
                    }
                } else {
                    PluginManager.getLogger().error("Migrations folder not founded");
                }
            }
        }
    });

}
 
Example #24
Source File: PackageDependenciesNode.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
private Editor openTextEditor(boolean focus) {
  final OpenFileDescriptor descriptor = getDescriptor();
  if (descriptor != null) {
    return FileEditorManager.getInstance(getProject()).openTextEditor(descriptor, focus);
  }
  return null;
}
 
Example #25
Source File: OpenFloorc.java    From floobits-intellij with Apache License 2.0 5 votes vote down vote up
public void actionPerformed(AnActionEvent actionEvent) {
    Project project = actionEvent.getProject();

    FloobitsPlugin floobitsPlugin = FloobitsPlugin.getInstance(project);
    ContextImpl context = floobitsPlugin.context;
    File file = new File(Settings.floorcJsonPath);
    if (!file.exists()) {
        boolean created;
        try {
            created = file.createNewFile();
        } catch (IOException e) {
            context.errorMessage("Can't create ~/.floorc.json");
            Flog.error(e);
            return;
        }
        if (!created) {
            context.errorMessage("Can't create ~/.floorc.json");
            return;
        }
    }
    VirtualFile virtualFile = LocalFileSystem.getInstance().refreshAndFindFileByPath(Settings.floorcJsonPath);
    if (virtualFile == null) {
        context.errorMessage("No virtual file");
        return;
    }
    if (project == null) {
        context.errorMessage("Can't open ~/.floorc.json");
        return;
    }
    OpenFileDescriptor openFileDescriptor = new OpenFileDescriptor(project, virtualFile);
    FileEditorManager.getInstance(project).openTextEditor(openFileDescriptor, true);
}
 
Example #26
Source File: IdeUtil.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
@Override
public void run() {
    VirtualFile relativeDirectory = VfsUtil.findRelativeFile(targetPathRelative, project.getBaseDir());
    if(relativeDirectory != null) {
        PsiDirectory directory = PsiManager.getInstance(project).findDirectory(relativeDirectory);

        if(directory != null) {
            PsiFile virtualFile = createFile(directory, fileName, this.content);
            if(virtualFile != null) {
                new OpenFileDescriptor(project, virtualFile.getVirtualFile(), 0).navigate(true);
            }
        }
    }
}
 
Example #27
Source File: AbstractGotoSEContributor.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
protected Navigatable createExtendedNavigatable(PsiElement psi, String searchText, int modifiers) {
  VirtualFile file = PsiUtilCore.getVirtualFile(psi);
  Pair<Integer, Integer> position = getLineAndColumn(searchText);
  boolean positionSpecified = position.first >= 0 || position.second >= 0;
  if (file != null && positionSpecified) {
    OpenFileDescriptor descriptor = new OpenFileDescriptor(psi.getProject(), file, position.first, position.second);
    return descriptor.setUseCurrentWindow(openInCurrentWindow(modifiers));
  }

  return null;
}
 
Example #28
Source File: NewBundleCompilerPass.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Override
public void actionPerformed(@NotNull AnActionEvent event) {

    Project project = getEventProject(event);
    if(project == null) {
        this.setStatus(event, false);
        return;
    }

    PsiDirectory bundleDirContext = BundleClassGeneratorUtil.getBundleDirContext(event);
    if(bundleDirContext == null) {
        return;
    }

    final PhpClass phpClass = BundleClassGeneratorUtil.getBundleClassInDirectory(bundleDirContext);
    if(phpClass == null) {
        return;
    }

    new WriteCommandAction(project) {
        @Override
        protected void run(@NotNull Result result) throws Throwable {
            PsiElement psiFile = PhpBundleFileFactory.invokeCreateCompilerPass(phpClass, null);
            if(psiFile != null) {
                new OpenFileDescriptor(getProject(), psiFile.getContainingFile().getVirtualFile(), 0).navigate(true);
            }
        }

        @Override
        public String getGroupID() {
            return "Create CompilerClass";
        }
    }.execute();

}
 
Example #29
Source File: NewBundleFormAction.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Override
protected void write(@NotNull final Project project, @NotNull final PhpClass phpClass, @NotNull final String className) {
    new WriteCommandAction(project) {
        @Override
        protected void run(@NotNull Result result) throws Throwable {


            String fileTemplate = "form_type_defaults";
            if(PhpElementsUtil.getClassMethod(project, "\\Symfony\\Component\\Form\\AbstractType", "configureOptions") != null) {
                fileTemplate = "form_type_configure";
            }

            PsiElement bundleFile = null;
            try {

                bundleFile = PhpBundleFileFactory.createBundleFile(phpClass, fileTemplate, "Form\\" + className, new HashMap<String, String>() {{
                    put("name", fr.adrienbrault.idea.symfony2plugin.util.StringUtils.underscore(phpClass.getName() + className));
                }});

            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "Error:" + e.getMessage());
                return;
            }

            if(bundleFile != null) {
                new OpenFileDescriptor(getProject(), bundleFile.getContainingFile().getVirtualFile(), 0).navigate(true);
            }
        }

        @Override
        public String getGroupID() {
            return "Create FormType";
        }
    }.execute();

}
 
Example #30
Source File: PsiNavigateUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void navigate(@Nullable final PsiElement psiElement) {
  if (psiElement != null && psiElement.isValid()) {
    final PsiElement navigationElement = psiElement.getNavigationElement();
    final int offset = navigationElement instanceof PsiFile ? -1 : navigationElement.getTextOffset();
    final VirtualFile virtualFile = navigationElement.getContainingFile().getVirtualFile();
    if (virtualFile != null && virtualFile.isValid()) {
      new OpenFileDescriptor(navigationElement.getProject(), virtualFile, offset).navigate(true);
    }
  }
}