use of com.jetbrains.python.psi.PyDocStringOwner in project intellij-community by JetBrains.
the class PyQuotedStringIntention method isAvailable.
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (!(file instanceof PyFile)) {
return false;
}
PyStringLiteralExpression string = PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyStringLiteralExpression.class);
if (string != null) {
final PyDocStringOwner docStringOwner = PsiTreeUtil.getParentOfType(string, PyDocStringOwner.class);
if (docStringOwner != null) {
if (docStringOwner.getDocStringExpression() == string)
return false;
}
String stringText = string.getText();
int prefixLength = PyStringLiteralExpressionImpl.getPrefixLength(stringText);
stringText = stringText.substring(prefixLength);
if (stringText.length() >= 6) {
if (stringText.startsWith("'''") && stringText.endsWith("'''") || stringText.startsWith("\"\"\"") && stringText.endsWith("\"\"\""))
return false;
}
if (stringText.length() > 2) {
if (stringText.startsWith("'") && stringText.endsWith("'")) {
setText(PyBundle.message("INTN.quoted.string.single.to.double"));
return true;
}
if (stringText.startsWith("\"") && stringText.endsWith("\"")) {
setText(PyBundle.message("INTN.quoted.string.double.to.single"));
return true;
}
}
}
return false;
}
use of com.jetbrains.python.psi.PyDocStringOwner in project intellij-community by JetBrains.
the class PythonSpaceHandler method charTyped.
@Override
public Result charTyped(char c, Project project, @NotNull Editor editor, @NotNull PsiFile file) {
CodeInsightSettings codeInsightSettings = CodeInsightSettings.getInstance();
if (c == ' ' && codeInsightSettings.JAVADOC_STUB_ON_ENTER) {
int offset = editor.getCaretModel().getOffset();
PsiElement element = file.findElementAt(offset);
if (element == null && offset > 1) {
element = file.findElementAt(offset - 2);
}
if (element == null)
return Result.CONTINUE;
// """ or ''' plus space char
int expectedStringStart = offset - 4;
final Document document = editor.getDocument();
if (PythonEnterHandler.canGenerateDocstring(element, expectedStringStart, document) == DocstringState.INCOMPLETE) {
final PyDocStringOwner docOwner = PsiTreeUtil.getParentOfType(element, PyDocStringOwner.class);
if (docOwner != null) {
final String quotes = document.getText(TextRange.from(expectedStringStart, 3));
final String docString = PyDocstringGenerator.forDocStringOwner(docOwner).forceNewMode().withInferredParameters(true).withQuotes(quotes).buildDocString();
document.insertString(offset, docString.substring(3));
if (!StringUtil.isEmptyOrSpaces(docString.substring(3, docString.length() - 3))) {
editor.getCaretModel().moveCaretRelatively(100, 1, false, false, false);
}
return Result.STOP;
}
}
}
return Result.CONTINUE;
}
use of com.jetbrains.python.psi.PyDocStringOwner in project intellij-community by JetBrains.
the class PyTreeStructureProvider method getTopLevelElement.
@Override
public PsiElement getTopLevelElement(PsiElement element) {
PyPsiUtils.assertValid(element);
final Ref<PsiFile> containingFileRef = Ref.create();
ApplicationManager.getApplication().runReadAction(() -> containingFileRef.set(element.getContainingFile()));
final PsiFile containingFile = containingFileRef.get();
if (!(containingFile instanceof PyFile)) {
return null;
}
List<PsiElement> parents = new ArrayList<>();
PyDocStringOwner container = PsiTreeUtil.getParentOfType(element, PyDocStringOwner.class);
while (container != null) {
if (container instanceof PyFile) {
break;
}
parents.add(0, container);
container = PsiTreeUtil.getParentOfType(container, PyDocStringOwner.class);
}
for (PsiElement parent : parents) {
if (parent instanceof PyFunction) {
// we don't display any nodes under functions
return parent;
}
}
if (parents.size() > 0) {
return parents.get(parents.size() - 1);
}
return element.getContainingFile();
}
use of com.jetbrains.python.psi.PyDocStringOwner in project intellij-community by JetBrains.
the class PyTestFinder method findTestsForClass.
@NotNull
@Override
public Collection<PsiElement> findTestsForClass(@NotNull PsiElement element) {
PyDocStringOwner source = findSourceElement(element);
if (source == null)
return Collections.emptySet();
String sourceName = source.getName();
if (sourceName == null)
return Collections.emptySet();
List<Pair<? extends PsiNamedElement, Integer>> classesWithProximities = new ArrayList<>();
if (source instanceof PyClass) {
Collection<String> names = PyClassNameIndex.allKeys(element.getProject());
for (String eachName : names) {
if (eachName.contains(sourceName)) {
for (PyClass eachClass : PyClassNameIndex.find(eachName, element.getProject(), GlobalSearchScope.projectScope(element.getProject()))) {
if (PythonUnitTestUtil.isTestCaseClass(eachClass, null) || PythonDocTestUtil.isDocTestClass(eachClass)) {
classesWithProximities.add(new Pair<PsiNamedElement, Integer>(eachClass, TestFinderHelper.calcTestNameProximity(sourceName, eachName)));
}
}
}
}
} else {
Collection<String> names = PyFunctionNameIndex.allKeys(element.getProject());
for (String eachName : names) {
if (eachName.contains(sourceName)) {
for (PyFunction eachFunction : PyFunctionNameIndex.find(eachName, element.getProject(), GlobalSearchScope.projectScope(element.getProject()))) {
if (PythonUnitTestUtil.isTestCaseFunction(eachFunction) || PythonDocTestUtil.isDocTestFunction(eachFunction)) {
classesWithProximities.add(new Pair<PsiNamedElement, Integer>(eachFunction, TestFinderHelper.calcTestNameProximity(sourceName, eachName)));
}
}
}
}
}
return TestFinderHelper.getSortedElements(classesWithProximities, true);
}
Aggregations