kotlinx-resources
kotlinx-resources copied to clipboard
Auto-complete for accessing resource files
I would like for my IDE to be able to recommend suggestions so that I can quickly access resources when I'm writing the tests, without having to refer to the resources directory.
I have two suggestions:
-
Implement the
@Languagetag that theResources()class can inject languages, specifically thefile-referenceinjection.IntelliJ will then automatically suggest file paths
Unfortunately
@Languageisn't a Kotlin annotation, so it isn't suitable for Kotlin Multiplatform out of the box. But it can be converted to work for Kotlin Multiplatform projects, as demonstrated in Kotlinx Serialization. -
Generate code so that I can access the files via variables.
For example, given that resources contains
. └── resources/ └── test-data/ └── some-test-file.jsonThen the kotlinx-resources plugin could generate the following code:
// build/generated/src/main/kotlin/ResourcesAccessors.kt object Resource { /** `test-data/ directory */ object testData { /** Load the file `test-data/some-test-file.json */ val someTestFileJson = Resource("test-data/some-test-file.json") } }I could then quickly access the file in my test:
// src/commonTest/MyResourcesTests.kt class MyResourcesTests { @Test fun `test json`() { val someTestData = Resource.testData.someTestFileJson.readText() // ... } }
Thanks, that's a great suggestion! I favor the approach using @Language, similar to what kotlinx.serialization is doing. It's something I was unaware of before, but I'll explore it unless someone submits PR before I get to it.
I'd suggest the 2nd option, basically what android does with the R class. In case there is a change/removal it's being caught right away during compile time.