Search in sources :

Example 6 with FileManager

use of com.intellij.psi.impl.file.impl.FileManager in project intellij-community by JetBrains.

the class GroovyCodeFragment method clone.

@Override
protected GroovyCodeFragment clone() {
    final GroovyCodeFragment clone = (GroovyCodeFragment) cloneImpl((FileElement) calcTreeElement().clone());
    clone.myOriginalFile = this;
    clone.myPseudoImports.putAll(myPseudoImports);
    FileManager fileManager = ((PsiManagerEx) getManager()).getFileManager();
    SingleRootFileViewProvider cloneViewProvider = (SingleRootFileViewProvider) fileManager.createFileViewProvider(new LightVirtualFile(getName(), getLanguage(), getText()), false);
    cloneViewProvider.forceCachedPsi(clone);
    clone.myViewProvider = cloneViewProvider;
    return clone;
}
Also used : LightVirtualFile(com.intellij.testFramework.LightVirtualFile) FileElement(com.intellij.psi.impl.source.tree.FileElement) FileManager(com.intellij.psi.impl.file.impl.FileManager) PsiManagerEx(com.intellij.psi.impl.PsiManagerEx)

Example 7 with FileManager

use of com.intellij.psi.impl.file.impl.FileManager in project intellij-plugins by JetBrains.

the class DartExpressionCodeFragmentImpl method clone.

protected DartExpressionCodeFragmentImpl clone() {
    final DartExpressionCodeFragmentImpl clone = (DartExpressionCodeFragmentImpl) cloneImpl((FileElement) calcTreeElement().clone());
    clone.myPhysical = false;
    clone.myOriginalFile = this;
    final FileManager fileManager = ((PsiManagerEx) getManager()).getFileManager();
    final SingleRootFileViewProvider cloneViewProvider = (SingleRootFileViewProvider) fileManager.createFileViewProvider(new LightVirtualFile(getName(), getLanguage(), getText()), false);
    clone.myViewProvider = cloneViewProvider;
    cloneViewProvider.forceCachedPsi(clone);
    clone.init(getContentElementType(), getContentElementType());
    return clone;
}
Also used : LightVirtualFile(com.intellij.testFramework.LightVirtualFile) FileElement(com.intellij.psi.impl.source.tree.FileElement) SingleRootFileViewProvider(com.intellij.psi.SingleRootFileViewProvider) FileManager(com.intellij.psi.impl.file.impl.FileManager) PsiManagerEx(com.intellij.psi.impl.PsiManagerEx)

Example 8 with FileManager

use of com.intellij.psi.impl.file.impl.FileManager in project intellij-community by JetBrains.

the class PostprocessReformattingAspect method doPostponedFormattingInner.

private void doPostponedFormattingInner(@NotNull FileViewProvider key) {
    final List<ASTNode> astNodes = getContext().myReformatElements.remove(key);
    final Document document = key.getDocument();
    // Sort ranges by end offsets so that we won't need any offset adjustment after reformat or reindent
    if (document == null)
        return;
    final VirtualFile virtualFile = key.getVirtualFile();
    if (!virtualFile.isValid())
        return;
    PsiManager manager = key.getManager();
    if (manager instanceof PsiManagerEx) {
        FileManager fileManager = ((PsiManagerEx) manager).getFileManager();
        FileViewProvider viewProvider = fileManager.findCachedViewProvider(virtualFile);
        if (viewProvider != key) {
            // viewProvider was invalidated e.g. due to language level change
            if (viewProvider == null)
                viewProvider = fileManager.findViewProvider(virtualFile);
            if (viewProvider != null) {
                key = viewProvider;
            }
        }
    }
    final TreeSet<PostprocessFormattingTask> postProcessTasks = new TreeSet<>();
    Collection<Disposable> toDispose = ContainerUtilRt.newArrayList();
    try {
        // process all roots in viewProvider to find marked for reformat before elements and create appropriate range markers
        handleReformatMarkers(key, postProcessTasks);
        toDispose.addAll(postProcessTasks);
        // then we create ranges by changed nodes. One per node. There ranges can intersect. Ranges are sorted by end offset.
        if (astNodes != null)
            createActionsMap(astNodes, key, postProcessTasks);
        if (Boolean.getBoolean("check.psi.is.valid") && ApplicationManager.getApplication().isUnitTestMode()) {
            checkPsiIsCorrect(key);
        }
        while (!postProcessTasks.isEmpty()) {
            // now we have to normalize actions so that they not intersect and ordered in most appropriate way
            // (free reformatting -> reindent -> formatting under reindent)
            final List<PostponedAction> normalizedActions = normalizeAndReorderPostponedActions(postProcessTasks, document);
            toDispose.addAll(normalizedActions);
            // only in following loop real changes in document are made
            for (final PostponedAction normalizedAction : normalizedActions) {
                CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(myPsiManager.getProject());
                boolean old = settings.ENABLE_JAVADOC_FORMATTING;
                settings.ENABLE_JAVADOC_FORMATTING = false;
                try {
                    normalizedAction.execute(key);
                } finally {
                    settings.ENABLE_JAVADOC_FORMATTING = old;
                }
            }
        }
    } finally {
        for (Disposable disposable : toDispose) {
            //noinspection SSBasedInspection
            disposable.dispose();
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Disposable(com.intellij.openapi.Disposable) Document(com.intellij.openapi.editor.Document) FileManager(com.intellij.psi.impl.file.impl.FileManager) PsiManagerEx(com.intellij.psi.impl.PsiManagerEx) CodeStyleSettings(com.intellij.psi.codeStyle.CodeStyleSettings) ASTNode(com.intellij.lang.ASTNode)

Aggregations

FileManager (com.intellij.psi.impl.file.impl.FileManager)8 PsiManagerEx (com.intellij.psi.impl.PsiManagerEx)6 LightVirtualFile (com.intellij.testFramework.LightVirtualFile)6 FileElement (com.intellij.psi.impl.source.tree.FileElement)5 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 SingleRootFileViewProvider (com.intellij.psi.SingleRootFileViewProvider)2 ASTNode (com.intellij.lang.ASTNode)1 Disposable (com.intellij.openapi.Disposable)1 Document (com.intellij.openapi.editor.Document)1 CodeStyleSettings (com.intellij.psi.codeStyle.CodeStyleSettings)1 FileManagerImpl (com.intellij.psi.impl.file.impl.FileManagerImpl)1 Nullable (org.jetbrains.annotations.Nullable)1