Search in sources :

Example 16 with PyStringLiteralExpression

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

the class PyDunderAllReference method resolve.

@Override
public PsiElement resolve() {
    final PyStringLiteralExpression element = getElement();
    final String name = element.getStringValue();
    PyFile containingFile = (PyFile) element.getContainingFile();
    return containingFile.getElementNamed(name);
}
Also used : PyStringLiteralExpression(com.jetbrains.python.psi.PyStringLiteralExpression) PyFile(com.jetbrains.python.psi.PyFile)

Example 17 with PyStringLiteralExpression

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

the class PyKeywordTypedHandler method getOverTypeResult.

@Nullable
public Result getOverTypeResult(Editor editor, PsiFile file) {
    final Document document = editor.getDocument();
    final int offset = editor.getCaretModel().getOffset();
    PsiElement token = file.findElementAt(offset - 1);
    // sanity check: beyond EOL
    if (token == null || offset >= document.getTextLength())
        return Result.CONTINUE;
    PsiElement here_elt = file.findElementAt(offset);
    if (here_elt == null)
        return Result.CONTINUE;
    if (here_elt instanceof PyStringLiteralExpression || here_elt.getParent() instanceof PyStringLiteralExpression)
        return Result.CONTINUE;
    // double colons aren't found in Python's syntax, so we can safely overtype a colon everywhere but strings.
    String here_text = here_elt.getText();
    if (":".equals(here_text)) {
        // overtype, that is, jump over
        editor.getCaretModel().moveToOffset(offset + 1);
        return Result.STOP;
    }
    return null;
}
Also used : PyStringLiteralExpression(com.jetbrains.python.psi.PyStringLiteralExpression) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 18 with PyStringLiteralExpression

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

the class PyMagicLiteralRenameProcessor method renameElement.

@Override
public void renameElement(final PsiElement element, final String newName, final UsageInfo[] usages, @Nullable final RefactoringElementListener listener) {
    Preconditions.checkArgument(canProcessElement(element), "Element can't be renamed, call #canProcessElement first " + element);
    element.replace(PyElementGenerator.getInstance(element.getProject()).createStringLiteral((PyStringLiteralExpression) element, newName));
    for (final UsageInfo usage : usages) {
        final PsiReference reference = usage.getReference();
        if (reference != null) {
            reference.handleElementRename(newName);
        }
    }
}
Also used : PyStringLiteralExpression(com.jetbrains.python.psi.PyStringLiteralExpression) PsiReference(com.intellij.psi.PsiReference) UsageInfo(com.intellij.usageView.UsageInfo)

Example 19 with PyStringLiteralExpression

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

the class RemovePrefixQuickFix method applyFix.

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    final PyStringLiteralExpression pyString = as(descriptor.getPsiElement(), PyStringLiteralExpression.class);
    if (pyString != null) {
        final PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
        for (ASTNode node : pyString.getStringNodes()) {
            final String nodeText = node.getText();
            final int prefixLength = PyStringLiteralExpressionImpl.getPrefixLength(nodeText);
            if (nodeText.substring(0, prefixLength).equalsIgnoreCase(myPrefix)) {
                final PyStringLiteralExpression replacement = elementGenerator.createStringLiteralAlreadyEscaped(nodeText.substring(prefixLength));
                node.getPsi().replace(replacement.getFirstChild());
            }
        }
    }
}
Also used : PyStringLiteralExpression(com.jetbrains.python.psi.PyStringLiteralExpression) ASTNode(com.intellij.lang.ASTNode) PyElementGenerator(com.jetbrains.python.psi.PyElementGenerator)

Example 20 with PyStringLiteralExpression

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

the class PyQuotedStringIntention method doInvoke.

public void doInvoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
    PyStringLiteralExpression string = PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyStringLiteralExpression.class);
    PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
    if (string != null) {
        final String stringText = string.getText();
        int prefixLength = PyStringLiteralExpressionImpl.getPrefixLength(stringText);
        final String text = stringText.substring(prefixLength);
        if (text.startsWith("'") && text.endsWith("'")) {
            String result = convertSingleToDoubleQuoted(stringText);
            PyStringLiteralExpression st = elementGenerator.createStringLiteralAlreadyEscaped(result);
            string.replace(st);
        }
        if (text.startsWith("\"") && text.endsWith("\"")) {
            String result = convertDoubleToSingleQuoted(stringText);
            PyStringLiteralExpression st = elementGenerator.createStringLiteralAlreadyEscaped(result);
            string.replace(st);
        }
    }
}
Also used : PyStringLiteralExpression(com.jetbrains.python.psi.PyStringLiteralExpression) PyElementGenerator(com.jetbrains.python.psi.PyElementGenerator)

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