Skip to content

Commit 62ad5e8

Browse files
ZacSweerstschuchortdev
authored andcommitted
Add kotlin and java SourceFile factories
These allow annotating the contents parameter with `@Language`, which in turn is great for consumers because IntelliJ will then apply syntax highlighting to these parameters. This also assumes they're already indented (which is necessary for the syntax highlighting), while allowing specification of is it needs to be treated as not already indented
1 parent 8679389 commit 62ad5e8

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/main/kotlin/com/tschuchort/compiletesting/SourceFile.kt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.tschuchort.compiletesting
22

33
import okio.buffer
44
import okio.sink
5+
import org.intellij.lang.annotations.Language
56
import java.io.File
67

78
/**
@@ -11,6 +12,22 @@ abstract class SourceFile {
1112
internal abstract fun writeIfNeeded(dir: File): File
1213

1314
companion object {
15+
/**
16+
* Create a new Java source file for the compilation when the compilation is run
17+
*/
18+
fun java(name: String, @Language("java") contents: String, trimIndent: Boolean = true): SourceFile {
19+
val finalContents = if (trimIndent) contents.trimIndent() else contents
20+
return new(name, finalContents)
21+
}
22+
23+
/**
24+
* Create a new Kotlin source file for the compilation when the compilation is run
25+
*/
26+
fun kotlin(name: String, @Language("kotlin") contents: String, trimIndent: Boolean = true): SourceFile {
27+
val finalContents = if (trimIndent) contents.trimIndent() else contents
28+
return new(name, finalContents)
29+
}
30+
1431
/**
1532
* Create a new source file for the compilation when the compilation is run
1633
*/
@@ -38,4 +55,4 @@ abstract class SourceFile {
3855
override fun writeIfNeeded(dir: File): File = path
3956
}
4057
}
41-
}
58+
}

0 commit comments

Comments
 (0)