Search in sources :

Example 21 with InjectedLanguageManager

use of com.intellij.lang.injection.InjectedLanguageManager in project intellij-community by JetBrains.

the class PsiDiamondTypeUtil method areTypeArgumentsRedundant.

public static boolean areTypeArgumentsRedundant(PsiType[] typeArguments, PsiCallExpression expression, boolean constructorRef, @Nullable PsiMethod method, PsiTypeParameter[] typeParameters) {
    try {
        final PsiElement copy;
        final PsiType typeByParent = PsiTypesUtil.getExpectedTypeByParent(expression);
        if (typeByParent != null) {
            final String arrayInitializer = "new " + typeByParent.getCanonicalText() + "[]{0}";
            final Project project = expression.getProject();
            final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(project).getElementFactory();
            PsiNewExpression newExpr = (PsiNewExpression) elementFactory.createExpressionFromText(arrayInitializer, expression);
            //ensure refs to inner classes are collapsed to avoid raw types (container type would be raw in qualified text)
            newExpr = (PsiNewExpression) JavaCodeStyleManager.getInstance(project).shortenClassReferences(newExpr);
            final PsiArrayInitializerExpression initializer = newExpr.getArrayInitializer();
            LOG.assertTrue(initializer != null);
            copy = initializer.getInitializers()[0].replace(expression);
        } else {
            final PsiExpressionList argumentList = expression.getArgumentList();
            final int offset = (argumentList != null ? argumentList : expression).getTextRange().getStartOffset();
            final PsiCall call = LambdaUtil.treeWalkUp(expression);
            if (call != null) {
                final PsiCall callCopy = LambdaUtil.copyTopLevelCall(call);
                copy = callCopy != null ? callCopy.findElementAt(offset - call.getTextRange().getStartOffset()) : null;
            } else {
                final InjectedLanguageManager injectedLanguageManager = InjectedLanguageManager.getInstance(expression.getProject());
                if (injectedLanguageManager.getInjectionHost(expression) != null) {
                    return false;
                }
                final PsiFile containingFile = expression.getContainingFile();
                final PsiFile fileCopy = (PsiFile) containingFile.copy();
                copy = fileCopy.findElementAt(offset);
                if (method != null && method.getContainingFile() == containingFile) {
                    final PsiElement startMethodElementInCopy = fileCopy.findElementAt(method.getTextOffset());
                    method = PsiTreeUtil.getParentOfType(startMethodElementInCopy, PsiMethod.class);
                    if (method == null) {
                        //lombok generated builder
                        return false;
                    }
                }
            }
        }
        final PsiCallExpression exprCopy = PsiTreeUtil.getParentOfType(copy, PsiCallExpression.class, false);
        if (exprCopy != null) {
            final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(exprCopy.getProject()).getElementFactory();
            if (constructorRef) {
                if (!(exprCopy instanceof PsiNewExpression) || !isInferenceEquivalent(typeArguments, elementFactory, (PsiNewExpression) exprCopy)) {
                    return false;
                }
            } else {
                LOG.assertTrue(method != null);
                if (!isInferenceEquivalent(typeArguments, elementFactory, exprCopy, method, typeParameters)) {
                    return false;
                }
            }
        }
    } catch (IncorrectOperationException e) {
        LOG.info(e);
        return false;
    }
    return true;
}
Also used : InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) Project(com.intellij.openapi.project.Project) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 22 with InjectedLanguageManager

use of com.intellij.lang.injection.InjectedLanguageManager in project intellij-plugins by JetBrains.

the class ProblemsHolder method getLineNumber.

private static int getLineNumber(PsiElement element) {
    InjectedLanguageManager manager = InjectedLanguageManager.getInstance(element.getProject());
    final int elementTextOffset = manager.injectedToHost(element, element.getTextOffset());
    final PsiFile psiFile = manager.getTopLevelFile(element);
    final Document document = PsiDocumentManager.getInstance(element.getProject()).getCachedDocument(psiFile);
    assert document != null;
    return document.getLineNumber(elementTextOffset) + 1;
}
Also used : InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) PsiFile(com.intellij.psi.PsiFile) Document(com.intellij.openapi.editor.Document)

Example 23 with InjectedLanguageManager

use of com.intellij.lang.injection.InjectedLanguageManager in project intellij-community by JetBrains.

the class InjectedGeneralHighlightingPass method getInjectedPsiFiles.

@NotNull
private Set<PsiFile> getInjectedPsiFiles(@NotNull final List<PsiElement> elements1, @NotNull final List<PsiElement> elements2, @NotNull final ProgressIndicator progress) {
    ApplicationManager.getApplication().assertReadAccessAllowed();
    List<DocumentWindow> injected = InjectedLanguageUtil.getCachedInjectedDocuments(myFile);
    final Collection<PsiElement> hosts = new THashSet<>(elements1.size() + elements2.size() + injected.size());
    //since change in one place can lead to invalidation of injected PSI in (completely) other place.
    for (DocumentWindow documentRange : injected) {
        progress.checkCanceled();
        if (!documentRange.isValid())
            continue;
        PsiFile file = PsiDocumentManager.getInstance(myProject).getPsiFile(documentRange);
        if (file == null)
            continue;
        PsiElement context = InjectedLanguageManager.getInstance(file.getProject()).getInjectionHost(file);
        if (context != null && context.isValid() && !file.getProject().isDisposed() && (myUpdateAll || myRestrictRange.intersects(context.getTextRange()))) {
            hosts.add(context);
        }
    }
    InjectedLanguageManagerImpl injectedLanguageManager = InjectedLanguageManagerImpl.getInstanceImpl(myProject);
    Processor<PsiElement> collectInjectableProcessor = Processors.cancelableCollectProcessor(hosts);
    injectedLanguageManager.processInjectableElements(elements1, collectInjectableProcessor);
    injectedLanguageManager.processInjectableElements(elements2, collectInjectableProcessor);
    final Set<PsiFile> outInjected = new THashSet<>();
    final PsiLanguageInjectionHost.InjectedPsiVisitor visitor = new PsiLanguageInjectionHost.InjectedPsiVisitor() {

        @Override
        public void visit(@NotNull PsiFile injectedPsi, @NotNull List<PsiLanguageInjectionHost.Shred> places) {
            synchronized (outInjected) {
                outInjected.add(injectedPsi);
            }
        }
    };
    if (!JobLauncher.getInstance().invokeConcurrentlyUnderProgress(new ArrayList<>(hosts), progress, true, element -> {
        ApplicationManager.getApplication().assertReadAccessAllowed();
        progress.checkCanceled();
        InjectedLanguageUtil.enumerate(element, myFile, false, visitor);
        return true;
    })) {
        throw new ProcessCanceledException();
    }
    synchronized (outInjected) {
        return outInjected;
    }
}
Also used : Language(com.intellij.lang.Language) InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) com.intellij.openapi.util(com.intellij.openapi.util) java.util(java.util) IElementType(com.intellij.psi.tree.IElementType) JobLauncher(com.intellij.concurrency.JobLauncher) InjectedLanguageManagerImpl(com.intellij.psi.impl.source.tree.injected.InjectedLanguageManagerImpl) Document(com.intellij.openapi.editor.Document) THashSet(gnu.trove.THashSet) ContainerUtil(com.intellij.util.containers.ContainerUtil) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) SyntaxHighlighter(com.intellij.openapi.fileTypes.SyntaxHighlighter) Place(com.intellij.psi.impl.source.tree.injected.Place) Project(com.intellij.openapi.project.Project) HighlightInfoHolder(com.intellij.codeInsight.daemon.impl.analysis.HighlightInfoHolder) DumbAware(com.intellij.openapi.project.DumbAware) SyntaxHighlighterFactory(com.intellij.openapi.fileTypes.SyntaxHighlighterFactory) InjectedLanguageUtil(com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil) CommonProcessors(com.intellij.util.CommonProcessors) ProgressManager(com.intellij.openapi.progress.ProgressManager) DocumentWindow(com.intellij.injected.editor.DocumentWindow) Processors(com.intellij.util.Processors) HighlighterColors(com.intellij.openapi.editor.HighlighterColors) Editor(com.intellij.openapi.editor.Editor) Nullable(org.jetbrains.annotations.Nullable) TextAttributesKey(com.intellij.openapi.editor.colors.TextAttributesKey) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) EditorColors(com.intellij.openapi.editor.colors.EditorColors) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) Processor(com.intellij.util.Processor) ApplicationManager(com.intellij.openapi.application.ApplicationManager) Registry(com.intellij.openapi.util.registry.Registry) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) NotNull(org.jetbrains.annotations.NotNull) THashSet(gnu.trove.THashSet) DocumentWindow(com.intellij.injected.editor.DocumentWindow) InjectedLanguageManagerImpl(com.intellij.psi.impl.source.tree.injected.InjectedLanguageManagerImpl) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) NotNull(org.jetbrains.annotations.NotNull)

Example 24 with InjectedLanguageManager

use of com.intellij.lang.injection.InjectedLanguageManager in project intellij-community by JetBrains.

the class LocalInspectionsPass method addHighlightsFromResults.

private void addHighlightsFromResults(@NotNull List<HighlightInfo> outInfos, @NotNull ProgressIndicator indicator) {
    InspectionProfile inspectionProfile = InspectionProjectProfileManager.getInstance(myProject).getCurrentProfile();
    PsiDocumentManager documentManager = PsiDocumentManager.getInstance(myProject);
    InjectedLanguageManager ilManager = InjectedLanguageManager.getInstance(myProject);
    Set<Pair<TextRange, String>> emptyActionRegistered = new THashSet<>();
    for (Map.Entry<PsiFile, List<InspectionResult>> entry : result.entrySet()) {
        indicator.checkCanceled();
        PsiFile file = entry.getKey();
        Document documentRange = documentManager.getDocument(file);
        if (documentRange == null)
            continue;
        List<InspectionResult> resultList = entry.getValue();
        synchronized (resultList) {
            for (InspectionResult inspectionResult : resultList) {
                indicator.checkCanceled();
                LocalInspectionToolWrapper tool = inspectionResult.tool;
                HighlightSeverity severity = inspectionProfile.getErrorLevel(HighlightDisplayKey.find(tool.getShortName()), file).getSeverity();
                for (ProblemDescriptor descriptor : inspectionResult.foundProblems) {
                    indicator.checkCanceled();
                    PsiElement element = descriptor.getPsiElement();
                    if (element != null) {
                        createHighlightsForDescriptor(outInfos, emptyActionRegistered, ilManager, file, documentRange, tool, severity, descriptor, element);
                    }
                }
            }
        }
    }
}
Also used : HighlightSeverity(com.intellij.lang.annotation.HighlightSeverity) Document(com.intellij.openapi.editor.Document) THashSet(gnu.trove.THashSet) InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) THashMap(gnu.trove.THashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Pair(com.intellij.openapi.util.Pair)

Example 25 with InjectedLanguageManager

use of com.intellij.lang.injection.InjectedLanguageManager in project intellij-community by JetBrains.

the class LocalInspectionsPass method addDescriptorsFromInjectedResults.

private void addDescriptorsFromInjectedResults(@NotNull InspectionManager iManager, @NotNull GlobalInspectionContextImpl context) {
    InjectedLanguageManager ilManager = InjectedLanguageManager.getInstance(myProject);
    PsiDocumentManager documentManager = PsiDocumentManager.getInstance(myProject);
    for (Map.Entry<PsiFile, List<InspectionResult>> entry : result.entrySet()) {
        PsiFile file = entry.getKey();
        // not injected
        if (file == getFile())
            continue;
        DocumentWindow documentRange = (DocumentWindow) documentManager.getDocument(file);
        List<InspectionResult> resultList = entry.getValue();
        for (InspectionResult inspectionResult : resultList) {
            LocalInspectionToolWrapper toolWrapper = inspectionResult.tool;
            for (ProblemDescriptor descriptor : inspectionResult.foundProblems) {
                PsiElement psiElement = descriptor.getPsiElement();
                if (psiElement == null)
                    continue;
                if (SuppressionUtil.inspectionResultSuppressed(psiElement, toolWrapper.getTool()))
                    continue;
                List<TextRange> editables = ilManager.intersectWithAllEditableFragments(file, ((ProblemDescriptorBase) descriptor).getTextRange());
                for (TextRange editable : editables) {
                    TextRange hostRange = documentRange.injectedToHost(editable);
                    QuickFix[] fixes = descriptor.getFixes();
                    LocalQuickFix[] localFixes = null;
                    if (fixes != null) {
                        localFixes = new LocalQuickFix[fixes.length];
                        for (int k = 0; k < fixes.length; k++) {
                            QuickFix fix = fixes[k];
                            localFixes[k] = (LocalQuickFix) fix;
                        }
                    }
                    ProblemDescriptor patchedDescriptor = iManager.createProblemDescriptor(getFile(), hostRange, descriptor.getDescriptionTemplate(), descriptor.getHighlightType(), true, localFixes);
                    addDescriptors(toolWrapper, patchedDescriptor, context);
                }
            }
        }
    }
}
Also used : TextRange(com.intellij.openapi.util.TextRange) InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) DocumentWindow(com.intellij.injected.editor.DocumentWindow) THashMap(gnu.trove.THashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Aggregations

InjectedLanguageManager (com.intellij.lang.injection.InjectedLanguageManager)41 TextRange (com.intellij.openapi.util.TextRange)12 PsiElement (com.intellij.psi.PsiElement)12 PsiFile (com.intellij.psi.PsiFile)11 PsiLanguageInjectionHost (com.intellij.psi.PsiLanguageInjectionHost)10 Pair (com.intellij.openapi.util.Pair)7 Nullable (org.jetbrains.annotations.Nullable)7 Document (com.intellij.openapi.editor.Document)5 Project (com.intellij.openapi.project.Project)5 DocumentWindow (com.intellij.injected.editor.DocumentWindow)4 NotNull (org.jetbrains.annotations.NotNull)4 ASTNode (com.intellij.lang.ASTNode)2 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)2 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2 ScopeOwner (com.jetbrains.python.codeInsight.controlflow.ScopeOwner)2 THashMap (gnu.trove.THashMap)2 THashSet (gnu.trove.THashSet)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 HighlightInfoHolder (com.intellij.codeInsight.daemon.impl.analysis.HighlightInfoHolder)1 TemplateBuilder (com.intellij.codeInsight.template.TemplateBuilder)1