Search in sources :

Example 26 with PyStringLiteralExpression

use of com.jetbrains.python.psi.PyStringLiteralExpression in project intellij-community by JetBrains.

the class PyStringLiteralTest method createLiteralFromText.

private PyStringLiteralExpression createLiteralFromText(final String text) {
    final PsiFile file = PsiFileFactory.getInstance(myFixture.getProject()).createFileFromText("test.py", "a = " + text);
    final PyStringLiteralExpression expr = PsiTreeUtil.getParentOfType(file.findElementAt(5), PyStringLiteralExpression.class);
    assert expr != null;
    return expr;
}
Also used : PyStringLiteralExpression(com.jetbrains.python.psi.PyStringLiteralExpression) PsiFile(com.intellij.psi.PsiFile)

Example 27 with PyStringLiteralExpression

use of com.jetbrains.python.psi.PyStringLiteralExpression in project intellij-community by JetBrains.

the class DocStringReferenceProvider method getReferencesByElement.

@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull final PsiElement element, @NotNull ProcessingContext context) {
    if (element == DocStringUtil.getParentDefinitionDocString(element)) {
        final PyStringLiteralExpression expr = (PyStringLiteralExpression) element;
        final List<TextRange> ranges = expr.getStringValueTextRanges();
        final String exprText = expr.getText();
        final TextRange textRange = PyStringLiteralExpressionImpl.getNodeTextRange(exprText);
        final String text = textRange.substring(exprText);
        if (!ranges.isEmpty()) {
            final List<PsiReference> result = new ArrayList<>();
            final int offset = ranges.get(0).getStartOffset();
            // XXX: It does not work with multielement docstrings
            StructuredDocString docString = DocStringUtil.parse(text, element);
            if (docString instanceof TagBasedDocString) {
                final TagBasedDocString taggedDocString = (TagBasedDocString) docString;
                result.addAll(referencesFromNames(expr, offset, docString, taggedDocString.getTagArguments(TagBasedDocString.PARAM_TAGS), ReferenceType.PARAMETER));
                result.addAll(referencesFromNames(expr, offset, docString, taggedDocString.getTagArguments(TagBasedDocString.PARAM_TYPE_TAGS), ReferenceType.PARAMETER_TYPE));
                result.addAll(referencesFromNames(expr, offset, docString, docString.getKeywordArgumentSubstrings(), ReferenceType.KEYWORD));
                result.addAll(referencesFromNames(expr, offset, docString, taggedDocString.getTagArguments("var"), ReferenceType.VARIABLE));
                result.addAll(referencesFromNames(expr, offset, docString, taggedDocString.getTagArguments("cvar"), ReferenceType.CLASS_VARIABLE));
                result.addAll(referencesFromNames(expr, offset, docString, taggedDocString.getTagArguments("ivar"), ReferenceType.INSTANCE_VARIABLE));
                result.addAll(returnTypes(element, docString, offset));
            } else if (docString instanceof SectionBasedDocString) {
                final SectionBasedDocString sectioned = (SectionBasedDocString) docString;
                result.addAll(referencesFromFields(expr, offset, sectioned.getParameterFields(), ReferenceType.PARAMETER));
                result.addAll(referencesFromFields(expr, offset, sectioned.getKeywordArgumentFields(), ReferenceType.KEYWORD));
                result.addAll(referencesFromFields(expr, offset, sectioned.getAttributeFields(), PyUtil.isTopLevel(element) ? ReferenceType.GLOBAL_VARIABLE : ReferenceType.INSTANCE_VARIABLE));
                result.addAll(referencesFromFields(expr, offset, sectioned.getReturnFields(), null));
            }
            return result.toArray(new PsiReference[result.size()]);
        }
    }
    return PsiReference.EMPTY_ARRAY;
}
Also used : StructuredDocString(com.jetbrains.python.psi.StructuredDocString) PyStringLiteralExpression(com.jetbrains.python.psi.PyStringLiteralExpression) ArrayList(java.util.ArrayList) PsiReference(com.intellij.psi.PsiReference) TextRange(com.intellij.openapi.util.TextRange) StructuredDocString(com.jetbrains.python.psi.StructuredDocString) NotNull(org.jetbrains.annotations.NotNull)

Example 28 with PyStringLiteralExpression

use of com.jetbrains.python.psi.PyStringLiteralExpression in project intellij-community by JetBrains.

the class PyLiteralSelectionHandler method select.

@Override
public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) {
    final PyStringLiteralExpression literal = PsiTreeUtil.getParentOfType(e, PyStringLiteralExpression.class);
    if (literal != null) {
        List<TextRange> ranges = literal.getStringValueTextRanges();
        List<ASTNode> nodes = literal.getStringNodes();
        for (int i = 0; i < ranges.size(); i++) {
            TextRange stringRange = ranges.get(i);
            TextRange offsetRange = stringRange.shiftRight(literal.getTextRange().getStartOffset());
            if (offsetRange.contains(cursorOffset) && offsetRange.getLength() > 1) {
                List<TextRange> result = new ArrayList<>();
                SelectWordUtil.addWordHonoringEscapeSequences(editorText, nodes.get(i).getTextRange(), cursorOffset, new PyStringLiteralLexer(nodes.get(i).getElementType()), result);
                result.add(offsetRange);
                return result;
            }
        }
    }
    return Collections.emptyList();
}
Also used : PyStringLiteralLexer(com.jetbrains.python.lexer.PyStringLiteralLexer) PyStringLiteralExpression(com.jetbrains.python.psi.PyStringLiteralExpression) ASTNode(com.intellij.lang.ASTNode) ArrayList(java.util.ArrayList) TextRange(com.intellij.openapi.util.TextRange)

Aggregations

PyStringLiteralExpression (com.jetbrains.python.psi.PyStringLiteralExpression)28 TextRange (com.intellij.openapi.util.TextRange)10 PsiElement (com.intellij.psi.PsiElement)6 NotNull (org.jetbrains.annotations.NotNull)5 ASTNode (com.intellij.lang.ASTNode)4 PyElementGenerator (com.jetbrains.python.psi.PyElementGenerator)4 ArrayList (java.util.ArrayList)3 Nullable (org.jetbrains.annotations.Nullable)3 PsiFile (com.intellij.psi.PsiFile)2 PsiReference (com.intellij.psi.PsiReference)2 UsageInfo (com.intellij.usageView.UsageInfo)2 PyFile (com.jetbrains.python.psi.PyFile)2 PyBlockEvaluator (com.jetbrains.python.psi.impl.blockEvaluator.PyBlockEvaluator)2 Map (java.util.Map)2 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)1 Document (com.intellij.openapi.editor.Document)1 Editor (com.intellij.openapi.editor.Editor)1 Module (com.intellij.openapi.module.Module)1 Project (com.intellij.openapi.project.Project)1