AGENTS.md
compiler/psi/AGENTS.mdAGENTS.md
Quality
81/100
Scores the file, not the repository.Length
877 words
14 headings · 5 code blocksRepository
53k
— · pushed 0 days agoLast changed
3 days ago
First indexed 3 days ago.1# Kotlin PSI (Program Structure Interface)23PSI represents Kotlin source code as a syntax tree. It is the foundation for code analysis, navigation, and refactoring in both the compiler and IDE.45## Relationship with Analysis API67PSI provides **syntax** information (structure of code). Analysis API builds on top of PSI to provide **semantic** information (meaning of code).89```10Source Code → PSI Tree (syntax) → Analysis API (semantics) → Symbols11```1213- `KtResolvable` interface marks PSI elements that can be resolved to Analysis API symbols14- When working with PSI, you often need Analysis API to understand what the code means15- See [analysis/AGENTS.md](../../analysis/AGENTS.md) for Analysis API guidelines1617## Module Structure1819- `psi-api/` - Core PSI interfaces (`KtElement`, `KtExpression`, `KtDeclaration`)20- `psi-impl/` - Implementations and stubs for incremental compilation21- `psi-frontend-utils/` - Compiler integration utilities22- `psi-utils/` - Helper utilities2324## Main Classes2526```27KtElement (root interface)28├── KtExpression (calls, literals, operators, etc.)29│ ├── KtCallExpression30│ ├── KtBinaryExpression31│ ├── KtLambdaExpression32│ └── ...33└── KtDeclaration (classes, functions, properties)34 ├── KtClass, KtObjectDeclaration35 ├── KtNamedFunction36 ├── KtProperty37 └── ...38```3940- `KtFile` - root of a Kotlin file's PSI tree41- `KtPsiFactory` - factory for creating PSI elements programmatically4243## Key Patterns4445**Visitor pattern** for AST traversal:46- `KtVisitor<R, D>` - base visitor with return type R and data D47- `KtTreeVisitor<D>` - recursive tree traversal4849**Stubs** for performance:50- Binary PSI representation for faster parsing51- Used for library files and caching5253## PSI Development Rules5455### Shared Principles with Analysis API5657PSI and Analysis API share common development principles. Before contributing:5859→ READ [`analysis/docs/contribution-guide/api-development.md`](../../analysis/docs/contribution-guide/api-development.md) for API design principles60→ READ [`analysis/docs/contribution-guide/api-evolution.md`](../../analysis/docs/contribution-guide/api-evolution.md) for stability and deprecation6162### Java-Kotlin Interoperability6364**J2K Conversion Limitations:**6566Converting Java PSI classes to Kotlin is NOT always possible. Before attempting:67681. **`@JvmName` unavailable in interfaces** — in some cases it is impossible to convert Java methods to Kotlin properties in a binary-compatible way since `@JvmName` cannot be used to fix potential clashes.69702. **Platform type handling** — IntelliJ Platform APIs use Java types extensively; Kotlin's null-safety interop requires careful handling.71 - The classic example is `PsiElement.getParent()` returning `PsiElement!`. After conversion to Kotlin it becomes either `PsiElement?` or `PsiElement` – both of them are breaking changes.72 A workaround is to delegate the implementation to a Java method and keep the return type implicit.73743. **Binary compatibility** — PSI classes are widely used; the binary and source compatibility must be preserved as much as possible.7576**Guidance:** Always consult with PSI maintainers before converting Java classes to Kotlin.7778### PSI-Specific Notes7980**Naming:** All PSI types use the `Kt` prefix (vs `Ka` for Analysis API).8182**Stability annotations:**83- `@KtExperimentalApi` — Experimental public API84- `@KtImplementationDetail` — Internal implementation85- `@KtNonPublicApi` — JetBrains-internal APIs86- `@KtPsiInconsistencyHandling` — Code handling inconsistent PSI states8788**Java-Kotlin interop:** See the "Java-Kotlin Interoperability" section in [api-development.md](../../analysis/docs/contribution-guide/api-development.md).8990**PSI-specific naming patterns:**91- `visit` prefix for visitor methods (e.g., `visitCallExpression`)92- `create` prefix for factory methods in `KtPsiFactory` (e.g., `createExpression`)9394### Documenting KtElement Classes9596General documentation rules from [api-development.md](../../analysis/docs/contribution-guide/api-development.md) apply to all PSI classes. This section describes additional requirements specific to concrete classes implementing `KtElement`.9798**Required documentation for concrete KtElement classes:**991001. **Class description** — A simple explanation of which Kotlin language concept or syntax construct the class represents.1011022. **Code example** — A code snippet showing the syntax in context. Use ASCII-art markers (`^___^`) to indicate the specific portion that the class represents.103104Example documentation format:105```kotlin106/**107 * Represents a function call expression.108 *109 * ### Example:110 *111 * ```kotlin112 * fun main() {113 * println(0)114 * // ^_________^115 * }116 * ```117 */118class KtCallExpression : ...119```120121**Reference examples:**122- `KtCallExpression` and `KtAnnotationEntry` demonstrate the code example format with ASCII-art markers.123124**Test coverage requirement:**125126All concrete `KtElement` classes must be covered by tests in `compiler/psi/psi-impl/testData/psi/`:127- Each test consists of a `.kt` file containing example Kotlin code and a corresponding `.txt` file showing the expected PSI tree structure128- These tests serve as documentation showing which code constructs map to which PSI elements129- When adding a new `KtElement` class, add corresponding test cases demonstrating the syntax it represents130131## Working with Test Data132133PSI test data (`compiler/psi/psi-impl/testData/`) is managed by the same test data manager as the Analysis API, since the134`test-data-manager` convention is applied to `:compiler:psi:psi-impl`. When modifying test data files or running generated tests135(`*Generated`) that compare output against `.txt` files, use `updateTestData` (to rewrite files) or `checkTestData` (to verify only)136instead of standard test commands.137138```bash139# Update all PSI test data140./gradlew :compiler:psi:psi-impl:updateTestData141142# Update by directory (preferred for iteration)143./gradlew updateTestData -Porg.jetbrains.kotlin.testDataManager.options.testDataPath=compiler/psi/psi-impl/testData/psi/annotation/144```145146Note that a single `.kt` file under `testData/psi/` feeds several suites across two modules: the PSI tree (`.txt`) from147`:compiler:psi:psi-impl` (`PsiParsingTest`), plus source stubs (`.stubs.txt`), compiled stubs (`.compiled.stubs.txt`,148`.knm.compiled.stubs.txt`) and decompiled text (`.decompiledText.txt`, `.knm.decompiledText.txt`) from `:analysis:stubs`. Prefer a149path-filtered `updateTestData` from the repo root so that every affected module is picked up.150151→ READ [`analysis/AGENTS.md`](../../analysis/AGENTS.md) ("Working with Test Data") for the full set of options and the rationale152153## Detailed Documentation154155WHEN modifying PSI interfaces or adding new element types:156→ Explore [psi-api/src/org/jetbrains/kotlin/psi/](psi-api/src/org/jetbrains/kotlin/psi/) for existing patterns157158WHEN working with PSI visitors:159→ READ [psi-api/src/org/jetbrains/kotlin/psi/KtVisitor.java](psi-api/src/org/jetbrains/kotlin/psi/KtVisitor.java)160→ READ [psi-api/src/org/jetbrains/kotlin/psi/KtTreeVisitor.java](psi-api/src/org/jetbrains/kotlin/psi/KtTreeVisitor.java)161162WHEN creating PSI elements programmatically:163→ READ [psi-api/src/org/jetbrains/kotlin/psi/KtPsiFactory.kt](psi-api/src/org/jetbrains/kotlin/psi/KtPsiFactory.kt)164165WHEN working with stubs:166→ READ [`analysis/stubs/README.md`](../../analysis/stubs/README.md)167
Also in JetBrains/kotlin
Diff this repo’s formatsOne repository carrying more than one format is the comparison this product exists for: does anyone actually write different content in each file, or is one a copy of the other?
| Repository | Format | Stack | Covers | Score | Changed |
|---|---|---|---|---|---|
| JetBrains/kotlincompiler/AGENTS.md · 53k | AGENTS.md | buildtestgit | 52/100 | 3 days ago | |
| JetBrains/kotlincompiler/build-tools/AGENTS.md · 53k | AGENTS.md | buildteststylearch+2 | 89/100 | 3 days ago | |
| JetBrains/kotlinCLAUDE.md · 53k | CLAUDE.md | agent-behaviour | 25/100 | 3 days ago | |
| JetBrains/kotlinanalysis/AGENTS.md · 53k | AGENTS.md | teststylearchapi+1 | 86/100 | 3 days ago | |
| JetBrains/kotlinanalysis/test-data-manager/AGENTS.md · 53k | AGENTS.md | teststylearchagent-behaviour | 66/100 | 3 days ago | |
| JetBrains/kotlincompiler/fir/analysis-tests/AGENTS.md · 53k | AGENTS.md | testlint-formatarchdeployment | 74/100 | 3 days ago |
Similar configs
Same format, overlapping stack, ranked by quality.
| Repository | Format | Stack | Covers | Score | Changed |
|---|---|---|---|---|---|
| elastic/elasticsearchx-pack/plugin/inference/AGENTS.md · 78k | AGENTS.md | buildtestlint-formatstyle+3 | 100/100 | 3 days ago | |
| elastic/elasticsearchx-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/AGENTS.md · 78k | AGENTS.md | buildtestlint-formatstyle+2 | 100/100 | 3 days ago | |
| react/react-nativepackages/react-native-compatibility-check/AGENTS.md · 126k | AGENTS.md | testlint-formatstylearch+4 | 99/100 | 3 days ago | |
| kurikomi-labs/komi-storeAGENTS.md · 17k | AGENTS.md | buildlint-formatstylearch+1 | 97/100 | 3 days ago | |
| tiann/KernelSUAGENTS.md · 18k | AGENTS.md | setupbuildlint-formatstyle+4 | 97/100 | 3 days ago | |
| elastic/elasticsearchAGENTS.md · 78k | AGENTS.md | buildtestlint-formatstyle+6 | 96/100 | 3 days ago | |
| alibaba/nacosAGENTS.md · 33k | AGENTS.md | buildtestlint-formatstyle+6 | 96/100 | 3 days ago | |
| ktorio/ktorAGENTS.md · 14k | AGENTS.md | buildlint-formatstylearch+7 | 96/100 | 3 days ago |
