From e09752bdb731490788326bf60c2e4aaeb5b07aa2 Mon Sep 17 00:00:00 2001 From: Kun Li Date: Thu, 5 Oct 2023 17:30:45 -0700 Subject: [PATCH] Support KString --- .../internal/KotlinTreeParserVisitor.java | 35 +++++++++++++++++-- .../kotlin/internal/PsiTreePrinter.java | 2 ++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java b/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java index b0271c055..efcc2fc2f 100644 --- a/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java +++ b/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java @@ -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 @@ -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) ); @@ -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 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) { diff --git a/src/main/java/org/openrewrite/kotlin/internal/PsiTreePrinter.java b/src/main/java/org/openrewrite/kotlin/internal/PsiTreePrinter.java index b8f68640f..bfc8d9a6d 100644 --- a/src/main/java/org/openrewrite/kotlin/internal/PsiTreePrinter.java +++ b/src/main/java/org/openrewrite/kotlin/internal/PsiTreePrinter.java @@ -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 ) {