Phase 2's goal was "functionality unchanged, modules separated", successfully splitting the original command execution functionality into three independent modules, improving code maintainability and extensibility.
File: src/main/kotlin/com/github/switch2ai/config/ConfigurationManager.kt
Functionality:
- Manage AI configuration information (AIConfig)
- Manage action configuration information (ActionConfig)
- Provide default configuration and configuration loading functionality
- Support for obtaining commands for specified AI and actions
Core Classes:
data class AIConfig(
val name: String,
val shortcut: String
)
data class ActionConfig(
val name: String,
val description: String,
val keyMap: String,
val commands: Map<String, String>
)
data class PluginConfig(
val aiList: Map<String, AIConfig>,
val actions: Map<String, ActionConfig>
)Design Notes:
- AI configuration focuses on AI basic information (name and shortcut)
- Action configuration contains complete command mapping, supporting different AI command configurations
- This design makes AI and action responsibilities more clearly separated
File: src/main/kotlin/com/github/switch2ai/utils/InformationReader.kt
Functionality:
- Read command execution context from ActionEvent
- Obtain file path, cursor position, selected text, and other information
- Support variable replacement functionality
- Validate command context validity
Core Classes:
data class CommandContext(
val filePath: String,
val line: Int,
val column: Int,
val selection: String?,
val projectPath: String?
)Supported Variables:
${filePath}: Current file absolute path${line}: Current cursor line number${column}: Current cursor column number${selection}: Selected text${projectPath}: Project path
Note: Variable format uses ${variableName} instead of $variableName to avoid conflicts with system variables
File: src/main/kotlin/com/github/switch2ai/executor/CommandExecutor.kt
Functionality:
- Execute system commands
- Parse command strings into arrays
- Record execution results and error information
- Provide UI feedback (success/failure messages)
Core Classes:
data class ExecutionResult(
val success: Boolean,
val command: String,
val output: String?,
val error: String?,
val exitCode: Int?
)File: src/main/kotlin/com/github/switch2ai/executor/CommandProcessor.kt
Functionality:
- Integrate the above three modules
- Provide unified command processing interface
- Support for obtaining available actions and AI lists
- Validate action and AI combination validity
- Uses new modular architecture
- Code reduced from 115 lines to 25 lines
- Functionality unchanged, but structure clearer
- Now imports
CommandProcessorfromexecutorpackage, reflecting clear module dependencies
- Configuration Module: Responsible for configuration management
- Information Reading Module: Responsible for context information retrieval
- Command Execution Module: Responsible for command execution
- Command Processor: Responsible for module coordination
- Clear code structure, easy to understand
- Each module has single responsibility, small modification impact scope
- Low code duplication
- New functionality only requires modifying corresponding modules
- Adding new AI or actions only requires configuration modification
- Clear interfaces between modules, easy to extend
- Each module can be tested independently
- Clear dependencies between modules
- Easy to write unit tests
- Flexible combination between modules
- Avoid duplicate code
- Improve development efficiency
val commandProcessor = CommandProcessor()
val result = commandProcessor.processCommandWithDefaultAI(event, "switch2ai")val commandProcessor = CommandProcessor()
val result = commandProcessor.processCommand(event, "generateUnitTest", "cursor")val commandProcessor = CommandProcessor()
val actions = commandProcessor.getAvailableActions(project)
val ais = commandProcessor.getAvailableAIs(project)According to promptWord.md planning, the next step will enter Phase 3:
- Implement configuration-based AI, action registration and shortcut binding
- Support configuration persistence and dynamic updates
- Add right-click menu support
- Complete YAML support for configuration files
- Handle shortcut conflicts and configuration change prompts
Phase 3 has been completed with important architectural optimizations:
- Abstract Parent Class Design: Created
AbstractDynamicActionRegistry<T>abstract parent class - Code Reuse: Achieved significant code reuse through inheritance
- Logic Simplification: Two dynamic registries' code volume significantly reduced
- Unified Configuration: Merged configuration management functionality into
AppSettingsState
- Data Class Merging: Used unified
AIConfigDataandActionConfigDatatypes - Configuration Synchronization: Implemented automatic synchronization mechanism for configuration changes
- Performance Optimization: Avoided unnecessary object creation and type conversion
- Extensibility Enhancement: Adding new dynamic registries became very simple
Phase 2 successfully completed modular refactoring, splitting the original monolithic code into modules with clear responsibilities, improving code quality and maintainability. All functionality remains unchanged, but the code structure is clearer, laying a good foundation for subsequent functionality extensions.
According to the latest code adjustments, main changes include:
- Package Structure Adjustment: Moved execution-related modules to
executorpackage, making package structure clearer - AI Configuration Simplification:
AIConfigclass removedcommandsfield, focusing on AI basic information - Dependency Relationship Optimization: Action classes now import dependencies from
executorpackage, reflecting clear module dependencies - Configuration Loading Enhancement: Configuration loading comments updated to support YAML or other formats, preparing for Phase 3 configuration persistence
These fine-tunings further optimize the modular architecture, laying a more solid foundation for Phase 3 development.