eure-ls 0.2.0

Language Server Protocol implementation for Eure
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
//! LSP-specific queries that convert to LSP types.

use eure::query::{
    CompletionItem, CompletionKind, DiagnosticMessage, DiagnosticSeverity, GetFileDiagnostics,
    GetSemanticTokens, SemanticToken, TextFile, get_completions, get_hover,
};
use lsp_types::{
    CompletionItem as LspCompletionItem, CompletionItemKind, CompletionTextEdit, Diagnostic,
    DiagnosticSeverity as LspSeverity, Documentation, Hover, HoverContents, MarkupContent,
    MarkupKind, NumberOrString, Position, Range, SemanticToken as LspSemanticToken, SemanticTokens,
    TextEdit,
};
use query_flow::{Db, QueryError, query};

/// LSP-formatted completion.
///
/// Wraps `get_completions` and converts to LSP `CompletionItem`s. `offset` is
/// the cursor position as a byte offset (see [`position_to_offset`]).
///
/// Like `get_completions`, this is a plain function rather than a query so
/// that per-cursor results are not memoized.
pub fn lsp_completion(
    db: &impl Db,
    file: &TextFile,
    offset: u32,
) -> Result<Vec<LspCompletionItem>, QueryError> {
    let items = get_completions(db, file, offset)?;
    let source: std::sync::Arc<eure::query::TextFileContent> = db.asset(file.clone())?;
    let line_offsets = compute_line_offsets(source.get());
    Ok(items
        .iter()
        .map(|item| convert_completion_item(item, source.get(), &line_offsets))
        .collect())
}

fn convert_completion_item(
    item: &CompletionItem,
    source: &str,
    line_offsets: &[usize],
) -> LspCompletionItem {
    let range = Range {
        start: offset_to_lsp_position(item.replace.start as usize, source, line_offsets),
        end: offset_to_lsp_position(item.replace.end as usize, source, line_offsets),
    };
    LspCompletionItem {
        label: item.label.clone(),
        kind: Some(convert_completion_kind(item.kind)),
        detail: item.detail.clone(),
        documentation: item.documentation.as_ref().map(|value| {
            Documentation::MarkupContent(MarkupContent {
                kind: MarkupKind::Markdown,
                value: value.clone(),
            })
        }),
        deprecated: item.deprecated.then_some(true),
        filter_text: Some(item.label.clone()),
        text_edit: Some(CompletionTextEdit::Edit(TextEdit {
            range,
            new_text: item.label.clone(),
        })),
        ..Default::default()
    }
}

fn convert_completion_kind(kind: CompletionKind) -> CompletionItemKind {
    match kind {
        CompletionKind::Field => CompletionItemKind::FIELD,
        CompletionKind::Extension => CompletionItemKind::PROPERTY,
        CompletionKind::Variant => CompletionItemKind::ENUM_MEMBER,
        CompletionKind::Value => CompletionItemKind::VALUE,
    }
}

/// LSP-formatted hover.
///
/// Wraps `get_hover` and converts to an LSP `Hover` with markdown contents
/// and the range of the hovered key or value. `offset` is the cursor
/// position as a byte offset (see [`position_to_offset`]).
///
/// Like `get_hover`, this is a plain function rather than a query so that
/// per-cursor results are not memoized.
pub fn lsp_hover(db: &impl Db, file: &TextFile, offset: u32) -> Result<Option<Hover>, QueryError> {
    let Some(hover) = get_hover(db, file, offset)? else {
        return Ok(None);
    };
    let source: std::sync::Arc<eure::query::TextFileContent> = db.asset(file.clone())?;
    let line_offsets = compute_line_offsets(source.get());
    Ok(Some(Hover {
        contents: HoverContents::Markup(MarkupContent {
            kind: MarkupKind::Markdown,
            value: hover.contents,
        }),
        range: Some(Range {
            start: offset_to_lsp_position(hover.span.start as usize, source.get(), &line_offsets),
            end: offset_to_lsp_position(hover.span.end as usize, source.get(), &line_offsets),
        }),
    }))
}

/// Definition ranges use the target file's own UTF-16 coordinates.
pub fn lsp_definition(
    db: &impl Db,
    file: &TextFile,
    offset: u32,
) -> Result<Vec<lsp_types::LocationLink>, QueryError> {
    let definitions = eure::query::get_definition(db, file, offset)?;
    let source = db.asset(file.clone())?;
    let offsets = compute_line_offsets(source.get());
    definitions
        .into_iter()
        .map(|definition| {
            let target = db.asset(definition.file.clone())?;
            let target_offsets = compute_line_offsets(target.get());
            Ok(lsp_types::LocationLink {
                origin_selection_range: Some(Range {
                    start: offset_to_lsp_position(
                        definition.origin.start as usize,
                        source.get(),
                        &offsets,
                    ),
                    end: offset_to_lsp_position(
                        definition.origin.end as usize,
                        source.get(),
                        &offsets,
                    ),
                }),
                target_uri: crate::uri_utils::text_file_to_uri(&definition.file).parse()?,
                target_range: Range {
                    start: offset_to_lsp_position(
                        definition.range.start as usize,
                        target.get(),
                        &target_offsets,
                    ),
                    end: offset_to_lsp_position(
                        definition.range.end as usize,
                        target.get(),
                        &target_offsets,
                    ),
                },
                target_selection_range: Range {
                    start: offset_to_lsp_position(
                        definition.selection.start as usize,
                        target.get(),
                        &target_offsets,
                    ),
                    end: offset_to_lsp_position(
                        definition.selection.end as usize,
                        target.get(),
                        &target_offsets,
                    ),
                },
            })
        })
        .collect()
}

/// Convert an LSP position (UTF-16 based) to a byte offset in `source`.
///
/// Positions past the end of a line clamp to the line end; positions past
/// the last line clamp to the end of the source.
pub fn position_to_offset(source: &str, position: Position) -> usize {
    let line_offsets = compute_line_offsets(source);
    let Some(&line_start) = line_offsets.get(position.line as usize) else {
        return source.len();
    };
    let line_end = line_offsets
        .get(position.line as usize + 1)
        .map(|&next| next - 1)
        .unwrap_or(source.len());
    let line = &source[line_start..line_end];
    let mut utf16_units = 0u32;
    for (byte_index, c) in line.char_indices() {
        if utf16_units >= position.character {
            return line_start + byte_index;
        }
        utf16_units += c.len_utf16() as u32;
    }
    line_end
}

/// LSP-formatted semantic tokens query.
///
/// Wraps `GetSemanticTokens` and converts to LSP `SemanticTokens` format.
#[query]
pub fn lsp_semantic_tokens(
    db: &impl Db,
    file: TextFile,
    source: String,
) -> Result<SemanticTokens, QueryError> {
    let tokens = db.query(GetSemanticTokens::new(file.clone()))?;
    Ok(convert_tokens(&tokens, &source))
}

/// LSP-formatted diagnostics query, grouped by file.
///
/// Wraps `GetFileDiagnostics` and converts to LSP `Diagnostic` format.
/// Returns diagnostics grouped by file, so that each file can receive
/// its own publishDiagnostics notification.
#[query]
pub fn lsp_diagnostics(
    db: &impl Db,
    file: TextFile,
) -> Result<Vec<(TextFile, Vec<Diagnostic>)>, QueryError> {
    let diagnostics = db.query(GetFileDiagnostics::new(file.clone()))?;

    // Group diagnostics by file
    let mut by_file: std::collections::HashMap<TextFile, Vec<DiagnosticMessage>> =
        std::collections::HashMap::new();

    // Always include the target file so diagnostics are cleared when errors are fixed
    by_file.insert(file, vec![]);

    for d in diagnostics.iter() {
        by_file.entry(d.file.clone()).or_default().push(d.clone());
    }

    // Convert each group to LSP diagnostics using the correct source
    let mut result = Vec::new();
    for (diag_file, file_diagnostics) in by_file {
        let source: std::sync::Arc<eure::query::TextFileContent> = db.asset(diag_file.clone())?;
        let line_offsets = compute_line_offsets(source.get());
        let lsp_diagnostics: Vec<Diagnostic> = file_diagnostics
            .iter()
            .map(|d| convert_diagnostic(d, source.get(), &line_offsets))
            .collect();
        result.push((diag_file, lsp_diagnostics));
    }

    Ok(result)
}

/// LSP-formatted diagnostics for a single file.
///
/// Wraps `GetFileDiagnostics` and converts to LSP `Diagnostic` format.
/// Returns diagnostics only for the specified file.
#[query]
pub fn lsp_file_diagnostics(db: &impl Db, file: TextFile) -> Result<Vec<Diagnostic>, QueryError> {
    let diagnostics = db.query(GetFileDiagnostics::new(file.clone()))?;

    // Get source for position conversion
    let source: std::sync::Arc<eure::query::TextFileContent> = db.asset(file.clone())?;
    let line_offsets = compute_line_offsets(source.get());

    // Convert to LSP diagnostics
    let lsp_diagnostics: Vec<Diagnostic> = diagnostics
        .iter()
        .filter(|d| d.file == file) // Only include diagnostics for this file
        .map(|d| convert_diagnostic(d, source.get(), &line_offsets))
        .collect();

    Ok(lsp_diagnostics)
}

/// Convert internal semantic tokens to LSP format.
///
/// LSP semantic tokens use a delta encoding:
/// - Each token is encoded as (deltaLine, deltaStartChar, length, tokenType, tokenModifiers)
/// - deltaLine is relative to the previous token's line
/// - deltaStartChar is relative to the previous token's start (or line start if on new line)
/// - All character positions and lengths are in UTF-16 code units
fn convert_tokens(tokens: &[SemanticToken], source: &str) -> SemanticTokens {
    let line_offsets = compute_line_offsets(source);

    let mut data = Vec::new();
    let mut prev_line = 0u32;
    let mut prev_start = 0u32;

    for token in tokens {
        let start = token.start as usize;
        let end = start + token.length as usize;
        let (line, char) = offset_to_position(start, source, &line_offsets);
        let length = byte_len_to_utf16_len(source, start, end);

        let delta_line = line - prev_line;
        let delta_start = if delta_line == 0 {
            char - prev_start
        } else {
            char
        };

        data.push(LspSemanticToken {
            delta_line,
            delta_start,
            length,
            token_type: token.token_type as u32,
            token_modifiers_bitset: token.modifiers,
        });

        prev_line = line;
        prev_start = char;
    }

    SemanticTokens {
        result_id: None,
        data,
    }
}

/// Convert internal diagnostic to LSP format.
fn convert_diagnostic(msg: &DiagnosticMessage, source: &str, line_offsets: &[usize]) -> Diagnostic {
    let start = offset_to_lsp_position(msg.start, source, line_offsets);
    let end = offset_to_lsp_position(msg.end, source, line_offsets);

    Diagnostic {
        range: Range { start, end },
        severity: Some(convert_severity(msg.severity)),
        code: msg.code.clone().map(NumberOrString::String),
        code_description: None,
        source: Some("eure".to_string()),
        message: msg.message.clone(),
        related_information: None,
        tags: None,
        data: None,
    }
}

/// Convert internal severity to LSP severity.
fn convert_severity(severity: DiagnosticSeverity) -> LspSeverity {
    match severity {
        DiagnosticSeverity::Error => LspSeverity::ERROR,
        DiagnosticSeverity::Warning => LspSeverity::WARNING,
        DiagnosticSeverity::Info => LspSeverity::INFORMATION,
        DiagnosticSeverity::Hint => LspSeverity::HINT,
    }
}

/// Compute line offsets for a source string.
///
/// Returns a vector where `line_offsets[i]` is the byte offset of line `i`.
fn compute_line_offsets(source: &str) -> Vec<usize> {
    let mut offsets = vec![0];
    for (i, c) in source.char_indices() {
        if c == '\n' {
            offsets.push(i + 1);
        }
    }
    offsets
}

/// Convert a byte offset to (line, character) position.
///
/// Line is 0-indexed. Character is in UTF-16 code units (as required by LSP).
fn offset_to_position(offset: usize, source: &str, line_offsets: &[usize]) -> (u32, u32) {
    let line = line_offsets.iter().rposition(|&o| o <= offset).unwrap_or(0);
    let line_start = line_offsets[line];
    // Count UTF-16 code units from line start to offset
    let end = offset.min(source.len());
    let line_content = &source[line_start..end];
    let utf16_offset: usize = line_content.chars().map(|c| c.len_utf16()).sum();
    (line as u32, utf16_offset as u32)
}

/// Convert a byte offset to LSP Position with UTF-16 character position.
fn offset_to_lsp_position(offset: usize, source: &str, line_offsets: &[usize]) -> Position {
    let (line, character) = offset_to_position(offset, source, line_offsets);
    Position { line, character }
}

/// Convert a byte length to UTF-16 code unit length.
fn byte_len_to_utf16_len(source: &str, start: usize, end: usize) -> u32 {
    let end = end.min(source.len());
    let start = start.min(end);
    source[start..end]
        .chars()
        .map(|c| c.len_utf16())
        .sum::<usize>() as u32
}

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

    #[test]
    fn test_compute_line_offsets() {
        let source = "hello\nworld\n";
        let offsets = compute_line_offsets(source);
        assert_eq!(offsets, vec![0, 6, 12]);
    }

    #[test]
    fn test_offset_to_position_ascii() {
        let source = "hello\nworld\n";
        let offsets = compute_line_offsets(source);
        assert_eq!(offset_to_position(0, source, &offsets), (0, 0));
        assert_eq!(offset_to_position(5, source, &offsets), (0, 5));
        assert_eq!(offset_to_position(6, source, &offsets), (1, 0));
        assert_eq!(offset_to_position(11, source, &offsets), (1, 5));
    }

    #[test]
    fn test_offset_to_position_utf8() {
        // "日本語" is 9 bytes (3 chars × 3 bytes each), but 3 UTF-16 code units
        let source = "日本語\ntest";
        let offsets = compute_line_offsets(source);
        // Byte offset 0 -> (line 0, char 0)
        assert_eq!(offset_to_position(0, source, &offsets), (0, 0));
        // Byte offset 3 (after 日) -> (line 0, char 1)
        assert_eq!(offset_to_position(3, source, &offsets), (0, 1));
        // Byte offset 6 (after 日本) -> (line 0, char 2)
        assert_eq!(offset_to_position(6, source, &offsets), (0, 2));
        // Byte offset 9 (after 日本語) -> (line 0, char 3)
        assert_eq!(offset_to_position(9, source, &offsets), (0, 3));
        // Byte offset 10 (after \n) -> (line 1, char 0)
        assert_eq!(offset_to_position(10, source, &offsets), (1, 0));
    }

    #[test]
    fn test_offset_to_position_emoji() {
        // "😀" is 4 bytes in UTF-8, but 2 UTF-16 code units (surrogate pair)
        let source = "😀a";
        let offsets = compute_line_offsets(source);
        // Byte offset 0 -> (line 0, char 0)
        assert_eq!(offset_to_position(0, source, &offsets), (0, 0));
        // Byte offset 4 (after 😀) -> (line 0, char 2) because emoji is 2 UTF-16 units
        assert_eq!(offset_to_position(4, source, &offsets), (0, 2));
        // Byte offset 5 (after 😀a) -> (line 0, char 3)
        assert_eq!(offset_to_position(5, source, &offsets), (0, 3));
    }

    #[test]
    fn test_position_to_offset() {
        let source = "日本語\ntest";
        assert_eq!(position_to_offset(source, Position::new(0, 0)), 0);
        assert_eq!(position_to_offset(source, Position::new(0, 2)), 6);
        assert_eq!(position_to_offset(source, Position::new(0, 3)), 9);
        // Past the end of the line clamps to the line end (before the newline)
        assert_eq!(position_to_offset(source, Position::new(0, 10)), 9);
        assert_eq!(position_to_offset(source, Position::new(1, 4)), 14);
        // Past the last line clamps to the end of the source
        assert_eq!(position_to_offset(source, Position::new(5, 0)), 14);
        // Surrogate pair counts as two UTF-16 units
        assert_eq!(position_to_offset("😀a", Position::new(0, 2)), 4);
    }

    #[test]
    fn test_byte_len_to_utf16_len() {
        // ASCII: 1 byte = 1 UTF-16 unit
        assert_eq!(byte_len_to_utf16_len("hello", 0, 5), 5);
        // Japanese: 3 bytes per char, 1 UTF-16 unit per char
        assert_eq!(byte_len_to_utf16_len("日本語", 0, 9), 3);
        // Emoji: 4 bytes, 2 UTF-16 units
        assert_eq!(byte_len_to_utf16_len("😀", 0, 4), 2);
    }
}