I can see sources generated by Dagger/KSP in e.g. ...
# intellij
w
I can see sources generated by Dagger/KSP in e.g. find usages window, despite having
show results in generated code
unchecked. I have similar issue with other generated code like classes from Apollo-Kotlin plugin. I see https://youtrack.jetbrains.com/issue/KT-45161/Gradle-Support-registering-generated-sources-with-the-Kotlin-model, but is there a reliable way to workaround this and mark the sources as generated easily?
2
c
are you looking for something like this?
Copy code
plugins {
    idea
}
Copy code
idea {
    module {
        generatedSourceDirs.add(File(layout.buildDirectory.get().asFile, "generated-sources/kotlin-dsl-accessors/kotlin"))
        generatedSourceDirs.add(File(layout.buildDirectory.get().asFile, "generated-sources/kotlin-dsl-plugins/kotlin"))
    }
}
w
probably yes, though I was hoping there's some easier way that doesn't require using
idea
plugin, even if it would be something to configure in IDE
I can't believe everyone uses
idea
plugin which would mean folks just always get generated code usage suggestions etc.? 😕
m
+1. Feels like something that should come by default. Similarly to how I don’t have to tell IDEA what my dependencies are, I wish IDEA could get that information from the KGP model.
w
alright 😄 Is it really not compatible, or only its tasks? 🤔 should I only apply this plugin when Gradle is invoked for IDE sync (if I can know that in the first place)?
b
tangentially, shouldn't
build
folders ignored by default, like
.git
is for instance? (In Settings | Editor | File Types | Ignored File Types and Folders)
2
w
according to the hint in settings, ignored files are not indexed, which actually was a problem in the past (and occasionally still is) where calling generated code would be red in IDE
b
oooh good point
e
w
I seem to have some luck with the following live plugin:
Copy code
import com.intellij.openapi.extensions.LoadingOrder
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.GeneratedSourcesFilter
import com.intellij.openapi.vfs.VirtualFile

class MarkGeneratedDirectoriesPlugin : GeneratedSourcesFilter() {

    override fun isGeneratedSource(file: VirtualFile, project: Project): Boolean {
        val path = file.path
        return path.contains("/build/generated/ksp/") || path.contains("/build/generated/source")
    }
}

GeneratedSourcesFilter.EP_NAME.point.registerExtension(MarkGeneratedDirectoriesPlugin(), LoadingOrder.LAST, pluginDisposable)
loaded in https://github.com/dkandalov/live-plugin/. I don't know how to mark sources as generated once, but this seems to filter out the generated files from
find usages
results
Asked for the same a bit back..
oh hey, I was there 😄
😁 1
e
Maybe we should package that as an actual plugin.. until this is fixed at least 😁
👀 1
m
I think we still want the generated sources being marked as a “source root” (for navigation)?
But also have extra metadata that says this “source root” is “generated” so that the
show results in generated code
setting applies
Also probably lint could use that to exclude the generated code by default
But I would expect indexing/navigation to happen for generated sources in all cases?
w
Yeah I don't know exactly what the plugin that I wrote does, just sharing since it's a low-effort somewhat-workaround that might help 🙂 It does
ignore
the files, I don't see the paths I marked as generated when searching for usages, and I do see them again when I check
show results in generated code
. The code calling to generated files is also green. This is all much better than the default behavior already. I don't see entire directory marked as generated sources root, and I expect the plugin to be inefficient since I see the filter method called a lot. For now I'll observe if I can see performance impact and if yes, I'll look for a way to mark entire directories as generated (possibly overriding them being marked as
sources
I guess?). But IntelliJ platform is new to me so I struggle to even know where to start 😄
👀 1
👍 1
b
(at least we could integrate that in the Apollo IJ plugin so Apollo generated sources are advertised to be generated)
💯 1
w
and in general
I think we still want the generated sources being marked as a “source root” (for navigation)?
I don't think so, I mean generally if you select some directory under
build
you can mark it either as
source root
or
generated source root
. I think using
source root
is precisely not great because it shows usages in those files even when
show usages in generated sources
option is disabled. 90% of the time I don't care what generated sources do, and I'd even say that for
search usages
action, generated code is never relevant — if there are no other usages, usually when I remove the class, the generated code disappears too
oh, which is what you wrote in the second message about metadata 😄 anyway IJ already has all this functionality, they just don't let KSP/plugins say which sources are generated, for whatever reason 😕
👍 1
m
Ah yes!
I’m coming from Gradle
SourceDirectorySet
where I understand a “source root” as an item of SourceDirectorySet.srcDirs() but not sure what the actual definition for IDEA is.
It’s somewhat related. IIRC removing the
srcDir()
removes the blue folder in IDEA
Ideally Gradle plugins could call something like
srcDir(directory, isGenerated)
w
yep exactly, calling
srcDir()
right now is the only thing you can do but it has downsides. Overall the issue is disappointing since it used to work for Kapt iirc and having generated code marked as generated is such an important functionality. But yeah, currently KGP doesn't have a
generated source directory
source set/metadata iiuc
1
b
I do see this in Android Studio - is this an AS vs IJ difference maybe?
w
is that AS or Android plugin? Since I have IJ but with Android plugin enabled 🤔
b
I'm under the impression that it should essentially be the same thing, but maybe that's oversimplification (also the one coming with IJ is old compared to what is in AS)
🤔 1