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);
}
}
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();
}
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);
}
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);
}
}
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);
}
Aggregations