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;
}
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;
}
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;
}
}
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);
}
}
}
}
}
}
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);
}
}
}
}
}
Aggregations