use of com.intellij.lang.injection.InjectedLanguageManager in project intellij-community by JetBrains.
the class AddImportHelper method addFromImportStatement.
/**
* Adds a new {@link PyFromImportStatement} statement within other top-level imports or as specified by anchor.
*
* @param file where to operate
* @param newImport new "from import" statement to insert. It may be generated, because it won't be used for resolving anyway.
* You might want to use overloaded version of this method to generate such statement automatically.
* @param anchor place where the imported name was used. It will be used to determine proper block where new import should be inserted,
* e.g. inside conditional block or try/except statement. Also if anchor is another import statement, new import statement
* will be inserted right after it.
* @see #addFromImportStatement(PsiFile, String, String, String, ImportPriority, PsiElement)
* @see #addFromImportStatement
*/
public static void addFromImportStatement(@NotNull PsiFile file, @NotNull PyFromImportStatement newImport, @Nullable ImportPriority priority, @Nullable PsiElement anchor) {
try {
final PyImportStatementBase parentImport = PsiTreeUtil.getParentOfType(anchor, PyImportStatementBase.class, false);
final InjectedLanguageManager manager = InjectedLanguageManager.getInstance(file.getProject());
final PsiLanguageInjectionHost injectionHost = manager.getInjectionHost(file);
final boolean insideDoctest = file instanceof PyDocstringFile && injectionHost != null && DocStringUtil.getParentDefinitionDocString(injectionHost) == injectionHost;
final PsiElement insertParent;
if (parentImport != null && parentImport.getContainingFile() == file) {
insertParent = parentImport.getParent();
} else if (injectionHost != null && !insideDoctest) {
insertParent = manager.getTopLevelFile(file);
} else {
insertParent = file;
}
if (insideDoctest) {
final PsiElement element = insertParent.addBefore(newImport, getInsertPosition(insertParent, newImport, priority));
PsiElement whitespace = element.getNextSibling();
if (!(whitespace instanceof PsiWhiteSpace)) {
whitespace = PsiParserFacade.SERVICE.getInstance(file.getProject()).createWhiteSpaceFromText(" >>> ");
}
insertParent.addBefore(whitespace, element);
} else if (anchor instanceof PyImportStatementBase) {
insertParent.addAfter(newImport, anchor);
} else {
insertParent.addBefore(newImport, getInsertPosition(insertParent, newImport, priority));
}
} catch (IncorrectOperationException e) {
LOG.error(e);
}
}
use of com.intellij.lang.injection.InjectedLanguageManager in project intellij-community by JetBrains.
the class PyDocReference method getHostScopeOwner.
@Nullable
private ScopeOwner getHostScopeOwner() {
final InjectedLanguageManager languageManager = InjectedLanguageManager.getInstance(myElement.getProject());
final PsiLanguageInjectionHost host = languageManager.getInjectionHost(myElement);
if (host != null) {
final PsiFile file = host.getContainingFile();
ScopeOwner result = ScopeUtil.getScopeOwner(host);
if (result == null && file instanceof ScopeOwner) {
result = (ScopeOwner) file;
}
return result;
}
return null;
}
use of com.intellij.lang.injection.InjectedLanguageManager in project intellij-community by JetBrains.
the class PyDocReference method findContainingStringNode.
@Nullable
private static ASTNode findContainingStringNode(@NotNull PsiElement injectedElement, @NotNull PyStringLiteralExpression host) {
final InjectedLanguageManager manager = InjectedLanguageManager.getInstance(host.getProject());
final List<Pair<PsiElement, TextRange>> files = manager.getInjectedPsiFiles(host);
if (files != null) {
final PsiFile injectedFile = injectedElement.getContainingFile();
final Pair<PsiElement, TextRange> first = ContainerUtil.find(files, pair -> pair.getFirst() == injectedFile);
if (first != null) {
final int hostOffset = -host.getTextRange().getStartOffset();
for (ASTNode node : host.getStringNodes()) {
final TextRange relativeNodeRange = node.getTextRange().shiftRight(hostOffset);
if (relativeNodeRange.contains(first.getSecond())) {
return node;
}
}
}
}
return null;
}
use of com.intellij.lang.injection.InjectedLanguageManager in project intellij-community by JetBrains.
the class PyDocReference method multiResolve.
@NotNull
@Override
public ResolveResult[] multiResolve(boolean incompleteCode) {
ResolveResult[] results = super.multiResolve(incompleteCode);
if (results.length == 0) {
final InjectedLanguageManager languageManager = InjectedLanguageManager.getInstance(myElement.getProject());
final PsiLanguageInjectionHost host = languageManager.getInjectionHost(myElement);
final String referencedName = myElement.getReferencedName();
if (referencedName == null)
return ResolveResult.EMPTY_ARRAY;
if (host != null) {
final List<Pair<PsiElement, TextRange>> files = languageManager.getInjectedPsiFiles(host);
if (files != null) {
for (Pair<PsiElement, TextRange> pair : files) {
final PyResolveProcessor processor = new PyResolveProcessor(referencedName);
PyResolveUtil.scopeCrawlUp(processor, (ScopeOwner) pair.getFirst(), referencedName, pair.getFirst());
final List<RatedResolveResult> resultList = getResultsFromProcessor(referencedName, processor, pair.getFirst(), pair.getFirst());
if (resultList.size() > 0) {
List<RatedResolveResult> ret = RatedResolveResult.sorted(resultList);
return ret.toArray(new RatedResolveResult[ret.size()]);
}
}
}
final PyResolveProcessor processor = new PyResolveProcessor(referencedName);
final ScopeOwner scopeOwner = getHostScopeOwner();
if (scopeOwner != null) {
final PsiFile topLevel = scopeOwner.getContainingFile();
PyResolveUtil.scopeCrawlUp(processor, scopeOwner, referencedName, topLevel);
final PsiElement referenceAnchor = getScopeControlFlowAnchor(host);
final List<RatedResolveResult> resultList = getResultsFromProcessor(referencedName, processor, referenceAnchor, topLevel);
if (resultList.size() > 0) {
final List<RatedResolveResult> ret = RatedResolveResult.sorted(resultList);
return ret.toArray(new RatedResolveResult[ret.size()]);
}
}
}
}
return results;
}
use of com.intellij.lang.injection.InjectedLanguageManager in project intellij-community by JetBrains.
the class PyInjectionResolveTest method doResolve.
@Override
protected PsiElement doResolve() {
myFixture.configureByFile("resolve/" + getTestName(false) + ".py");
final PsiFile psiFile = myFixture.getFile();
final int markerOffset = findMarkerOffset(psiFile);
final InjectedLanguageManager manager = InjectedLanguageManager.getInstance(myFixture.getProject());
final PsiElement injectedElement = manager.findInjectedElementAt(psiFile, markerOffset);
assertNotNull("no injected element found at <ref> position", injectedElement);
PsiReference reference = null;
final PyReferenceOwner referenceOwner = PsiTreeUtil.getParentOfType(injectedElement, PyReferenceOwner.class);
if (referenceOwner != null) {
reference = referenceOwner.getReference();
}
assertNotNull("no reference found at <ref> position", reference);
return reference.resolve();
}
Aggregations