From 819c8ec784dc3576bf62ca0b30cd5bf81d830af5 Mon Sep 17 00:00:00 2001 From: Kun Li Date: Tue, 7 Nov 2023 21:14:00 -0800 Subject: [PATCH] Fix import parsing --- .../kotlin/internal/KotlinTreeParserVisitor.java | 14 ++++++++++---- .../org/openrewrite/kotlin/tree/ImportTest.java | 2 +- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java b/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java index a8b8f1e99..5dddde28d 100644 --- a/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java +++ b/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java @@ -2361,10 +2361,16 @@ public J visitImportDirective(KtImportDirective importDirective, ExecutionContex ASTNode node = importDirective.getNode().findChildByType(KtTokens.IMPORT_KEYWORD); LeafPsiElement importPsi = (LeafPsiElement) node; - String text = nodeRangeText( - findFirstNonSpaceNextSibling(importPsi).getNode(), - importDirective.isAllUnder() ? importDirective.getNode().findChildByType(KtTokens.MUL) - : importDirective.getNode().findChildByType(KtNodeTypes.DOT_QUALIFIED_EXPRESSION)); + PsiElement first = findFirstNonSpaceNextSibling(importPsi); + ASTNode last = importDirective.isAllUnder() ? importDirective.getNode().findChildByType(KtTokens.MUL) + : importDirective.getNode().findChildByType(KtNodeTypes.DOT_QUALIFIED_EXPRESSION); + if (last == null) { + PsiElement lastPsi = findLastNotSpaceChild(importDirective); + if (lastPsi != null) { + last = lastPsi.getNode(); + } + } + String text = nodeRangeText(first != null ? first.getNode() : null, last); J reference = TypeTree.build(text); // FIXME: this creates a shallow class for a resolvable type. reference = reference.withPrefix(suffix(importPsi)); diff --git a/src/test/java/org/openrewrite/kotlin/tree/ImportTest.java b/src/test/java/org/openrewrite/kotlin/tree/ImportTest.java index df29a5483..c7103a78a 100644 --- a/src/test/java/org/openrewrite/kotlin/tree/ImportTest.java +++ b/src/test/java/org/openrewrite/kotlin/tree/ImportTest.java @@ -96,7 +96,7 @@ class A { void methodName() { rewriteRun( kotlin("fun Class.createInstance() {}"), - kotlin("import createInstance") + kotlin("import createInstance /*C1*/") ); }