Skip to content

Commit

Permalink
Minor polish
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Nov 18, 2024
1 parent a5d18d5 commit ec039e0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
25 changes: 11 additions & 14 deletions src/main/java/org/openrewrite/kotlin/KotlinParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import org.jetbrains.kotlin.utils.PathUtil;
import org.jspecify.annotations.Nullable;
import org.openrewrite.*;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.internal.JavaTypeCache;
import org.openrewrite.java.marker.JavaSourceSet;
Expand All @@ -87,7 +88,6 @@
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.Collections.emptyList;
Expand All @@ -101,7 +101,7 @@
import static org.jetbrains.kotlin.config.JVMConfigurationKeys.DO_NOT_CLEAR_BINDING_CONTEXT;
import static org.jetbrains.kotlin.config.JVMConfigurationKeys.LINK_VIA_SIGNATURES;
import static org.jetbrains.kotlin.incremental.IncrementalFirJvmCompilerRunnerKt.configureBaseRoots;
import static org.openrewrite.kotlin.KotlinParser.SourcePathFromSourceTextResolver.*;
import static org.openrewrite.kotlin.KotlinParser.SourcePathFromSourceTextResolver.determinePath;

@SuppressWarnings("CommentedOutCode")
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
Expand All @@ -117,7 +117,7 @@ public class KotlinParser implements Parser {
private final Collection<Path> classpath;

@Nullable
private final Collection<Input> dependsOn;
private final List<Input> dependsOn;

private final List<NamedStyles> styles;
private final boolean logCompilationWarningsAndErrors;
Expand All @@ -143,7 +143,7 @@ public Stream<SourceFile> parse(@Language("kotlin") String... sources) {
String pkg = packageMatcher.find() ? packageMatcher.group(1).replace('.', '/') + "/" : "";

String className = Optional.ofNullable(simpleName.apply(sourceFile))
.orElse(Long.toString(System.nanoTime())) + ".kt";
.orElse(Long.toString(System.nanoTime())) + ".kt";

Path path = Paths.get(pkg + className);
return new Input(
Expand All @@ -166,8 +166,7 @@ public Stream<SourceFile> parseInputs(Iterable<Input> sources, @Nullable Path re
// TODO: FIR and disposable may not be necessary using the IR.
Disposable disposable = Disposer.newDisposable();
CompiledSource compilerCus;
List<Input> acceptedInputs = dependsOn == null ? new ArrayList<>() : new ArrayList<>(dependsOn);
acceptedInputs.addAll(acceptedInputs(sources).collect(Collectors.toList()));
List<Input> acceptedInputs = ListUtils.concatAll(dependsOn, acceptedInputs(sources).collect(toList()));
try {
compilerCus = parse(acceptedInputs, disposable, pctx);
} catch (Exception e) {
Expand Down Expand Up @@ -259,7 +258,7 @@ public static class Builder extends Parser.Builder {
@Nullable
private Collection<Path> classpath = emptyList();

private Collection<Input> dependsOn = emptyList();
private List<Input> dependsOn = emptyList();
private JavaTypeCache typeCache = new JavaTypeCache();
private boolean logCompilationWarningsAndErrors;
private final List<NamedStyles> styles = new ArrayList<>();
Expand Down Expand Up @@ -626,9 +625,9 @@ private List<Integer> getCRLFLocations(String source) {
}

static class SourcePathFromSourceTextResolver {
static Pattern packagePattern = Pattern.compile("^package\\s+(.+)\\s");
static Pattern classPattern = Pattern.compile("(class|interface|enum class)\\s*(<[^>]*>)?\\s+(\\w+)");
static Pattern publicClassPattern = Pattern.compile("public\\s+" + classPattern.pattern());
private static final Pattern packagePattern = Pattern.compile("^package\\s+(\\S+)");
private static final Pattern classPattern = Pattern.compile("(class|interface|enum class)\\s*(<[^>]*>)?\\s+(\\w+)");
private static final Pattern publicClassPattern = Pattern.compile("public\\s+" + classPattern.pattern());

private static Optional<String> matchClassPattern(Pattern pattern, String source) {
Matcher classMatcher = pattern.matcher(source);
Expand All @@ -640,13 +639,11 @@ private static Optional<String> matchClassPattern(Pattern pattern, String source

static Path determinePath(String prefix, String sourceCode) {
String className = matchClassPattern(publicClassPattern, sourceCode)
.orElseGet(() -> matchClassPattern(classPattern, sourceCode).orElse(Long.toString(System.nanoTime())));

.orElseGet(() -> matchClassPattern(classPattern, sourceCode)
.orElse(Long.toString(System.nanoTime())));
Matcher packageMatcher = packagePattern.matcher(sourceCode);
String pkg = packageMatcher.find() ? packageMatcher.group(1).replace('.', '/') + "/" : "";

return Paths.get(pkg, prefix + className + ".kt");
}

}
}
15 changes: 6 additions & 9 deletions src/test/java/org/openrewrite/kotlin/KotlinParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,21 @@
*/
package org.openrewrite.kotlin;

import org.intellij.lang.annotations.Language;
import org.junit.jupiter.api.Test;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.kotlin.Assertions.kotlin;

class KotlinParserTest implements RewriteTest {

@Language("kotlin")
private String myClassDefinition = """
package foo.bar
class MyClass
""";

@Test
void classDefinitionFromDependsOn() {
rewriteRun(
spec -> spec.parser(KotlinParser.builder().dependsOn(myClassDefinition)),
spec -> spec.parser(KotlinParser.builder().dependsOn("""
package foo.bar
class MyClass
""")),
kotlin(
"""
import foo.bar.MyClass
Expand All @@ -43,4 +39,5 @@ void classDefinitionFromDependsOn() {
)
);
}

}

0 comments on commit ec039e0

Please sign in to comment.