use of com.jetbrains.python.documentation.doctest.PyDocstringFile 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.jetbrains.python.documentation.doctest.PyDocstringFile in project intellij-community by JetBrains.
the class PyGenerateDocstringIntention method isAvailable.
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (!(file instanceof PyFile) || file instanceof PyDocstringFile)
return false;
PsiElement elementAt = PyUtil.findNonWhitespaceAtOffset(file, editor.getCaretModel().getOffset());
if (elementAt == null) {
return false;
}
PyFunction function = PsiTreeUtil.getParentOfType(elementAt, PyFunction.class);
final PyStatementList statementList = PsiTreeUtil.getParentOfType(elementAt, PyStatementList.class, false, PyFunction.class);
if (function == null || statementList != null) {
return false;
}
if (!elementAt.equals(function.getNameNode()))
return false;
return isAvailableForFunction(function);
}
use of com.jetbrains.python.documentation.doctest.PyDocstringFile in project intellij-community by JetBrains.
the class PyAnnotateTypesIntention method isAvailable.
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (!(file instanceof PyFile) || file instanceof PyDocstringFile)
return false;
updateText();
final PsiElement elementAt = PyUtil.findNonWhitespaceAtOffset(file, editor.getCaretModel().getOffset());
if (elementAt == null)
return false;
if (resolvesToFunction(elementAt, new Function<PyFunction, Boolean>() {
@Override
public Boolean apply(PyFunction input) {
return true;
}
})) {
updateText();
return true;
}
return false;
}
Aggregations