Search in sources :

Example 11 with PsiLanguageInjectionHost

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;
}
Also used : Language(com.intellij.lang.Language) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) Pair(com.intellij.openapi.util.Pair) Nullable(org.jetbrains.annotations.Nullable)

Example 12 with PsiLanguageInjectionHost

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()]);
}
Also used : PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) THashSet(gnu.trove.THashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with PsiLanguageInjectionHost

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;
    });
}
Also used : InjectedLanguage(org.intellij.plugins.intelliLang.inject.InjectedLanguage) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) TextRange(com.intellij.openapi.util.TextRange) PsiFile(com.intellij.psi.PsiFile)

Example 14 with PsiLanguageInjectionHost

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();
    }
}
Also used : Arrays(java.util.Arrays) ArrayUtil(com.intellij.util.ArrayUtil) PersistentStateComponent(com.intellij.openapi.components.PersistentStateComponent) NonNls(org.jetbrains.annotations.NonNls) ContainerUtil(com.intellij.util.containers.ContainerUtil) CDATA(org.jdom.CDATA) Matcher(java.util.regex.Matcher) Comparing(com.intellij.openapi.util.Comparing) SmartList(com.intellij.util.SmartList) PatternCompilerFactory(com.intellij.patterns.compiler.PatternCompilerFactory) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) PsiElement(com.intellij.psi.PsiElement) Logger(com.intellij.openapi.diagnostic.Logger) LiteralTextEscaper(com.intellij.psi.LiteralTextEscaper) ElementManipulators(com.intellij.psi.ElementManipulators) ProgressManager(com.intellij.openapi.progress.ProgressManager) InjectorUtils(org.intellij.plugins.intelliLang.inject.InjectorUtils) StringUtil(com.intellij.openapi.util.text.StringUtil) Key(com.intellij.openapi.util.Key) TextRange(com.intellij.openapi.util.TextRange) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) RegExp(org.intellij.lang.annotations.RegExp) StringPattern(com.intellij.patterns.StringPattern) Pattern(java.util.regex.Pattern) PatternCompiler(com.intellij.patterns.compiler.PatternCompiler) NotNull(org.jetbrains.annotations.NotNull) Element(org.jdom.Element) Collections(java.util.Collections) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) TextRange(com.intellij.openapi.util.TextRange) NotNull(org.jetbrains.annotations.NotNull)

Example 15 with PsiLanguageInjectionHost

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;
    }
}
Also used : PsiComment(com.intellij.psi.PsiComment) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) BaseInjection(org.intellij.plugins.intelliLang.inject.config.BaseInjection)

Aggregations

PsiLanguageInjectionHost (com.intellij.psi.PsiLanguageInjectionHost)41 PsiElement (com.intellij.psi.PsiElement)23 TextRange (com.intellij.openapi.util.TextRange)19 InjectedLanguageManager (com.intellij.lang.injection.InjectedLanguageManager)10 NotNull (org.jetbrains.annotations.NotNull)10 PsiFile (com.intellij.psi.PsiFile)9 Pair (com.intellij.openapi.util.Pair)8 Language (com.intellij.lang.Language)6 XmlAttribute (com.intellij.psi.xml.XmlAttribute)5 List (java.util.List)4 Nullable (org.jetbrains.annotations.Nullable)4 ASTNode (com.intellij.lang.ASTNode)3 XmlAttributeValueImpl (com.intellij.psi.impl.source.xml.XmlAttributeValueImpl)3 ScopeOwner (com.jetbrains.python.codeInsight.controlflow.ScopeOwner)3 InjectedLanguage (org.intellij.plugins.intelliLang.inject.InjectedLanguage)3 NonNls (org.jetbrains.annotations.NonNls)3 Project (com.intellij.openapi.project.Project)2 HtmlTag (com.intellij.psi.html.HtmlTag)2 TemplateBuilder (com.intellij.codeInsight.template.TemplateBuilder)1 AnnotationBackedDescriptor (com.intellij.lang.javascript.flex.AnnotationBackedDescriptor)1