Search in sources :

Example 31 with VirtualFile

use of com.intellij.openapi.vfs.VirtualFile in project buck by facebook.

the class BuckGotoProvider method getGotoDeclarationTarget.

@Override
public PsiElement getGotoDeclarationTarget(@Nullable PsiElement source, Editor editor) {
    if (source != null && source.getLanguage() instanceof BuckLanguage) {
        // The parent type of the element must be BuckValue.
        BuckValue value = PsiTreeUtil.getParentOfType(source, BuckValue.class);
        if (value == null) {
            return null;
        }
        final Project project = editor.getProject();
        if (project == null) {
            return null;
        }
        String target = source.getText();
        if ((target.startsWith("'") && target.endsWith("'")) || (target.startsWith("\"") && target.endsWith("\""))) {
            target = target.substring(1, target.length() - 1);
        }
        VirtualFile targetFile = // Try to find the BUCK file
        Optional.fromNullable(BuckBuildUtil.getBuckFileFromAbsoluteTarget(project, target)).or(Optional.fromNullable(source.getContainingFile().getParent().getVirtualFile().findFileByRelativePath(target))).orNull();
        if (targetFile == null) {
            return null;
        }
        project.getMessageBus().syncPublisher(IntellijBuckAction.EVENT).consume(this.getClass().toString());
        return PsiManager.getInstance(project).findFile(targetFile);
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) BuckValue(com.facebook.buck.intellij.ideabuck.lang.psi.BuckValue) Project(com.intellij.openapi.project.Project) BuckLanguage(com.facebook.buck.intellij.ideabuck.lang.BuckLanguage)

Example 32 with VirtualFile

use of com.intellij.openapi.vfs.VirtualFile in project buck by facebook.

the class BuckToolWindowFactory method handleClickOnError.

private void handleClickOnError(BuckTreeNodeDetailError node) {
    TreeNode parentNode = node.getParent();
    if (parentNode instanceof BuckTreeNodeFileError) {
        BuckTreeNodeFileError buckParentNode = (BuckTreeNodeFileError) parentNode;
        DataContext dataContext = DataManager.getInstance().getDataContext();
        Project project = DataKeys.PROJECT.getData(dataContext);
        String relativePath = buckParentNode.getFilePath().replace(project.getBasePath(), "");
        VirtualFile virtualFile = project.getBaseDir().findFileByRelativePath(relativePath);
        OpenFileDescriptor openFileDescriptor = new OpenFileDescriptor(project, virtualFile, node.getLine() - 1, node.getColumn() - 1);
        openFileDescriptor.navigate(true);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) DataContext(com.intellij.openapi.actionSystem.DataContext) TreeNode(javax.swing.tree.TreeNode) BuckTreeNodeFileError(com.facebook.buck.intellij.ideabuck.ui.tree.BuckTreeNodeFileError) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor)

Example 33 with VirtualFile

use of com.intellij.openapi.vfs.VirtualFile in project buck by facebook.

the class BuckCopyPasteProcessor method referenceNameToBuckFile.

private VirtualFile referenceNameToBuckFile(Project project, String reference) {
    // First test if it is a absolute path of a file.
    File tryFile = new File(reference);
    if (tryFile != null) {
        VirtualFile file = VfsUtil.findFileByIoFile(tryFile, true);
        if (file != null) {
            return BuckBuildUtil.getBuckFileFromDirectory(file.getParent());
        }
    }
    // Try class firstly.
    PsiClass classElement = JavaPsiFacade.getInstance(project).findClass(reference, GlobalSearchScope.allScope(project));
    if (classElement != null) {
        VirtualFile file = PsiUtilCore.getVirtualFile(classElement);
        return BuckBuildUtil.getBuckFileFromDirectory(file.getParent());
    }
    // Then try package.
    PsiPackage packageElement = JavaPsiFacade.getInstance(project).findPackage(reference);
    if (packageElement != null) {
        PsiDirectory directory = packageElement.getDirectories()[0];
        return BuckBuildUtil.getBuckFileFromDirectory(directory.getVirtualFile());
    }
    // Extract the package from the reference.
    int index = reference.lastIndexOf(".");
    if (index == -1) {
        return null;
    }
    reference = reference.substring(0, index);
    // Try to find the package again.
    packageElement = JavaPsiFacade.getInstance(project).findPackage(reference);
    if (packageElement != null) {
        PsiDirectory directory = packageElement.getDirectories()[0];
        return BuckBuildUtil.getBuckFileFromDirectory(directory.getVirtualFile());
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiDirectory(com.intellij.psi.PsiDirectory) PsiClass(com.intellij.psi.PsiClass) PsiPackage(com.intellij.psi.PsiPackage) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) BuckFile(com.facebook.buck.intellij.ideabuck.lang.BuckFile) File(java.io.File)

Example 34 with VirtualFile

use of com.intellij.openapi.vfs.VirtualFile in project android-selector-intellij-plugin by importre.

the class AndroidSelector method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    final VirtualFile dir = e.getData(LangDataKeys.VIRTUAL_FILE);
    if (dir == null) {
        return;
    }
    Project project = e.getProject();
    AndroidSelectorDialog dialog = new AndroidSelectorDialog(project, dir);
    dialog.show();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) AndroidSelectorDialog(importre.intellij.android.selector.form.AndroidSelectorDialog)

Example 35 with VirtualFile

use of com.intellij.openapi.vfs.VirtualFile in project android-selector-intellij-plugin by importre.

the class AndroidSelectorDialog method createDrawableV21.

private void createDrawableV21(String filename, String color, String pressed) throws Exception {
    VirtualFile child = dir.findChild(drawableV21Dir);
    if (child == null) {
        child = dir.createChildDirectory(null, drawableV21Dir);
    }
    VirtualFile newXmlFile = child.findChild(filename);
    if (newXmlFile != null && newXmlFile.exists()) {
        newXmlFile.delete(null);
    }
    newXmlFile = child.createChildData(null, filename);
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.newDocument();
    Element root = doc.createElement("ripple");
    root.setAttributeNS(nsUri, "xmlns:android", androidUri);
    root.setAttribute("android:color", pressed);
    doc.appendChild(root);
    Element item = doc.createElement("item");
    item.setAttribute("android:drawable", color);
    root.appendChild(item);
    OutputStream os = newXmlFile.getOutputStream(null);
    PrintWriter out = new PrintWriter(os);
    StringWriter writer = new StringWriter();
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(INDENT_SPACE, "4");
    transformer.transform(new DOMSource(doc), new StreamResult(writer));
    out.println(writer.getBuffer().toString());
    out.close();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document)

Aggregations

VirtualFile (com.intellij.openapi.vfs.VirtualFile)5465 File (java.io.File)762 Project (com.intellij.openapi.project.Project)720 Nullable (org.jetbrains.annotations.Nullable)720 NotNull (org.jetbrains.annotations.NotNull)703 PsiFile (com.intellij.psi.PsiFile)571 Module (com.intellij.openapi.module.Module)501 IOException (java.io.IOException)327 ArrayList (java.util.ArrayList)260 Document (com.intellij.openapi.editor.Document)244 PsiElement (com.intellij.psi.PsiElement)209 Test (org.junit.Test)196 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)124 PsiDirectory (com.intellij.psi.PsiDirectory)124 XmlFile (com.intellij.psi.xml.XmlFile)124 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)116 Editor (com.intellij.openapi.editor.Editor)115 FileChooserDescriptor (com.intellij.openapi.fileChooser.FileChooserDescriptor)101 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)91 List (java.util.List)90