lattice 0.2.1

A markdown predicate linter and backlink reconciler, shipped as an LSP server.
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2026 Two Wells <contact@twowells.dev>

//! Minimal LSP protocol types.
//!
//! Only the subset Lattice actually uses. Serialized/deserialized with serde
//! to match the LSP JSON wire format.

use serde::{Deserialize, Serialize};

// ---------------------------------------------------------------------------
// Primitives
// ---------------------------------------------------------------------------

/// 0-based line and character offset.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Position {
    /// 0-based line number.
    pub line: u32,
    /// 0-based UTF-16 character offset.
    pub character: u32,
}

/// A range in a text document.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Range {
    /// Start position (inclusive).
    pub start: Position,
    /// End position (exclusive).
    pub end: Position,
}

/// A location in a document.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Location {
    /// Document URI.
    pub uri: String,
    /// Range within the document.
    pub range: Range,
}

/// A text edit.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TextEdit {
    /// Range to replace.
    pub range: Range,
    /// Replacement text.
    pub new_text: String,
}

// ---------------------------------------------------------------------------
// Initialization
// ---------------------------------------------------------------------------

/// Subset of `InitializeParams` we read.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct InitializeParams {
    /// Workspace folders.
    pub workspace_folders: Option<Vec<WorkspaceFolder>>,
    /// Deprecated root URI fallback.
    pub root_uri: Option<String>,
}

/// A workspace folder.
#[derive(Debug, Deserialize)]
pub struct WorkspaceFolder {
    /// Folder URI.
    pub uri: String,
}

// ---------------------------------------------------------------------------
// Text document identification
// ---------------------------------------------------------------------------

/// Identifies a text document.
#[derive(Debug, Deserialize)]
pub struct TextDocumentIdentifier {
    /// Document URI.
    pub uri: String,
}

/// Identifies a versioned text document.
#[derive(Debug, Deserialize)]
pub struct VersionedTextDocumentIdentifier {
    /// Document URI.
    pub uri: String,
}

/// A text document item (full content).
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TextDocumentItem {
    /// Document URI.
    pub uri: String,
    /// Document content.
    pub text: String,
}

/// Position in a text document.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TextDocumentPositionParams {
    /// The document.
    pub text_document: TextDocumentIdentifier,
    /// The position.
    pub position: Position,
}

// ---------------------------------------------------------------------------
// Notifications
// ---------------------------------------------------------------------------

/// `textDocument/didOpen` params.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DidOpenTextDocumentParams {
    /// The opened document.
    pub text_document: TextDocumentItem,
}

/// `textDocument/didSave` params.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DidSaveTextDocumentParams {
    /// The saved document.
    pub text_document: TextDocumentIdentifier,
    /// Full content if `includeText` was requested.
    pub text: Option<String>,
}

/// `textDocument/didChange` params.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DidChangeTextDocumentParams {
    /// The changed document.
    pub text_document: VersionedTextDocumentIdentifier,
    /// Content changes (we request FULL sync, so last entry has full text).
    pub content_changes: Vec<TextDocumentContentChangeEvent>,
}

/// A content change event.
#[derive(Debug, Deserialize)]
pub struct TextDocumentContentChangeEvent {
    /// The new text of the document (for FULL sync).
    pub text: String,
}

/// `workspace/didChangeWorkspaceFolders` params.
#[derive(Debug, Deserialize)]
pub struct DidChangeWorkspaceFoldersParams {
    /// The change event.
    pub event: WorkspaceFoldersChangeEvent,
}

/// Workspace folder change event.
#[derive(Debug, Deserialize)]
pub struct WorkspaceFoldersChangeEvent {
    /// Added folders.
    pub added: Vec<WorkspaceFolder>,
    /// Removed folders.
    pub removed: Vec<WorkspaceFolder>,
}

// ---------------------------------------------------------------------------
// Requests — params
// ---------------------------------------------------------------------------

/// `textDocument/documentSymbol` params.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DocumentSymbolParams {
    /// The document.
    pub text_document: TextDocumentIdentifier,
}

/// `workspace/symbol` params.
#[derive(Debug, Deserialize)]
pub struct WorkspaceSymbolParams {
    /// Search query.
    pub query: String,
}

/// `textDocument/rename` params.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RenameParams {
    /// The document and position.
    pub text_document: TextDocumentIdentifier,
    /// The position.
    pub position: Position,
    /// The new name.
    pub new_name: String,
}

/// `textDocument/references` params.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ReferenceParams {
    /// The document.
    pub text_document: TextDocumentIdentifier,
    /// The position.
    pub position: Position,
}

/// `typeHierarchy/supertypes` or `typeHierarchy/subtypes` params.
#[derive(Debug, Deserialize)]
pub struct TypeHierarchyParams {
    /// The item to resolve.
    pub item: HierarchyItem,
}

/// `callHierarchy/incomingCalls` or `callHierarchy/outgoingCalls` params.
#[derive(Debug, Deserialize)]
pub struct CallHierarchyParams {
    /// The item to resolve.
    pub item: HierarchyItem,
}

// ---------------------------------------------------------------------------
// Requests — responses
// ---------------------------------------------------------------------------

/// A document symbol with nested children.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DocumentSymbol {
    /// Symbol name.
    pub name: String,
    /// Optional detail string (e.g. dimensions, display text).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub detail: Option<String>,
    /// Symbol kind (numeric LSP `SymbolKind`).
    pub kind: u32,
    /// Full range of the symbol.
    pub range: Range,
    /// Range of the symbol name for selection.
    pub selection_range: Range,
    /// Child symbols.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub children: Option<Vec<Self>>,
}

/// A flat symbol (workspace symbol results).
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SymbolInformation {
    /// Symbol name.
    pub name: String,
    /// Symbol kind.
    pub kind: u32,
    /// Location.
    pub location: Location,
    /// Container name.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub container_name: Option<String>,
}

/// A workspace edit.
#[derive(Debug, Clone, Serialize)]
pub struct WorkspaceEdit {
    /// URI → edits mapping.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub changes: Option<std::collections::HashMap<String, Vec<TextEdit>>>,
}

/// An LSP diagnostic.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Diagnostic {
    /// Range.
    pub range: Range,
    /// Severity (1=Error, 2=Warning, 3=Information, 4=Hint).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub severity: Option<u32>,
    /// Source identifier.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source: Option<String>,
    /// Message.
    pub message: String,
}

/// `textDocument/publishDiagnostics` params.
#[derive(Debug, Serialize)]
pub struct PublishDiagnosticsParams {
    /// Document URI.
    pub uri: String,
    /// Diagnostics.
    pub diagnostics: Vec<Diagnostic>,
}

/// A hierarchy item used for both type hierarchy and call hierarchy.
///
/// Serialized/deserialized identically for both protocols — the LSP wire
/// format is the same.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HierarchyItem {
    /// Symbol name.
    pub name: String,
    /// Symbol kind.
    pub kind: u32,
    /// Document URI.
    pub uri: String,
    /// Full range.
    pub range: Range,
    /// Selection range.
    pub selection_range: Range,
    /// Optional detail string.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub detail: Option<String>,
    /// Opaque data preserved between prepare and resolve.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub data: Option<serde_json::Value>,
}

/// A document link (ctrl-clickable).
#[derive(Debug, Clone, Serialize)]
pub struct DocumentLink {
    /// Range of the link in the source document.
    pub range: Range,
    /// Target URI.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub target: Option<String>,
}

/// A folding range.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct FoldingRange {
    /// 0-based start line.
    pub start_line: u32,
    /// 0-based end line.
    pub end_line: u32,
    /// Folding range kind.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub kind: Option<String>,
}

/// A hover result.
#[derive(Debug, Clone, Serialize)]
pub struct Hover {
    /// Hover content.
    pub contents: MarkupContent,
}

/// Markup content for hover.
#[derive(Debug, Clone, Serialize)]
pub struct MarkupContent {
    /// Content kind ("markdown" or "plaintext").
    pub kind: String,
    /// The actual content.
    pub value: String,
}

/// Full document diagnostic report.
#[derive(Debug, Clone, Serialize)]
pub struct FullDocumentDiagnosticReport {
    /// Report kind — always "full".
    pub kind: String,
    /// Diagnostics.
    pub items: Vec<Diagnostic>,
}

/// A single workspace document diagnostic report.
#[derive(Debug, Clone, Serialize)]
pub struct WorkspaceDocumentDiagnosticReport {
    /// Report kind — always "full".
    pub kind: String,
    /// Document URI.
    pub uri: String,
    /// Diagnostics.
    pub items: Vec<Diagnostic>,
}

/// Workspace diagnostic report.
#[derive(Debug, Clone, Serialize)]
pub struct WorkspaceDiagnosticReport {
    /// Per-document reports.
    pub items: Vec<WorkspaceDocumentDiagnosticReport>,
}

/// `textDocument/formatting` params.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DocumentFormattingParams {
    /// The document to format.
    pub text_document: TextDocumentIdentifier,
}

/// `textDocument/diagnostic` params.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DocumentDiagnosticParams {
    /// The document to get diagnostics for.
    pub text_document: TextDocumentIdentifier,
}

/// `textDocument/semanticTokens/full` params.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SemanticTokensParams {
    /// The document to tokenize.
    pub text_document: TextDocumentIdentifier,
}

/// `textDocument/semanticTokens/range` params.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SemanticTokensRangeParams {
    /// The document to tokenize.
    pub text_document: TextDocumentIdentifier,
    /// The range to restrict the tokens to.
    pub range: Range,
}

/// A semantic-tokens result: the flat delta-encoded `data` array.
///
/// `data` is a flat sequence of 5-tuples
/// `[deltaLine, deltaStartChar, length, tokenType, tokenModifiers]` — see the
/// LSP `SemanticTokens` type. The offsets are in the negotiated position
/// encoding (UTF-16 by default).
#[derive(Debug, Clone, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SemanticTokens {
    /// The flat delta-encoded token data (groups of five `u32`).
    pub data: Vec<u32>,
}

/// A completion item.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CompletionItem {
    /// Display label (also the default inserted text).
    pub label: String,
    /// Completion item kind (numeric LSP `CompletionItemKind`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub kind: Option<u32>,
    /// Optional detail string (e.g. the inverse predicate, or a target URL).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub detail: Option<String>,
    /// Text the client filters against the typed prefix.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub filter_text: Option<String>,
    /// Text the client orders items by.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sort_text: Option<String>,
    /// Replacement edit applied when the item is selected.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub text_edit: Option<TextEdit>,
}

/// A completion result list.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CompletionList {
    /// Whether the list is incomplete (recomputed as the user types further).
    pub is_incomplete: bool,
    /// The completion items.
    pub items: Vec<CompletionItem>,
}

/// An incoming call.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CallHierarchyIncomingCall {
    /// The calling item.
    pub from: HierarchyItem,
    /// Ranges in the caller where the call appears.
    pub from_ranges: Vec<Range>,
}

/// An outgoing call.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CallHierarchyOutgoingCall {
    /// The called item.
    pub to: HierarchyItem,
    /// Ranges in the current item where the call appears.
    pub from_ranges: Vec<Range>,
}

// ---------------------------------------------------------------------------
// LSP constants
// ---------------------------------------------------------------------------

/// LSP `SymbolKind` constants (subset).
pub mod symbol_kind {
    /// File (media embeds: image, video, audio, iframe).
    pub const FILE: u32 = 1;
    /// Module (scope containers: blockquote, admonition, details, generic containers).
    pub const MODULE: u32 = 2;
    /// Class (headings — type definitions in the document hierarchy).
    pub const CLASS: u32 = 5;
    /// Field (table column headers).
    pub const FIELD: u32 = 8;
    /// Function (links — graph edges).
    pub const FUNCTION: u32 = 12;
    /// Constant (footnote definitions).
    pub const CONSTANT: u32 = 14;
    /// Object (opaque content blocks: code blocks, math).
    pub const OBJECT: u32 = 19;
    /// Struct (data containers: tables, lists).
    pub const STRUCT: u32 = 23;
    /// Event (form elements: `<input>`, `<select>`, `<textarea>`).
    pub const EVENT: u32 = 24;
}

/// LSP `CompletionItemKind` constants (subset).
pub mod completion_item_kind {
    /// Value (heading fragments / anchors).
    pub const VALUE: u32 = 12;
    /// Keyword (predicate vocabulary).
    pub const KEYWORD: u32 = 14;
    /// File (link-target files).
    pub const FILE: u32 = 17;
    /// Reference (link reference labels).
    pub const REFERENCE: u32 = 18;
    /// Folder (link-target directories).
    pub const FOLDER: u32 = 19;
    /// Constant (footnote labels).
    pub const CONSTANT: u32 = 21;
}

/// LSP `DiagnosticSeverity` constants.
pub mod diagnostic_severity {
    /// Error.
    pub const ERROR: u32 = 1;
    /// Warning.
    pub const WARNING: u32 = 2;
    /// Information.
    pub const INFORMATION: u32 = 3;
    /// Hint.
    pub const HINT: u32 = 4;
}

/// LSP method name constants.
pub mod method {
    // Notifications
    /// `textDocument/didOpen`.
    pub const DID_OPEN: &str = "textDocument/didOpen";
    /// `textDocument/didSave`.
    pub const DID_SAVE: &str = "textDocument/didSave";
    /// `textDocument/didChange`.
    pub const DID_CHANGE: &str = "textDocument/didChange";
    /// `workspace/didChangeWorkspaceFolders`.
    pub const DID_CHANGE_WORKSPACE_FOLDERS: &str = "workspace/didChangeWorkspaceFolders";
    /// `textDocument/publishDiagnostics`.
    pub const PUBLISH_DIAGNOSTICS: &str = "textDocument/publishDiagnostics";

    // Requests
    /// `textDocument/documentSymbol`.
    pub const DOCUMENT_SYMBOL: &str = "textDocument/documentSymbol";
    /// `workspace/symbol`.
    pub const WORKSPACE_SYMBOL: &str = "workspace/symbol";
    /// `textDocument/prepareRename`.
    pub const PREPARE_RENAME: &str = "textDocument/prepareRename";
    /// `textDocument/rename`.
    pub const RENAME: &str = "textDocument/rename";
    /// `textDocument/references`.
    pub const REFERENCES: &str = "textDocument/references";
    /// `textDocument/declaration`.
    pub const DECLARATION: &str = "textDocument/declaration";
    /// `textDocument/definition`.
    pub const DEFINITION: &str = "textDocument/definition";
    /// `textDocument/typeDefinition`.
    pub const TYPE_DEFINITION: &str = "textDocument/typeDefinition";
    /// `textDocument/implementation`.
    pub const IMPLEMENTATION: &str = "textDocument/implementation";
    /// `textDocument/prepareTypeHierarchy`.
    pub const PREPARE_TYPE_HIERARCHY: &str = "textDocument/prepareTypeHierarchy";
    /// `typeHierarchy/supertypes`.
    pub const TYPE_HIERARCHY_SUPERTYPES: &str = "typeHierarchy/supertypes";
    /// `typeHierarchy/subtypes`.
    pub const TYPE_HIERARCHY_SUBTYPES: &str = "typeHierarchy/subtypes";
    /// `textDocument/prepareCallHierarchy`.
    pub const PREPARE_CALL_HIERARCHY: &str = "textDocument/prepareCallHierarchy";
    /// `callHierarchy/incomingCalls`.
    pub const CALL_HIERARCHY_INCOMING: &str = "callHierarchy/incomingCalls";
    /// `callHierarchy/outgoingCalls`.
    pub const CALL_HIERARCHY_OUTGOING: &str = "callHierarchy/outgoingCalls";
    /// `textDocument/documentLink`.
    pub const DOCUMENT_LINK: &str = "textDocument/documentLink";
    /// `textDocument/foldingRange`.
    pub const FOLDING_RANGE: &str = "textDocument/foldingRange";
    /// `textDocument/hover`.
    pub const HOVER: &str = "textDocument/hover";
    /// `textDocument/diagnostic`.
    pub const DOCUMENT_DIAGNOSTIC: &str = "textDocument/diagnostic";
    /// `workspace/diagnostic`.
    pub const WORKSPACE_DIAGNOSTIC: &str = "workspace/diagnostic";
    /// `textDocument/formatting`.
    pub const FORMATTING: &str = "textDocument/formatting";
    /// `textDocument/completion`.
    pub const COMPLETION: &str = "textDocument/completion";
    /// `textDocument/semanticTokens/full`.
    pub const SEMANTIC_TOKENS_FULL: &str = "textDocument/semanticTokens/full";
    /// `textDocument/semanticTokens/range`.
    pub const SEMANTIC_TOKENS_RANGE: &str = "textDocument/semanticTokens/range";
}