Skip to content

Commit

Permalink
Support KString
Browse files Browse the repository at this point in the history
  • Loading branch information
kunli2 committed Oct 6, 2023
1 parent ca3557c commit e09752b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,17 @@ public J visitBinaryWithTypeRHSExpression(KtBinaryExpressionWithTypeRHS expressi

@Override
public J visitBlockStringTemplateEntry(KtBlockStringTemplateEntry entry, ExecutionContext data) {
throw new UnsupportedOperationException("TODO");
J tree = entry.getExpression().accept(this, data);
boolean inBraces = true;

return new K.KString.Value(
randomId(),
Space.EMPTY,
Markers.EMPTY,
tree,
suffix(entry.getExpression()),
inBraces
);
}

@Override
Expand Down Expand Up @@ -362,12 +372,16 @@ public J visitLiteralStringTemplateEntry(KtLiteralStringTemplateEntry entry, Exe
throw new UnsupportedOperationException("Unsupported KtStringTemplateEntry child");
}

boolean quoted = entry.getPrevSibling().getNode().getElementType() == KtTokens.OPEN_QUOTE &&
entry.getNextSibling().getNode().getElementType() == KtTokens.CLOSING_QUOTE;

String valueSource = quoted ? "\"" + leaf.getText() + "\"" : leaf.getText();
return new J.Literal(
Tree.randomId(),
Space.EMPTY,
Markers.EMPTY,
leaf.getText(),
"\"" + leaf.getText() + "\"", // todo, support text block
valueSource, // todo, support text block
null,
primitiveType(entry)
);
Expand Down Expand Up @@ -1696,8 +1710,23 @@ public J visitSimpleNameExpression(KtSimpleNameExpression expression, ExecutionC
@Override
public J visitStringTemplateExpression(KtStringTemplateExpression expression, ExecutionContext data) {
KtStringTemplateEntry[] entries = expression.getEntries();

if (entries.length > 1) {
throw new UnsupportedOperationException("Unsupported constant expression elementType, TODO");
String delimiter = "\"";
List<J> values = new ArrayList<>();

for (KtStringTemplateEntry entry : entries) {
values.add(entry.accept(this, data));
}

return new K.KString(
randomId(),
prefix(expression),
Markers.EMPTY,
delimiter,
values,
type(expression)
);
}

if (entries.length == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,8 @@ private static String printTreeElement(Tree tree) {
|| tree instanceof J.TypeParameter
|| tree instanceof K.CompilationUnit
|| tree instanceof K.StatementExpression
|| tree instanceof K.KString
|| tree instanceof K.KString.Value
|| tree instanceof K.ExpressionStatement
|| tree instanceof J.Package
) {
Expand Down

0 comments on commit e09752b

Please sign in to comment.