Search in sources :

Example 11 with PyStringLiteralExpression

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

the class ConvertDocstringQuickFix method applyFix.

public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    PsiElement expression = descriptor.getPsiElement();
    if (expression instanceof PyStringLiteralExpression && expression.isWritable()) {
        PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
        String stringText = expression.getText();
        int prefixLength = PyStringLiteralExpressionImpl.getPrefixLength(stringText);
        String prefix = stringText.substring(0, prefixLength);
        String content = stringText.substring(prefixLength);
        if (content.startsWith("'''")) {
            content = content.substring(3, content.length() - 3);
        } else if (content.startsWith("\"\"\""))
            return;
        else {
            content = content.length() == 1 ? "" : content.substring(1, content.length() - 1);
        }
        if (content.endsWith("\""))
            content = StringUtil.replaceSubstring(content, TextRange.create(content.length() - 1, content.length()), "\\\"");
        PyExpression newString = elementGenerator.createDocstring(prefix + "\"\"\"" + content + "\"\"\"").getExpression();
        expression.replace(newString);
    }
}
Also used : PyStringLiteralExpression(com.jetbrains.python.psi.PyStringLiteralExpression) PyExpression(com.jetbrains.python.psi.PyExpression) PyElementGenerator(com.jetbrains.python.psi.PyElementGenerator) PsiElement(com.intellij.psi.PsiElement)

Example 12 with PyStringLiteralExpression

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

the class PyRemoveParameterQuickFix method applyFix.

public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    final PsiElement parameter = descriptor.getPsiElement();
    assert parameter instanceof PyParameter;
    final PyFunction function = PsiTreeUtil.getParentOfType(parameter, PyFunction.class);
    if (function != null) {
        final PyResolveContext resolveContext = PyResolveContext.noImplicits();
        StreamEx.of(PyRefactoringUtil.findUsages(function, false)).map(UsageInfo::getElement).nonNull().map(PsiElement::getParent).select(PyCallExpression.class).flatMap(callExpression -> callExpression.multiMapArguments(resolveContext).stream()).flatMap(mapping -> mapping.getMappedParameters().entrySet().stream()).filter(entry -> entry.getValue() == parameter).forEach(entry -> entry.getKey().delete());
        final PyStringLiteralExpression docStringExpression = function.getDocStringExpression();
        final String parameterName = ((PyParameter) parameter).getName();
        if (docStringExpression != null && parameterName != null) {
            PyDocstringGenerator.forDocStringOwner(function).withoutParam(parameterName).buildAndInsert();
        }
    }
    parameter.delete();
}
Also used : PyBundle(com.jetbrains.python.PyBundle) PyStringLiteralExpression(com.jetbrains.python.psi.PyStringLiteralExpression) UsageInfo(com.intellij.usageView.UsageInfo) PyParameter(com.jetbrains.python.psi.PyParameter) PyResolveContext(com.jetbrains.python.psi.resolve.PyResolveContext) PyRefactoringUtil(com.jetbrains.python.refactoring.PyRefactoringUtil) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) PyCallExpression(com.jetbrains.python.psi.PyCallExpression) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) StreamEx(one.util.streamex.StreamEx) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) PyDocstringGenerator(com.jetbrains.python.documentation.docstrings.PyDocstringGenerator) NotNull(org.jetbrains.annotations.NotNull) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) PyFunction(com.jetbrains.python.psi.PyFunction) PyFunction(com.jetbrains.python.psi.PyFunction) PyStringLiteralExpression(com.jetbrains.python.psi.PyStringLiteralExpression) PyResolveContext(com.jetbrains.python.psi.resolve.PyResolveContext) PsiElement(com.intellij.psi.PsiElement) PyParameter(com.jetbrains.python.psi.PyParameter)

Example 13 with PyStringLiteralExpression

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

the class PyBlockEvaluatorTest method testDictAssignNoEvaluate.

public void testDictAssignNoEvaluate() {
    PyBlockEvaluator eval = doEvaluate("a={}\na['b']='c'", true);
    Map map = (Map) eval.getValue("a");
    assertEquals(1, map.size());
    assertTrue(map.get("b") instanceof PyStringLiteralExpression);
}
Also used : PyStringLiteralExpression(com.jetbrains.python.psi.PyStringLiteralExpression) PyBlockEvaluator(com.jetbrains.python.psi.impl.blockEvaluator.PyBlockEvaluator) Map(java.util.Map)

Example 14 with PyStringLiteralExpression

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

the class PyDocstringLanguageInjector method getLanguagesToInject.

@Override
public void getLanguagesToInject(@NotNull final PsiLanguageInjectionHost host, @NotNull final InjectedLanguagePlaces injectionPlacesRegistrar) {
    if (!(host instanceof PyStringLiteralExpression)) {
        return;
    }
    final Module module = ModuleUtilCore.findModuleForPsiElement(host);
    if (module == null || !PyDocumentationSettings.getInstance(module).isAnalyzeDoctest())
        return;
    if (DocStringUtil.getParentDefinitionDocString(host) == host) {
        int start = 0;
        int end = host.getTextLength() - 1;
        final String text = host.getText();
        final Pair<String, String> quotes = PyStringLiteralUtil.getQuotes(text);
        final List<String> strings = StringUtil.split(text, "\n", false);
        boolean gotExample = false;
        int currentPosition = 0;
        int maxPosition = text.length();
        boolean endsWithSlash = false;
        for (String string : strings) {
            final String trimmedString = string.trim();
            if (!trimmedString.startsWith(">>>") && !trimmedString.startsWith("...") && gotExample && start < end) {
                gotExample = false;
                if (!endsWithSlash)
                    injectionPlacesRegistrar.addPlace(PyDocstringLanguageDialect.getInstance(), TextRange.create(start, end), null, null);
            }
            final String closingQuote = quotes == null ? text.substring(0, 1) : quotes.second;
            if (endsWithSlash && !trimmedString.endsWith("\\")) {
                endsWithSlash = false;
                injectionPlacesRegistrar.addPlace(PyDocstringLanguageDialect.getInstance(), TextRange.create(start, getEndOffset(currentPosition, string, maxPosition, closingQuote)), null, null);
            }
            if (trimmedString.startsWith(">>>")) {
                if (trimmedString.endsWith("\\"))
                    endsWithSlash = true;
                if (!gotExample)
                    start = currentPosition;
                gotExample = true;
                end = getEndOffset(currentPosition, string, maxPosition, closingQuote);
            } else if (trimmedString.startsWith("...") && gotExample) {
                if (trimmedString.endsWith("\\"))
                    endsWithSlash = true;
                end = getEndOffset(currentPosition, string, maxPosition, closingQuote);
            }
            currentPosition += string.length();
        }
        if (gotExample && start < end)
            injectionPlacesRegistrar.addPlace(PyDocstringLanguageDialect.getInstance(), TextRange.create(start, end), null, null);
    }
}
Also used : PyStringLiteralExpression(com.jetbrains.python.psi.PyStringLiteralExpression) Module(com.intellij.openapi.module.Module)

Example 15 with PyStringLiteralExpression

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

the class PyStringLiteralExpressionManipulator method handleContentChange.

@Override
public PyStringLiteralExpressionImpl handleContentChange(@NotNull PyStringLiteralExpressionImpl element, @NotNull TextRange range, String newContent) {
    final PyElementGenerator elementGenerator = PyElementGenerator.getInstance(element.getProject());
    final String escapedText = calculateEscapedText(element.getText(), range, newContent);
    final PyStringLiteralExpression escaped = elementGenerator.createStringLiteralAlreadyEscaped(escapedText);
    return (PyStringLiteralExpressionImpl) element.replace(escaped);
}
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