Search in sources :

Example 76 with OpenFileDescriptor

use of com.intellij.openapi.fileEditor.OpenFileDescriptor in project psiviewer by cmf.

the class EditorCaretMover method openInEditor.

public Editor openInEditor(PsiElement element) {
    PsiFile psiFile;
    int i;
    if (element instanceof PsiFile) {
        psiFile = (PsiFile) element;
        i = -1;
    } else {
        psiFile = PluginPsiUtil.getContainingFile(element);
        i = element.getTextOffset();
    }
    if (psiFile == null)
        return null;
    final VirtualFile virtualFile = psiFile.getVirtualFile();
    if (virtualFile == null)
        return null;
    // 20050826 IDEA 5.0.1 #3461
    OpenFileDescriptor fileDesc = new OpenFileDescriptor(_project, virtualFile, i);
    disableMovementOneTime();
    return FileEditorManager.getInstance(_project).openTextEditor(fileDesc, false);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor)

Example 77 with OpenFileDescriptor

use of com.intellij.openapi.fileEditor.OpenFileDescriptor in project intellij by bazelbuild.

the class BlazeBuildSystemServiceTest method testAddDependencyWithBuildTargetPsi.

@Test
public void testAddDependencyWithBuildTargetPsi() throws Exception {
    PsiElement buildTargetPsi = mock(PsiElement.class);
    PsiFile psiFile = mock(PsiFile.class);
    BuildReferenceManager buildReferenceManager = BuildReferenceManager.getInstance(project);
    when(buildReferenceManager.resolveLabel(Label.create("//foo:bar"))).thenReturn(buildTargetPsi);
    when(buildTargetPsi.getContainingFile()).thenReturn(psiFile);
    when(buildTargetPsi.getTextOffset()).thenReturn(1337);
    VirtualFile buildFile = VirtualFileSystemProvider.getInstance().getSystem().findFileByPath("/foo/BUILD");
    assertThat(buildFile).isNotNull();
    when(psiFile.getVirtualFile()).thenReturn(buildFile);
    // Doesn't matter.
    String dependency = "com.android.foo:bar";
    service.addDependency(module, dependency);
    ArgumentCaptor<OpenFileDescriptor> descriptorCaptor = ArgumentCaptor.forClass(OpenFileDescriptor.class);
    verify(FileEditorManager.getInstance(project)).openTextEditor(descriptorCaptor.capture(), eq(true));
    OpenFileDescriptor descriptor = descriptorCaptor.getValue();
    assertThat(descriptor.getProject()).isEqualTo(project);
    assertThat(descriptor.getFile()).isEqualTo(buildFile);
    assertThat(descriptor.getOffset()).isEqualTo(1337);
    verifyNoMoreInteractions(FileEditorManager.getInstance(project));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) MockVirtualFile(com.intellij.mock.MockVirtualFile) PsiFile(com.intellij.psi.PsiFile) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) BuildReferenceManager(com.google.idea.blaze.base.lang.buildfile.references.BuildReferenceManager) PsiElement(com.intellij.psi.PsiElement) Test(org.junit.Test)

Example 78 with OpenFileDescriptor

use of com.intellij.openapi.fileEditor.OpenFileDescriptor in project intellij by bazelbuild.

the class AddGeneratedResourceDirectoryNavigatable method addDirectoryToProjectView.

private static void addDirectoryToProjectView(Project project, File projectViewFile, ArtifactLocation generatedResDir) {
    ProjectViewEdit edit = ProjectViewEdit.editLocalProjectView(project, builder -> {
        ListSection<GenfilesPath> existingSection = builder.getLast(GeneratedAndroidResourcesSection.KEY);
        ListSection.Builder<GenfilesPath> directoryBuilder = ListSection.update(GeneratedAndroidResourcesSection.KEY, existingSection);
        directoryBuilder.add(new GenfilesPath(generatedResDir.getRelativePath()));
        builder.replace(existingSection, directoryBuilder);
        return true;
    });
    if (edit == null) {
        Messages.showErrorDialog("Could not modify project view. Check for errors in your project view and try again", "Error");
        return;
    }
    edit.apply();
    VirtualFile projectView = VfsUtil.findFileByIoFile(projectViewFile, false);
    if (projectView != null) {
        FileEditorManager.getInstance(project).openEditor(new OpenFileDescriptor(project, projectView), true);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ListSection(com.google.idea.blaze.base.projectview.section.ListSection) ProjectViewEdit(com.google.idea.blaze.base.projectview.ProjectViewEdit) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) GenfilesPath(com.google.idea.blaze.android.projectview.GenfilesPath)

Example 79 with OpenFileDescriptor

use of com.intellij.openapi.fileEditor.OpenFileDescriptor in project intellij by bazelbuild.

the class BlazeBuildSystemService method addDependency.

@Override
public void addDependency(Module module, String artifact) {
    Project project = module.getProject();
    BlazeProjectData blazeProjectData = BlazeProjectDataManager.getInstance(project).getBlazeProjectData();
    if (blazeProjectData == null) {
        return;
    }
    AndroidResourceModuleRegistry registry = AndroidResourceModuleRegistry.getInstance(project);
    TargetIdeInfo targetIdeInfo = blazeProjectData.targetMap.get(registry.getTargetKey(module));
    if (targetIdeInfo == null || targetIdeInfo.buildFile == 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.key.label);
    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.
        File buildIoFile = blazeProjectData.artifactLocationDecoder.decode(targetIdeInfo.buildFile);
        VirtualFile buildVirtualFile = VfsUtils.resolveVirtualFile(buildIoFile);
        if (buildVirtualFile != null) {
            fileEditorManager.openFile(buildVirtualFile, true);
        }
    }
}
Also used : TargetIdeInfo(com.google.idea.blaze.base.ideinfo.TargetIdeInfo) VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) BlazeProjectData(com.google.idea.blaze.base.model.BlazeProjectData) AndroidResourceModuleRegistry(com.google.idea.blaze.android.sync.model.AndroidResourceModuleRegistry) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) PsiElement(com.intellij.psi.PsiElement)

Example 80 with OpenFileDescriptor

use of com.intellij.openapi.fileEditor.OpenFileDescriptor in project intellij by bazelbuild.

the class ProjectViewHelper method openProjectViewFile.

static void openProjectViewFile(Project project, ProjectViewFile projectViewFile) {
    File file = projectViewFile.projectViewFile;
    if (file != null) {
        VirtualFile virtualFile = VfsUtil.findFileByIoFile(file, true);
        if (virtualFile != null) {
            OpenFileDescriptor descriptor = new OpenFileDescriptor(project, virtualFile);
            FileEditorManager.getInstance(project).openTextEditor(descriptor, true);
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) ProjectViewFile(com.google.idea.blaze.base.projectview.ProjectViewSet.ProjectViewFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

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