Search in sources :

Example 6 with PyStringLiteralExpression

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

the class PyStringLiteralTest method testEscaperOffsetInLongUnicodeEscape.

public void testEscaperOffsetInLongUnicodeEscape() {
    final PyStringLiteralExpression expr = createLiteralFromText("u'XXX a\\U0001F600\\U0001F600b YYY'");
    final LiteralTextEscaper<? extends PsiLanguageInjectionHost> escaper = expr.createLiteralTextEscaper();
    final TextRange range = TextRange.create(6, 28);
    assertEquals(6, escaper.getOffsetInHost(0, range));
    assertEquals(7, escaper.getOffsetInHost(1, range));
    // Each \\U0001F600 is represented as a surrogate pair, hence 2 characters-wide step in decoded text
    assertEquals(17, escaper.getOffsetInHost(3, range));
    assertEquals(27, escaper.getOffsetInHost(5, range));
    assertEquals(28, escaper.getOffsetInHost(6, range));
    assertEquals(-1, escaper.getOffsetInHost(7, range));
}
Also used : PyStringLiteralExpression(com.jetbrains.python.psi.PyStringLiteralExpression) TextRange(com.intellij.openapi.util.TextRange)

Example 7 with PyStringLiteralExpression

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

the class PyCompletionConfidence method shouldSkipAutopopup.

@NotNull
@Override
public ThreeState shouldSkipAutopopup(@NotNull PsiElement contextElement, @NotNull PsiFile psiFile, int offset) {
    ASTNode node = contextElement.getNode();
    if (node != null) {
        if (node.getElementType() == PyTokenTypes.FLOAT_LITERAL) {
            return ThreeState.YES;
        }
        if (PyTokenTypes.STRING_NODES.contains(node.getElementType())) {
            final PsiElement parent = contextElement.getParent();
            if (parent instanceof PyStringLiteralExpression) {
                final List<TextRange> ranges = ((PyStringLiteralExpression) parent).getStringValueTextRanges();
                int relativeOffset = offset - parent.getTextRange().getStartOffset();
                if (ranges.size() > 0 && relativeOffset < ranges.get(0).getStartOffset()) {
                    return ThreeState.YES;
                }
            }
        }
    }
    return super.shouldSkipAutopopup(contextElement, psiFile, offset);
}
Also used : PyStringLiteralExpression(com.jetbrains.python.psi.PyStringLiteralExpression) ASTNode(com.intellij.lang.ASTNode) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with PyStringLiteralExpression

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

the class PyMagicLiteralRenameHandler method getStringLiteral.

/**
   * @return string literal under data context or null if not a literal.
   * This method is fast, so it can safely be used at {@link #isAvailableOnDataContext(DataContext)}
   */
@Nullable
private static PyStringLiteralExpression getStringLiteral(@NotNull final DataContext dataContext) {
    final Editor editor = CommonDataKeys.EDITOR.getData(dataContext);
    if (editor == null) {
        return null;
    }
    final PsiFile file = CommonDataKeys.PSI_FILE.getData(dataContext);
    if (file == null) {
        return null;
    }
    final PsiElement element = getElement(file, editor);
    if (element instanceof PyStringLiteralExpression) {
        return (PyStringLiteralExpression) element;
    }
    return null;
}
Also used : PyStringLiteralExpression(com.jetbrains.python.psi.PyStringLiteralExpression) PsiFile(com.intellij.psi.PsiFile) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with PyStringLiteralExpression

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

the class PyFStringsInjector method getLanguagesToInject.

@Override
public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull PsiElement context) {
    final PyStringLiteralExpression pyString = as(context, PyStringLiteralExpression.class);
    if (pyString == null) {
        return;
    }
    for (ASTNode node : pyString.getStringNodes()) {
        final int relNodeOffset = node.getTextRange().getStartOffset() - pyString.getTextRange().getStartOffset();
        for (Fragment offsets : getInjectionRanges(node)) {
            if (offsets.containsNamedUnicodeEscape())
                continue;
            registrar.startInjecting(PyDocstringLanguageDialect.getInstance());
            registrar.addPlace(null, null, pyString, offsets.getContentRange().shiftRight(relNodeOffset));
            registrar.doneInjecting();
        }
    }
}
Also used : PyStringLiteralExpression(com.jetbrains.python.psi.PyStringLiteralExpression) ASTNode(com.intellij.lang.ASTNode) Fragment(com.jetbrains.python.codeInsight.fstrings.FStringParser.Fragment)

Example 10 with PyStringLiteralExpression

use of com.jetbrains.python.psi.PyStringLiteralExpression 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)

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