Otherwise, `org.jetbrains.kotlin.idea.testGenerator.all.AllTestsGeneratedTest.testAllTestsIsUpToDate` fails with an error like: ``` com.intellij.platform.testFramework.core.FileComparisonFailedError: 'GradleBuildFileHighlightingTestGenerated.java' is not up to date Use 'Generate Kotlin Tests' run configuration to regenerate tests at org.jetbrains.kotlin.testGenerator.generator.TestGeneratorKt.write(TestGenerator.kt:156) at org.jetbrains.kotlin.testGenerator.generator.TestGenerator.write(TestGenerator.kt:72) at org.jetbrains.kotlin.testGenerator.generator.TestGenerator.write(TestGenerator.kt:32) at org.jetbrains.kotlin.fe10.testGenerator.Fe10GenerateTestsKt.generateK1Tests(Fe10GenerateTests.kt:172) at org.jetbrains.kotlin.idea.testGenerator.all.AllTestsGeneratedTest.testAllTestsIsUpToDate(AllTestsGeneratedTest.kt:10) at java.base/java.lang.reflect.Method.invoke(Method.java:580) at junit.framework.TestCase.runTest(TestCase.java:177) at junit.framework.TestCase.runBare(TestCase.java:142) at junit.framework.TestResult$1.protect(TestResult.java:122) at junit.framework.TestResult.runProtected(TestResult.java:142) at junit.framework.TestResult.run(TestResult.java:125) at junit.framework.TestCase.run(TestCase.java:130) at junit.framework.TestSuite.runTest(TestSuite.java:241) at junit.framework.TestSuite.run(TestSuite.java:236) at com.intellij.TestAll.runOrCollectNextTest(TestAll.java:399) at com.intellij.TestAll.run(TestAll.java:280) at junit.framework.TestSuite.runTest(TestSuite.java:241) at junit.framework.TestSuite.run(TestSuite.java:236) ``` GitOrigin-RevId: c8a0adce00acd8196ac923bf62425f682be70931
Compiler Reference Index
Compiler reference index service – is a service that allows to reduce the search scope of a declaration usages for compiled projects.
Basis: a large codebase with many declarations and usages with common names like getInstance
Action: find usages of some getInstance declaration
Problem: a lot of false positive candidate files caused by text search
Solution: extract usage information from the compiler to reduce the search scope
What should I do to get the maximum benefit of the index?
Compile project before searching.
How can I check the benefit of the index?
We have Show Compiler Index Status internal action to check the difference between Use Scope and Code Usage Scope.
Q: How to use it?
A: Put the caret at the declaration and call the action
Top level API
PsiSearchHelper#getCodeUsageScopeKotlinScopeOptimizerandJavaCompilerReferencesCodeUsageScopeOptimizerunder the hood
Structure questions
Who writes the index?
- Kotlin – the Kotlin compiler by
IncrementalJvmCache - Java – the JPS javac extension by
JavaBackwardReferenceRegistrar
Who reads the index?
- Kotlin –
KotlinCompilerReferenceIndexStorage - Java –
BackwardReferenceReader
Which module is compiled?
The producer on the JPS plugin side
- Kotlin –
KotlinCompilerReferenceIndexBuilder - Java –
JavaBackwardReferenceIndexBuilder
The consumer on the IDE side
- Kotlin –
DirtyScopeHolderbyKotlinCompilerReferenceIndexService - Java –
DirtyScopeHolderbyCompilerReferenceServiceImpl
Service lifecycle
Project startup
- Preload
CompilerReferenceServiceImplandKotlinCompilerReferenceIndexServiceon the project startup- Subscribe on the VFS events to track changed files
- Subscribe on
BuildManagerListenerto handle the start and the end of a build - Subscribe on
PortableCachesLoadListenerto drop the index to avoid JPS cache corruption
- Schedule
CompilerManager#isUpToDatecheck byIsUpToDateCheckStartupActivityasbackgroundPostStartupActivity
Build started
- Increase
activeBuildCount - Close the index storage if it is open
Build finished
- Decrease
activeBuildCount - Remove compiled modules from the dirty scope
- If
activeBuildCount == 0then open the index storage
Dirty scope
The dirty scope operates with modules. All modules are dirty be default (except for up to date case).
We should track changed files to provide the correct search scope. A change in the file causes the current and all dependent modules to be marked as "dirty".
The scope also contains modules excluded from compilation.
How to calculate the search scope?
The result search scope should contain files from:
- the compiler index
- the dirty scope
- other languages
- libraries if we should search in the libraries scope
What is next?
- KTIJ-19973 – the optimized scope should include the places of declaration definitions as well
- Motivation: this is required to optimize the search of declaration definitions like overrides
- IDEA-281133 – Rewrite
CompilerReferenceServiceandKotlinCompilerReferenceIndexServiceinto one service- Problem: we have two related and similar services