mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-19 04:51:24 +07:00
PY-75537 Implement PyAstNamedParameter.getRepr()(PyFrontendElementTypesFacadeImpl.kt)
GitOrigin-RevId: ef38af29fd176324f4c1bec503a116a5b4f351b4
This commit is contained in:
committed by
intellij-monorepo-bot
parent
6a3cfa9f34
commit
9034b13880
@@ -17,9 +17,7 @@ package com.jetbrains.python.ast;
|
||||
|
||||
import com.intellij.lang.ASTNode;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.stubs.IStubElementType;
|
||||
import com.intellij.psi.stubs.StubElement;
|
||||
import com.jetbrains.python.PyElementTypes;
|
||||
import com.intellij.util.ObjectUtils;
|
||||
import com.jetbrains.python.PyTokenTypes;
|
||||
import com.jetbrains.python.PythonDialectsTokenSetProvider;
|
||||
import com.jetbrains.python.ast.impl.ParamHelperCore;
|
||||
@@ -94,7 +92,17 @@ public interface PyAstNamedParameter extends PyAstParameter, PsiNamedElement, Ps
|
||||
* Includes asterisks for *param and **param, and name.
|
||||
*/
|
||||
@NotNull
|
||||
String getRepr(boolean includeDefaultValue);
|
||||
default String getRepr(boolean includeDefaultValue) {
|
||||
final StringBuilder sb = new StringBuilder();
|
||||
|
||||
sb.append(ParamHelperCore.getNameInSignature(this));
|
||||
|
||||
if (includeDefaultValue) {
|
||||
sb.append(ObjectUtils.notNull(ParamHelperCore.getDefaultValuePartInSignature(getDefaultValueText(), false), ""));
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
package com.jetbrains.python.ast.impl;
|
||||
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.jetbrains.python.ast.PyAstExpression;
|
||||
import com.jetbrains.python.ast.PyAstNamedParameter;
|
||||
import com.jetbrains.python.ast.PyAstStringLiteralExpression;
|
||||
import com.jetbrains.python.psi.PyStringLiteralCoreUtil;
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@ApiStatus.Experimental
|
||||
@@ -12,6 +16,49 @@ public final class ParamHelperCore {
|
||||
private ParamHelperCore() {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static String getNameInSignature(@NotNull PyAstNamedParameter parameter) {
|
||||
final StringBuilder sb = new StringBuilder();
|
||||
|
||||
if (parameter.isPositionalContainer()) sb.append("*");
|
||||
else if (parameter.isKeywordContainer()) sb.append("**");
|
||||
|
||||
final String name = parameter.getName();
|
||||
sb.append(name != null ? name : "...");
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param defaultValue string returned by {@link PyCallableParameter#getDefaultValueText()} or {@link PyParameter#getDefaultValueText()}.
|
||||
* @param parameterRenderedAsTyped true if parameter is rendered with type annotation.
|
||||
* @return equal sign (surrounded with spaces if {@code parameterRenderedAsTyped}) +
|
||||
* {@code defaultValue} (with body escaped if it is a string literal)
|
||||
*/
|
||||
@Nullable
|
||||
@Contract("null, _->null")
|
||||
public static String getDefaultValuePartInSignature(@Nullable String defaultValue, boolean parameterRenderedAsTyped) {
|
||||
if (defaultValue == null) return null;
|
||||
|
||||
final StringBuilder sb = new StringBuilder();
|
||||
|
||||
// According to PEP 8 equal sign should be surrounded by spaces if annotation is present
|
||||
sb.append(parameterRenderedAsTyped ? " = " : "=");
|
||||
|
||||
final Pair<String, String> quotes = PyStringLiteralCoreUtil.getQuotes(defaultValue);
|
||||
if (quotes != null) {
|
||||
final String value = defaultValue.substring(quotes.getFirst().length(), defaultValue.length() - quotes.getSecond().length());
|
||||
sb.append(quotes.getFirst());
|
||||
StringUtil.escapeStringCharacters(value.length(), value, sb);
|
||||
sb.append(quotes.getSecond());
|
||||
}
|
||||
else {
|
||||
sb.append(defaultValue);
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param defaultValue expression returned by {@link PyCallableParameter#getDefaultValue()} or {@link PyParameter#getDefaultValue()}.
|
||||
* @return {@code defaultValue} value surrounded by quotes if it is a multi-line string literal, {@code defaultValue} text otherwise.
|
||||
|
||||
Reference in New Issue
Block a user