Search in sources :

Example 1 with PyDocStringOwner

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;
}
Also used : PyStringLiteralExpression(com.jetbrains.python.psi.PyStringLiteralExpression) PyDocStringOwner(com.jetbrains.python.psi.PyDocStringOwner) PyFile(com.jetbrains.python.psi.PyFile)

Example 2 with PyDocStringOwner

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;
}
Also used : CodeInsightSettings(com.intellij.codeInsight.CodeInsightSettings) PyDocStringOwner(com.jetbrains.python.psi.PyDocStringOwner) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement)

Example 3 with PyDocStringOwner

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();
}
Also used : PyFunction(com.jetbrains.python.psi.PyFunction) ArrayList(java.util.ArrayList) PyDocStringOwner(com.jetbrains.python.psi.PyDocStringOwner) PsiFile(com.intellij.psi.PsiFile) PyFile(com.jetbrains.python.psi.PyFile) PsiElement(com.intellij.psi.PsiElement)

Example 4 with PyDocStringOwner

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);
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyFunction(com.jetbrains.python.psi.PyFunction) PsiNamedElement(com.intellij.psi.PsiNamedElement) ArrayList(java.util.ArrayList) PyDocStringOwner(com.jetbrains.python.psi.PyDocStringOwner) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PyDocStringOwner (com.jetbrains.python.psi.PyDocStringOwner)4 PsiElement (com.intellij.psi.PsiElement)2 PyFile (com.jetbrains.python.psi.PyFile)2 PyFunction (com.jetbrains.python.psi.PyFunction)2 ArrayList (java.util.ArrayList)2 CodeInsightSettings (com.intellij.codeInsight.CodeInsightSettings)1 Document (com.intellij.openapi.editor.Document)1 Pair (com.intellij.openapi.util.Pair)1 PsiFile (com.intellij.psi.PsiFile)1 PsiNamedElement (com.intellij.psi.PsiNamedElement)1 PyClass (com.jetbrains.python.psi.PyClass)1 PyStringLiteralExpression (com.jetbrains.python.psi.PyStringLiteralExpression)1 NotNull (org.jetbrains.annotations.NotNull)1