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