ontocore-lsp 0.14.0

Language server for OntoCode (OntoCore LSP via ontocore-lsp)
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
use ontocore_catalog::{CatalogStats, ClassHierarchy, EntityDetail, SubclassEdge};
use ontocore_core::{Diagnostic, Entity, OntologyDocument};
use ontocore_reasoner::ReasonerSnapshot;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

#[derive(Debug, Clone, Serialize)]
pub struct DiagnosticSummary {
    pub code: String,
    pub severity: String,
    pub message: String,
    pub file: String,
    pub line: Option<u64>,
    pub column: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub entity_iri: Option<String>,
}

impl From<&Diagnostic> for DiagnosticSummary {
    fn from(d: &Diagnostic) -> Self {
        Self {
            code: d.display_code(),
            severity: d.severity.as_str().to_string(),
            message: d.message.clone(),
            file: d.file.display().to_string(),
            line: d.range.line,
            column: d.range.column,
            entity_iri: d.entity_iri.clone(),
        }
    }
}

#[derive(Debug, Deserialize)]
pub struct IndexWorkspaceParams {
    /// Workspace root URI (`file://…`). Accepts legacy camelCase `workspaceUri` during migration.
    #[serde(alias = "workspaceUri", default)]
    pub workspace_uri: Option<String>,
    /// Persist parse snapshots under `.ontocore/cache/`.
    #[serde(default)]
    pub disk_cache: bool,
}

#[derive(Debug, Serialize)]
pub struct IndexWorkspaceResult {
    pub stats: CatalogStats,
    pub indexed_at: u64,
}

#[derive(Debug, Serialize)]
pub struct CatalogSnapshot {
    pub documents: Vec<OntologyDocument>,
    pub entities: Vec<Entity>,
    pub hierarchy: ClassHierarchy,
    pub diagnostics: Vec<DiagnosticSummary>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reasoner: Option<ReasonerSnapshot>,
}

#[derive(Debug, Deserialize)]
pub struct GetEntityParams {
    pub iri: String,
}

#[derive(Debug, Serialize)]
pub struct GetEntityResult {
    pub detail: EntityDetail,
}

#[derive(Debug, Deserialize)]
pub struct ApplyAxiomPatchParams {
    pub document_uri: String,
    /// JSON array of patch operations (Turtle [`ontocore_owl::PatchOp`] or OBO [`ontocore_obo::OboPatchOp`]).
    pub patches: serde_json::Value,
    #[serde(default)]
    pub preview_only: bool,
}

#[derive(Debug, Deserialize)]
pub struct QueryParams {
    pub sql: String,
}

#[derive(Debug, Deserialize)]
pub struct SparqlParams {
    pub query: String,
}

#[derive(Debug, Serialize)]
pub struct TabularQueryResult {
    pub columns: Vec<String>,
    pub rows: Vec<BTreeMap<String, String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub truncated: Option<bool>,
}

#[derive(Debug, Deserialize)]
pub struct ParseManchesterParams {
    pub expression: String,
    pub axiom_kind: String,
    #[serde(default)]
    pub entity_iri: Option<String>,
    #[serde(default)]
    pub document_uri: Option<String>,
}

#[derive(Debug, Serialize)]
pub struct ManchesterCompletions {
    pub classes: Vec<String>,
    pub object_properties: Vec<String>,
    pub data_properties: Vec<String>,
    pub datatypes: Vec<String>,
}

#[derive(Debug, Serialize)]
pub struct ParseManchesterResult {
    pub normalized: String,
    pub turtle_fragment: String,
    pub tree: serde_json::Value,
    pub diagnostics: Vec<ontocore_owl::PatchDiagnostic>,
    pub completions: ManchesterCompletions,
}

#[derive(Debug, Serialize)]
pub struct ApplyAxiomPatchResult {
    #[serde(flatten)]
    pub patch: ontocore_owl::ApplyPatchResult,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub entity_detail: Option<EntityDetail>,
    /// Set when the patch was written but workspace reindex failed.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reindex_warning: Option<String>,
    /// Full-document edit so the client can sync open editors with disk.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub workspace_edit: Option<lsp_types::WorkspaceEdit>,
}

/// LSP JSON error payload for custom `ontocore/*` methods (not [`ontocore_core::OntoCoreError`]).
#[derive(Debug, Serialize)]
pub struct LspErrorPayload {
    pub code: String,
    pub message: String,
    pub recoverable: bool,
    pub user_action: Option<String>,
}

impl LspErrorPayload {
    pub fn not_indexed() -> Self {
        Self {
            code: "NOT_INDEXED".to_string(),
            message: "Workspace has not been indexed yet".to_string(),
            recoverable: true,
            user_action: Some("Run OntoCode: Index Workspace".to_string()),
        }
    }

    pub fn not_found(iri: &str) -> Self {
        Self {
            code: "ENTITY_NOT_FOUND".to_string(),
            message: format!("Entity not found: {iri}"),
            recoverable: true,
            user_action: None,
        }
    }

    pub fn index_failed(message: String) -> Self {
        Self {
            code: "INDEX_FAILED".to_string(),
            message,
            recoverable: true,
            user_action: Some("Check ontology files for parse errors".to_string()),
        }
    }

    pub fn invalid_params(message: String) -> Self {
        Self { code: "INVALID_PARAMS".to_string(), message, recoverable: true, user_action: None }
    }

    pub fn graph_failed(message: String) -> Self {
        Self {
            code: "GRAPH_FAILED".to_string(),
            message,
            recoverable: true,
            user_action: Some("Adjust graph kind, root IRI, or filters".to_string()),
        }
    }

    pub fn robot_failed(message: String) -> Self {
        Self {
            code: "ROBOT_FAILED".to_string(),
            message,
            recoverable: true,
            user_action: Some("Check ROBOT CLI installation and arguments".to_string()),
        }
    }

    pub fn patch_invalid(message: String) -> Self {
        Self {
            code: "PATCH_INVALID".to_string(),
            message,
            recoverable: true,
            user_action: Some("Check patch parameters and entity IRIs".to_string()),
        }
    }

    pub fn unsupported_format(message: String) -> Self {
        Self {
            code: "UNSUPPORTED_FORMAT".to_string(),
            message,
            recoverable: true,
            user_action: Some("Save as Turtle (.ttl) for write-back".to_string()),
        }
    }

    pub fn query_failed(message: String) -> Self {
        Self {
            code: "QUERY_FAILED".to_string(),
            message,
            recoverable: true,
            user_action: Some("Check query syntax and virtual table names".to_string()),
        }
    }

    pub fn manchester_invalid(message: String) -> Self {
        Self {
            code: "MANCHESTER_INVALID".to_string(),
            message,
            recoverable: true,
            user_action: Some("Fix the Manchester class expression".to_string()),
        }
    }

    pub fn applied_not_indexed(message: String) -> Self {
        Self {
            code: "APPLIED_NOT_INDEXED".to_string(),
            message,
            recoverable: true,
            user_action: Some("Patch was saved; run OntoCode: Index Workspace".to_string()),
        }
    }

    pub fn reasoner_failed(message: String) -> Self {
        Self {
            code: "REASONER_FAILED".to_string(),
            message,
            recoverable: true,
            user_action: Some(
                "Try a different reasoner profile or fix ontology axioms".to_string(),
            ),
        }
    }

    pub fn explanation_failed(message: String) -> Self {
        Self {
            code: "EXPLANATION_FAILED".to_string(),
            message,
            recoverable: true,
            user_action: Some("Run the reasoner first or choose another class".to_string()),
        }
    }

    pub fn refactor_failed(message: String) -> Self {
        Self {
            code: "REFACTOR_FAILED".to_string(),
            message,
            recoverable: true,
            user_action: Some("Preview the refactor plan and check Turtle files".to_string()),
        }
    }
}

#[derive(Debug, Deserialize)]
pub struct RunReasonerParams {
    #[serde(default = "default_reasoner_profile")]
    pub profile: String,
    #[serde(default = "default_auto_profile")]
    pub auto_detect: bool,
}

fn default_reasoner_profile() -> String {
    "el".to_string()
}

fn default_auto_profile() -> bool {
    true
}

#[derive(Debug, Serialize)]
pub struct RunReasonerResult {
    pub profile_used: String,
    pub consistent: bool,
    pub unsatisfiable: Vec<String>,
    pub inferred_edge_count: usize,
    pub new_inferences: Vec<SubclassEdge>,
    pub warnings: Vec<ontocore_reasoner::ReasonerWarning>,
    pub duration_ms: u64,
    pub snapshot: ReasonerSnapshot,
}

#[derive(Debug, Deserialize)]
pub struct GetExplanationParams {
    pub class_iri: String,
    #[serde(default = "default_reasoner_profile")]
    pub profile: String,
}

#[derive(Debug, Serialize)]
pub struct GetGraphResult {
    pub graph: ontocore_catalog::GraphPayload,
}

#[derive(Debug, Deserialize)]
pub struct RunRobotParams {
    pub subcommand: String,
    #[serde(default)]
    pub args: Vec<String>,
    #[serde(default)]
    pub robot_path: Option<String>,
}

#[derive(Debug, Serialize)]
pub struct RunRobotResult {
    pub exit_code: i32,
    pub stdout: String,
    pub stderr: String,
}

#[derive(Debug, Serialize)]
pub struct GetExplanationResult {
    pub class_iri: String,
    pub steps: Vec<ontocore_reasoner::ExplanationStep>,
    pub text: String,
}

#[derive(Debug, Deserialize)]
pub struct FindUsagesParams {
    pub iri: String,
}

#[derive(Debug, Serialize)]
pub struct UsageSummary {
    pub iri: String,
    pub referenced_iri: String,
    pub file: String,
    pub line: Option<u64>,
    pub column: Option<u64>,
    pub kind: String,
    pub context: String,
}

#[derive(Debug, Serialize)]
pub struct FindUsagesResult {
    pub usages: Vec<UsageSummary>,
}

#[derive(Debug, Deserialize)]
pub struct PreviewRefactorParams {
    #[serde(flatten)]
    pub request: ontocore_refactor::RefactorRequest,
}

#[derive(Debug, Serialize)]
pub struct PreviewRefactorResult {
    #[serde(flatten)]
    pub plan: ontocore_refactor::RefactorPlan,
}

#[derive(Debug, Deserialize)]
pub struct ApplyRefactorParams {
    pub plan: ontocore_refactor::RefactorPlan,
    pub request: ontocore_refactor::RefactorRequest,
    #[serde(default)]
    pub preview_only: bool,
}

#[derive(Debug, Deserialize)]
pub struct SemanticDiffParams {
    /// Git left ref (e.g. `main`) or `WORKSPACE` for indexed catalog.
    #[serde(default)]
    pub left_ref: Option<String>,
    /// Git right ref or `WORKTREE` for working tree / indexed workspace.
    #[serde(default)]
    pub right_ref: Option<String>,
    /// Optional left directory when comparing two paths on disk.
    #[serde(default)]
    pub left_path: Option<String>,
    /// Optional right directory.
    #[serde(default)]
    pub right_path: Option<String>,
    /// When true, enrich the diff with reasoner unsatisfiability changes.
    #[serde(default)]
    pub reasoner: bool,
    /// Output format hint: `pr-summary` returns Markdown in `formatted`.
    #[serde(default)]
    pub format: Option<String>,
}

#[derive(Debug, Serialize)]
pub struct SemanticDiffResult {
    pub diff: ontocore_diff::DiffResult,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub formatted: Option<String>,
}

#[derive(Debug, Serialize)]
pub struct ListSqlSchemaResult {
    pub tables: Vec<ontocore_query::SqlTableSchema>,
}

#[derive(Debug, Serialize)]
pub struct ApplyRefactorResult {
    pub files_written: usize,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reindex_warning: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub workspace_edit: Option<lsp_types::WorkspaceEdit>,
}

#[derive(Debug, Serialize)]
pub struct ListPluginsResult {
    pub plugins: Vec<ontocore_plugin::PluginDescriptor>,
}

#[derive(Debug, Deserialize)]
pub struct RunPluginParams {
    pub plugin_id: String,
    #[serde(default = "default_validate_action")]
    pub action: String,
    #[serde(default)]
    pub step: Option<String>,
}

fn default_validate_action() -> String {
    "validate".to_string()
}

#[derive(Debug, Serialize)]
pub struct RunPluginResult {
    pub diagnostics: Vec<DiagnosticSummary>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub output_paths: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub logs: Option<String>,
    pub success: bool,
}