Search in sources :

Example 46 with OpenFileDescriptor

use of com.intellij.openapi.fileEditor.OpenFileDescriptor in project intellij-community by JetBrains.

the class GotoFileAction method gotoActionPerformed.

@Override
public void gotoActionPerformed(AnActionEvent e) {
    final Project project = e.getData(CommonDataKeys.PROJECT);
    if (project == null)
        return;
    FeatureUsageTracker.getInstance().triggerFeatureUsed("navigation.popup.file");
    final GotoFileModel gotoFileModel = new GotoFileModel(project);
    GotoActionCallback<FileType> callback = new GotoActionCallback<FileType>() {

        @Override
        protected ChooseByNameFilter<FileType> createFilter(@NotNull ChooseByNamePopup popup) {
            return new GotoFileFilter(popup, gotoFileModel, project);
        }

        @Override
        public void elementChosen(final ChooseByNamePopup popup, final Object element) {
            if (element == null)
                return;
            ApplicationManager.getApplication().assertIsDispatchThread();
            Navigatable n = (Navigatable) element;
            //this is for better cursor position
            if (element instanceof PsiFile) {
                VirtualFile file = ((PsiFile) element).getVirtualFile();
                if (file == null)
                    return;
                OpenFileDescriptor descriptor = new OpenFileDescriptor(project, file, popup.getLinePosition(), popup.getColumnPosition());
                n = descriptor.setUseCurrentWindow(popup.isOpenInCurrentWindowRequested());
            }
            if (n.canNavigate()) {
                n.navigate(true);
            }
        }
    };
    GotoFileItemProvider provider = new GotoFileItemProvider(project, getPsiContext(e), gotoFileModel);
    showNavigationPopup(e, gotoFileModel, callback, IdeBundle.message("go.to.file.toolwindow.title"), true, true, provider);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) NotNull(org.jetbrains.annotations.NotNull) Navigatable(com.intellij.pom.Navigatable) Project(com.intellij.openapi.project.Project) FileType(com.intellij.openapi.fileTypes.FileType) ChooseByNamePopup(com.intellij.ide.util.gotoByName.ChooseByNamePopup) GotoFileModel(com.intellij.ide.util.gotoByName.GotoFileModel) PsiFile(com.intellij.psi.PsiFile) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor)

Example 47 with OpenFileDescriptor

use of com.intellij.openapi.fileEditor.OpenFileDescriptor in project intellij-community by JetBrains.

the class EditorHelper method openInEditor.

@Nullable
public static FileEditor openInEditor(@NotNull PsiElement element, boolean switchToText) {
    PsiFile file;
    int offset;
    if (element instanceof PsiFile) {
        file = (PsiFile) element;
        offset = -1;
    } else {
        file = element.getContainingFile();
        offset = element.getTextOffset();
    }
    //SCR44414
    if (file == null)
        return null;
    VirtualFile virtualFile = file.getVirtualFile();
    if (virtualFile == null)
        return null;
    OpenFileDescriptor descriptor = new OpenFileDescriptor(element.getProject(), virtualFile, offset);
    Project project = element.getProject();
    if (offset == -1 && !switchToText) {
        FileEditorManager.getInstance(project).openEditor(descriptor, false);
    } else {
        FileEditorManager.getInstance(project).openTextEditor(descriptor, false);
    }
    return FileEditorManager.getInstance(project).getSelectedEditor(virtualFile);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) Nullable(org.jetbrains.annotations.Nullable)

Example 48 with OpenFileDescriptor

use of com.intellij.openapi.fileEditor.OpenFileDescriptor in project intellij-community by JetBrains.

the class GotoDeclarationAction method navigateInCurrentEditor.

private static boolean navigateInCurrentEditor(@NotNull PsiElement element, @NotNull PsiFile currentFile, @NotNull Editor currentEditor) {
    if (element.getContainingFile() == currentFile) {
        int offset = element.getTextOffset();
        PsiElement leaf = currentFile.findElementAt(offset);
        // there are fake elements with custom navigation (e.g. opening URL in browser) that override getContainingFile for various reasons
        if (leaf != null && PsiTreeUtil.isAncestor(element, leaf, false)) {
            Project project = element.getProject();
            CommandProcessor.getInstance().executeCommand(project, () -> {
                IdeDocumentHistory.getInstance(project).includeCurrentCommandAsNavigation();
                new OpenFileDescriptor(project, currentFile.getViewProvider().getVirtualFile(), offset).navigateIn(currentEditor);
            }, "", null);
            return true;
        }
    }
    return false;
}
Also used : Project(com.intellij.openapi.project.Project) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) RelativePoint(com.intellij.ui.awt.RelativePoint) PsiElement(com.intellij.psi.PsiElement)

Example 49 with OpenFileDescriptor

use of com.intellij.openapi.fileEditor.OpenFileDescriptor in project intellij-community by JetBrains.

the class GotoTypeDeclarationAction method navigate.

private static void navigate(@NotNull Project project, @NotNull PsiElement symbolType) {
    PsiElement element = symbolType.getNavigationElement();
    assert element != null : "SymbolType :" + symbolType + "; file: " + symbolType.getContainingFile();
    VirtualFile file = element.getContainingFile().getVirtualFile();
    if (file != null) {
        OpenFileDescriptor descriptor = new OpenFileDescriptor(project, file, element.getTextOffset());
        FileEditorManager.getInstance(project).openTextEditor(descriptor, true);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor)

Example 50 with OpenFileDescriptor

use of com.intellij.openapi.fileEditor.OpenFileDescriptor in project intellij-community by JetBrains.

the class TemplateBuilderImpl method run.

@Override
public void run() {
    final Project project = myFile.getProject();
    VirtualFile file = myFile.getVirtualFile();
    assert file != null : "Virtual file is null for " + myFile;
    OpenFileDescriptor descriptor = new OpenFileDescriptor(project, file);
    final Editor editor = FileEditorManager.getInstance(project).openTextEditor(descriptor, true);
    assert editor != null : "Editor is null";
    run(editor, false);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) Editor(com.intellij.openapi.editor.Editor)

Aggregations

OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)155 VirtualFile (com.intellij.openapi.vfs.VirtualFile)97 Project (com.intellij.openapi.project.Project)40 Editor (com.intellij.openapi.editor.Editor)30 PsiElement (com.intellij.psi.PsiElement)27 PsiFile (com.intellij.psi.PsiFile)21 Navigatable (com.intellij.pom.Navigatable)19 Nullable (org.jetbrains.annotations.Nullable)18 FileEditorManager (com.intellij.openapi.fileEditor.FileEditorManager)10 Document (com.intellij.openapi.editor.Document)9 FileEditor (com.intellij.openapi.fileEditor.FileEditor)9 TextRange (com.intellij.openapi.util.TextRange)9 IncorrectOperationException (com.intellij.util.IncorrectOperationException)9 File (java.io.File)9 Result (com.intellij.openapi.application.Result)8 EditorImpl (com.intellij.openapi.editor.impl.EditorImpl)8 NotNull (org.jetbrains.annotations.NotNull)6 TemplateBuilder (com.intellij.codeInsight.template.TemplateBuilder)5 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)5 XmlTag (com.intellij.psi.xml.XmlTag)5