Search in sources :

Example 1 with PsiFile

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

the class HbStructureViewFactory method getStructureViewBuilder.

@Nullable
@Override
public StructureViewBuilder getStructureViewBuilder(final PsiFile psiFile) {
    return new TemplateLanguageStructureViewBuilder(psiFile) {

        @Override
        protected StructureViewComposite.StructureViewDescriptor createMainView(FileEditor fileEditor, PsiFile mainFile) {
            if (!psiFile.isValid())
                return null;
            final StructureViewBuilder builder = new TreeBasedStructureViewBuilder() {

                @NotNull
                @Override
                public StructureViewModel createStructureViewModel(@Nullable Editor editor) {
                    return new HbStructureViewModel((HbPsiFile) psiFile, editor);
                }
            };
            StructureView structureView = builder.createStructureView(fileEditor, psiFile.getProject());
            return new StructureViewComposite.StructureViewDescriptor(HbLanguage.INSTANCE.getDisplayName(), structureView, HbFileType.INSTANCE.getIcon());
        }
    };
}
Also used : TreeBasedStructureViewBuilder(com.intellij.ide.structureView.TreeBasedStructureViewBuilder) TemplateLanguageStructureViewBuilder(com.intellij.ide.structureView.impl.TemplateLanguageStructureViewBuilder) StructureViewBuilder(com.intellij.ide.structureView.StructureViewBuilder) FileEditor(com.intellij.openapi.fileEditor.FileEditor) StructureView(com.intellij.ide.structureView.StructureView) TreeBasedStructureViewBuilder(com.intellij.ide.structureView.TreeBasedStructureViewBuilder) HbPsiFile(com.dmarcotte.handlebars.psi.HbPsiFile) PsiFile(com.intellij.psi.PsiFile) TemplateLanguageStructureViewBuilder(com.intellij.ide.structureView.impl.TemplateLanguageStructureViewBuilder) Editor(com.intellij.openapi.editor.Editor) FileEditor(com.intellij.openapi.fileEditor.FileEditor) Nullable(org.jetbrains.annotations.Nullable) StructureViewComposite(com.intellij.ide.structureView.impl.StructureViewComposite) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with PsiFile

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

the class HbsEmmetTest method testSimpleTagsWithHtmlSubstitutor.

public void testSimpleTagsWithHtmlSubstitutor() {
    HbTestUtils.setOpenHtmlAsHandlebars(true, getProject(), getTestRootDisposable());
    final PsiFile file = myFixture.configureByText("test.html", "div>span<caret>");
    assertInstanceOf(file, HbPsiFile.class);
    TemplateManager.getInstance(getProject()).startTemplate(myFixture.getEditor(), TemplateSettings.TAB_CHAR);
    myFixture.checkResult("<div><span></span></div>");
}
Also used : HbPsiFile(com.dmarcotte.handlebars.psi.HbPsiFile) PsiFile(com.intellij.psi.PsiFile)

Example 3 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 4 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 5 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)1776 VirtualFile (com.intellij.openapi.vfs.VirtualFile)490 PsiElement (com.intellij.psi.PsiElement)466 Nullable (org.jetbrains.annotations.Nullable)267 Project (com.intellij.openapi.project.Project)266 NotNull (org.jetbrains.annotations.NotNull)248 Document (com.intellij.openapi.editor.Document)181 Editor (com.intellij.openapi.editor.Editor)166 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)106 TextRange (com.intellij.openapi.util.TextRange)88 ArrayList (java.util.ArrayList)81 XmlTag (com.intellij.psi.xml.XmlTag)68 File (java.io.File)58 PsiManager (com.intellij.psi.PsiManager)56 PsiClass (com.intellij.psi.PsiClass)50 List (java.util.List)46 Language (com.intellij.lang.Language)45