Search in sources :

Example 11 with InjectedLanguageManager

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

the class InplaceRefactoring method checkRangeContainsOffset.

private boolean checkRangeContainsOffset(int offset, final TextRange textRange, PsiElement element, int shiftOffset) {
    final InjectedLanguageManager injectedLanguageManager = InjectedLanguageManager.getInstance(myProject);
    final PsiLanguageInjectionHost injectionHost = injectedLanguageManager.getInjectionHost(element);
    if (injectionHost != null) {
        final PsiElement nameIdentifier = getNameIdentifier();
        final PsiLanguageInjectionHost initialInjectedHost = nameIdentifier != null ? injectedLanguageManager.getInjectionHost(nameIdentifier) : null;
        if (initialInjectedHost != null && initialInjectedHost != injectionHost) {
            return false;
        }
        return injectedLanguageManager.injectedToHost(element, textRange).shiftRight(shiftOffset).containsOffset(offset);
    }
    return textRange.shiftRight(shiftOffset).containsOffset(offset);
}
Also used : InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager)

Example 12 with InjectedLanguageManager

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

the class SafeDeleteProcessor method isInside.

public static boolean isInside(PsiElement place, PsiElement ancestor) {
    if (ancestor instanceof PsiDirectoryContainer) {
        final PsiDirectory[] directories = ((PsiDirectoryContainer) ancestor).getDirectories(place.getResolveScope());
        for (PsiDirectory directory : directories) {
            if (isInside(place, directory))
                return true;
        }
    }
    if (ancestor instanceof PsiFile) {
        for (PsiFile file : ((PsiFile) ancestor).getViewProvider().getAllFiles()) {
            if (PsiTreeUtil.isAncestor(file, place, false))
                return true;
        }
    }
    boolean isAncestor = PsiTreeUtil.isAncestor(ancestor, place, false);
    if (!isAncestor && ancestor instanceof PsiNameIdentifierOwner) {
        final PsiElement nameIdentifier = ((PsiNameIdentifierOwner) ancestor).getNameIdentifier();
        if (nameIdentifier != null && !PsiTreeUtil.isAncestor(ancestor, nameIdentifier, true)) {
            isAncestor = PsiTreeUtil.isAncestor(nameIdentifier.getParent(), place, false);
        }
    }
    if (!isAncestor) {
        final InjectedLanguageManager injectedLanguageManager = InjectedLanguageManager.getInstance(place.getProject());
        PsiLanguageInjectionHost host = injectedLanguageManager.getInjectionHost(place);
        while (host != null) {
            if (PsiTreeUtil.isAncestor(ancestor, host, false)) {
                isAncestor = true;
                break;
            }
            host = injectedLanguageManager.getInjectionHost(host);
        }
    }
    return isAncestor;
}
Also used : InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager)

Example 13 with InjectedLanguageManager

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

the class CodeCompletionHandlerBase method createCompletionContext.

private static CompletionContext createCompletionContext(PsiFile originalFile, OffsetsInFile hostCopyOffsets) {
    CompletionAssertions.assertHostInfo(hostCopyOffsets.getFile(), hostCopyOffsets.getOffsets());
    int hostStartOffset = hostCopyOffsets.getOffsets().getOffset(CompletionInitializationContext.START_OFFSET);
    OffsetsInFile translatedOffsets = hostCopyOffsets.toInjectedIfAny(hostStartOffset);
    InjectedLanguageManager injectedLanguageManager = InjectedLanguageManager.getInstance(originalFile.getProject());
    PsiFile injected = translatedOffsets == hostCopyOffsets ? null : translatedOffsets.getFile();
    if (injected != null) {
        if (injected instanceof PsiFileImpl) {
            ((PsiFileImpl) injected).setOriginalFile(originalFile);
        }
        DocumentWindow documentWindow = InjectedLanguageUtil.getDocumentWindow(injected);
        CompletionAssertions.assertInjectedOffsets(hostStartOffset, injectedLanguageManager, injected, documentWindow);
    }
    CompletionContext context = new CompletionContext(translatedOffsets.getFile(), translatedOffsets.getOffsets());
    CompletionAssertions.assertFinalOffsets(originalFile, context, injected);
    return context;
}
Also used : InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) DocumentWindow(com.intellij.injected.editor.DocumentWindow) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) PsiFile(com.intellij.psi.PsiFile)

Example 14 with InjectedLanguageManager

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

the class EditorFoldingInfo method getPsiElementRange.

@Nullable
TextRange getPsiElementRange(@NotNull FoldRegion region) {
    PsiElement element = getPsiElement(region);
    if (element == null)
        return null;
    PsiFile containingFile = element.getContainingFile();
    InjectedLanguageManager injectedManager = InjectedLanguageManager.getInstance(containingFile.getProject());
    boolean isInjected = injectedManager.isInjectedFragment(containingFile);
    TextRange range = element.getTextRange();
    if (isInjected) {
        range = injectedManager.injectedToHost(element, range);
    }
    return range;
}
Also used : InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) PsiFile(com.intellij.psi.PsiFile) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 15 with InjectedLanguageManager

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

the class UpdateFoldRegionsOperation method removeInvalidRegions.

private void removeInvalidRegions(@NotNull EditorFoldingInfo info, @NotNull FoldingModelEx foldingModel, FoldingUpdate.FoldingMap elementsToFold, @NotNull Map<TextRange, Boolean> rangeToExpandStatusMap) {
    List<FoldRegion> toRemove = new ArrayList<>();
    InjectedLanguageManager injectedManager = InjectedLanguageManager.getInstance(myProject);
    for (FoldRegion region : foldingModel.getAllFoldRegions()) {
        if (myKeepCollapsedRegions && !region.isExpanded() && !regionOrGroupCanBeRemovedWhenCollapsed(region))
            continue;
        PsiElement element = info.getPsiElement(region);
        if (element != null) {
            PsiFile containingFile = element.getContainingFile();
            boolean isInjected = injectedManager.isInjectedFragment(containingFile);
            if (isInjected != myForInjected)
                continue;
        }
        final Collection<FoldingDescriptor> descriptors;
        if (element != null && !(descriptors = elementsToFold.get(element)).isEmpty()) {
            boolean matchingDescriptorFound = false;
            FoldingDescriptor[] array = descriptors.toArray(new FoldingDescriptor[descriptors.size()]);
            for (FoldingDescriptor descriptor : array) {
                TextRange range = descriptor.getRange();
                if (TextRange.areSegmentsEqual(region, range)) {
                    matchingDescriptorFound = true;
                    if (!region.isValid() || region.getGroup() != null || descriptor.getGroup() != null || !region.getPlaceholderText().equals(descriptor.getPlaceholderText()) || range.getLength() < 2) {
                        rangeToExpandStatusMap.put(range, region.isExpanded());
                        toRemove.add(region);
                        break;
                    } else {
                        elementsToFold.remove(element, descriptor);
                    }
                }
            }
            if (!matchingDescriptorFound) {
                if (Registry.is("editor.durable.folding.state")) {
                    for (FoldingDescriptor descriptor : descriptors) {
                        rangeToExpandStatusMap.put(descriptor.getRange(), region.isExpanded());
                    }
                }
                toRemove.add(region);
            }
        } else if (region.isValid() && info.isLightRegion(region)) {
            boolean isExpanded = region.isExpanded();
            rangeToExpandStatusMap.put(TextRange.create(region), isExpanded);
        } else {
            toRemove.add(region);
        }
    }
    for (final FoldRegion region : toRemove) {
        foldingModel.removeFoldRegion(region);
        info.removeRegion(region);
    }
}
Also used : InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) FoldRegion(com.intellij.openapi.editor.FoldRegion) PsiFile(com.intellij.psi.PsiFile) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement)

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