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