merman-lsp 0.8.0-alpha.3

Language server for Merman diagnostics and completion foundations.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
use crate::document_store::DEFAULT_LSP_MAX_SOURCE_BYTES;
use merman_analysis::{AnalysisRuleProfile, DiagnosticSeverity};
pub use merman_analysis::{RULE_CATALOG_RESPONSE_VERSION, RuleCatalogEntry, RuleCatalogResponse};
use merman_editor_core::{
    DocumentUri, EditorLocation, Position as CorePosition, Range as CoreRange,
};
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use tower_lsp::lsp_types::{Location, Position, Range, Url};

pub const EXPERIMENTAL_SCHEMA_VERSION: u32 = 1;
pub const CONFIG_SCHEMA_RESPONSE_VERSION: u32 = 1;
pub const RULE_CATALOG_METHOD: &str = "merman/ruleCatalog";
pub const CONFIG_SCHEMA_METHOD: &str = "merman/configSchema";

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WorkspaceEditEncoding {
    DocumentChanges,
    Changes,
}

impl WorkspaceEditEncoding {
    pub const fn from_document_changes_support(supported: bool) -> Self {
        if supported {
            Self::DocumentChanges
        } else {
            Self::Changes
        }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ConfigSchemaResponse {
    pub version: u32,
    pub rule_catalog_method: String,
    pub accepted_roots: Vec<String>,
    pub profiles: Vec<String>,
    pub severities: Vec<String>,
    pub configurable_rule_ids: Vec<String>,
    pub schema: Value,
}

impl ConfigSchemaResponse {
    pub fn current() -> Self {
        let profiles = lint_profiles();
        let severities = lint_severities();
        let configurable_rule_ids = configurable_rule_ids();
        Self {
            version: CONFIG_SCHEMA_RESPONSE_VERSION,
            rule_catalog_method: RULE_CATALOG_METHOD.to_string(),
            accepted_roots: vec![
                "direct".to_string(),
                "merman".to_string(),
                "analysis".to_string(),
            ],
            schema: analysis_options_schema(&profiles, &severities, &configurable_rule_ids),
            profiles,
            severities,
            configurable_rule_ids,
        }
    }
}

pub fn experimental_capabilities() -> serde_json::Value {
    json!({
        "merman": {
            "schemaVersion": EXPERIMENTAL_SCHEMA_VERSION,
            "requests": {
                "ruleCatalog": RULE_CATALOG_METHOD,
                "configSchema": CONFIG_SCHEMA_METHOD
            }
        }
    })
}

pub fn core_position_from_lsp(position: Position) -> CorePosition {
    CorePosition::new(position.line as usize, position.character as usize)
}

pub fn range_to_lsp(range: CoreRange) -> Range {
    Range::new(
        Position::new(range.start.line as u32, range.start.character as u32),
        Position::new(range.end.line as u32, range.end.character as u32),
    )
}

pub fn document_uri_to_lsp(uri: &DocumentUri, fallback_uri: &Url) -> Url {
    Url::parse(uri.as_str()).unwrap_or_else(|_| fallback_uri.clone())
}

pub fn location_to_lsp(location: EditorLocation, fallback_uri: &Url) -> Location {
    let uri = document_uri_to_lsp(&location.uri, fallback_uri);
    Location::new(uri, range_to_lsp(location.range))
}

fn profile_name(profile: AnalysisRuleProfile) -> &'static str {
    profile.as_str()
}

fn severity_name(severity: DiagnosticSeverity) -> &'static str {
    severity.as_str()
}

fn lint_profiles() -> Vec<String> {
    [
        AnalysisRuleProfile::Core,
        AnalysisRuleProfile::Recommended,
        AnalysisRuleProfile::Strict,
    ]
    .into_iter()
    .map(profile_name)
    .map(str::to_string)
    .collect()
}

fn lint_severities() -> Vec<String> {
    [
        DiagnosticSeverity::Error,
        DiagnosticSeverity::Warning,
        DiagnosticSeverity::Info,
        DiagnosticSeverity::Hint,
    ]
    .into_iter()
    .map(severity_name)
    .map(str::to_string)
    .collect()
}

fn configurable_rule_ids() -> Vec<String> {
    merman_analysis::configurable_rule_catalog()
        .into_iter()
        .map(|rule| rule.id.to_string())
        .collect()
}

fn analysis_options_schema(
    profiles: &[String],
    severities: &[String],
    configurable_rule_ids: &[String],
) -> Value {
    json!({
        "$schema": "https://json-schema.org/draft/2020-12/schema",
        "title": "Merman analysis options",
        "description": "Options accepted by Merman LSP initializationOptions and workspace/didChangeConfiguration. Clients may pass these options directly, or under a merman or analysis object.",
        "$defs": {
            "ruleId": {
                "type": "string",
                "enum": configurable_rule_ids,
                "description": "A configurable Merman analysis rule id."
            },
            "severity": {
                "type": "string",
                "enum": severities,
                "description": "Diagnostic severity for an explicit rule override."
            },
            "analysisOptions": {
                "type": "object",
                "additionalProperties": true,
                "properties": {
                    "fixed_today": {
                        "type": "string",
                        "pattern": "^\\d{4}-\\d{2}-\\d{2}$",
                        "description": "Fixed local date used by time-sensitive analysis in YYYY-MM-DD format."
                    },
                    "fixed_local_offset_minutes": {
                        "type": "integer",
                        "minimum": -1439,
                        "maximum": 1439,
                        "description": "Fixed local UTC offset in minutes."
                    },
                    "site_config": {
                        "type": "object",
                        "additionalProperties": true,
                        "description": "Mermaid site configuration forwarded to the shared parser/config layer."
                    },
                    "parse": {
                        "type": "object",
                        "additionalProperties": true,
                        "properties": {
                            "suppress_errors": {
                                "type": "boolean",
                                "default": false,
                                "description": "Parse leniently when true."
                            }
                        }
                    },
                    "resources": {
                        "type": "object",
                        "additionalProperties": true,
                        "properties": {
                            "max_source_bytes": {
                                "type": "integer",
                                "minimum": 0,
                                "default": DEFAULT_LSP_MAX_SOURCE_BYTES,
                                "description": "Maximum source bytes accepted by analysis before a resource diagnostic is emitted. Use 0 or omit the field to use the LSP default."
                            }
                        }
                    },
                    "lint": {
                        "type": "object",
                        "additionalProperties": true,
                        "properties": {
                            "profile": {
                                "type": "string",
                                "enum": profiles,
                                "default": "core",
                                "description": "Base lint profile. Recommended and strict may enable additional governed authoring rules."
                            },
                            "enable_rules": {
                                "type": "array",
                                "items": { "$ref": "#/$defs/ruleId" },
                                "uniqueItems": true,
                                "description": "Configurable rule ids to enable explicitly."
                            },
                            "disable_rules": {
                                "type": "array",
                                "items": { "$ref": "#/$defs/ruleId" },
                                "uniqueItems": true,
                                "description": "Configurable rule ids to disable explicitly."
                            },
                            "rule_severities": {
                                "type": "array",
                                "items": {
                                    "type": "object",
                                    "required": ["rule_id", "severity"],
                                    "additionalProperties": true,
                                    "properties": {
                                        "rule_id": { "$ref": "#/$defs/ruleId" },
                                        "severity": { "$ref": "#/$defs/severity" }
                                    }
                                },
                                "description": "Per-rule diagnostic severity overrides."
                            }
                        }
                    }
                }
            }
        },
        "allOf": [
            { "$ref": "#/$defs/analysisOptions" },
            {
                "type": "object",
                "additionalProperties": true,
                "properties": {
                    "merman": { "$ref": "#/$defs/analysisOptions" },
                    "analysis": { "$ref": "#/$defs/analysisOptions" }
                }
            }
        ]
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn rule_catalog_response_contains_governed_authoring_rule() {
        let catalog = RuleCatalogResponse::current();

        assert_eq!(catalog.version, RULE_CATALOG_RESPONSE_VERSION);
        assert!(catalog.rules.iter().any(|rule| {
            rule.id == "merman.authoring.flowchart.explicit_direction"
                && rule.origin.as_str() == "merman_authoring"
                && rule.default_profile.as_str() == "recommended"
                && rule
                    .evidence
                    .contains(&"docs/adr/0072-lint-rule-governance.md")
                && rule.configurable
                && rule.fixable
        }));
        assert!(catalog.rules.iter().any(|rule| {
            rule.id == "merman.authoring.config.prefer_frontmatter_config"
                && rule.origin.as_str() == "merman_authoring"
                && rule.default_profile.as_str() == "recommended"
                && rule.default_severity.as_str() == "hint"
                && rule.category.as_str() == "config"
                && rule.evidence.contains(
                    &"https://github.com/mermaid-js/mermaid/blob/41646dfd43ac83f001b03c70605feb036afae46d/packages/mermaid/src/docs/config/directives.md",
                )
                && rule.configurable
                && rule.fixable
        }));
        assert!(catalog.rules.iter().any(|rule| {
            rule.id == "merman.compatibility.config.deprecated_flowchart_html_labels"
                && rule.origin.as_str() == "mermaid_compatibility"
                && rule.default_profile.as_str() == "core"
                && rule.default_enabled
                && rule.default_severity.as_str() == "warning"
                && rule.category.as_str() == "config"
                && rule.evidence.contains(
                    &"https://github.com/mermaid-js/mermaid/blob/41646dfd43ac83f001b03c70605feb036afae46d/packages/mermaid/src/docs/config/directives.md",
                )
                && rule.configurable
                && !rule.fixable
        }));
        assert!(catalog.rules.iter().any(|rule| {
            rule.id == "merman.compatibility.config.deprecated_external_diagram_loading"
                && rule.origin.as_str() == "mermaid_compatibility"
                && rule.default_profile.as_str() == "core"
                && rule.default_enabled
                && rule.default_severity.as_str() == "warning"
                && rule.category.as_str() == "config"
                && rule.evidence.contains(
                    &"https://github.com/mermaid-js/mermaid/blob/41646dfd43ac83f001b03c70605feb036afae46d/packages/mermaid/src/config.ts",
                )
                && rule.configurable
                && !rule.fixable
        }));
    }

    #[test]
    fn experimental_capability_advertises_rule_catalog_request() {
        let capabilities = experimental_capabilities();

        assert_eq!(
            capabilities["merman"]["requests"]["ruleCatalog"],
            RULE_CATALOG_METHOD
        );
        assert_eq!(
            capabilities["merman"]["requests"]["configSchema"],
            CONFIG_SCHEMA_METHOD
        );
        assert_eq!(
            capabilities["merman"]["schemaVersion"],
            EXPERIMENTAL_SCHEMA_VERSION
        );
    }

    #[test]
    fn config_schema_response_describes_lint_settings() {
        let response = ConfigSchemaResponse::current();

        assert_eq!(response.version, CONFIG_SCHEMA_RESPONSE_VERSION);
        assert_eq!(response.rule_catalog_method, RULE_CATALOG_METHOD);
        assert_eq!(response.profiles, ["core", "recommended", "strict"]);
        assert_eq!(response.severities, ["error", "warning", "info", "hint"]);
        assert!(
            response
                .configurable_rule_ids
                .contains(&"merman.authoring.config.prefer_frontmatter_config".to_string())
        );
        assert!(
            response
                .configurable_rule_ids
                .contains(&"merman.authoring.flowchart.explicit_direction".to_string())
        );
        assert!(
            response.configurable_rule_ids.contains(
                &"merman.compatibility.config.deprecated_flowchart_html_labels".to_string()
            )
        );
        assert!(response.configurable_rule_ids.contains(
            &"merman.compatibility.config.deprecated_external_diagram_loading".to_string()
        ));
        assert_eq!(
            response.schema["$defs"]["analysisOptions"]["properties"]["lint"]["properties"]["profile"]
                ["enum"],
            json!(["core", "recommended", "strict"])
        );
        assert_eq!(
            response.schema["$defs"]["ruleId"]["enum"],
            json!(response.configurable_rule_ids)
        );
        assert_eq!(
            response.schema["$defs"]["severity"]["enum"],
            json!(["error", "warning", "info", "hint"])
        );
        assert_eq!(
            response.schema["$defs"]["analysisOptions"]["properties"]["resources"]["properties"]["max_source_bytes"]
                ["default"],
            json!(DEFAULT_LSP_MAX_SOURCE_BYTES)
        );
        assert_eq!(
            response.schema["allOf"][0],
            json!({ "$ref": "#/$defs/analysisOptions" })
        );
        assert_eq!(
            response.schema["allOf"][1]["properties"]["merman"],
            json!({ "$ref": "#/$defs/analysisOptions" })
        );
        assert_eq!(
            response.schema["allOf"][1]["properties"]["analysis"],
            json!({ "$ref": "#/$defs/analysisOptions" })
        );
    }

    #[test]
    fn vscode_analysis_settings_match_lsp_config_schema_keys() {
        let response = ConfigSchemaResponse::current();
        let mut schema_keys = std::collections::BTreeSet::new();
        collect_analysis_schema_leaf_keys(
            &response.schema["$defs"]["analysisOptions"],
            "",
            &mut schema_keys,
        );

        let package_json_path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
            .join("../../tools/vscode-extension/package.json");
        let package_json: Value = serde_json::from_str(
            &std::fs::read_to_string(package_json_path)
                .expect("expected VS Code package.json to be readable"),
        )
        .expect("expected VS Code package.json to parse as JSON");
        let mut vscode_keys = std::collections::BTreeSet::new();
        collect_vscode_analysis_setting_keys(&package_json, &mut vscode_keys);

        assert_eq!(vscode_keys, schema_keys);
    }

    fn collect_analysis_schema_leaf_keys(
        schema: &Value,
        prefix: &str,
        keys: &mut std::collections::BTreeSet<String>,
    ) {
        let Some(properties) = schema["properties"].as_object() else {
            return;
        };

        for (key, value) in properties {
            let full_key = if prefix.is_empty() {
                key.to_string()
            } else {
                format!("{prefix}.{key}")
            };
            if value["properties"].is_object() {
                collect_analysis_schema_leaf_keys(value, &full_key, keys);
            } else {
                keys.insert(full_key);
            }
        }
    }

    fn collect_vscode_analysis_setting_keys(
        package_json: &Value,
        keys: &mut std::collections::BTreeSet<String>,
    ) {
        let Some(configuration) = package_json["contributes"]["configuration"].as_array() else {
            return;
        };

        for section in configuration {
            let Some(properties) = section["properties"].as_object() else {
                continue;
            };
            for key in properties.keys() {
                if let Some(analysis_key) = key.strip_prefix("merman.analysis.") {
                    keys.insert(analysis_key.to_string());
                }
            }
        }
    }
}