Skip to content

Latest commit

 

History

History
194 lines (153 loc) · 6.96 KB

File metadata and controls

194 lines (153 loc) · 6.96 KB

Phase 2: Modular Refactoring Complete

Overview

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.

Completed Modules

1. Configuration Module (ConfigurationManager)

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

2. Information Reading Module (InformationReader)

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

3. Command Execution Module (CommandExecutor)

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?
)

4. Command Processor (CommandProcessor)

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

Refactored Action Classes

ExecuteCustomCommandAction

  • Uses new modular architecture
  • Code reduced from 115 lines to 25 lines
  • Functionality unchanged, but structure clearer
  • Now imports CommandProcessor from executor package, reflecting clear module dependencies

Advantages of Modular Architecture

1. Responsibility Separation

  • 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

2. Maintainability

  • Clear code structure, easy to understand
  • Each module has single responsibility, small modification impact scope
  • Low code duplication

3. Extensibility

  • New functionality only requires modifying corresponding modules
  • Adding new AI or actions only requires configuration modification
  • Clear interfaces between modules, easy to extend

4. Testability

  • Each module can be tested independently
  • Clear dependencies between modules
  • Easy to write unit tests

5. Code Reuse

  • Flexible combination between modules
  • Avoid duplicate code
  • Improve development efficiency

Usage Examples

Basic Usage

val commandProcessor = CommandProcessor()
val result = commandProcessor.processCommandWithDefaultAI(event, "switch2ai")

Execute with Specified AI

val commandProcessor = CommandProcessor()
val result = commandProcessor.processCommand(event, "generateUnitTest", "cursor")

Get Available Configuration

val commandProcessor = CommandProcessor()
val actions = commandProcessor.getAvailableActions(project)
val ais = commandProcessor.getAvailableAIs(project)

Next Steps

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 Update Notes

Phase 3 has been completed with important architectural optimizations:

Architectural Optimization Results

  • 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

Technical Improvements

  • Data Class Merging: Used unified AIConfigData and ActionConfigData types
  • 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

Summary

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.

Fine-tuning Notes

According to the latest code adjustments, main changes include:

  1. Package Structure Adjustment: Moved execution-related modules to executor package, making package structure clearer
  2. AI Configuration Simplification: AIConfig class removed commands field, focusing on AI basic information
  3. Dependency Relationship Optimization: Action classes now import dependencies from executor package, reflecting clear module dependencies
  4. 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.