Skip to content

Commit ff86214

Browse files
design doc for kcl watcher (#1119)
Signed-off-by: Abhishek Kumar <[email protected]>
1 parent c84b12d commit ff86214

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

docs/design/ide_kpm_watcher.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
## KCL Watch System
2+
3+
### 1. Introduction
4+
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.
5+
6+
### 2. Design Research
7+
8+
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:
9+
10+
**Typescript:**
11+
- Offers a file watching API through libraries like `fs.watch` or `chokidar`.
12+
- Core interface functionalities include initializing a watcher, registering watch paths, defining event handlers, and error handling.
13+
- Configuration can be done via `tsconfig.json` to customize watch behavior.
14+
15+
**Pyright:**
16+
- Utilizes the `watchdog` library for file system monitoring.
17+
- Core interface functionalities align with those of other languages: initializing a watcher, registering watch paths, handling events, and error handling.
18+
- Reference: [Watchdog](https://github.com/gorakhargosh/watchdog)
19+
20+
**Rust Analyzer:**
21+
- Uses the `notify` crate for file system monitoring.
22+
- Core interface includes initializing a watcher, registering watch paths, defining event handlers, and handling errors.
23+
- Configuration is done via `watch.toml` to customize watch tasks, file patterns to watch, ignore patterns, and commands to execute upon file changes.
24+
- Reference: [Cargo Watch](https://crates.io/crates/cargo-watch)
25+
26+
### 3. Architecture Overview
27+
28+
#### 3.1 Watcher
29+
- Utilizes a file system notification library (e.g., `notify`) to monitor changes to files and directories.
30+
- Maintains a list of registered watch paths and corresponding event handlers.
31+
- Runs asynchronously to minimize latency and improve responsiveness.
32+
33+
#### 3.2 File Type Detector
34+
- A file detector analyzes files to determine their types based on their extensions like `.k, .mod, .JSON, .YAML`, etc.
35+
- Provides a mapping of file types to corresponding handlers.
36+
37+
#### 3.3 Handler Registry
38+
- Registers and manages operations for different file types defined above.
39+
- Handlers define actions to be executed upon file system events (e.g., file creation, modification, deletion).
40+
41+
#### 3.4 Configuration Manager
42+
- Manages configuration settings for the KCL Watch System.
43+
- Allows developers to specify watch paths, file types, and handler configurations.
44+
- Supports configuration via configuration files (e.g., `kcl.toml`).
45+
46+
### 4. Core API of Watch System (Rough design, actual implementation might differ during development)
47+
48+
#### 4.1 Watcher API
49+
- `initialize()`: Initializes the watcher component.
50+
- `registerPath(path: string)`: Registers a path to monitor for file system events.
51+
- `unregisterPath(path: string)`: Unregisters a path from monitoring.
52+
- `on(event: string, handler: Function)`: Registers an event handler for a specific file system event.
53+
- `start()`: Starts the watcher to begin monitoring registered paths.
54+
- `stop()`: Stops the watcher and clears all registered paths and handlers.
55+
56+
#### 4.2 File Type Detector API
57+
- `detectFileType(filePath: string): string`: Determines the file type based on its extension.
58+
59+
#### 4.3 Handler Registry API
60+
- `registerHandler(fileType: string, handler: Function)`: Registers a handler for a specific file type.
61+
- `unregisterHandler(fileType: string)`: Unregisters a handler for a specific file type.
62+
63+
#### 4.4 Configuration Manager API
64+
- `loadConfig(configPath: string)`: Loads configuration settings from a specified configuration file.
65+
- `setWatchPaths(paths: string[])`: Sets the watch paths based on the configuration.
66+
- `setHandlerConfigurations(config: Object)`: Sets handler configurations based on the configuration file.
67+
68+
### 5. Architecture Diagram (Mermaid.js)
69+
70+
```mermaid
71+
graph LR
72+
A[KCL] --> B[Watcher]
73+
B --> C[File Type Detector]
74+
C --> D[Handler Registry]
75+
D --> E[Configuration Manager]
76+
E --> B
77+
78+
B -->|File System Events| F[Event Handling]
79+
C -->|File Type| F
80+
D -->|Handler Actions| F
81+
E -->|Configuration Settings| F
82+
```
83+
84+
### 6. Integration with KCL Language Server(to be implemented)
85+
86+
To integrate the KCL Watch System with the KCL language server, I am going follow these steps:
87+
88+
#### 6.1 Import Necessary Modules
89+
- In `main.rs`, import the required modules from the KCL Watch System(to be implemented).
90+
91+
#### 6.2 Initialize KCL Watch System
92+
- Then I will modify the `run_server` function in `main.rs` to initialize the KCL Watch System.
93+
94+
#### 6.3 Integrate File Watching with Language Server
95+
- Then I will modify the `main_loop` function in `main_loop.rs` to handle file watching events.
96+
97+
#### 6.4 Implement Watch Handlers
98+
- Also need to create a new module `kcl_watch_system.rs` to implement the watch system integration.
99+
100+
#### 6.5 Update File Watching Event Handling
101+
- Then In `notification.rs`, add the necessary event handlers for file watching events.

0 commit comments

Comments
 (0)