Skip to main content

php_lsp/
config.rs

1/// Per-category diagnostic toggle flags.
2/// The master `enabled` switch defaults to `true`. Individual category flags
3/// also default to `true`, so all diagnostics are on out of the box; set
4/// `initializationOptions.diagnostics.enabled = false` to silence everything,
5/// or turn off specific categories individually.
6#[derive(Debug, Clone)]
7pub struct DiagnosticsConfig {
8    /// Master switch: when `false`, no diagnostics are emitted. Defaults to `true`.
9    pub enabled: bool,
10    /// Undefined variable references.
11    pub undefined_variables: bool,
12    /// Calls to undefined functions.
13    pub undefined_functions: bool,
14    /// References to undefined classes / interfaces / traits.
15    pub undefined_classes: bool,
16    /// Wrong number of arguments passed to a function.
17    pub arity_errors: bool,
18    /// Return-type mismatches.
19    pub type_errors: bool,
20    /// Calls to `@deprecated` members.
21    pub deprecated_calls: bool,
22    /// Duplicate class / function declarations.
23    pub duplicate_declarations: bool,
24    /// Unused-symbol warnings (unused variables / parameters / methods /
25    /// properties / functions). New in mir 0.22; defaults to `false` so the
26    /// LSP doesn't add noisy warnings to existing workspaces without an
27    /// opt-in. Toggle via `diagnostics.unusedSymbols` in initializationOptions.
28    pub unused_symbols: bool,
29}
30
31impl Default for DiagnosticsConfig {
32    fn default() -> Self {
33        DiagnosticsConfig {
34            enabled: true,
35            undefined_variables: true,
36            undefined_functions: true,
37            undefined_classes: true,
38            arity_errors: true,
39            type_errors: true,
40            deprecated_calls: true,
41            duplicate_declarations: true,
42            unused_symbols: false,
43        }
44    }
45}
46
47impl DiagnosticsConfig {
48    /// All categories on. Used in tests and by clients that explicitly enable
49    /// diagnostics without overriding individual flags.
50    #[cfg(test)]
51    pub fn all_enabled() -> Self {
52        DiagnosticsConfig {
53            enabled: true,
54            ..DiagnosticsConfig::default()
55        }
56    }
57
58    pub(crate) fn from_value(v: &serde_json::Value) -> Self {
59        let mut cfg = DiagnosticsConfig::default();
60        let Some(obj) = v.as_object() else { return cfg };
61        let flag = |key: &str| obj.get(key).and_then(|x| x.as_bool()).unwrap_or(true);
62        cfg.enabled = obj.get("enabled").and_then(|x| x.as_bool()).unwrap_or(true);
63        cfg.undefined_variables = flag("undefinedVariables");
64        cfg.undefined_functions = flag("undefinedFunctions");
65        cfg.undefined_classes = flag("undefinedClasses");
66        cfg.arity_errors = flag("arityErrors");
67        cfg.type_errors = flag("typeErrors");
68        cfg.deprecated_calls = flag("deprecatedCalls");
69        cfg.duplicate_declarations = flag("duplicateDeclarations");
70        cfg.unused_symbols = obj
71            .get("unusedSymbols")
72            .and_then(|x| x.as_bool())
73            .unwrap_or(false);
74        cfg
75    }
76}
77
78/// Per-feature capability toggles. All default to `true` (enabled).
79/// Set `initializationOptions.features.<name> = false` to suppress a capability.
80#[derive(Debug, Clone)]
81pub struct FeaturesConfig {
82    pub completion: bool,
83    pub hover: bool,
84    pub definition: bool,
85    pub declaration: bool,
86    pub references: bool,
87    pub document_symbols: bool,
88    pub workspace_symbols: bool,
89    pub rename: bool,
90    pub signature_help: bool,
91    pub inlay_hints: bool,
92    pub semantic_tokens: bool,
93    pub selection_range: bool,
94    pub call_hierarchy: bool,
95    pub document_highlight: bool,
96    pub implementation: bool,
97    pub code_action: bool,
98    pub type_definition: bool,
99    pub code_lens: bool,
100    pub formatting: bool,
101    pub range_formatting: bool,
102    pub on_type_formatting: bool,
103    pub document_link: bool,
104    pub linked_editing_range: bool,
105    pub inline_values: bool,
106}
107
108impl Default for FeaturesConfig {
109    fn default() -> Self {
110        FeaturesConfig {
111            completion: true,
112            hover: true,
113            definition: true,
114            declaration: true,
115            references: true,
116            document_symbols: true,
117            workspace_symbols: true,
118            rename: true,
119            signature_help: true,
120            inlay_hints: true,
121            semantic_tokens: true,
122            selection_range: true,
123            call_hierarchy: true,
124            document_highlight: true,
125            implementation: true,
126            code_action: true,
127            type_definition: true,
128            code_lens: true,
129            formatting: true,
130            range_formatting: true,
131            on_type_formatting: true,
132            document_link: true,
133            linked_editing_range: true,
134            inline_values: true,
135        }
136    }
137}
138
139impl FeaturesConfig {
140    pub(crate) fn from_value(v: &serde_json::Value) -> Self {
141        let mut cfg = FeaturesConfig::default();
142        let Some(obj) = v.as_object() else { return cfg };
143        let flag = |key: &str| obj.get(key).and_then(|x| x.as_bool()).unwrap_or(true);
144        cfg.completion = flag("completion");
145        cfg.hover = flag("hover");
146        cfg.definition = flag("definition");
147        cfg.declaration = flag("declaration");
148        cfg.references = flag("references");
149        cfg.document_symbols = flag("documentSymbols");
150        cfg.workspace_symbols = flag("workspaceSymbols");
151        cfg.rename = flag("rename");
152        cfg.signature_help = flag("signatureHelp");
153        cfg.inlay_hints = flag("inlayHints");
154        cfg.semantic_tokens = flag("semanticTokens");
155        cfg.selection_range = flag("selectionRange");
156        cfg.call_hierarchy = flag("callHierarchy");
157        cfg.document_highlight = flag("documentHighlight");
158        cfg.implementation = flag("implementation");
159        cfg.code_action = flag("codeAction");
160        cfg.type_definition = flag("typeDefinition");
161        cfg.code_lens = flag("codeLens");
162        cfg.formatting = flag("formatting");
163        cfg.range_formatting = flag("rangeFormatting");
164        cfg.on_type_formatting = flag("onTypeFormatting");
165        cfg.document_link = flag("documentLink");
166        cfg.linked_editing_range = flag("linkedEditingRange");
167        cfg.inline_values = flag("inlineValues");
168        cfg
169    }
170}
171
172/// Maximum number of PHP files indexed during a workspace scan.
173/// Prevents excessive memory use on projects with very large vendor trees.
174pub const MAX_INDEXED_FILES: usize = 50_000;
175
176/// Configuration received from the client via `initializationOptions`.
177#[derive(Debug, Clone)]
178pub struct LspConfig {
179    /// PHP version string, e.g. `"8.1"`.  Set explicitly via `initializationOptions`
180    /// or auto-detected from `composer.json` / the `php` binary at startup.
181    pub php_version: Option<String>,
182    /// Glob patterns for paths to exclude from workspace indexing.
183    pub exclude_paths: Vec<String>,
184    /// Glob patterns for paths that must be indexed even if they match an
185    /// `excludePaths` entry.  Patterns are matched against path components
186    /// (same semantics as `excludePaths`).  Example: `["vendor/yiisoft"]`.
187    pub include_paths: Vec<String>,
188    /// Per-category diagnostic toggles.
189    pub diagnostics: DiagnosticsConfig,
190    /// Per-feature capability toggles.
191    pub features: FeaturesConfig,
192    /// Hard cap on the number of PHP files indexed during a workspace scan.
193    /// Defaults to [`MAX_INDEXED_FILES`]. Set lower via `initializationOptions`
194    /// to reduce memory on projects with very large vendor trees.
195    pub max_indexed_files: usize,
196    /// Whether to eagerly index `vendor/` during the workspace scan.
197    ///
198    /// Default `false`: `vendor/` is skipped on scan; vendor files load on
199    /// demand via PSR-4 resolution (composer autoload + per-file parse). This
200    /// keeps `$/php-lsp/indexReady` fast on real-world projects where vendor
201    /// dwarfs the workspace.
202    ///
203    /// Set `true` for full workspace-symbol coverage of vendor and find-
204    /// implementations / type-hierarchy against vendor types — at the cost of
205    /// a slower initial scan.
206    pub index_vendor: bool,
207}
208
209impl Default for LspConfig {
210    fn default() -> Self {
211        LspConfig {
212            php_version: None,
213            exclude_paths: Vec::new(),
214            include_paths: Vec::new(),
215            diagnostics: DiagnosticsConfig::default(),
216            features: FeaturesConfig::default(),
217            max_indexed_files: MAX_INDEXED_FILES,
218            index_vendor: false,
219        }
220    }
221}
222
223impl LspConfig {
224    /// Merge a `.php-lsp.json` value with editor `initializationOptions` /
225    /// `workspace/configuration`. Editor settings win per-key; `excludePaths`
226    /// arrays are **concatenated** (file entries first, editor entries appended)
227    /// rather than replaced, since exclusion patterns are additive.
228    ///
229    /// Hot-reload of `.php-lsp.json` on file change is not supported; the file
230    /// is only read during `initialize` and `did_change_configuration`.
231    pub(crate) fn merge_project_configs(
232        file: Option<&serde_json::Value>,
233        editor: Option<&serde_json::Value>,
234    ) -> serde_json::Value {
235        let mut merged = file
236            .cloned()
237            .unwrap_or(serde_json::Value::Object(Default::default()));
238        let Some(editor_obj) = editor.and_then(|e| e.as_object()) else {
239            return merged;
240        };
241        let merged_obj = merged
242            .as_object_mut()
243            .expect("merged base is always an object");
244        for (key, val) in editor_obj {
245            // Both excludePaths and includePaths are concatenated rather than replaced.
246            if key == "excludePaths" || key == "includePaths" {
247                let file_arr = merged_obj
248                    .get(key)
249                    .and_then(|v| v.as_array())
250                    .cloned()
251                    .unwrap_or_default();
252                let editor_arr = val.as_array().cloned().unwrap_or_default();
253                merged_obj.insert(
254                    key.clone(),
255                    serde_json::Value::Array([file_arr, editor_arr].concat()),
256                );
257            } else {
258                merged_obj.insert(key.clone(), val.clone());
259            }
260        }
261        merged
262    }
263
264    pub(crate) fn from_value(v: &serde_json::Value) -> Self {
265        let mut cfg = LspConfig::default();
266        if let Some(ver) = v.get("phpVersion").and_then(|x| x.as_str()) {
267            if crate::autoload::is_valid_php_version(ver) {
268                cfg.php_version = Some(ver.to_string());
269            } else {
270                // Invalid version: skip environment detection, use the latest stubs.
271                cfg.php_version = Some(crate::autoload::PHP_8_5.to_string());
272            }
273        }
274        if let Some(arr) = v.get("excludePaths").and_then(|x| x.as_array()) {
275            cfg.exclude_paths = arr
276                .iter()
277                .filter_map(|x| x.as_str().map(str::to_string))
278                .collect();
279        }
280        if let Some(arr) = v.get("includePaths").and_then(|x| x.as_array()) {
281            cfg.include_paths = arr
282                .iter()
283                .filter_map(|x| x.as_str().map(str::to_string))
284                .collect();
285        }
286        if let Some(diag_val) = v.get("diagnostics") {
287            cfg.diagnostics = DiagnosticsConfig::from_value(diag_val);
288        }
289        if let Some(feat_val) = v.get("features") {
290            cfg.features = FeaturesConfig::from_value(feat_val);
291        }
292        if let Some(n) = v.get("maxIndexedFiles").and_then(|x| x.as_u64()) {
293            cfg.max_indexed_files = n as usize;
294        }
295        if let Some(b) = v.get("indexVendor").and_then(|x| x.as_bool()) {
296            cfg.index_vendor = b;
297        }
298        cfg
299    }
300}