|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Essential Build Commands |
| 6 | + |
| 7 | +```bash |
| 8 | +# Build the plugin |
| 9 | +./gradlew build |
| 10 | + |
| 11 | +# Generate parser and lexer from BNF/Flex files (required before build) |
| 12 | +./gradlew generateLexer generateParser |
| 13 | + |
| 14 | +# Run tests |
| 15 | +./gradlew test |
| 16 | + |
| 17 | +# Run single test class |
| 18 | +./gradlew test --tests "org.domaframework.doma.intellij.formatter.SqlFormatterTest" |
| 19 | + |
| 20 | +# Code formatting and linting |
| 21 | +./gradlew spotlessApply |
| 22 | +./gradlew spotlessCheck |
| 23 | + |
| 24 | +# Run IntelliJ IDE with plugin for testing |
| 25 | +./gradlew runIde |
| 26 | + |
| 27 | +# Build plugin distribution |
| 28 | +./gradlew buildPlugin |
| 29 | + |
| 30 | +# Verify plugin compatibility |
| 31 | +./gradlew verifyPlugin |
| 32 | +``` |
| 33 | +--- |
| 34 | +## Architecture Overview |
| 35 | + |
| 36 | +This is an IntelliJ IDEA plugin that provides comprehensive development support for the Doma framework (a Java ORM). The plugin bridges the gap between DAO (Data Access Object) Java/Kotlin classes and their corresponding SQL template files. |
| 37 | + |
| 38 | +### Custom Language Support |
| 39 | + |
| 40 | +The plugin defines a custom language "DomaSql" that extends standard SQL with Doma-specific directives: |
| 41 | +- **Parser/Lexer**: Generated from `Sql.bnf` and `Sql.flex` files using GrammarKit |
| 42 | +- **Language Definition**: `SqlLanguage`, `SqlFileType`, `SqlParserDefinition` |
| 43 | +- **Custom Directives**: `/*%if*/`, `/*%for*/`, `/*# embedded */`, `/* bind variables */` |
| 44 | + |
| 45 | +### Key Components Architecture |
| 46 | + |
| 47 | +**Navigation & Actions**: Bidirectional jumping between DAO methods and SQL files |
| 48 | +- `action/dao/JumpToSQLFromDaoAction` - DAO method → SQL file |
| 49 | +- `action/sql/JumpToDaoFromSQLAction` - SQL file → DAO method |
| 50 | +- `gutter/` - Visual gutter icons for quick navigation |
| 51 | + |
| 52 | +**Code Analysis & Validation**: |
| 53 | +- `inspection/dao/` - DAO-related inspections (parameter types, return types, SQL file existence) |
| 54 | +- `inspection/sql/` - SQL-related inspections (bind variables, function calls, directive types) |
| 55 | +- `common/validation/result/` - Validation result types for all checks |
| 56 | + |
| 57 | +**IDE Integration**: |
| 58 | +- `contributor/sql/SqlCompletionContributor` - Code completion for SQL files |
| 59 | +- `formatter/` - SQL formatting with custom spacing and block handling |
| 60 | +- `reference/` - PSI reference resolution for symbols in SQL files |
| 61 | +- `refactoring/dao/` - Automatic SQL file/directory renaming when DAOs are refactored |
| 62 | + |
| 63 | +### Critical Relationships |
| 64 | + |
| 65 | +**DAO ↔ SQL Mapping**: The plugin uses naming conventions to map DAO methods to SQL files: |
| 66 | +- DAO class `com.example.EmployeeDao` maps to SQL directory `META-INF/com/example/EmployeeDao/` |
| 67 | +- Method `findByName` maps to SQL file `findByName.sql` |
| 68 | + |
| 69 | +**PSI Integration**: Heavily uses IntelliJ's PSI (Program Structure Interface): |
| 70 | +- `common/psi/PsiDaoMethod` - Core class representing DAO methods with SQL file resolution |
| 71 | +- `common/dao/DaoClass` - Utilities for detecting `@Dao` annotated classes |
| 72 | +- Custom PSI elements for SQL expressions and directives |
| 73 | + |
| 74 | +## Development Notes |
| 75 | + |
| 76 | +### Parser Generation |
| 77 | +Always run `generateLexer` and `generateParser` tasks after modifying `Sql.bnf` or `Sql.flex`. Generated files go to `src/main/gen/` and are excluded from version control but included in compilation. |
| 78 | + |
| 79 | +### Testing Strategy |
| 80 | +Tests use `DomaProjectDescriptor` to set up IntelliJ test environments with Doma dependencies. Test data is in `src/test/testData/` with separate directories for different feature tests. |
| 81 | + |
| 82 | +### Configuration Support |
| 83 | +The plugin reads `doma.compile.config` files to support custom ExpressionFunctions. This configuration is cached and invalidated based on file modification timestamps. |
| 84 | + |
| 85 | +### Formatter Implementation |
| 86 | +The SQL formatter uses a custom block-based approach with `SqlBlock` hierarchy and `SqlFormattingModelBuilder`. It's currently in preview mode with limited customization options. |
| 87 | + |
| 88 | +--- |
| 89 | +# Tasks for Claude Code |
| 90 | +If you edit the code, run `./gradle spotless` and `/gradle check`. |
| 91 | + |
| 92 | +--- |
| 93 | +# Code Guidelines |
| 94 | +Custom language implementations are written in Java, while plugin features are implemented in Kotlin. |
| 95 | +Custom language lexers and parsers are auto-generated using Grammar-Kit. |
| 96 | + |
| 97 | +## Architecture |
| 98 | +Plugin architecture organizes features into separate packages, with sub-packages categorized by class roles. |
| 99 | +Features are separated into packages for DAO classes and SQL files. |
| 100 | +## Package Structure |
| 101 | +``` |
| 102 | +Feature Package |
| 103 | +├── dao |
| 104 | +│ └── AnAction subclass |
| 105 | +└── sql |
| 106 | + └── AnAction subclass |
| 107 | +``` |
| 108 | + |
| 109 | +### Actions |
| 110 | +Action functionality for navigating between DAO files and SQL files |
| 111 | + |
| 112 | +- **[Feature Package](src/main/kotlin/org/domaframework/doma/intellij/action)** |
| 113 | +- **Main Classes**: `AnAction` subclasses |
| 114 | + |
| 115 | +**Class Naming Rules**: |
| 116 | +Class names should have `Action` as a suffix. |
| 117 | +Classes should not have properties; necessary information should be obtained within functions. |
| 118 | + |
| 119 | +### LineMarker |
| 120 | +Line marker functionality for DAO methods and DOMA directives in SQL |
| 121 | + |
| 122 | +- **[Feature Package](src/main/kotlin/org/domaframework/doma/intellij/gutter)** |
| 123 | +- **Main Classes**: `LineMarkerProvider` subclasses |
| 124 | + - Line marker class for DAO: `DaoLineMarkerProvider` |
| 125 | + - Line marker class for SQL: `SqlLineMarkerProvider` |
| 126 | + - **Class Naming Rules**: Use `Provider` as a suffix |
| 127 | + |
| 128 | +### Inspections |
| 129 | +Code inspection functionality for DAO methods and DOMA directives in SQL |
| 130 | + |
| 131 | +- **[Feature Package](src/main/kotlin/org/domaframework/doma/intellij/inspection)** |
| 132 | +- **Main Classes**: `InspectionTool` |
| 133 | + - Code inspection class for DAO: `AbstractBaseJavaLocalInspectionTool` subclasses |
| 134 | + - Code inspection class for SQL: `LocalInspectionTool` subclasses |
| 135 | + - **Class Naming Rules**: Use `Inspection` as a suffix |
| 136 | +- **Related Classes** |
| 137 | + - Processor: Classes that check inspection target elements. Provides common check processing for inspection targets that meet specific conditions |
| 138 | + - **Class Naming Rules**: Use `Checker` or `Processor` as a suffix depending on the purpose |
| 139 | + - Provider: Provider that returns a list of `InspectionTool` subclasses |
| 140 | + - **Class Naming Rules**: Use `Provider` as a suffix |
| 141 | + - Visitor: Classes that search for inspection target elements. Called from `InspectionTool` |
| 142 | + - **Class Naming Rules**: Have the same name as the calling `InspectionTool` with `Visitor` as a suffix |
| 143 | + - QuickFix: Classes that provide fix actions for inspection results |
| 144 | + - Consists of a `Factory` object that provides quick fix objects and the main `QuickFix` object |
| 145 | + - **Class Naming Rules** |
| 146 | + - `Factory` object: Use `QuickFixFactory` as a suffix |
| 147 | + - `QuickFix` object: Use `QuickFix` as a suffix |
| 148 | + - [ValidationResult](src/main/kotlin/org/domaframework/doma/intellij/common/validation/result): Classes that provide error messages and highlights for code inspection results |
| 149 | + - **Class Naming Rules**: Use `ValidationResult` as a suffix. Name classes according to the message resources to be displayed |
| 150 | + |
| 151 | +### Completion |
| 152 | +Code completion functionality for DOMA directive syntax in SQL |
| 153 | +- **[Feature Package](src/main/kotlin/org/domaframework/doma/intellij/contributor/sql)** |
| 154 | +- **Related Classes** |
| 155 | + - CompletionContributor: Entry point for code completion |
| 156 | + - **Class Naming Rules**: Use `CompletionContributor` as a suffix |
| 157 | + - Provider: Classes that provide completion candidates in specific contexts |
| 158 | + - **Class Naming Rules**: Use `CompletionProvider` as a suffix |
| 159 | + - Processor: Classes that filter and transform completion candidates |
| 160 | + - **Class Naming Rules**: Use `Processor` as a suffix |
| 161 | + - [Handler](src/main/kotlin/org/domaframework/doma/intellij/common/sql/directive): Classes that generate suggestion candidates for each directive type |
| 162 | + - **Class Naming Rules**: Use `Handler` as a suffix |
| 163 | + - [Collector](src/main/kotlin/org/domaframework/doma/intellij/common/sql/directive/collector): Classes that collect directive suggestion candidates. Called from `Handler` |
| 164 | + - **Class Naming Rules**: Use `Collector` as a suffix |
0 commit comments