@Override public void visitPyStringLiteralExpression(PyStringLiteralExpression node) { final Pair<PsiElement, TextRange> data = node.getUserData(PyReplaceExpressionUtil.SELECTION_BREAKS_AST_NODE); if (data != null) { final PsiElement parent = data.getFirst(); final String text = parent.getText(); final Pair<String, String> detectedQuotes = PythonStringUtil.getQuotes(text); final Pair<String, String> quotes = detectedQuotes != null ? detectedQuotes : Pair.create("'", "'"); final TextRange range = data.getSecond(); final String substring = range.substring(text); myResult.append(quotes.getFirst() + substring + quotes.getSecond()); } else { ASTNode child = node.getNode().getFirstChildNode(); while (child != null) { String text = child.getText(); if (child.getElementType() == TokenType.WHITE_SPACE) { if (text.contains("\n")) { if (!text.contains("\\")) { myResult.append("\\"); } myResult.append(text); } } else { myResult.append(text); } child = child.getTreeNext(); } } }
@Override public void visitElement(PsiElement element) { if (element.getChildren().length == 0) { myResult.append(element.getText()); } else { super.visitElement(element); } }
@Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { final PsiElement element = descriptor.getPsiElement(); final PyFunction function = PsiTreeUtil.getParentOfType(element, PyFunction.class, false); assert function != null; final PyParameterList parameterList = function.getParameterList(); final PyParameter[] parameters = parameterList.getParameters(); final PyElementGenerator generator = PyElementGenerator.getInstance(project); String selfText = parameters.length != 0 ? parameters[0].getText() : PyNames.CANONICAL_SELF; final StringBuilder functionText = new StringBuilder("def foo(" + selfText); if (myHasValue) { String valueText = parameters.length > 1 ? parameters[1].getText() : "value"; functionText.append(", ").append(valueText); } functionText.append("): pass"); final PyParameterList list = generator .createFromText( LanguageLevel.forElement(element), PyFunction.class, functionText.toString()) .getParameterList(); parameterList.replace(list); }
@NotNull private static String buildDisplayMethodName(@NotNull final PyFunction pyFunction) { final StringBuilder builder = new StringBuilder(pyFunction.getName()); builder.append('('); final PyParameter[] arguments = pyFunction.getParameterList().getParameters(); for (final PyParameter parameter : arguments) { builder.append(parameter.getName()); if (arguments.length > 1 && parameter != arguments[arguments.length - 1]) { builder.append(", "); } } builder.append(')'); return builder.toString(); }
@NotNull public String getPresentableText(@NotNull String myName) { final StringBuilder sb = new StringBuilder(getQualifiedName(myName, myPath, myImportElement)); PsiElement parent = null; if (myImportElement != null) { parent = myImportElement.getParent(); } if (myImportable instanceof PyFunction) { sb.append(((PyFunction) myImportable).getParameterList().getPresentableText(false)); } else if (myImportable instanceof PyClass) { final List<String> supers = ContainerUtil.mapNotNull( ((PyClass) myImportable).getSuperClasses(), new Function<PyClass, String>() { @Override public String fun(PyClass cls) { return PyUtil.isObjectClass(cls) ? null : cls.getName(); } }); if (!supers.isEmpty()) { sb.append("("); StringUtil.join(supers, ", ", sb); sb.append(")"); } } if (parent instanceof PyFromImportStatement) { sb.append(" from "); final PyFromImportStatement fromImportStatement = (PyFromImportStatement) parent; sb.append(StringUtil.repeat(".", fromImportStatement.getRelativeLevel())); final PyReferenceExpression source = fromImportStatement.getImportSource(); if (source != null) { sb.append(source.getReferencedName()); } } return sb.toString(); }
/** * Helper method that builds an import path, handling all these "import foo", "import foo as bar", * "from bar import foo", etc. Either importPath or importSource must be not null. * * @param name what is ultimately imported. * @param importPath known path to import the name. * @param source known ImportElement to import the name; its 'as' clause is used if present. * @return a properly qualified name. */ @NotNull public static String getQualifiedName( @NotNull String name, @Nullable QualifiedName importPath, @Nullable PyImportElement source) { final StringBuilder sb = new StringBuilder(); if (source != null) { final PsiElement parent = source.getParent(); if (parent instanceof PyFromImportStatement) { sb.append(name); } else { sb.append(source.getVisibleName()).append(".").append(name); } } else { if (importPath != null && importPath.getComponentCount() > 0) { sb.append(importPath).append("."); } sb.append(name); } return sb.toString(); }
public String result() { return myResult.toString(); }
@Override public void visitWhiteSpace(PsiWhiteSpace space) { myResult.append(space.getText().replace('\n', ' ').replace("\\", "")); }