Search in sources :

Example 21 with PsiManagerEx

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

the class PsiLiteralExpressionImpl method getType.

@Override
public PsiType getType() {
    final IElementType type = getLiteralElementType();
    if (type == JavaTokenType.INTEGER_LITERAL) {
        return PsiType.INT;
    }
    if (type == JavaTokenType.LONG_LITERAL) {
        return PsiType.LONG;
    }
    if (type == JavaTokenType.FLOAT_LITERAL) {
        return PsiType.FLOAT;
    }
    if (type == JavaTokenType.DOUBLE_LITERAL) {
        return PsiType.DOUBLE;
    }
    if (type == JavaTokenType.CHARACTER_LITERAL) {
        return PsiType.CHAR;
    }
    if (type == JavaTokenType.STRING_LITERAL) {
        PsiManagerEx manager = getManager();
        GlobalSearchScope resolveScope = ResolveScopeManager.getElementResolveScope(this);
        return PsiType.getJavaLangString(manager, resolveScope);
    }
    if (type == JavaTokenType.TRUE_KEYWORD || type == JavaTokenType.FALSE_KEYWORD) {
        return PsiType.BOOLEAN;
    }
    if (type == JavaTokenType.NULL_KEYWORD) {
        return PsiType.NULL;
    }
    return null;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiManagerEx(com.intellij.psi.impl.PsiManagerEx)

Example 22 with PsiManagerEx

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

the class PsiReferenceExpressionImpl method bindToElementViaStaticImport.

@Override
public PsiElement bindToElementViaStaticImport(@NotNull PsiClass qualifierClass) throws IncorrectOperationException {
    String qualifiedName = qualifierClass.getQualifiedName();
    if (qualifiedName == null)
        throw new IncorrectOperationException();
    if (getQualifierExpression() != null) {
        throw new IncorrectOperationException("Reference is qualified: " + getText());
    }
    if (!isPhysical()) {
        // don't qualify reference: the isReferenceTo() check fails anyway, whether we have a static import for this member or not
        return this;
    }
    String staticName = getReferenceName();
    PsiFile containingFile = getContainingFile();
    PsiImportList importList = null;
    boolean doImportStatic;
    if (containingFile instanceof PsiJavaFile) {
        importList = ((PsiJavaFile) containingFile).getImportList();
        assert importList != null : containingFile;
        PsiImportStatementBase singleImportStatement = importList.findSingleImportStatement(staticName);
        doImportStatic = singleImportStatement == null;
        if (singleImportStatement instanceof PsiImportStaticStatement) {
            String qName = qualifierClass.getQualifiedName() + "." + staticName;
            if (qName.equals(singleImportStatement.getImportReference().getQualifiedName()))
                return this;
        }
    } else {
        doImportStatic = false;
    }
    if (doImportStatic) {
        bindToElementViaStaticImport(qualifierClass, staticName, importList);
    } else {
        PsiManagerEx manager = getManager();
        PsiReferenceExpression classRef = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory().createReferenceExpression(qualifierClass);
        final CharTable treeCharTab = SharedImplUtil.findCharTableByTree(this);
        LeafElement dot = Factory.createSingleLeafElement(JavaTokenType.DOT, ".", 0, 1, treeCharTab, manager);
        addInternal(dot, dot, SourceTreeToPsiMap.psiElementToTree(getParameterList()), Boolean.TRUE);
        addBefore(classRef, SourceTreeToPsiMap.treeElementToPsi(dot));
    }
    return this;
}
Also used : PsiManagerEx(com.intellij.psi.impl.PsiManagerEx)

Example 23 with PsiManagerEx

use of com.intellij.psi.impl.PsiManagerEx 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)

Example 24 with PsiManagerEx

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

the class InjectedLanguageUtil method clearCaches.

public static void clearCaches(@NotNull PsiFile injected, @NotNull DocumentWindowImpl documentWindow) {
    VirtualFileWindowImpl virtualFile = (VirtualFileWindowImpl) injected.getVirtualFile();
    PsiManagerEx psiManagerEx = (PsiManagerEx) injected.getManager();
    if (psiManagerEx.getProject().isDisposed())
        return;
    DebugUtil.startPsiModification("injected clearCaches");
    try {
        psiManagerEx.getFileManager().setViewProvider(virtualFile, null);
    } finally {
        DebugUtil.finishPsiModification();
    }
    PsiElement context = InjectedLanguageManager.getInstance(injected.getProject()).getInjectionHost(injected);
    PsiFile hostFile;
    if (context != null) {
        hostFile = context.getContainingFile();
    } else {
        VirtualFile delegate = virtualFile.getDelegate();
        hostFile = delegate.isValid() ? psiManagerEx.findFile(delegate) : null;
    }
    if (hostFile != null) {
        // modification of cachedInjectedDocuments must be under PsiLock
        synchronized (InjectedLanguageManagerImpl.ourInjectionPsiLock) {
            List<DocumentWindow> cachedInjectedDocuments = getCachedInjectedDocuments(hostFile);
            for (int i = cachedInjectedDocuments.size() - 1; i >= 0; i--) {
                DocumentWindow cachedInjectedDocument = cachedInjectedDocuments.get(i);
                if (cachedInjectedDocument == documentWindow) {
                    cachedInjectedDocuments.remove(i);
                }
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) PsiManagerEx(com.intellij.psi.impl.PsiManagerEx)

Example 25 with PsiManagerEx

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

the class GrEnumTypeDefinitionImpl method getDefEnumMethods.

private PsiMethod[] getDefEnumMethods() {
    return CachedValuesManager.getCachedValue(this, () -> {
        PsiMethod[] defMethods = new PsiMethod[4];
        final PsiManagerEx manager = getManager();
        final PsiElementFactory factory = JavaPsiFacade.getElementFactory(getProject());
        defMethods[0] = new LightMethodBuilder(manager, GroovyLanguage.INSTANCE, "values").setMethodReturnType(factory.createTypeFromText(CommonClassNames.JAVA_UTIL_COLLECTION + "<" + getName() + ">", this)).setContainingClass(this).addModifier(PsiModifier.PUBLIC).addModifier(PsiModifier.STATIC);
        defMethods[1] = new LightMethodBuilder(manager, GroovyLanguage.INSTANCE, "next").setMethodReturnType(factory.createType(this)).setContainingClass(this).addModifier(PsiModifier.PUBLIC);
        defMethods[2] = new LightMethodBuilder(manager, GroovyLanguage.INSTANCE, "previous").setMethodReturnType(factory.createType(this)).setContainingClass(this).addModifier(PsiModifier.PUBLIC);
        defMethods[3] = new LightMethodBuilder(manager, GroovyLanguage.INSTANCE, "valueOf").setMethodReturnType(factory.createType(this)).setContainingClass(this).addParameter("name", CommonClassNames.JAVA_LANG_STRING).addModifier(PsiModifier.PUBLIC).addModifier(PsiModifier.STATIC);
        return CachedValueProvider.Result.create(defMethods, this);
    });
}
Also used : LightMethodBuilder(com.intellij.psi.impl.light.LightMethodBuilder) PsiManagerEx(com.intellij.psi.impl.PsiManagerEx)

Aggregations

PsiManagerEx (com.intellij.psi.impl.PsiManagerEx)25 PsiModificationTracker (com.intellij.psi.util.PsiModificationTracker)7 FileManager (com.intellij.psi.impl.file.impl.FileManager)6 LightVirtualFile (com.intellij.testFramework.LightVirtualFile)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 FileElement (com.intellij.psi.impl.source.tree.FileElement)5 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)3 Document (com.intellij.openapi.editor.Document)2 SingleRootFileViewProvider (com.intellij.psi.SingleRootFileViewProvider)2 XmlFile (com.intellij.psi.xml.XmlFile)2 File (java.io.File)2 ASTNode (com.intellij.lang.ASTNode)1 Disposable (com.intellij.openapi.Disposable)1 FileType (com.intellij.openapi.fileTypes.FileType)1 Project (com.intellij.openapi.project.Project)1 VirtualFileFilter (com.intellij.openapi.vfs.VirtualFileFilter)1 PsiClass (com.intellij.psi.PsiClass)1 PsiClassType (com.intellij.psi.PsiClassType)1 PsiElement (com.intellij.psi.PsiElement)1 PsiElementFactory (com.intellij.psi.PsiElementFactory)1