|
| 1 | +# lsp-client-js |
| 2 | + |
| 3 | +A lightweight LSP (Language Server Protocol) client implementation in TypeScript using Bun. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Decentralized Capabilities**: Uses a feature-based architecture (inspired by the Python version's Mixins) to manage LSP capabilities independently. |
| 8 | +- **Robust JSON-RPC**: Leverages `vscode-jsonrpc` for reliable communication with language servers. |
| 9 | +- **Bun Optimized**: Built with Bun for high performance and modern TypeScript support. |
| 10 | +- **Async Iterators**: Uses async generators for managing the client lifecycle. |
| 11 | + |
| 12 | +## Architecture |
| 13 | + |
| 14 | +The project follows the design principles outlined in `docs/DESIGN.md`: |
| 15 | + |
| 16 | +1. **Feature Registry**: Instead of Python's MRO, we use an explicit registration pattern for features (capabilities). |
| 17 | +2. **JSON-RPC Layer**: Built on top of `vscode-jsonrpc` which provides robust request/response matching and stream handling. |
| 18 | +3. **Lifecycle Management**: Async generators ensure proper initialization and cleanup (shutdown/exit/kill). |
| 19 | + |
| 20 | +## Development |
| 21 | + |
| 22 | +To run the examples: |
| 23 | + |
| 24 | +```bash |
| 25 | +# Basic hover example |
| 26 | +bun run examples/basic.ts |
| 27 | + |
| 28 | +# Diagnostics example |
| 29 | +bun run examples/diagnostics.ts |
| 30 | + |
| 31 | +# Pyrefly specific examples |
| 32 | +bun run examples/pyrefly.ts |
| 33 | +bun run examples/pyrefly_diagnostics.ts |
| 34 | + |
| 35 | +# TypeScript `using` syntax examples |
| 36 | +bun run examples/using_syntax.ts |
| 37 | +bun run examples/using_advanced.ts |
| 38 | +bun run examples/using_async.ts |
| 39 | + |
| 40 | +# Rust-analyzer example |
| 41 | +bun run examples/rust-analyzer.ts |
| 42 | +``` |
| 43 | + |
| 44 | +## TypeScript `using` Syntax Integration |
| 45 | + |
| 46 | +This project showcases TypeScript 5.2+'s **Explicit Resource Management** feature: |
| 47 | + |
| 48 | +### Key Features Implemented |
| 49 | + |
| 50 | +1. **Automatic Resource Cleanup**: LSP server connections are automatically disposed |
| 51 | +2. **Exception Safety**: Resources are cleaned up even when errors occur |
| 52 | +3. **Clean Syntax**: Eliminates manual `try/finally` blocks for resource management |
| 53 | +4. **Stack-Based Order**: Disposal happens in correct LIFO order |
| 54 | + |
| 55 | +### Usage Examples |
| 56 | + |
| 57 | +#### Basic Resource Management |
| 58 | +```typescript |
| 59 | +{ |
| 60 | + using server = new SafeLSPServer(['pyrefly', 'lsp']); |
| 61 | + const connection = await server.start(); |
| 62 | + // Connection automatically disposed when scope ends |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +#### Error Safety |
| 67 | +```typescript |
| 68 | +try { |
| 69 | + using server = new SafeLSPServer(['pyrefly', 'lsp']); |
| 70 | + await server.start(); |
| 71 | + throw new Error('Something went wrong'); |
| 72 | +} catch (e) { |
| 73 | + // Server is automatically disposed even with errors |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +#### Async Resource Management |
| 78 | +```typescript |
| 79 | +await using connection = await server.start(); |
| 80 | +// Async resources are automatically disposed |
| 81 | +``` |
| 82 | + |
| 83 | +#### LSP Client Integration |
| 84 | +```typescript |
| 85 | +for await (const client of lspClient.start()) { |
| 86 | + // Client lifecycle automatically managed |
| 87 | + // Server disposed when generator exits |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +### Implementation Details |
| 92 | + |
| 93 | +The project provides: |
| 94 | +- **`SafeLSPServer`**: Implements `Disposable` for `using` compatibility |
| 95 | +- **Enhanced LSPClient**: Uses `using` for automatic connection disposal |
| 96 | +- **Resource Safety**: Ensures servers are killed and connections disposed |
| 97 | + |
| 98 | +## Testing with Pyrefly |
| 99 | + |
| 100 | +The project includes comprehensive tests with Pyrefly LSP server. To run the tests: |
| 101 | + |
| 102 | +1. Ensure Pyrefly is installed: `pip install pyrefly` or `uv tool install pyrefly` |
| 103 | +2. Create a `pyproject.toml` file in your project root with Pyrefly configuration |
| 104 | +3. Run the Pyrefly examples to test hover, definition, and diagnostics features |
0 commit comments