Skip to content

Commit 39c6332

Browse files
committed
Consolidate LSP backends session class into a single, generic implementation
Tons of enhancements to make Pyrefly work on all existing pyright features
1 parent 5dc18a6 commit 39c6332

15 files changed

Lines changed: 1389 additions & 2074 deletions

File tree

.gitignore

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# FIXME: Created in tests
2+
/pyrefly.toml
3+
14
# Created by https://www.toptal.com/developers/gitignore/api/linux,macos,python,visualstudiocode
25
# Edit at https://www.toptal.com/developers/gitignore?templates=linux,macos,python,visualstudiocode
36

@@ -23,7 +26,8 @@
2326
.LSOverride
2427

2528
# Icon must end with two \r
26-
Icon
29+
Icon
30+
2731

2832
# Thumbnails
2933
._*
@@ -240,4 +244,3 @@ pyrightconfig.json
240244
.ionide
241245

242246
# End of https://www.toptal.com/developers/gitignore/api/linux,macos,python,visualstudiocode
243-

CLAUDE.md

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,27 +41,38 @@ This is a zero-dependency Python library providing typed LSP (Language Server Pr
4141
- Handles JSON-RPC protocol, message framing, and async request/response correlation
4242
- Provides `.send` (requests) and `.notify` (notifications) interfaces
4343

44-
**Session Protocol (`lsp_types/session.py`)**
45-
- `Session`: Protocol defining standard LSP session interface
46-
- Abstract interface for `shutdown()`, `update_code()`, `get_diagnostics()`, etc.
47-
- Implemented by language-specific sessions like `PyrightSession`
44+
**Session System (`lsp_types/session.py`)**
45+
- `Session`: Concrete LSP session implementation using pluggable backends
46+
- `LSPBackend`: Protocol defining backend-specific operations (config, process launch, capabilities)
47+
- Consolidated implementation with common LSP functionality shared across all backends
48+
- Standard interface for `shutdown()`, `update_code()`, `get_diagnostics()`, etc.
4849

4950
**Generic Process Pooling (`lsp_types/pool.py`)**
5051
- `LSPProcessPool`: Language-server agnostic process pooling for performance optimization
5152
- `PooledLSPProcess`: Wrapper for `LSPProcess` with recycling state management
5253
- Reusable across different LSP implementations (not just Pyright)
5354
- Handles process lifecycle: creation, reuse, idle cleanup, and shutdown
5455

56+
**Backend Integrations**
57+
5558
**Pyright Integration (`lsp_types/pyright/`)**
56-
- `PyrightSession`: High-level Pyright LSP session implementation
59+
- `PyrightBackend`: Backend implementation for Pyright LSP server
5760
- `config_schema.py`: Auto-generated Pyright configuration types
58-
- **Key Design**: PyrightSession uses generic `LSPProcessPool` for session reuse
61+
- `session.py`: Factory functions and backward-compatible wrappers
62+
- **Key Design**: Uses consolidated `Session` class with `PyrightBackend` for specialization
63+
64+
**Pyrefly Integration (`lsp_types/pyrefly/`)**
65+
- `PyreflyBackend`: Backend implementation for Pyrefly LSP server (Facebook's Rust-based type checker)
66+
- `config_schema.py`: Auto-generated Pyrefly configuration types
67+
- `session.py`: Factory functions and backward-compatible wrappers
68+
- **Key Design**: Uses consolidated `Session` class with `PyreflyBackend` for specialization
5969

6070
### Type Generation Pipeline
6171

6272
**Schema Sources:**
6373
- `assets/lsprotocol/lsp.schema.json`: Official LSP protocol schema
6474
- `assets/lsps/pyright.schema.json`: Pyright-specific configuration schema
75+
- `assets/lsps/pyrefly.schema.json`: Pyrefly-specific configuration schema
6576

6677
**Generation Process:**
6778
1. `download_schemas.py`: Fetches latest schemas from upstream
@@ -74,13 +85,17 @@ This is a zero-dependency Python library providing typed LSP (Language Server Pr
7485
**Process Pool Tests**
7586
- `tests/test_pool.py`: Direct `LSPProcessPool` testing with generic interface
7687
- `tests/test_pyright/test_session_pool.py`: Pool integration testing through Pyright sessions
88+
- `tests/test_pyrefly/test_session_pool.py`: Pool integration testing through Pyrefly sessions
7789
- Comprehensive pool behavior testing (creation, recycling, limits, cleanup)
7890
- Performance benchmarks comparing pooled vs non-pooled sessions
7991
- Concurrent usage scenarios and idle process management
8092

81-
**Session Tests (`tests/test_pyright/test_pyright_session.py`)**
82-
- Core LSP functionality testing (diagnostics, hover, completion)
83-
- Integration testing with actual Pyright language server
93+
**Session Tests**
94+
- `tests/test_session.py`: Core consolidated Session class functionality
95+
- `tests/test_pyright/test_pyright_session.py`: Pyright-specific LSP functionality testing
96+
- `tests/test_pyrefly/test_pyrefly_session.py`: Pyrefly-specific LSP functionality testing
97+
- Integration testing with actual language servers (diagnostics, hover, completion)
98+
- Backend-specific configuration and behavior validation
8499

85100
### Dependencies
86101

@@ -93,6 +108,14 @@ This is a zero-dependency Python library providing typed LSP (Language Server Pr
93108
### Important Notes
94109

95110
- Always prefix test commands with `uv run`
96-
- Pool tests require `pyright-langserver` binary available in PATH
111+
- Pool tests require `pyright-langserver` and/or `pyrefly` binaries available in PATH
97112
- Type generation requires Python 3.11+ for modern TypedDict features
98113
- Generated types should not be manually edited - regenerate from schemas
114+
115+
### Architecture Design Patterns
116+
117+
**Backend Pattern**: LSP server integrations use the `LSPBackend` protocol to separate backend-specific logic (configuration formats, command-line arguments, capabilities) from common session management. This enables:
118+
- Code reuse across different LSP implementations
119+
- Easy addition of new LSP backends
120+
- Consistent API while supporting diverse configuration needs
121+
- Testable isolation of backend-specific behavior

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ def greet(name: str) -> str:
6969
return 123
7070
"""
7171

72-
pyright_session = await PyrightSession.create(initial_code=code)
72+
pyright_session = await Session.create(PyrightBackend(), initial_code=code)
7373
diagnostics = await pyright_session.get_diagnostics()
7474

75-
assert diagnostics["diagnostics"] != []
75+
assert diagnostics != []
7676

7777
code = """\
7878
def greet(name: str) -> str:
@@ -82,7 +82,7 @@ def greet(name: str) -> str:
8282
assert await pyright_session.update_code(code) == 2
8383

8484
diagnostics = await pyright_session.get_diagnostics()
85-
assert diagnostics["diagnostics"] == []
85+
assert diagnostics == []
8686
```
8787

8888
## Development

0 commit comments

Comments
 (0)