summer-lsp 0.5.0

Language Server Protocol implementation for summer-rs framework
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
//! 文档管理模块

use dashmap::DashMap;
use lsp_types::{TextDocumentContentChangeEvent, Url};

/// 文档管理器
pub struct DocumentManager {
    /// 文档缓存(DashMap 本身就是并发安全的)
    documents: DashMap<Url, Document>,
}

/// 文档
#[derive(Debug, Clone)]
pub struct Document {
    /// 文档 URI
    pub uri: Url,
    /// 文档版本
    pub version: i32,
    /// 文档内容
    pub content: String,
    /// 语言 ID
    pub language_id: String,
}

impl DocumentManager {
    /// 创建新的文档管理器
    pub fn new() -> Self {
        Self {
            documents: DashMap::new(),
        }
    }

    /// 打开文档
    pub fn open(&self, uri: Url, version: i32, content: String, language_id: String) {
        let doc = Document {
            uri: uri.clone(),
            version,
            content,
            language_id,
        };
        self.documents.insert(uri, doc);
    }

    /// 修改文档
    pub fn change(&self, uri: &Url, version: i32, changes: Vec<TextDocumentContentChangeEvent>) {
        if let Some(mut doc) = self.documents.get_mut(uri) {
            doc.version = version;

            // 应用修改
            for change in changes {
                if let Some(range) = change.range {
                    // 增量修改
                    if let Err(e) =
                        Self::apply_incremental_change(&mut doc.content, range, &change.text)
                    {
                        tracing::error!("Failed to apply incremental change: {}", e);
                        // 降级到全量更新
                        doc.content = change.text;
                    }
                } else {
                    // 全量修改
                    doc.content = change.text;
                }
            }
        }
    }

    /// 应用增量修改
    fn apply_incremental_change(
        content: &mut String,
        range: lsp_types::Range,
        text: &str,
    ) -> Result<(), String> {
        // 将内容按行分割
        let lines: Vec<&str> = content.lines().collect();

        // 验证范围的有效性
        let start_line = range.start.line as usize;
        let end_line = range.end.line as usize;

        if start_line > lines.len() || end_line > lines.len() {
            return Err(format!(
                "Invalid range: start_line={}, end_line={}, total_lines={}",
                start_line,
                end_line,
                lines.len()
            ));
        }

        // 计算起始和结束位置的字节偏移
        let start_offset = Self::position_to_offset(content, range.start)?;
        let end_offset = Self::position_to_offset(content, range.end)?;

        if start_offset > end_offset || end_offset > content.len() {
            return Err(format!(
                "Invalid offsets: start={}, end={}, content_len={}",
                start_offset,
                end_offset,
                content.len()
            ));
        }

        // 构建新内容
        let mut new_content =
            String::with_capacity(content.len() - (end_offset - start_offset) + text.len());
        new_content.push_str(&content[..start_offset]);
        new_content.push_str(text);
        new_content.push_str(&content[end_offset..]);

        *content = new_content;
        Ok(())
    }

    /// 将 LSP Position 转换为字节偏移
    fn position_to_offset(content: &str, position: lsp_types::Position) -> Result<usize, String> {
        let mut offset = 0;
        let mut current_line = 0;
        let target_line = position.line as usize;
        let target_char = position.character as usize;

        for line in content.lines() {
            if current_line == target_line {
                // 找到目标行,计算字符偏移
                let char_offset = Self::char_offset_to_byte_offset(line, target_char)?;
                return Ok(offset + char_offset);
            }

            // 移动到下一行(包括换行符)
            offset += line.len() + 1; // +1 for '\n'
            current_line += 1;
        }

        // 如果到达文件末尾
        if current_line == target_line && target_char == 0 {
            return Ok(offset);
        }

        Err(format!(
            "Position out of bounds: line={}, char={}, total_lines={}",
            target_line, target_char, current_line
        ))
    }

    /// 将字符偏移转换为字节偏移
    fn char_offset_to_byte_offset(line: &str, char_offset: usize) -> Result<usize, String> {
        let mut byte_offset = 0;
        let mut current_char = 0;

        for ch in line.chars() {
            if current_char == char_offset {
                return Ok(byte_offset);
            }
            byte_offset += ch.len_utf8();
            current_char += 1;
        }

        // 允许在行尾
        if current_char == char_offset {
            return Ok(byte_offset);
        }

        Err(format!(
            "Character offset out of bounds: char_offset={}, line_length={}",
            char_offset, current_char
        ))
    }

    /// 关闭文档
    pub fn close(&self, uri: &Url) {
        self.documents.remove(uri);
    }

    /// 获取文档(返回克隆以避免锁竞争)
    pub fn get(&self, uri: &Url) -> Option<Document> {
        self.documents.get(uri).map(|doc| doc.clone())
    }

    /// 获取文档的只读引用(用于快速访问)
    pub fn with_document<F, R>(&self, uri: &Url, f: F) -> Option<R>
    where
        F: FnOnce(&Document) -> R,
    {
        self.documents.get(uri).map(|doc| f(&doc))
    }
}

impl Default for DocumentManager {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use lsp_types::{Position, Range};

    #[test]
    fn test_open_document() {
        let manager = DocumentManager::new();
        let uri: Url = "file:///test.toml".parse().unwrap();

        manager.open(uri.clone(), 1, "content".to_string(), "toml".to_string());

        let doc = manager.get(&uri).unwrap();
        assert_eq!(doc.version, 1);
        assert_eq!(doc.content, "content");
        assert_eq!(doc.language_id, "toml");
    }

    #[test]
    fn test_close_document() {
        let manager = DocumentManager::new();
        let uri: Url = "file:///test.toml".parse().unwrap();

        manager.open(uri.clone(), 1, "content".to_string(), "toml".to_string());
        assert!(manager.get(&uri).is_some());

        manager.close(&uri);
        assert!(manager.get(&uri).is_none());
    }

    #[test]
    fn test_full_content_change() {
        let manager = DocumentManager::new();
        let uri: Url = "file:///test.toml".parse().unwrap();

        manager.open(
            uri.clone(),
            1,
            "old content".to_string(),
            "toml".to_string(),
        );

        let changes = vec![TextDocumentContentChangeEvent {
            range: None,
            range_length: None,
            text: "new content".to_string(),
        }];

        manager.change(&uri, 2, changes);

        let doc = manager.get(&uri).unwrap();
        assert_eq!(doc.version, 2);
        assert_eq!(doc.content, "new content");
    }

    #[test]
    fn test_incremental_change_single_line() {
        let manager = DocumentManager::new();
        let uri: Url = "file:///test.toml".parse().unwrap();

        manager.open(
            uri.clone(),
            1,
            "hello world".to_string(),
            "toml".to_string(),
        );

        // 替换 "world" 为 "rust"
        let changes = vec![TextDocumentContentChangeEvent {
            range: Some(Range {
                start: Position {
                    line: 0,
                    character: 6,
                },
                end: Position {
                    line: 0,
                    character: 11,
                },
            }),
            range_length: None,
            text: "rust".to_string(),
        }];

        manager.change(&uri, 2, changes);

        let doc = manager.get(&uri).unwrap();
        assert_eq!(doc.content, "hello rust");
    }

    #[test]
    fn test_incremental_change_multiline() {
        let manager = DocumentManager::new();
        let uri: Url = "file:///test.toml".parse().unwrap();

        let initial_content = "line 1\nline 2\nline 3";
        manager.open(
            uri.clone(),
            1,
            initial_content.to_string(),
            "toml".to_string(),
        );

        // 替换第二行的 "line 2" 为 "modified"
        let changes = vec![TextDocumentContentChangeEvent {
            range: Some(Range {
                start: Position {
                    line: 1,
                    character: 0,
                },
                end: Position {
                    line: 1,
                    character: 6,
                },
            }),
            range_length: None,
            text: "modified".to_string(),
        }];

        manager.change(&uri, 2, changes);

        let doc = manager.get(&uri).unwrap();
        assert_eq!(doc.content, "line 1\nmodified\nline 3");
    }

    #[test]
    fn test_incremental_change_insert() {
        let manager = DocumentManager::new();
        let uri: Url = "file:///test.toml".parse().unwrap();

        manager.open(uri.clone(), 1, "hello".to_string(), "toml".to_string());

        // 在 "hello" 后插入 " world"
        let changes = vec![TextDocumentContentChangeEvent {
            range: Some(Range {
                start: Position {
                    line: 0,
                    character: 5,
                },
                end: Position {
                    line: 0,
                    character: 5,
                },
            }),
            range_length: None,
            text: " world".to_string(),
        }];

        manager.change(&uri, 2, changes);

        let doc = manager.get(&uri).unwrap();
        assert_eq!(doc.content, "hello world");
    }

    #[test]
    fn test_incremental_change_delete() {
        let manager = DocumentManager::new();
        let uri: Url = "file:///test.toml".parse().unwrap();

        manager.open(
            uri.clone(),
            1,
            "hello world".to_string(),
            "toml".to_string(),
        );

        // 删除 " world"
        let changes = vec![TextDocumentContentChangeEvent {
            range: Some(Range {
                start: Position {
                    line: 0,
                    character: 5,
                },
                end: Position {
                    line: 0,
                    character: 11,
                },
            }),
            range_length: None,
            text: "".to_string(),
        }];

        manager.change(&uri, 2, changes);

        let doc = manager.get(&uri).unwrap();
        assert_eq!(doc.content, "hello");
    }

    #[test]
    fn test_incremental_change_utf8() {
        let manager = DocumentManager::new();
        let uri: Url = "file:///test.toml".parse().unwrap();

        manager.open(uri.clone(), 1, "你好世界".to_string(), "toml".to_string());

        // 替换 "世界" 为 "Rust"
        let changes = vec![TextDocumentContentChangeEvent {
            range: Some(Range {
                start: Position {
                    line: 0,
                    character: 2,
                },
                end: Position {
                    line: 0,
                    character: 4,
                },
            }),
            range_length: None,
            text: "Rust".to_string(),
        }];

        manager.change(&uri, 2, changes);

        let doc = manager.get(&uri).unwrap();
        assert_eq!(doc.content, "你好Rust");
    }

    #[test]
    fn test_with_document() {
        let manager = DocumentManager::new();
        let uri: Url = "file:///test.toml".parse().unwrap();

        manager.open(uri.clone(), 1, "content".to_string(), "toml".to_string());

        let length = manager.with_document(&uri, |doc| doc.content.len());
        assert_eq!(length, Some(7));

        let not_found = manager.with_document(&"file:///notfound.toml".parse().unwrap(), |doc| {
            doc.content.len()
        });
        assert_eq!(not_found, None);
    }

    #[test]
    fn test_position_to_offset() {
        let content = "line 1\nline 2\nline 3";

        // 第一行开始
        let offset = DocumentManager::position_to_offset(
            content,
            Position {
                line: 0,
                character: 0,
            },
        )
        .unwrap();
        assert_eq!(offset, 0);

        // 第一行 "line" 后
        let offset = DocumentManager::position_to_offset(
            content,
            Position {
                line: 0,
                character: 4,
            },
        )
        .unwrap();
        assert_eq!(offset, 4);

        // 第二行开始
        let offset = DocumentManager::position_to_offset(
            content,
            Position {
                line: 1,
                character: 0,
            },
        )
        .unwrap();
        assert_eq!(offset, 7); // "line 1\n"

        // 第三行开始
        let offset = DocumentManager::position_to_offset(
            content,
            Position {
                line: 2,
                character: 0,
            },
        )
        .unwrap();
        assert_eq!(offset, 14); // "line 1\nline 2\n"
    }

    #[test]
    fn test_char_offset_to_byte_offset() {
        // ASCII
        let line = "hello";
        let offset = DocumentManager::char_offset_to_byte_offset(line, 2).unwrap();
        assert_eq!(offset, 2);

        // UTF-8
        let line = "你好世界";
        let offset = DocumentManager::char_offset_to_byte_offset(line, 2).unwrap();
        assert_eq!(offset, 6); // 每个中文字符 3 字节

        // 行尾
        let offset = DocumentManager::char_offset_to_byte_offset(line, 4).unwrap();
        assert_eq!(offset, 12);
    }

    #[test]
    fn test_multiple_changes() {
        let manager = DocumentManager::new();
        let uri: Url = "file:///test.toml".parse().unwrap();

        manager.open(uri.clone(), 1, "a b c".to_string(), "toml".to_string());

        // 应用多个修改
        let changes = vec![
            TextDocumentContentChangeEvent {
                range: Some(Range {
                    start: Position {
                        line: 0,
                        character: 0,
                    },
                    end: Position {
                        line: 0,
                        character: 1,
                    },
                }),
                range_length: None,
                text: "x".to_string(),
            },
            TextDocumentContentChangeEvent {
                range: Some(Range {
                    start: Position {
                        line: 0,
                        character: 2,
                    },
                    end: Position {
                        line: 0,
                        character: 3,
                    },
                }),
                range_length: None,
                text: "y".to_string(),
            },
        ];

        manager.change(&uri, 2, changes);

        let doc = manager.get(&uri).unwrap();
        // 注意:LSP 的修改是按顺序应用的,每次修改后文档内容会改变
        // 第一次修改: "a b c" -> "x b c"
        // 第二次修改: "x b c" -> "x y c"
        assert_eq!(doc.content, "x y c");
    }
}