Class TemplatePolicy
add-template decides regardless of the language it writes.
A dropped component has to be named, and the name depends on what the palette
sent rather than on how any language spells a declaration: an id if
it has one, otherwise its text, otherwise its label, otherwise the class.
Java and Kotlin must agree on that, or the same drop produces
saveButton in one and button in the other for no reason a
developer could see.
Extracted from JavaRewriterUtil, which delegates here rather than
keeping its own copy — the way StylePolicy and BadgePolicy
were before it. Only the policy moved; both languages keep their own
rendering and their own idea of which names are already taken.
The delegation is the point, not tidiness. This started as a copy, and the first thing the Java tests found when it stopped being one was a divergence already in it: an empty label was ignored here and became the identifier "empty" there. Two copies of a decision drift, which is the mismatch this class exists to prevent. Raised in review of #9803.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordWhat a component has to be constructed with, and what that costs. -
Field Summary
FieldsModifier and TypeFieldDescriptionEvery call by which a container takes a component. -
Method Summary
Modifier and TypeMethodDescriptionattachMethod(Class<?> parentType, Class<?> childType) The call that attaches a child to its parent, read from the parent's API.static StringbaseVariableName(CopilotComponent component, String classSimpleName, UnaryOperator<String> toIdentifier) The name a dropped component wants, before anything checks whether it is free.static StringbaseVariableName(String classSimpleName, String id, String innerText, String text, String label, UnaryOperator<String> toIdentifier) The same rule from the parts rather than from a component.constructorFor(CopilotComponent component, String classSimpleName) How to construct this component, for the components that cannot be constructed with nothing.static booleanisKotlinHardKeyword(String name) static StringrequestedVariableName(CopilotComponent component)
-
Field Details
-
ATTACH_CALLS
Every call by which a container takes a component.The union of what
attachMethod(java.lang.Class<?>, java.lang.Class<?>)may write and the plain forms a developer writes by hand, because a locator looking for "what attached this" has to recognise both. Shared so that the writer and the locator cannot disagree about what an attach looks like — one deciding to writeaddItemwhile the other only knowsaddwould mean a component Copilot had just added could not be found again.Exact names, never a prefix test:
addClassNamesandaddThemeVariantsconfigure the receiver and begin with the same three letters, so matching onaddwould read them as attaches and put new configuration above them instead of below.
-
-
Method Details
-
baseVariableName
public static String baseVariableName(CopilotComponent component, String classSimpleName, UnaryOperator<String> toIdentifier) The name a dropped component wants, before anything checks whether it is free.The order is what the palette makes most recognisable in the source: a component the developer gave an
idis called that, one with text or a label is called after it, and anything else falls back to its class. The caller decides what to do when the name is taken, because "taken" means something different in each language's scoping.- Parameters:
component- the component as the palette described itclassSimpleName- the simple name of the class that will be instantiatedtoIdentifier- how to turn a human string into an identifier, which is the one piece of this that a caller already has- Returns:
- the base name, which may already be in use
-
baseVariableName
public static String baseVariableName(String classSimpleName, String id, String innerText, String text, String label, UnaryOperator<String> toIdentifier) The same rule from the parts rather than from a component.For a caller that has the strings and no
CopilotComponent— renaming one that already exists, rather than naming one being created. The order is the rule, and it lives in one place whichever way it is reached.- Parameters:
classSimpleName- the simple name of the component's classid- the id property, ornullinnerText- the text between the tags, ornulltext- the text property, ornulllabel- the label property, ornulltoIdentifier- how to turn a human string into an identifier- Returns:
- the base name, which may already be in use
-
isKotlinHardKeyword
- Parameters:
name- a candidate identifier- Returns:
- whether Kotlin would reject it as a declaration name
-
attachMethod
The call that attaches a child to its parent, read from the parent's API.Asked of the type rather than the tag, because the tag cannot answer it. A table from tag to method name was wrong in both directions: it had no entry for a custom component, or for
FormLayout.addFormRow, and it answeredaddfor everything it did not know — so a Checkbox dropped into a CheckboxGroup gotcheckboxGroup.add(orderId), and a CheckboxGroup has noadd(Component)at all. Reflection over the parent knows what the table could only guess, including for a class Copilot has never heard of. Raised in review of #9803.- Parameters:
parentType- the container being added tochildType- the component being added- Returns:
- the method to call, or empty when the parent takes this child through none of the known calls — the caller refuses rather than writing a call that does not compile
-
requestedVariableName
- Returns:
- the name the palette asked for, or
nullwhen it did not ask. A copied or pasted component carries the name it had, and keeping it is what makes a paste read like the thing it was copied from
-
constructorFor
public static TemplatePolicy.ConstructorCall constructorFor(CopilotComponent component, String classSimpleName) How to construct this component, for the components that cannot be constructed with nothing.Mirrors
JavaRewriterUtil.createComponentConstructor, which made these decisions in JavaParser objects — the decision is the same in any language and only the rendering differs, so it moved here when Kotlin needed it.Gridis deliberately absent. Its argument is a class literal, which isDataEntity.classin Java andDataEntity::class.javain Kotlin, and it needs a record generated alongside it — more than a spelling difference, so it stays with the Java path until someone writes it properly for both.- Parameters:
component- the component as the palette described itclassSimpleName- the simple name of the class being instantiated- Returns:
- the call, never
null;TemplatePolicy.ConstructorCall.EMPTYwhen nothing is needed
-