mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Update PythonDocumentationProvider.describeExpression
This commit is contained in:
@@ -41,11 +41,14 @@ class DocumentationBuilderKit {
|
||||
static final Function<Iterable<String>, Iterable<String>> LSame2 = what -> what;
|
||||
|
||||
@NotNull
|
||||
static final Function<String, String> TO_ONE_LINE_AND_ESCAPE = s -> StringUtil.escapeXml(s.replace('\n', ' '));
|
||||
static final Function<String, String> ESCAPE_ONLY = StringUtil::escapeXml;
|
||||
|
||||
@NotNull
|
||||
static final Function<String, String> TO_ONE_LINE_AND_ESCAPE = s -> ESCAPE_ONLY.apply(s.replace('\n', ' '));
|
||||
|
||||
@NotNull
|
||||
static final Function<String, String> ESCAPE_AND_SAVE_NEW_LINES_AND_SPACES =
|
||||
s -> StringUtil.escapeXml(s).replace("\n", BR).replace(" ", CommonXmlStrings.NBSP);
|
||||
s -> ESCAPE_ONLY.apply(s).replace("\n", BR).replace(" ", CommonXmlStrings.NBSP);
|
||||
|
||||
@NotNull
|
||||
static final Function<String, String> WRAP_IN_ITALIC = s -> "<i>" + s + "</i>";
|
||||
|
||||
@@ -86,6 +86,8 @@ public class PythonDocumentationProvider extends AbstractDocumentationProvider i
|
||||
}
|
||||
}
|
||||
|
||||
final TypeEvalContext context = TypeEvalContext.userInitiated(originalElement.getProject(), originalElement.getContainingFile());
|
||||
|
||||
if (element instanceof PyFunction) {
|
||||
final PyFunction func = (PyFunction)element;
|
||||
final StringBuilder cat = new StringBuilder();
|
||||
@@ -121,13 +123,12 @@ public class PythonDocumentationProvider extends AbstractDocumentationProvider i
|
||||
summary = docString.getSummary();
|
||||
}
|
||||
|
||||
final TypeEvalContext context = TypeEvalContext.userInitiated(originalElement.getProject(), originalElement.getContainingFile());
|
||||
return describeDecorators(cls, Function.identity(), TO_ONE_LINE_AND_ESCAPE, ", ", "\n")
|
||||
.add(describeClass(cls, Function.identity(), TO_ONE_LINE_AND_ESCAPE, false, false, context))
|
||||
.toString() + "\n" + summary;
|
||||
}
|
||||
else if (element instanceof PyExpression) {
|
||||
return describeExpression((PyExpression)element, originalElement);
|
||||
return describeExpression((PyExpression)element, originalElement, ESCAPE_ONLY, context);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -158,35 +159,37 @@ public class PythonDocumentationProvider extends AbstractDocumentationProvider i
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static String describeExpression(@NotNull PyExpression expr, @NotNull PsiElement originalElement) {
|
||||
final String name = expr.getName();
|
||||
private static String describeExpression(@NotNull PyExpression expression,
|
||||
@NotNull PsiElement originalElement,
|
||||
@NotNull Function<String, String> escaper,
|
||||
@NotNull TypeEvalContext context) {
|
||||
final String name = expression.getName();
|
||||
if (name != null) {
|
||||
final StringBuilder result = new StringBuilder((expr instanceof PyNamedParameter) ? "parameter" : "variable");
|
||||
final StringBuilder result = new StringBuilder(expression instanceof PyNamedParameter ? "parameter" : "variable");
|
||||
result.append(String.format(" \"%s\"", name));
|
||||
if (expr instanceof PyNamedParameter) {
|
||||
final PyFunction function = PsiTreeUtil.getParentOfType(expr, PyFunction.class);
|
||||
|
||||
if (expression instanceof PyNamedParameter) {
|
||||
final PyFunction function = PsiTreeUtil.getParentOfType(expression, PyFunction.class);
|
||||
if (function != null) {
|
||||
result.append(" of ").append(function.getContainingClass() == null ? "function" : "method");
|
||||
result.append(String.format(" \"%s\"", function.getName()));
|
||||
result
|
||||
.append(" of ")
|
||||
.append(function.getContainingClass() == null ? "function" : "method")
|
||||
.append(String.format(" \"%s\"", function.getName()));
|
||||
}
|
||||
}
|
||||
|
||||
if (originalElement instanceof PyTypedElement) {
|
||||
final String typeName = getTypeName(((PyTypedElement)originalElement));
|
||||
final String typeName = getTypeName(context.getType(((PyTypedElement)originalElement)), context);
|
||||
result
|
||||
.append("\n")
|
||||
.append(String.format("Inferred type: %s", typeName));
|
||||
}
|
||||
return result.toString();
|
||||
|
||||
return escaper.apply(result.toString());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String getTypeName(@NotNull PyTypedElement element) {
|
||||
final TypeEvalContext context = TypeEvalContext.userInitiated(element.getProject(), element.getContainingFile());
|
||||
return getTypeName(context.getType(element), context);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param type type which name will be calculated
|
||||
* @param context type evaluation context
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
variable "x"
|
||||
variable "x"
|
||||
Inferred type: Union[str, int]
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
parameter "self" of method "foo"
|
||||
parameter "self" of method "foo"
|
||||
Inferred type: A
|
||||
|
||||
Reference in New Issue
Block a user