Marking source roots for IDE integrations

I’m trying to break ground on a plugin that registers a new type of source set. For as much reading as I have done, I can’t quite find a definitive source of truth on how my new source set ought to be registered.

I know so far as to extend each top-level source set, so for example:

sourceSets {
  main { // or all {}
    kotlin { ... }
    abc { ... }
  }
}

For the abc source sets, I add their sourceDirectories to allSource; something like:

sourceSets {
  all { topLevelSourceSet ->
    abc { abcSourceSet ->
      // sourceDirectories will later resolve to 'src/${abcSourceSet.name}/abc'
      topLevelSourceSet.allSource.srcDirs(abcSourceSet.abc.sourceDirectories)
    }
  }
}

I imagine this should be enough for IDEs to recognize src/main/abc or src/test/abc as a source root. But after some digging, IntelliJ requires registering the source roots:

project.plugins.withId("idea") { plugin ->
  val idea = plugin as? IdeaPlugin ?: return@withId // just to be sure
  abcSourceSets.all { abcSourceSet ->
    idea.model.module.apply {
      // I've tried many forms of this, +=, project.files, etc
      sourceDirs.addAll(abcSourceSets.abc.sourceDirectories)
    }
  }
}

But, as it turns out, idea is only applied after the project is evaluated. I could require that idea be applied before my plugin, but even the kotlin plugin doesn’t require this. I still haven’t figured out how to invoke the correct configuration. I’m stuck–please help!

I know so far as to extend each top-level source set

Sounds quite very very very wrong.

I imagine this should be enough for IDEs to recognize src/main/abc or src/test/abc as a source root.

Just

sourceSets {
    abc
}

That’s it.

But it is src/abc/java and src/abc/kotlin and so on.
Not src/main/abc and src/test/abc.

main and test are source sets.
java and kotlin are source directory sets, that would again be something completely different.
For that you might for example look at the java, groovy or Kotlin/JVM plugins.

But after some digging, IntelliJ requires registering the source roots:

It does not, especially if you use proper IDE import.

I see what you mean to explain, but my goal isn’t to add a new top level source set. I see that kotlin tracks its own Kotlin source sets, but also extends the project source sets. This is my goal; I want to extend, say, main so that authors can add source files under src/main/abc for my “ABC Compiler,” so to speak.

Im taking a lot of cues from kotlin; I’ve modeled a ABCSourceSet after KotlinSourceSet and added a ABCSourceSet.abc: SourceDirectorySet like KotlinSourceSet.kotlin: SourceDirectorySet. I’m at an impasse with IDE integrations.