Skip to content

Commit

Permalink
Fix #29 Kotlin imports can be renamed
Browse files Browse the repository at this point in the history
  • Loading branch information
sambsnyd committed Mar 3, 2023
1 parent ce7ac10 commit 617c064
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1480,12 +1480,24 @@ public J visitImport(FirImport firImport, ExecutionContext ctx) {
firImport.getImportedFqName().asString();
J.FieldAccess qualid = TypeTree.build(packageName).withPrefix(space);
skip(qualid.toString());
JLeftPadded<J.Identifier> alias = null;
if(firImport.getAliasName() != null) {
Space asPrefix = sourceBefore("as");
Space aliasPrefix = whitespace();
String aliasText = firImport.getAliasName().asString();
cursor += aliasText.length();
// This feels not quite right, could probably record type information here
J.Identifier aliasId = createIdentifier(aliasText)
.withPrefix(aliasPrefix);
alias = padLeft(asPrefix, aliasId);
}
return new J.Import(
randomId(),
prefix,
Markers.EMPTY,
statik,
qualid);
qualid,
alias);
}

@Override
Expand Down
27 changes: 25 additions & 2 deletions src/main/java/org/openrewrite/kotlin/internal/KotlinPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,25 @@ public J visitIdentifier(J.Identifier ident, PrintOutputCapture<P> p) {
return ident;
}

@Override
public J visitImport(J.Import impoort, PrintOutputCapture<P> p) {
beforeSyntax(impoort, Space.Location.IMPORT_PREFIX, p);
p.append("import");
if (impoort.isStatic()) {
visitSpace(impoort.getPadding().getStatic().getBefore(), Space.Location.STATIC_IMPORT, p);
p.append("static");
}
visit(impoort.getQualid(), p);
JLeftPadded<J.Identifier> alias = impoort.getPadding().getAlias();
if(alias != null) {
visitSpace(alias.getBefore(), Space.Location.IMPORT_ALIAS_PREFIX, p);
p.append("as");
visit(alias.getElement(), p);
}
afterSyntax(impoort, p);
return impoort;
}

@Override
public J visitInstanceOf(J.InstanceOf instanceOf, PrintOutputCapture<P> p) {
beforeSyntax(instanceOf, Space.Location.INSTANCEOF_PREFIX, p);
Expand Down Expand Up @@ -776,6 +795,10 @@ private void beforeSyntax(K k, KSpace.Location loc, PrintOutputCapture<P> p) {
beforeSyntax(k.getPrefix(), k.getMarkers(), loc, p);
}

private void beforeSyntax(J j, Space.Location loc, PrintOutputCapture<P> p) {
beforeSyntax(j.getPrefix(), j.getMarkers(), loc, p);
}

private void beforeSyntax(Space prefix, Markers markers, @Nullable KSpace.Location loc, PrintOutputCapture<P> p) {
for (Marker marker : markers.getMarkers()) {
p.append(p.getMarkerPrinter().beforePrefix(marker, new Cursor(getCursor(), marker), JAVA_MARKER_WRAPPER));
Expand Down Expand Up @@ -806,8 +829,8 @@ private void beforeSyntax(Space prefix, Markers markers, @Nullable Space.Locatio
}
}

private void afterSyntax(K k, PrintOutputCapture<P> p) {
afterSyntax(k.getMarkers(), p);
private void afterSyntax(J j, PrintOutputCapture<P> p) {
afterSyntax(j.getMarkers(), p);
}

private void afterSyntax(Markers markers, PrintOutputCapture<P> p) {
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/org/openrewrite/kotlin/tree/ImportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,11 @@ void wildCard() {
kotlin("import kotlin.collections.*")
);
}

@Test
void alias() {
rewriteRun(
kotlin("import kotlin.collections.List as L")
);
}
}

0 comments on commit 617c064

Please sign in to comment.