use of com.intellij.psi.impl.source.tree.injected.Place in project intellij-community by JetBrains.
the class QuickEditAction method getExistingHandler.
public static QuickEditHandler getExistingHandler(@NotNull PsiFile injectedFile) {
Place shreds = InjectedLanguageUtil.getShreds(injectedFile);
DocumentWindow documentWindow = InjectedLanguageUtil.getDocumentWindow(injectedFile);
if (shreds == null || documentWindow == null)
return null;
TextRange hostRange = TextRange.create(shreds.get(0).getHostRangeMarker().getStartOffset(), shreds.get(shreds.size() - 1).getHostRangeMarker().getEndOffset());
for (Editor editor : EditorFactory.getInstance().getAllEditors()) {
if (editor.getDocument() != documentWindow.getDelegate())
continue;
QuickEditHandler handler = editor.getUserData(QUICK_EDIT_HANDLER);
if (handler != null && handler.changesRange(hostRange))
return handler;
}
return null;
}
use of com.intellij.psi.impl.source.tree.injected.Place 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.psi.impl.source.tree.injected.Place in project intellij-community by JetBrains.
the class InjectedGeneralHighlightingPass method addInjectedPsiHighlights.
private boolean addInjectedPsiHighlights(@NotNull PsiFile injectedPsi, TextAttributes injectedAttributes, @NotNull Collection<HighlightInfo> outInfos, @NotNull ProgressIndicator progress, @NotNull InjectedLanguageManager injectedLanguageManager) {
DocumentWindow documentWindow = (DocumentWindow) PsiDocumentManager.getInstance(myProject).getCachedDocument(injectedPsi);
if (documentWindow == null)
return true;
Place places = InjectedLanguageUtil.getShreds(injectedPsi);
boolean addTooltips = places.size() < 100;
for (PsiLanguageInjectionHost.Shred place : places) {
PsiLanguageInjectionHost host = place.getHost();
if (host == null)
continue;
TextRange textRange = place.getRangeInsideHost().shiftRight(host.getTextRange().getStartOffset());
if (textRange.isEmpty())
continue;
HighlightInfo.Builder builder = HighlightInfo.newHighlightInfo(HighlightInfoType.INJECTED_LANGUAGE_BACKGROUND).range(textRange);
if (injectedAttributes != null && InjectedLanguageUtil.isHighlightInjectionBackground(host)) {
builder.textAttributes(injectedAttributes);
}
if (addTooltips) {
String desc = injectedPsi.getLanguage().getDisplayName() + ": " + injectedPsi.getText();
builder.unescapedToolTip(desc);
}
HighlightInfo info = builder.createUnconditionally();
info.setFromInjection(true);
outInfos.add(info);
}
HighlightInfoHolder holder = createInfoHolder(injectedPsi);
runHighlightVisitorsForInjected(injectedPsi, holder, progress);
for (int i = 0; i < holder.size(); i++) {
HighlightInfo info = holder.get(i);
final int startOffset = documentWindow.injectedToHost(info.startOffset);
final TextRange fixedTextRange = getFixedTextRange(documentWindow, startOffset);
addPatchedInfos(info, injectedPsi, documentWindow, injectedLanguageManager, fixedTextRange, outInfos);
}
int injectedStart = holder.size();
highlightInjectedSyntax(injectedPsi, holder);
for (int i = injectedStart; i < holder.size(); i++) {
HighlightInfo info = holder.get(i);
final int startOffset = info.startOffset;
final TextRange fixedTextRange = getFixedTextRange(documentWindow, startOffset);
if (fixedTextRange == null) {
info.setFromInjection(true);
outInfos.add(info);
} else {
HighlightInfo patched = new HighlightInfo(info.forcedTextAttributes, info.forcedTextAttributesKey, info.type, fixedTextRange.getStartOffset(), fixedTextRange.getEndOffset(), info.getDescription(), info.getToolTip(), info.type.getSeverity(null), info.isAfterEndOfLine(), null, false, 0, info.getProblemGroup(), info.getGutterIconRenderer());
patched.setFromInjection(true);
outInfos.add(patched);
}
}
if (!isDumbMode()) {
List<HighlightInfo> todos = new ArrayList<>();
highlightTodos(injectedPsi, injectedPsi.getText(), 0, injectedPsi.getTextLength(), progress, myPriorityRange, todos, todos);
for (HighlightInfo info : todos) {
addPatchedInfos(info, injectedPsi, documentWindow, injectedLanguageManager, null, outInfos);
}
}
advanceProgress(1);
return true;
}
use of com.intellij.psi.impl.source.tree.injected.Place in project intellij-community by JetBrains.
the class DocumentWindowImpl method areRangesEqual.
@Override
public boolean areRangesEqual(@NotNull DocumentWindow otherd) {
DocumentWindowImpl window = (DocumentWindowImpl) otherd;
Place shreds = getShreds();
Place otherShreds = window.getShreds();
if (shreds.size() != otherShreds.size())
return false;
for (int i = 0; i < shreds.size(); i++) {
PsiLanguageInjectionHost.Shred shred = shreds.get(i);
PsiLanguageInjectionHost.Shred otherShred = otherShreds.get(i);
if (!shred.getPrefix().equals(otherShred.getPrefix()))
return false;
if (!shred.getSuffix().equals(otherShred.getSuffix()))
return false;
Segment hostRange = shred.getHostRangeMarker();
Segment other = otherShred.getHostRangeMarker();
if (hostRange == null || other == null || hostRange.getStartOffset() != other.getStartOffset())
return false;
if (hostRange.getEndOffset() != other.getEndOffset())
return false;
}
return true;
}
Aggregations