kotlinx-resources icon indicating copy to clipboard operation
kotlinx-resources copied to clipboard

Auto-complete for accessing resource files

Open aSemy opened this issue 2 years ago • 2 comments

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:

  1. Implement the @Language tag that the Resources() class can inject languages, specifically the file-reference injection.

    IntelliJ will then automatically suggest file paths

    image

    Unfortunately @Language isn'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.

  2. Generate code so that I can access the files via variables.

    For example, given that resources contains

    .
    └── resources/
        └── test-data/
            └── some-test-file.json
    

    Then 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()
          // ...
      }
    }
    

aSemy avatar Jun 06 '23 09:06 aSemy

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.

goncalossilva avatar Jun 06 '23 14:06 goncalossilva

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.

jbruchanov avatar Jul 22 '23 15:07 jbruchanov