use of com.jetbrains.python.psi.types.PyClassTypeImpl in project intellij-community by JetBrains.
the class PyStringConcatenationToFormatIntention method doInvoke.
public void doInvoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
PsiElement element = PsiTreeUtil.getTopmostParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyBinaryExpression.class);
if (element == null)
return;
final LanguageLevel languageLevel = LanguageLevel.forElement(element);
final boolean useFormatMethod = languageLevel.isAtLeast(LanguageLevel.PYTHON27);
NotNullFunction<String, String> escaper = StringUtil.escaper(false, "\"\'\\");
StringBuilder stringLiteral = new StringBuilder();
List<String> parameters = new ArrayList<>();
Pair<String, String> quotes = Pair.create("\"", "\"");
boolean quotesDetected = false;
final TypeEvalContext context = TypeEvalContext.userInitiated(file.getProject(), file);
int paramCount = 0;
boolean isUnicode = false;
final PyClassTypeImpl unicodeType = PyBuiltinCache.getInstance(element).getObjectType("unicode");
for (PyExpression expression : getSimpleExpressions((PyBinaryExpression) element)) {
if (expression instanceof PyStringLiteralExpression) {
final PyType type = context.getType(expression);
if (type != null && type.equals(unicodeType)) {
isUnicode = true;
}
if (!quotesDetected) {
quotes = PyStringLiteralUtil.getQuotes(expression.getText());
quotesDetected = true;
}
String value = ((PyStringLiteralExpression) expression).getStringValue();
if (!useFormatMethod) {
value = value.replace("%", "%%");
}
stringLiteral.append(escaper.fun(value));
} else {
addParamToString(stringLiteral, paramCount, useFormatMethod);
parameters.add(expression.getText());
++paramCount;
}
}
if (quotes == null)
quotes = Pair.create("\"", "\"");
stringLiteral.insert(0, quotes.getFirst());
if (isUnicode && !quotes.getFirst().toLowerCase().contains("u"))
stringLiteral.insert(0, "u");
stringLiteral.append(quotes.getSecond());
PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
if (!parameters.isEmpty()) {
if (useFormatMethod) {
stringLiteral.append(".format(").append(StringUtil.join(parameters, ",")).append(")");
} else {
final String paramString = parameters.size() > 1 ? "(" + StringUtil.join(parameters, ",") + ")" : StringUtil.join(parameters, ",");
stringLiteral.append(" % ").append(paramString);
}
final PyExpression expression = elementGenerator.createFromText(LanguageLevel.getDefault(), PyExpressionStatement.class, stringLiteral.toString()).getExpression();
element.replace(expression);
} else {
PyStringLiteralExpression stringLiteralExpression = elementGenerator.createStringLiteralAlreadyEscaped(stringLiteral.toString());
element.replace(stringLiteralExpression);
}
}
use of com.jetbrains.python.psi.types.PyClassTypeImpl in project intellij-community by JetBrains.
the class AddFieldQuickFix method getClassType.
private static PyClassType getClassType(@NotNull final PsiElement element) {
if (element instanceof PyQualifiedExpression) {
final PyExpression qualifier = ((PyQualifiedExpression) element).getQualifier();
if (qualifier == null)
return null;
final PyType type = TypeEvalContext.userInitiated(element.getProject(), element.getContainingFile()).getType(qualifier);
return type instanceof PyClassType ? (PyClassType) type : null;
}
final PyClass aClass = PsiTreeUtil.getParentOfType(element, PyClass.class);
return aClass != null ? new PyClassTypeImpl(aClass, false) : null;
}
use of com.jetbrains.python.psi.types.PyClassTypeImpl in project intellij-community by JetBrains.
the class PyQtTypeProvider method getReturnType.
@Override
public Ref<PyType> getReturnType(@NotNull PyCallable callable, @NotNull TypeEvalContext context) {
if (PyNames.INIT.equals(callable.getName()) && callable instanceof PyFunction) {
final PyFunction function = (PyFunction) callable;
final PyClass containingClass = function.getContainingClass();
if (containingClass != null && ourQt4Signal.equals(containingClass.getName())) {
final String classQName = containingClass.getQualifiedName();
if (classQName != null) {
final QualifiedName name = QualifiedName.fromDottedString(classQName);
final String qtVersion = name.getComponents().get(0);
final PyClass aClass = PyClassNameIndex.findClass(qtVersion + "." + ourQtBoundSignal, function.getProject());
if (aClass != null) {
final PyType type = new PyClassTypeImpl(aClass, false);
return Ref.create(type);
}
}
}
}
return null;
}
use of com.jetbrains.python.psi.types.PyClassTypeImpl in project intellij-community by JetBrains.
the class PyQtTypeProvider method getCallableType.
@Nullable
@Override
public PyType getCallableType(@NotNull PyCallable callable, @NotNull TypeEvalContext context) {
if (callable instanceof PyFunction) {
final String qualifiedName = callable.getQualifiedName();
if (qualifiedName != null && qualifiedName.startsWith("PyQt")) {
final QualifiedName name = QualifiedName.fromDottedString(qualifiedName);
final String qtVersion = name.getComponents().get(0);
final String docstring = ((PyFunction) callable).getDocStringValue();
if (docstring != null && docstring.contains("[signal]")) {
final PyClass aClass = PyClassNameIndex.findClass(qtVersion + "." + ourQtBoundSignal, callable.getProject());
if (aClass != null)
return new PyClassTypeImpl(aClass, false);
}
}
}
return null;
}
use of com.jetbrains.python.psi.types.PyClassTypeImpl in project intellij-community by JetBrains.
the class AddMethodQuickFix method getClassType.
private static PyClassType getClassType(@NotNull final PsiElement problemElement) {
if ((problemElement instanceof PyQualifiedExpression)) {
final PyExpression qualifier = ((PyQualifiedExpression) problemElement).getQualifier();
if (qualifier == null)
return null;
final PyType type = TypeEvalContext.userInitiated(problemElement.getProject(), problemElement.getContainingFile()).getType(qualifier);
return type instanceof PyClassType ? (PyClassType) type : null;
}
final PyClass pyClass = PsiTreeUtil.getParentOfType(problemElement, PyClass.class);
return pyClass != null ? new PyClassTypeImpl(pyClass, false) : null;
}
Aggregations