Search in sources :

Example 26 with OpenFileDescriptor

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

the class CopyAbstractMethodImplementationHandler method copyImplementation.

private void copyImplementation(final PsiMethod sourceMethod) {
    if (!FileModificationService.getInstance().preparePsiElementForWrite(sourceMethod))
        return;
    final List<PsiMethod> generatedMethods = new ArrayList<>();
    new WriteCommandAction(myProject, getTargetFiles()) {

        @Override
        protected void run(@NotNull final Result result) throws Throwable {
            for (PsiEnumConstant enumConstant : myTargetEnumConstants) {
                PsiClass initializingClass = enumConstant.getOrCreateInitializingClass();
                myTargetClasses.add(initializingClass);
            }
            for (PsiClass psiClass : myTargetClasses) {
                final Collection<PsiMethod> methods = OverrideImplementUtil.overrideOrImplementMethod(psiClass, myMethod, true);
                final Iterator<PsiMethod> iterator = methods.iterator();
                if (!iterator.hasNext())
                    continue;
                PsiMethod overriddenMethod = iterator.next();
                final PsiCodeBlock body = overriddenMethod.getBody();
                final PsiCodeBlock sourceBody = sourceMethod.getBody();
                assert body != null && sourceBody != null;
                ChangeContextUtil.encodeContextInfo(sourceBody, true);
                final PsiElement newBody = body.replace(sourceBody.copy());
                ChangeContextUtil.decodeContextInfo(newBody, psiClass, null);
                PsiSubstitutor substitutor = TypeConversionUtil.getSuperClassSubstitutor(mySourceClass, psiClass, PsiSubstitutor.EMPTY);
                PsiElement anchor = OverrideImplementUtil.getDefaultAnchorToOverrideOrImplement(psiClass, sourceMethod, substitutor);
                try {
                    if (anchor != null) {
                        overriddenMethod = (PsiMethod) anchor.getParent().addBefore(overriddenMethod, anchor);
                    } else {
                        overriddenMethod = (PsiMethod) psiClass.addBefore(overriddenMethod, psiClass.getRBrace());
                    }
                    generatedMethods.add(overriddenMethod);
                } catch (IncorrectOperationException e) {
                    LOG.error(e);
                }
            }
        }
    }.execute();
    if (!generatedMethods.isEmpty()) {
        PsiMethod target = generatedMethods.get(0);
        PsiFile psiFile = target.getContainingFile();
        FileEditorManager fileEditorManager = FileEditorManager.getInstance(psiFile.getProject());
        Editor editor = fileEditorManager.openTextEditor(new OpenFileDescriptor(psiFile.getProject(), psiFile.getVirtualFile()), false);
        if (editor != null) {
            GenerateMembersUtil.positionCaret(editor, target, true);
            editor.getScrollingModel().scrollToCaret(ScrollType.CENTER);
        }
    }
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Result(com.intellij.openapi.application.Result) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) IncorrectOperationException(com.intellij.util.IncorrectOperationException) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) Editor(com.intellij.openapi.editor.Editor)

Example 27 with OpenFileDescriptor

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

the class CodeInsightTestCase method createEditor.

protected Editor createEditor(@NotNull VirtualFile file) {
    final FileEditorManager instance = FileEditorManager.getInstance(myProject);
    if (file.getFileType().isBinary())
        return null;
    PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
    Editor editor = instance.openTextEditor(new OpenFileDescriptor(myProject, file, 0), false);
    ((EditorImpl) editor).setCaretActive();
    PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
    DaemonCodeAnalyzer.getInstance(getProject()).restart();
    return editor;
}
Also used : FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) EditorImpl(com.intellij.openapi.editor.impl.EditorImpl) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) TextEditor(com.intellij.openapi.fileEditor.TextEditor) Editor(com.intellij.openapi.editor.Editor)

Example 28 with OpenFileDescriptor

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

the class CreateFromUsageBaseFix method positionCursor.

@Nullable("null means unable to open the editor")
protected static Editor positionCursor(@NotNull Project project, @NotNull PsiFile targetFile, @NotNull PsiElement element) {
    TextRange range = element.getTextRange();
    LOG.assertTrue(range != null, element.getClass());
    int textOffset = range.getStartOffset();
    VirtualFile file = targetFile.getVirtualFile();
    if (file == null) {
        file = PsiUtilCore.getVirtualFile(element);
        if (file == null)
            return null;
    }
    OpenFileDescriptor descriptor = new OpenFileDescriptor(project, file, textOffset);
    // avoid centering caret in editor if it's already visible
    descriptor.setScrollType(ScrollType.MAKE_VISIBLE);
    return FileEditorManager.getInstance(project).openTextEditor(descriptor, true);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) TextRange(com.intellij.openapi.util.TextRange) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) Nullable(org.jetbrains.annotations.Nullable)

Example 29 with OpenFileDescriptor

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

the class OpenRepositoryVersionAction method openRepositoryVersion.

private static void openRepositoryVersion(@NotNull Project project, @NotNull Change[] changes) {
    for (Change change : changes) {
        ContentRevision revision = change.getAfterRevision();
        if (revision == null || revision.getFile().isDirectory())
            continue;
        VirtualFile vFile = ContentRevisionVirtualFile.create(revision);
        Navigatable navigatable = new OpenFileDescriptor(project, vFile);
        navigatable.navigate(true);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ContentRevisionVirtualFile(com.intellij.openapi.vcs.vfs.ContentRevisionVirtualFile) ContentRevision(com.intellij.openapi.vcs.changes.ContentRevision) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) Change(com.intellij.openapi.vcs.changes.Change) Navigatable(com.intellij.pom.Navigatable)

Example 30 with OpenFileDescriptor

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

the class CommandLineProcessor method doOpenFile.

@Nullable
private static Project doOpenFile(VirtualFile file, int line, boolean tempProject) {
    Project[] projects = ProjectManager.getInstance().getOpenProjects();
    if (projects.length == 0 || tempProject) {
        PlatformProjectOpenProcessor processor = PlatformProjectOpenProcessor.getInstanceIfItExists();
        if (processor != null) {
            EnumSet<PlatformProjectOpenProcessor.Option> options = EnumSet.noneOf(PlatformProjectOpenProcessor.Option.class);
            if (tempProject) {
                options.add(PlatformProjectOpenProcessor.Option.TEMP_PROJECT);
                options.add(PlatformProjectOpenProcessor.Option.FORCE_NEW_FRAME);
            }
            return PlatformProjectOpenProcessor.doOpenProject(file, null, line, null, options);
        }
        Messages.showErrorDialog("No project found to open file in", "Cannot Open File");
        return null;
    } else {
        NonProjectFileWritingAccessProvider.allowWriting(file);
        Project project = findBestProject(file, projects);
        OpenFileDescriptor descriptor = line == -1 ? new OpenFileDescriptor(project, file) : new OpenFileDescriptor(project, file, line - 1, 0);
        descriptor.navigate(true);
        return project;
    }
}
Also used : Project(com.intellij.openapi.project.Project) PlatformProjectOpenProcessor(com.intellij.platform.PlatformProjectOpenProcessor) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) Nullable(org.jetbrains.annotations.Nullable)

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