返回 CodeWhale
LSP_PHP_CUSTOM.md
根目录 / docs / LSP_PHP_CUSTOM.md
1 # LSP: PHP Support & Custom Language Server Extension
2
3 > 阅读简体中文版:[zh_hans/LSP_PHP_CUSTOM.md](zh_hans/LSP_PHP_CUSTOM.md)
4 >
5 > v0.8.65+ | `codex/lsp-php-custom-servers`
6
7 ## Overview
8
9 This feature adds **PHP** to the built-in LSP language registry and introduces a
10 `[lsp.custom]` config section so users can register arbitrary LSP servers by
11 file extension — covering languages absent from the built-in `Language` enum
12 (Ruby, C#, Swift, Lua, etc.).
13
14 ## Changes
15
16 ### 1. PHP built-in support
17
18 - `Language::Php` variant added to the enum in `crates/tui/src/lsp/registry.rs`
19 - `.php` files are detected and routed to `intelephense --stdio` by default
20 - User can override via `[lsp.servers].php`
21
22 ### 2. Custom LSP server extension
23
24 New struct `CustomLspDef` (defined in `crates/tui/src/lsp/mod.rs` and mirrored
25 in `crates/config/src/lib.rs`):
26
27 ```rust
28 pub struct CustomLspDef {
29 pub language_id: String, // LSP languageId for textDocument/didOpen
30 pub command: String, // executable to spawn
31 pub args: Vec<String>, // arguments (default empty)
32 }
33 ```
34
35 New config field `LspConfig.custom: HashMap<String, CustomLspDef>` — keyed by
36 file extension (without dot), e.g. `"rb"`, `"cs"`, `"swift"`.
37
38 In `LspManager::diagnostics_for`, when the built-in registry returns
39 `Language::Other`, the manager checks the user's custom table before giving up.
40 Custom servers get their own lazy-spawn transport map and once-per-extension
41 missing-binary warnings (no log spam).
42
43 ### 3. Transport generalization
44
45 `StdioLspTransport::spawn` now accepts `&str language_id` instead of
46 `Language`, so both built-in and custom servers share the same transport
47 implementation. The old `Language` import is removed from `client.rs`.
48
49 ### 4. Polling pipeline extracted
50
51 `poll_diagnostics` is a new private method that both built-in and custom
52 diagnostics paths share — eliminating duplicated call/wait/filter/sort/truncate
53 logic.
54
55 ## Configuration
56
57 ### Built-in PHP (enabled by default if `intelephense` is on PATH)
58
59 ```toml
60 # No config needed — PHP .php files are detected automatically.
61 # Override the server if desired:
62 [lsp.servers]
63 php = ["phpactor", "language-server"]
64 ```
65
66 ### Custom language servers
67
68 ```toml
69 [lsp.custom.rb]
70 command = "ruby-lsp"
71 args = ["--stdio"]
72 language_id = "ruby"
73
74 [lsp.custom.cs]
75 command = "csharp-ls"
76 language_id = "csharp"
77
78 [lsp.custom.swift]
79 command = "sourcekit-lsp"
80 language_id = "swift"
81 ```
82
83 Keys are file extensions (no leading dot). The `args` field defaults to empty.
84 `language_id` must match what the LSP server expects in `textDocument/didOpen`.
85
86 ## Architecture
87
88 ```
89 edit_file / write_file / apply_patch success
90
91
92 LspManager.diagnostics_for(file)
93
94 ├── custom_for_extension(file) ── found? ──► transport_for_custom(ext, def)
95 │ │
96 ├── detect_language(file) ── Other? ──► return None (skip)
97
98 └── transport_for(lang)
99
100
101 poll_diagnostics(file, text, transport)
102
103
104 DiagnosticBlock → injected into session message stream
105 ```
106
107 ## Verification
108
109 ```
110 cargo test -p codewhale-tui --bin codewhale-tui lsp::
111 # 32 tests passed (3 new: detects_php_extension, language_ids_for_php,
112 # server_for_php_is_intelephense)
113 cargo clippy -p codewhale-tui --bin codewhale-tui
114 # lsp module: zero new warnings
115 ```
116
117 ## Files touched
118
119 | File | Change |
120 |------|--------|
121 | `crates/tui/src/lsp/registry.rs` | +Php variant, detection, server mapping, tests |
122 | `crates/tui/src/lsp/mod.rs` | +CustomLspDef, LspConfig.custom, LspManager custom fallback |
123 | `crates/tui/src/lsp/client.rs` | spawn accepts &str language_id |
124 | `crates/tui/src/config.rs` | LspConfigToml.custom + into_runtime |
125 | `crates/config/src/lib.rs` | LspConfigToml.custom + CustomLspDef |
126 | `config.example.toml` | PHP docs + custom extension examples |
127
127 lines MARKDOWN