Search in sources :

Example 66 with OpenFileDescriptor

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

the class OverrideResourceAction method forkResourceFile.

private static void forkResourceFile(@NotNull Project project, @NotNull final ResourceFolderType folderType, @NotNull final VirtualFile file, @Nullable final XmlFile xmlFile, @Nullable String myNewFolder, @Nullable Configuration configuration, boolean open) {
    final FolderConfiguration folderConfig;
    if (myNewFolder == null) {
        // Open a file chooser to select the configuration to be created
        VirtualFile parentFolder = file.getParent();
        assert parentFolder != null;
        VirtualFile res = parentFolder.getParent();
        folderConfig = selectFolderConfig(project, res, folderType);
    } else {
        folderConfig = FolderConfiguration.getConfigForFolder(myNewFolder);
    }
    if (folderConfig == null) {
        return;
    }
    final Computable<Pair<String, VirtualFile>> computable = new Computable<Pair<String, VirtualFile>>() {

        @Override
        public Pair<String, VirtualFile> compute() {
            String folderName = folderConfig.getFolderName(folderType);
            try {
                VirtualFile parentFolder = file.getParent();
                assert parentFolder != null;
                VirtualFile res = parentFolder.getParent();
                VirtualFile newParentFolder = res.findChild(folderName);
                if (newParentFolder == null) {
                    newParentFolder = res.createChildDirectory(this, folderName);
                    if (newParentFolder == null) {
                        String message = String.format("Could not create folder %1$s in %2$s", folderName, res.getPath());
                        return Pair.of(message, null);
                    }
                }
                final VirtualFile existing = newParentFolder.findChild(file.getName());
                if (existing != null && existing.exists()) {
                    String message = String.format("File 'res/%1$s/%2$s' already exists!", folderName, file.getName());
                    return Pair.of(message, null);
                }
                // Attempt to get the document from the PSI file rather than the file on disk: get edited contents too
                String text;
                if (xmlFile != null) {
                    text = xmlFile.getText();
                } else {
                    text = StreamUtil.readText(file.getInputStream(), "UTF-8");
                }
                VirtualFile newFile = newParentFolder.createChildData(this, file.getName());
                VfsUtil.saveText(newFile, text);
                return Pair.of(null, newFile);
            } catch (IOException e2) {
                String message = String.format("Failed to create File 'res/%1$s/%2$s' : %3$s", folderName, file.getName(), e2.getMessage());
                return Pair.of(message, null);
            }
        }
    };
    WriteCommandAction<Pair<String, VirtualFile>> action = new WriteCommandAction<Pair<String, VirtualFile>>(project, "Add Resource") {

        @Override
        protected void run(@NotNull Result<Pair<String, VirtualFile>> result) throws Throwable {
            result.setResult(computable.compute());
        }
    };
    Pair<String, VirtualFile> result = action.execute().getResultObject();
    String error = result.getFirst();
    VirtualFile newFile = result.getSecond();
    if (error != null) {
        Messages.showErrorDialog(project, error, "Create Resource");
    } else {
        // First create a compatible configuration based on the current configuration
        if (configuration != null) {
            ConfigurationManager configurationManager = configuration.getConfigurationManager();
            configurationManager.createSimilar(newFile, file);
        }
        if (open) {
            OpenFileDescriptor descriptor = new OpenFileDescriptor(project, newFile, -1);
            FileEditorManager.getInstance(project).openEditor(descriptor, true);
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) IOException(java.io.IOException) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) ConfigurationManager(com.android.tools.idea.configurations.ConfigurationManager) Computable(com.intellij.openapi.util.Computable) Pair(com.android.utils.Pair)

Example 67 with OpenFileDescriptor

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

the class PsiClassNavigation method navigate.

@Override
public void navigate(boolean requestFocus) {
    OpenFileDescriptor fileDescriptor = myOffset >= 0 ? new OpenFileDescriptor(myPsiFile.getProject(), myPsiFile.getVirtualFile(), myOffset) : new OpenFileDescriptor(myPsiFile.getProject(), myPsiFile.getVirtualFile(), myLine, 0);
    fileDescriptor.navigate(requestFocus);
}
Also used : OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor)

Example 68 with OpenFileDescriptor

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

the class ManifestPanel method goToDeclaration.

private void goToDeclaration(Node element) {
    List<? extends Actions.Record> records = ManifestUtils.getRecords(myManifest, element);
    for (Actions.Record record : records) {
        SourceFilePosition sourceFilePosition = ManifestUtils.getActionLocation(myFacet.getModule(), record);
        SourceFile sourceFile = sourceFilePosition.getFile();
        if (!SourceFile.UNKNOWN.equals(sourceFile)) {
            File ioFile = sourceFile.getSourceFile();
            if (ioFile != null) {
                VirtualFile file = LocalFileSystem.getInstance().findFileByIoFile(ioFile);
                assert file != null;
                int line = -1;
                int column = 0;
                SourcePosition sourcePosition = sourceFilePosition.getPosition();
                if (!SourcePosition.UNKNOWN.equals(sourcePosition)) {
                    line = sourcePosition.getStartLine();
                    column = sourcePosition.getStartColumn();
                }
                Project project = myFacet.getModule().getProject();
                OpenFileDescriptor descriptor = new OpenFileDescriptor(project, file, line, column);
                FileEditorManager.getInstance(project).openEditor(descriptor, true);
                break;
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) SourceFilePosition(com.android.ide.common.blame.SourceFilePosition) IdeActions(com.intellij.openapi.actionSystem.IdeActions) Actions(com.android.manifmerger.Actions) SourcePosition(com.android.ide.common.blame.SourcePosition) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) SourceFile(com.android.ide.common.blame.SourceFile) XmlFile(com.intellij.psi.xml.XmlFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) SourceFile(com.android.ide.common.blame.SourceFile) GradleBuildFile(com.android.tools.idea.gradle.parser.GradleBuildFile) File(java.io.File)

Example 69 with OpenFileDescriptor

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

the class HtmlLinkManager method openEditor.

private static boolean openEditor(@NotNull Project project, @NotNull VirtualFile file, int line, int column) {
    OpenFileDescriptor descriptor = new OpenFileDescriptor(project, file, line, column);
    FileEditorManager manager = FileEditorManager.getInstance(project);
    // Attempt to prefer text editor if it's available for this file
    if (manager.openTextEditor(descriptor, true) != null) {
        return true;
    }
    return !manager.openEditor(descriptor, true).isEmpty();
}
Also used : FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor)

Example 70 with OpenFileDescriptor

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

the class HtmlLinkManager method handleOpenStackUrl.

private static void handleOpenStackUrl(@NotNull String url, @NotNull Module module) {
    assert url.startsWith(URL_OPEN) : url;
    // Syntax: URL_OPEN + className + '#' + methodName + ';' + fileName + ':' + lineNumber;
    int start = URL_OPEN.length();
    int semi = url.indexOf(';', start);
    String className;
    String fileName;
    int line;
    if (semi != -1) {
        className = url.substring(start, semi);
        int colon = url.indexOf(':', semi + 1);
        if (colon != -1) {
            fileName = url.substring(semi + 1, colon);
            line = Integer.decode(url.substring(colon + 1));
        } else {
            fileName = url.substring(semi + 1);
            line = -1;
        }
    // Attempt to open file
    } else {
        className = url.substring(start);
        fileName = null;
        line = -1;
    }
    String method = null;
    int hash = className.indexOf('#');
    if (hash != -1) {
        method = className.substring(hash + 1);
        className = className.substring(0, hash);
    }
    Project project = module.getProject();
    PsiClass clz = JavaPsiFacade.getInstance(project).findClass(className, GlobalSearchScope.allScope(project));
    if (clz != null) {
        PsiFile containingFile = clz.getContainingFile();
        if (fileName != null && containingFile != null && line != -1) {
            VirtualFile virtualFile = containingFile.getVirtualFile();
            if (virtualFile != null) {
                String name = virtualFile.getName();
                if (fileName.equals(name)) {
                    // Use the line number rather than the methodName
                    openEditor(project, containingFile, line - 1, -1);
                    return;
                }
            }
        }
        if (method != null) {
            PsiMethod[] methodsByName = clz.findMethodsByName(method, true);
            for (PsiMethod m : methodsByName) {
                PsiFile psiFile = m.getContainingFile();
                if (psiFile != null) {
                    VirtualFile virtualFile = psiFile.getVirtualFile();
                    if (virtualFile != null) {
                        OpenFileDescriptor descriptor = new OpenFileDescriptor(project, virtualFile, m.getTextOffset());
                        FileEditorManager.getInstance(project).openEditor(descriptor, true);
                        return;
                    }
                }
            }
        }
        if (fileName != null) {
            PsiFile[] files = FilenameIndex.getFilesByName(project, fileName, GlobalSearchScope.allScope(project));
            for (PsiFile psiFile : files) {
                if (openEditor(project, psiFile, line != -1 ? line - 1 : -1, -1)) {
                    break;
                }
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor)

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