Search in sources :

Example 56 with PsiFile

use of com.intellij.psi.PsiFile in project idea-handlebars by dmarcotte.

the class HbFormatterTest method doFormatterActionTest.

private void doFormatterActionTest(final FormatRunnableFactory formatAction, final String beforeText, String textAfter, LanguageFileType templateDataLanguageType) {
    PsiFile baseFile = myFixture.configureByText("A.hbs", beforeText);
    VirtualFile virtualFile = baseFile.getVirtualFile();
    assert virtualFile != null;
    TemplateDataLanguageMappings.getInstance(getProject()).setMapping(virtualFile, templateDataLanguageType.getLanguage());
    // fetch a fresh instance of the file -- the template data mapping creates a new instance,
    // which was causing problems in PsiFileImpl.isValid()
    final PsiFile file = PsiManager.getInstance(getProject()).findFile(virtualFile);
    assert file != null;
    CommandProcessor.getInstance().executeCommand(getProject(), new Runnable() {

        @Override
        public void run() {
            ApplicationManager.getApplication().runWriteAction(formatAction.createFormatRunnable(file));
        }
    }, "", "");
    TemplateDataLanguageMappings.getInstance(getProject()).cleanupForNextTest();
    assertEquals("Reformat Code failed", prepareText(textAfter), prepareText(file.getText()));
    assertEquals("Reformat Code failed", prepareText(textAfter), prepareText(file.getText()));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile)

Example 57 with PsiFile

use of com.intellij.psi.PsiFile in project idea-handlebars by dmarcotte.

the class HbFormatterTest method doTextTest.

/**
   * This method runs both a full-file reformat on beforeText, and a line-by-line reformat.  Though the tests
   * would output slightly better errors if these were separate tests, enforcing that they are always both run
   * for any test defined is the easiest way to ensure that the line-by-line is not messed up by formatter changes
   *
   * @param beforeText               The text run the formatter on
   * @param textAfter                The expected result after running the formatter
   * @param templateDataLanguageType The templated language of the file
   * @throws IncorrectOperationException
   */
void doTextTest(final String beforeText, String textAfter, LanguageFileType templateDataLanguageType) throws IncorrectOperationException {
    // define action to run "Reformat Code" on the whole "file" defined by beforeText
    FormatRunnableFactory fullFormatRunnableFactory = new FormatRunnableFactory() {

        @Override
        Runnable createFormatRunnable(final PsiFile file) {
            return new Runnable() {

                @Override
                public void run() {
                    try {
                        TextRange rangeToUse = file.getTextRange();
                        CodeStyleManager styleManager = CodeStyleManager.getInstance(getProject());
                        styleManager.reformatText(file, rangeToUse.getStartOffset(), rangeToUse.getEndOffset());
                    } catch (IncorrectOperationException e) {
                        assertTrue(e.getLocalizedMessage(), false);
                    }
                }
            };
        }
    };
    // define action to run "Adjust line indent" on every line in the "file" defined by beforeText
    FormatRunnableFactory lineFormatRunnableFactory = new FormatRunnableFactory() {

        @Override
        Runnable createFormatRunnable(final PsiFile file) {
            return new Runnable() {

                @Override
                public void run() {
                    try {
                        final PsiDocumentManager manager = PsiDocumentManager.getInstance(getProject());
                        final Document document = manager.getDocument(file);
                        assert document != null;
                        for (int lineNum = 0; lineNum < document.getLineCount(); lineNum++) {
                            CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(getProject());
                            int offset = document.getLineStartOffset(lineNum);
                            // if this breaks at some point, we should
                            @SuppressWarnings("deprecation") boolean // instead of doing the indent directly
                            lineToBeIndented = codeStyleManager.isLineToBeIndented(file, offset);
                            if (lineToBeIndented) {
                                codeStyleManager.adjustLineIndent(file, offset);
                            }
                        }
                    } catch (IncorrectOperationException e) {
                        assertTrue(e.getLocalizedMessage(), false);
                    }
                }
            };
        }
    };
    doFormatterActionTest(fullFormatRunnableFactory, beforeText, textAfter, templateDataLanguageType);
    doFormatterActionTest(lineFormatRunnableFactory, beforeText, textAfter, templateDataLanguageType);
}
Also used : CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) PsiFile(com.intellij.psi.PsiFile) TextRange(com.intellij.openapi.util.TextRange) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Document(com.intellij.openapi.editor.Document) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 58 with PsiFile

use of com.intellij.psi.PsiFile in project buck by facebook.

the class BuckAutoDepsContributor method getVirtualFileFromClassname.

@Nullable
public VirtualFile getVirtualFileFromClassname(String classname) {
    GlobalSearchScope scope = GlobalSearchScope.allScope(mProject);
    PsiClass psiClass = JavaPsiFacade.getInstance(mProject).findClass(classname, scope);
    if (psiClass == null) {
        return null;
    }
    PsiFile psiFile = psiClass.getContainingFile();
    if (psiFile == null) {
        return null;
    }
    return psiFile.getVirtualFile();
}
Also used : GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiClass(com.intellij.psi.PsiClass) PsiFile(com.intellij.psi.PsiFile) Nullable(javax.annotation.Nullable)

Example 59 with PsiFile

use of com.intellij.psi.PsiFile in project android-parcelable-intellij-plugin by mcharmas.

the class ParcelableAction method getPsiClassFromContext.

private PsiClass getPsiClassFromContext(AnActionEvent e) {
    PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE);
    Editor editor = e.getData(PlatformDataKeys.EDITOR);
    if (psiFile == null || editor == null) {
        return null;
    }
    int offset = editor.getCaretModel().getOffset();
    PsiElement element = psiFile.findElementAt(offset);
    return PsiTreeUtil.getParentOfType(element, PsiClass.class);
}
Also used : PsiFile(com.intellij.psi.PsiFile) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement)

Example 60 with PsiFile

use of com.intellij.psi.PsiFile in project folding-plugin by dmytrodanylyk.

the class ProjectStructureProvider method createComposedFiles.

@NotNull
private List<AbstractTreeNode> createComposedFiles(@NotNull Collection<AbstractTreeNode> fileNodes, ViewSettings viewSettings) {
    List<AbstractTreeNode> resultList = new ArrayList<>();
    Project project = Utils.getCurrentProject();
    if (project != null) {
        HashSet<String> composedDirNameSet = new HashSet<>();
        List<AbstractTreeNode> notComposedFileNodes = new ArrayList<>();
        final boolean customPrefix = PropertiesComponent.getInstance().getBoolean(SettingConfigurable.PREFIX_CUSTOM_USE, false);
        Pattern pattern = customPrefix ? Pattern.compile(PropertiesComponent.getInstance().getValue(SettingConfigurable.PREFIX_PATTERN, SettingConfigurable.DEFAULT_PATTERN)) : null;
        for (AbstractTreeNode fileNode : fileNodes) {
            if (fileNode.getValue() instanceof PsiFile) {
                PsiFile psiFile = (PsiFile) fileNode.getValue();
                String fileName = psiFile.getName();
                if (customPrefix) {
                    Matcher m = pattern.matcher(fileName);
                    if (m.find()) {
                        String composedDirName = m.group(0);
                        composedDirNameSet.add(composedDirName);
                    } else {
                        notComposedFileNodes.add(fileNode);
                    }
                } else {
                    int endIndex = fileName.indexOf(COMPOSE_BY_CHAR);
                    if (endIndex != -1) {
                        String composedDirName = fileName.substring(0, endIndex);
                        composedDirNameSet.add(composedDirName);
                    } else {
                        notComposedFileNodes.add(fileNode);
                    }
                }
            }
        }
        for (String composedDirName : composedDirNameSet) {
            List<AbstractTreeNode> composedFileNodes = filterByDirName(fileNodes, composedDirName);
            PsiFile psiFile = (PsiFile) composedFileNodes.get(0).getValue();
            DirectoryNode composedDirNode = new DirectoryNode(project, viewSettings, psiFile, composedDirName);
            composedDirNode.addAllChildren(composedFileNodes);
            resultList.add(composedDirNode);
        }
        if (!notComposedFileNodes.isEmpty()) {
            PsiFile psiFile = (PsiFile) notComposedFileNodes.get(0).getValue();
            DirectoryNode composedDirNode = new DirectoryNode(project, viewSettings, psiFile, OTHER_NODE);
            composedDirNode.addAllChildren(notComposedFileNodes);
            resultList.add(composedDirNode);
        }
    }
    return resultList;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) HashSet(java.util.HashSet) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PsiFile (com.intellij.psi.PsiFile)1785 VirtualFile (com.intellij.openapi.vfs.VirtualFile)496 PsiElement (com.intellij.psi.PsiElement)468 Project (com.intellij.openapi.project.Project)267 Nullable (org.jetbrains.annotations.Nullable)267 NotNull (org.jetbrains.annotations.NotNull)248 Document (com.intellij.openapi.editor.Document)181 Editor (com.intellij.openapi.editor.Editor)168 XmlFile (com.intellij.psi.xml.XmlFile)126 PsiDirectory (com.intellij.psi.PsiDirectory)114 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)109 Module (com.intellij.openapi.module.Module)108 TextRange (com.intellij.openapi.util.TextRange)88 ArrayList (java.util.ArrayList)84 XmlTag (com.intellij.psi.xml.XmlTag)68 File (java.io.File)58 PsiManager (com.intellij.psi.PsiManager)56 PsiClass (com.intellij.psi.PsiClass)51 List (java.util.List)46 Language (com.intellij.lang.Language)45