use of com.intellij.psi.PsiLanguageInjectionHost in project intellij-community by JetBrains.
the class QuickEditAction method getRangePair.
@Nullable
protected Pair<PsiElement, TextRange> getRangePair(final PsiFile file, final Editor editor) {
final int offset = editor.getCaretModel().getOffset();
final PsiLanguageInjectionHost host = PsiTreeUtil.getParentOfType(file.findElementAt(offset), PsiLanguageInjectionHost.class, false);
if (host == null || ElementManipulators.getManipulator(host) == null)
return null;
final List<Pair<PsiElement, TextRange>> injections = InjectedLanguageManager.getInstance(host.getProject()).getInjectedPsiFiles(host);
if (injections == null || injections.isEmpty())
return null;
final int offsetInElement = offset - host.getTextRange().getStartOffset();
final Pair<PsiElement, TextRange> rangePair = ContainerUtil.find(injections, pair -> pair.second.containsRange(offsetInElement, offsetInElement));
if (rangePair != null) {
final Language language = rangePair.first.getContainingFile().getLanguage();
final Object action = language.getUserData(EDIT_ACTION_AVAILABLE);
if (action != null && action.equals(false))
return null;
myLastLanguageName = language.getDisplayName();
}
return rangePair;
}
use of com.intellij.psi.PsiLanguageInjectionHost in project intellij-community by JetBrains.
the class InspectionProfileEntry method getBatchSuppressActions.
@NotNull
@Override
public SuppressQuickFix[] getBatchSuppressActions(@Nullable PsiElement element) {
if (element == null) {
return SuppressQuickFix.EMPTY_ARRAY;
}
Set<SuppressQuickFix> fixes = new THashSet<>(new TObjectHashingStrategy<SuppressQuickFix>() {
@Override
public int computeHashCode(SuppressQuickFix object) {
int result = object instanceof InjectionAwareSuppressQuickFix ? ((InjectionAwareSuppressQuickFix) object).isShouldBeAppliedToInjectionHost().hashCode() : 0;
return 31 * result + object.getName().hashCode();
}
@Override
public boolean equals(SuppressQuickFix o1, SuppressQuickFix o2) {
if (o1 instanceof InjectionAwareSuppressQuickFix && o2 instanceof InjectionAwareSuppressQuickFix) {
if (((InjectionAwareSuppressQuickFix) o1).isShouldBeAppliedToInjectionHost() != ((InjectionAwareSuppressQuickFix) o2).isShouldBeAppliedToInjectionHost()) {
return false;
}
}
return o1.getName().equals(o2.getName());
}
});
Set<InspectionSuppressor> suppressors = getSuppressors(element);
final PsiLanguageInjectionHost injectionHost = InjectedLanguageManager.getInstance(element.getProject()).getInjectionHost(element);
if (injectionHost != null) {
Set<InspectionSuppressor> injectionHostSuppressors = getSuppressors(injectionHost);
for (InspectionSuppressor suppressor : injectionHostSuppressors) {
addAllSuppressActions(fixes, injectionHost, suppressor, ThreeState.YES, getSuppressId());
}
}
for (InspectionSuppressor suppressor : suppressors) {
addAllSuppressActions(fixes, element, suppressor, injectionHost != null ? ThreeState.NO : ThreeState.UNSURE, getSuppressId());
}
return fixes.toArray(new SuppressQuickFix[fixes.size()]);
}
use of com.intellij.psi.PsiLanguageInjectionHost in project intellij-community by JetBrains.
the class XmlLanguageInjector method getLanguagesToInject.
public void getLanguagesToInject(@NotNull final MultiHostRegistrar registrar, @NotNull PsiElement host) {
final XmlElement xmlElement = (XmlElement) host;
if (!isInIndex(xmlElement))
return;
final TreeSet<TextRange> ranges = new TreeSet<>(InjectorUtils.RANGE_COMPARATOR);
final PsiFile containingFile = xmlElement.getContainingFile();
final Ref<Boolean> unparsableRef = Ref.create();
getInjectedLanguage(xmlElement, unparsableRef, (language, list) -> {
for (Trinity<PsiLanguageInjectionHost, InjectedLanguage, TextRange> trinity : list) {
if (ranges.contains(trinity.third.shiftRight(trinity.first.getTextRange().getStartOffset())))
return true;
}
for (Trinity<PsiLanguageInjectionHost, InjectedLanguage, TextRange> trinity : list) {
final PsiLanguageInjectionHost host1 = trinity.first;
if (host1.getContainingFile() != containingFile)
continue;
final TextRange textRange = trinity.third;
ranges.add(textRange.shiftRight(host1.getTextRange().getStartOffset()));
}
InjectorUtils.registerInjection(language, list, containingFile, registrar);
InjectorUtils.registerSupport(mySupport, true, registrar);
if (Boolean.TRUE.equals(unparsableRef.get())) {
InjectorUtils.putInjectedFileUserData(registrar, InjectedLanguageUtil.FRANKENSTEIN_INJECTION, Boolean.TRUE);
}
return true;
});
}
use of com.intellij.psi.PsiLanguageInjectionHost in project intellij-community by JetBrains.
the class BaseInjection method getInjectedArea.
@NotNull
public List<TextRange> getInjectedArea(final PsiElement element) {
final TextRange textRange = ElementManipulators.getValueTextRange(element);
if (myCompiledValuePattern == null) {
return Collections.singletonList(textRange);
} else {
final LiteralTextEscaper<? extends PsiLanguageInjectionHost> textEscaper = ((PsiLanguageInjectionHost) element).createLiteralTextEscaper();
final StringBuilder sb = new StringBuilder();
textEscaper.decode(textRange, sb);
final List<TextRange> ranges = getMatchingRanges(myCompiledValuePattern.matcher(StringPattern.newBombedCharSequence(sb)), sb.length());
return !ranges.isEmpty() ? ContainerUtil.map(ranges, s -> new TextRange(textEscaper.getOffsetInHost(s.getStartOffset(), textRange), textEscaper.getOffsetInHost(s.getEndOffset(), textRange))) : Collections.<TextRange>emptyList();
}
}
use of com.intellij.psi.PsiLanguageInjectionHost in project intellij-community by JetBrains.
the class CommentLanguageInjector method getLanguagesToInject.
public void getLanguagesToInject(@NotNull final MultiHostRegistrar registrar, @NotNull final PsiElement context) {
if (!(context instanceof PsiLanguageInjectionHost) || context instanceof PsiComment)
return;
if (!((PsiLanguageInjectionHost) context).isValidHost())
return;
PsiLanguageInjectionHost host = (PsiLanguageInjectionHost) context;
boolean applicableFound = false;
for (LanguageInjectionSupport support : mySupports) {
if (!support.isApplicableTo(host))
continue;
if (support == myInjectorSupport && applicableFound)
continue;
applicableFound = true;
if (!support.useDefaultCommentInjector())
continue;
BaseInjection injection = support.findCommentInjection(host, null);
if (injection == null)
continue;
if (!InjectorUtils.registerInjectionSimple(host, injection, support, registrar))
continue;
return;
}
}
Aggregations