Project: KCL (Provisioning / Automation & Configuration)
File: ide_kpm_watcher.md

The KCL Watch System aims to monitor changes to files and directories within the KCL package directory. It dynamically detects file types based on their extensions and invokes corresponding handlers to perform actions upon changes. This system enhances the development experience by providing real-time feedback and enabling rapid iteration during development and continuous integration. I researched various file watching systems such as Pyright, TypeScript, and Rust Analyzer to understand their architecture and functionality. I found that these systems share a similar core architecture and interface functionalities: Typescript: Offers a file watching API through libraries like `fs.watch` or `chokidar`. Core interface functionalities include initializing a watcher, registering watch paths, defining event handlers, and error handling. Configuration can be done via `tsconfig.json` to customize watch behavior. Pyright: Utilizes the `watchdog` library for file system monitoring. Core interface functionalities align with those of other languages: initializing a watcher, registering watch paths, handling events, and error handling. Reference: Rust Analyzer: Uses the `notify` crate for file system monitoring. Core interface includes initializing a watcher, registering watch paths, defining event handlers, and handling errors. Configuration is done via `watch.toml` to customize watch tasks, file patterns to watch, ignore patterns, and commands to execute upon file changes. Reference: Utilizes a file system notification library (e.g., `notify`) to monitor changes to files and directories. Maintains a list of registered watch paths and corresponding event handlers. Runs asynchronously to minimize latency and improve responsiveness. A file detector analyzes files to determine their types based on their extensions like `.k, .mod, .JSON, .YAML`, etc. Provides a mapping of file types to corresponding handlers. Registers and manages operations for different file types defined above. Handlers define actions to be executed upon file system events (e.g., file creation, modification, deletion). Manages configuration settings for the KCL Watch System. Allows developers to specify watch paths, file types, and handler configurations. Supports configuration via configuration files (e.g., `kcl.toml`). `initialize()`: Initializes the watcher component. `registerPath(path: string)`: Registers a path to monitor for file system events. `unregisterPath(path: string)`: Unregisters a path from monitoring. `on(event: string, handler: Function)`: Registers an event handler for a specific file system event. `start()`: Starts the watcher to begin monitoring registered paths. `stop()`: Stops the watcher and clears all registered paths and handlers. `detectFileType(filePath: string): string`: Determines the file type based on its extension. `registerHandler(fileType: string, handler: Function)`: Registers a handler for a specific file type. `unregisterHandler(fileType: string)`: Unregisters a handler for a specific file type. `loadConfig(configPath: string)`: Loads configuration settings from a specified configuration file. `setWatchPaths(paths: string[])`: Sets the watch paths based on the configuration. `setHandlerConfigurations(config: Object)`: Sets handler configurations based on the configuration file. ```mermaid graph LR A[KCL] --> B[Watcher] B --> C[File Type Detector] C --> D[Handler Registry] D --> E[Configuration Manager] E --> B B -->|File System Events| F[Event Handling] C -->|File Type| F D -->|Handler Actions| F E -->|Configuration Settings| F ``` To integrate the KCL Watch System with the KCL language server, I am going follow these steps: In `main.rs`, import the required modules from the KCL Watch System(to be implemented). Then I will modify the `run_server` function in `main.rs` to initialize the KCL Watch System. Then I will modify the `mainloop` function in `mainloop.rs` to handle file watching events. Also need to create a new module `kclwatchsystem.rs` to implement the watch system integration. Then In `notification.rs`, add the necessary event handlers for file watching events.