diff options
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/syntax/FunctionSignature.java')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/syntax/FunctionSignature.java | 60 |
1 files changed, 47 insertions, 13 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/FunctionSignature.java b/src/main/java/com/google/devtools/build/lib/syntax/FunctionSignature.java index e1eda63b6a..b38556d34e 100644 --- a/src/main/java/com/google/devtools/build/lib/syntax/FunctionSignature.java +++ b/src/main/java/com/google/devtools/build/lib/syntax/FunctionSignature.java @@ -343,10 +343,24 @@ public abstract class FunctionSignature implements Serializable { FunctionSignature.<T>valueListOrNull(types)); } + public StringBuilder toStringBuilder(final StringBuilder sb) { + return toStringBuilder(sb, true, true, true, false); + } + /** - * Append a representation of this signature to a string buffer. + * Appends a representation of this signature to a string buffer. + * @param sb Output StringBuffer + * @param showNames Determines whether the names of arguments should be printed + * @param showDefaults Determines whether the default values of arguments should be printed (if + * present) + * @param skipMissingTypeNames Determines whether missing type names should be omitted (true) or + * replaced with "object" (false). If showNames is false, "object" is always used as a type name + * to prevent blank spaces. + * @param skipFirstMandatory Determines whether the first mandatory parameter should be omitted. */ - public StringBuilder toStringBuilder(final StringBuilder sb) { + public StringBuilder toStringBuilder(final StringBuilder sb, final boolean showNames, + final boolean showDefaults, final boolean skipMissingTypeNames, + final boolean skipFirstMandatory) { FunctionSignature signature = getSignature(); Shape shape = signature.getShape(); final ImmutableList<String> names = signature.getNames(); @@ -377,28 +391,46 @@ public abstract class FunctionSignature implements Serializable { isMore = true; } public void type(int i) { - if (types != null && types.get(i) != null) { - sb.append(": ").append(types.get(i).toString()); + // We have to assign an artificial type string when the type is null. + // This happens when either + // a) there is no type defined (such as in user-defined functions) or + // b) the type is java.lang.Object. + boolean noTypeDefined = (types == null || types.get(i) == null); + String typeString = noTypeDefined ? "object" : types.get(i).toString(); + if (noTypeDefined && showNames && skipMissingTypeNames) { + // This is the only case where we don't want to append typeString. + // If showNames = false, we ignore skipMissingTypeNames = true and append "object" + // in order to prevent blank spaces. + } else { + // We only append colons when there is a name. + if (showNames) { + sb.append(": "); + } + sb.append(typeString); } } public void mandatory(int i) { comma(); - sb.append(names.get(i)); + if (showNames) { + sb.append(names.get(i)); + } type(i); } public void optional(int i) { mandatory(i); - sb.append(" = "); - if (defaultValues == null) { - sb.append("?"); - } else { - Printer.write(sb, defaultValues.get(j++)); + if (showDefaults) { + sb.append(" = "); + if (defaultValues == null) { + sb.append("?"); + } else { + Printer.write(sb, defaultValues.get(j++)); + } } } }; Show show = new Show(); - int i = 0; + int i = skipFirstMandatory ? 1 : 0; for (; i < mandatoryPositionals; i++) { show.mandatory(i); } @@ -408,7 +440,7 @@ public abstract class FunctionSignature implements Serializable { if (hasStar) { show.comma(); sb.append("*"); - if (starArg) { + if (starArg && showNames) { sb.append(names.get(iStarArg)); } } @@ -421,7 +453,9 @@ public abstract class FunctionSignature implements Serializable { if (kwArg) { show.comma(); sb.append("**"); - sb.append(names.get(iKwArg)); + if (showNames) { + sb.append(names.get(iKwArg)); + } } return sb; |