use of com.intellij.lang.injection.InjectedLanguageManager in project intellij-community by JetBrains.
the class PyTypingTest method doTestInjectedText.
private void doTestInjectedText(@NotNull String text, @NotNull String expected) {
myFixture.configureByText(PythonFileType.INSTANCE, text);
final InjectedLanguageManager languageManager = InjectedLanguageManager.getInstance(myFixture.getProject());
final PsiLanguageInjectionHost host = languageManager.getInjectionHost(getElementAtCaret());
assertNotNull(host);
final List<Pair<PsiElement, TextRange>> files = languageManager.getInjectedPsiFiles(host);
assertNotNull(files);
assertFalse(files.isEmpty());
final PsiElement injected = files.get(0).getFirst();
assertEquals(expected, injected.getText());
}
use of com.intellij.lang.injection.InjectedLanguageManager in project intellij-community by JetBrains.
the class LowLevelSearchUtil method processTreeUp.
/**
* @return null to stop or last found TreeElement
* to be reused via <code>lastElement<code/> param in subsequent calls to avoid full tree rescan (n^2->n).
*/
private static TreeElement processTreeUp(@NotNull Project project, @NotNull TextOccurenceProcessor processor, @NotNull PsiElement scope, @NotNull StringSearcher searcher, final int offset, final boolean processInjectedPsi, @NotNull ProgressIndicator progress, TreeElement lastElement) {
if (scope instanceof PsiCompiledElement) {
throw new IllegalArgumentException("Scope is compiled, can't scan: " + scope);
}
final int scopeStartOffset = scope.getTextRange().getStartOffset();
final int patternLength = searcher.getPatternLength();
ASTNode scopeNode = scope.getNode();
boolean useTree = scopeNode != null;
assert scope.isValid();
int start;
TreeElement leafNode = null;
PsiElement leafElement = null;
if (useTree) {
leafNode = findNextLeafElementAt(scopeNode, lastElement, offset);
if (leafNode == null)
return lastElement;
start = offset - leafNode.getStartOffset() + scopeStartOffset;
} else {
if (scope instanceof PsiFile) {
leafElement = ((PsiFile) scope).getViewProvider().findElementAt(offset, scope.getLanguage());
} else {
leafElement = scope.findElementAt(offset);
}
if (leafElement == null)
return lastElement;
assert leafElement.isValid();
start = offset - leafElement.getTextRange().getStartOffset() + scopeStartOffset;
}
if (start < 0) {
throw new AssertionError("offset=" + offset + "; scopeStartOffset=" + scopeStartOffset + "; leafElement=" + leafElement + "; scope=" + scope + "; leafElement.isValid(): " + (leafElement == null ? null : leafElement.isValid()));
}
InjectedLanguageManager injectedLanguageManager = InjectedLanguageManager.getInstance(project);
lastElement = leafNode;
boolean contains = false;
PsiElement prev = null;
TreeElement prevNode = null;
PsiElement run = null;
while (run != scope) {
progress.checkCanceled();
if (useTree) {
start += prevNode == null ? 0 : prevNode.getStartOffsetInParent();
prevNode = leafNode;
run = leafNode.getPsi();
} else {
start += prev == null ? 0 : prev.getStartOffsetInParent();
prev = run;
run = leafElement;
}
//do not compute if already contains
if (!contains)
contains = run.getTextLength() - start >= patternLength;
if (contains) {
if (processInjectedPsi) {
Boolean result = processInjectedFile(run, processor, searcher, progress, injectedLanguageManager);
if (result != null) {
return result.booleanValue() ? lastElement : null;
}
}
if (!processor.execute(run, start)) {
return null;
}
}
if (useTree) {
leafNode = leafNode.getTreeParent();
if (leafNode == null)
break;
} else {
leafElement = leafElement.getParent();
if (leafElement == null)
break;
}
}
assert run == scope : "Malbuilt PSI; scopeNode: " + scope + "; containingFile:" + PsiTreeUtil.getParentOfType(scope, PsiFile.class, false) + "; leafNode: " + run + "; isAncestor=" + PsiTreeUtil.isAncestor(scope, run, false) + "; in same file: " + (PsiTreeUtil.getParentOfType(scope, PsiFile.class, false) == PsiTreeUtil.getParentOfType(run, PsiFile.class, false));
return lastElement;
}
use of com.intellij.lang.injection.InjectedLanguageManager in project intellij-community by JetBrains.
the class InjectedGeneralHighlightingPass method addInjectedPsiHighlights.
// returns false if canceled
private boolean addInjectedPsiHighlights(@NotNull final Set<PsiFile> injectedFiles, @NotNull final ProgressIndicator progress, @NotNull final Collection<HighlightInfo> outInfos) {
if (injectedFiles.isEmpty())
return true;
final InjectedLanguageManager injectedLanguageManager = InjectedLanguageManager.getInstance(myProject);
final TextAttributes injectedAttributes = myGlobalScheme.getAttributes(EditorColors.INJECTED_LANGUAGE_FRAGMENT);
return JobLauncher.getInstance().invokeConcurrentlyUnderProgress(new ArrayList<>(injectedFiles), progress, isFailFastOnAcquireReadAction(), injectedPsi -> addInjectedPsiHighlights(injectedPsi, injectedAttributes, outInfos, progress, injectedLanguageManager));
}
use of com.intellij.lang.injection.InjectedLanguageManager in project intellij-community by JetBrains.
the class HighlightUsagesHandler method getNameIdentifierRange.
@Nullable
public static TextRange getNameIdentifierRange(PsiFile file, PsiElement element) {
final InjectedLanguageManager injectedManager = InjectedLanguageManager.getInstance(element.getProject());
if (element instanceof PomTargetPsiElement) {
final PomTarget target = ((PomTargetPsiElement) element).getTarget();
if (target instanceof PsiDeclaredTarget) {
final PsiDeclaredTarget declaredTarget = (PsiDeclaredTarget) target;
final TextRange range = declaredTarget.getNameIdentifierRange();
if (range != null) {
if (range.getStartOffset() < 0 || range.getLength() <= 0) {
return null;
}
final PsiElement navElement = declaredTarget.getNavigationElement();
if (PsiUtilBase.isUnderPsiRoot(file, navElement)) {
return injectedManager.injectedToHost(navElement, range.shiftRight(navElement.getTextRange().getStartOffset()));
}
}
}
}
if (!PsiUtilBase.isUnderPsiRoot(file, element)) {
return null;
}
PsiElement identifier = IdentifierUtil.getNameIdentifier(element);
if (identifier != null && PsiUtilBase.isUnderPsiRoot(file, identifier)) {
return injectedManager.injectedToHost(identifier, identifier.getTextRange());
}
return null;
}
use of com.intellij.lang.injection.InjectedLanguageManager in project intellij-community by JetBrains.
the class TemplateLanguageErrorFilter method shouldHighlightErrorElement.
@Override
public boolean shouldHighlightErrorElement(@NotNull PsiErrorElement element) {
if (isKnownSubLanguage(element.getParent().getLanguage())) {
//
// Immediately discard filters with non-matching template class if already known
//
Class templateClass = element.getUserData(TEMPLATE_VIEW_PROVIDER_CLASS_KEY);
if (templateClass != null && (templateClass != myTemplateFileViewProviderClass))
return true;
PsiFile psiFile = element.getContainingFile();
int offset = element.getTextOffset();
InjectedLanguageManager injectedLanguageManager = InjectedLanguageManager.getInstance(element.getProject());
if (injectedLanguageManager.isInjectedFragment(psiFile)) {
PsiElement host = injectedLanguageManager.getInjectionHost(element);
if (host != null) {
psiFile = host.getContainingFile();
offset = injectedLanguageManager.injectedToHost(element, offset);
}
}
final FileViewProvider viewProvider = psiFile.getViewProvider();
element.putUserData(TEMPLATE_VIEW_PROVIDER_CLASS_KEY, viewProvider.getClass());
if (!(viewProvider.getClass() == myTemplateFileViewProviderClass)) {
return true;
}
//
if (shouldIgnoreErrorAt(viewProvider, offset) || shouldIgnoreErrorAt(viewProvider, offset + 1))
return false;
}
return true;
}
Aggregations