pinel-code-editor 0.1.0

Pinel's fork of iced-code-editor: a code editor widget for the Iced GUI framework with syntax highlighting, line numbers, and scrolling support.
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
//! Minimal LSP types and helpers used by the editor.

pub(crate) mod sync;

#[cfg(all(feature = "lsp-process", not(target_arch = "wasm32")))]
pub mod process;

/// A zero-based position in an LSP document.
///
/// Both fields are zero-based, unlike the one-based line numbers shown in the
/// editor's gutter.
///
/// # Example
///
/// ```
/// use iced_code_editor::LspPosition;
///
/// // The gutter's "line 3, column 1" is this position.
/// let position = LspPosition { line: 2, character: 0 };
/// assert_eq!(position.line, 2);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct LspPosition {
    /// Zero-based line index.
    pub line: u32,
    /// Zero-based character index on the line.
    pub character: u32,
}

/// Metadata describing the currently edited document.
///
/// The editor stamps `version` itself: [`CodeEditor::attach_lsp`] sets it to 1
/// on open, and each flushed batch of changes increments it.
///
/// # Example
///
/// ```
/// use iced_code_editor::LspDocument;
///
/// let document = LspDocument::new("file:///tmp/main.rs", "rust");
/// assert_eq!(document.uri, "file:///tmp/main.rs");
/// assert_eq!(document.language_id, "rust");
/// ```
///
/// [`CodeEditor::attach_lsp`]: crate::CodeEditor::attach_lsp
#[derive(Debug, Clone)]
pub struct LspDocument {
    /// Document URI.
    pub uri: String,
    /// Language identifier for syntax services.
    pub language_id: String,
    /// Version number used for LSP change notifications.
    pub version: i32,
}

impl LspDocument {
    /// Creates a new LSP document descriptor with version set to 0.
    ///
    /// # Arguments
    ///
    /// * `uri` - The document URI, e.g. `file:///path/to/main.rs`
    /// * `language_id` - The LSP language identifier, e.g. `rust`
    ///
    /// # Returns
    ///
    /// A descriptor at version 0; the editor stamps version 1 on open
    ///
    /// # Example
    ///
    /// ```
    /// use iced_code_editor::LspDocument;
    ///
    /// let document = LspDocument::new("file:///tmp/main.rs", "rust");
    /// assert_eq!(document.version, 0);
    /// ```
    pub fn new(uri: impl Into<String>, language_id: impl Into<String>) -> Self {
        Self {
            uri: uri.into(),
            language_id: language_id.into(),
            version: 0,
        }
    }
}

/// A text range in an LSP document.
///
/// `start` is inclusive and `end` is exclusive, so an empty range (a pure
/// insertion point) has `start == end`.
///
/// # Example
///
/// ```
/// use iced_code_editor::{LspPosition, LspRange};
///
/// // The first four characters of the first line.
/// let range = LspRange {
///     start: LspPosition { line: 0, character: 0 },
///     end: LspPosition { line: 0, character: 4 },
/// };
/// assert_eq!(range.end.character - range.start.character, 4);
/// ```
#[derive(Debug, Clone, Copy)]
pub struct LspRange {
    /// Range start (inclusive).
    pub start: LspPosition,
    /// Range end (exclusive).
    pub end: LspPosition,
}

/// A text change described by a range replacement.
///
/// An insertion is an empty `range` with non-empty `text`; a deletion is a
/// non-empty `range` with empty `text`.
///
/// # Example
///
/// ```
/// use iced_code_editor::{LspPosition, LspRange, LspTextChange};
///
/// // Insert "hi" at the start of the document: an empty range.
/// let insertion = LspTextChange {
///     range: LspRange {
///         start: LspPosition { line: 0, character: 0 },
///         end: LspPosition { line: 0, character: 0 },
///     },
///     text: "hi".to_string(),
/// };
/// assert_eq!(insertion.range.start, insertion.range.end);
/// ```
#[derive(Debug, Clone)]
pub struct LspTextChange {
    /// Range replaced by the change.
    pub range: LspRange,
    /// Inserted text.
    pub text: String,
}

/// A symbol in a document's outline, as reported by `textDocument/documentSymbol`.
///
/// Symbols form a tree: a method sits in the `children` of its type, which
/// sits in the `children` of its module. Servers that only return a flat list
/// are nested by range containment when the response is parsed.
///
/// # Example
///
/// ```
/// use iced_code_editor::{LspPosition, LspRange, LspSymbol};
///
/// let symbol = LspSymbol {
///     name: "main".to_string(),
///     range: LspRange {
///         start: LspPosition { line: 0, character: 0 },
///         end: LspPosition { line: 2, character: 1 },
///     },
///     children: Vec::new(),
/// };
/// assert!(symbol.contains_line(1));
/// assert!(!symbol.contains_line(3));
/// ```
#[derive(Debug, Clone)]
pub struct LspSymbol {
    /// Name shown for the symbol, such as a function or type name.
    pub name: String,
    /// Full range of the symbol, including its body.
    pub range: LspRange,
    /// Symbols declared inside this one.
    pub children: Vec<LspSymbol>,
}

impl LspSymbol {
    /// Returns `true` if the zero-based `line` falls inside this symbol's range.
    ///
    /// # Arguments
    ///
    /// * `line` - Zero-based line number to test
    ///
    /// # Returns
    ///
    /// `true` when `line` is between the range's start and end lines, inclusive
    pub fn contains_line(&self, line: u32) -> bool {
        self.range.start.line <= line && line <= self.range.end.line
    }
}

/// Returns the names of the symbols enclosing `line`, outermost first.
///
/// At each level the first symbol whose range contains `line` is entered, so
/// the result reads like a breadcrumb trail: `["Editor", "render"]` for a
/// line inside the `render` method of `Editor`.
///
/// # Arguments
///
/// * `symbols` - Top-level symbols of the document
/// * `line` - Zero-based line number
///
/// # Returns
///
/// The chain of enclosing symbol names, empty when `line` is outside every symbol
///
/// # Example
///
/// ```
/// use iced_code_editor::{LspPosition, LspRange, LspSymbol, symbol_path_at};
///
/// let range = |start, end| LspRange {
///     start: LspPosition { line: start, character: 0 },
///     end: LspPosition { line: end, character: 0 },
/// };
/// let symbols = vec![LspSymbol {
///     name: "Editor".to_string(),
///     range: range(0, 10),
///     children: vec![LspSymbol {
///         name: "render".to_string(),
///         range: range(2, 5),
///         children: Vec::new(),
///     }],
/// }];
///
/// assert_eq!(symbol_path_at(&symbols, 3), ["Editor", "render"]);
/// assert_eq!(symbol_path_at(&symbols, 8), ["Editor"]);
/// assert!(symbol_path_at(&symbols, 11).is_empty());
/// ```
pub fn symbol_path_at(symbols: &[LspSymbol], line: u32) -> Vec<String> {
    let mut path = Vec::new();
    let mut level = symbols;
    while let Some(symbol) = level.iter().find(|symbol| symbol.contains_line(line)) {
        path.push(symbol.name.clone());
        level = &symbol.children;
    }
    path
}

/// LSP client hooks invoked by the editor.
///
/// Every method has a no-op default, so an implementation only overrides the
/// notifications and requests it actually cares about. Attach one with
/// [`CodeEditor::attach_lsp`]; the editor never blocks on a response, so
/// request results come back through whatever channel the implementation
/// chooses.
///
/// # Example
///
/// ```
/// use std::cell::RefCell;
/// use std::rc::Rc;
///
/// use iced_code_editor::{CodeEditor, LspClient, LspDocument, LspTextChange};
///
/// /// A client that only records how many change batches it received.
/// struct CountingClient(Rc<RefCell<usize>>);
///
/// impl LspClient for CountingClient {
///     fn did_change(&mut self, _document: &LspDocument, changes: &[LspTextChange]) {
///         *self.0.borrow_mut() += changes.len();
///     }
/// }
///
/// let changes = Rc::new(RefCell::new(0));
/// let mut editor = CodeEditor::new("fn main() {}", "rs");
/// editor.attach_lsp(
///     Box::new(CountingClient(Rc::clone(&changes))),
///     LspDocument::new("file:///tmp/main.rs", "rust"),
/// );
///
/// // `did_open` fired, but no change has been flushed yet.
/// assert_eq!(*changes.borrow(), 0);
/// ```
///
/// [`CodeEditor::attach_lsp`]: crate::CodeEditor::attach_lsp
pub trait LspClient {
    /// Notifies the client that a document was opened.
    fn did_open(&mut self, _document: &LspDocument, _text: &str) {}
    /// Notifies the client that the document changed.
    fn did_change(&mut self, _document: &LspDocument, _changes: &[LspTextChange]) {}
    /// Notifies the client that the document was saved.
    fn did_save(&mut self, _document: &LspDocument, _text: &str) {}
    /// Notifies the client that the document was closed.
    fn did_close(&mut self, _document: &LspDocument) {}
    /// Requests hover information at the given position.
    fn request_hover(&mut self, _document: &LspDocument, _position: LspPosition) {}
    /// Requests completion items at the given position.
    fn request_completion(&mut self, _document: &LspDocument, _position: LspPosition) {}
    /// Requests the definition location(s) for the symbol at the given position.
    ///
    /// This method is called when the user triggers a "Go to Definition" action
    /// (e.g., via Ctrl+Click or a context menu). The client implementation should
    /// send a `textDocument/definition` request to the LSP server.
    fn request_definition(&mut self, _document: &LspDocument, _position: LspPosition) {}
    /// Requests the outline (`textDocument/documentSymbol`) of the document.
    ///
    /// The client implementation should send the request and report the
    /// symbols back through its own channel, as with the other requests.
    fn request_document_symbols(&mut self, _document: &LspDocument) {}
}

/// Computes a minimal text change between two snapshots.
///
/// The change is narrowed by trimming the common prefix and suffix, so a
/// one-character edit in a large document produces a one-character change
/// rather than a whole-document replacement. This is what lets the editor send
/// incremental `didChange` notifications instead of resending the buffer.
///
/// # Arguments
///
/// * `old` - The previous document contents
/// * `new` - The current document contents
///
/// # Returns
///
/// `Some(change)` describing the edit, or `None` when the snapshots are identical
///
/// # Example
///
/// ```
/// use iced_code_editor::compute_text_change;
///
/// // Identical snapshots produce no change at all.
/// assert!(compute_text_change("fn main() {}", "fn main() {}").is_none());
///
/// // Only the differing region is reported, not the whole line.
/// let change = compute_text_change("let x = 1;", "let x = 42;")
///     .expect("the snapshots differ");
/// assert_eq!(change.text, "42");
/// assert_eq!(change.range.start.character, 8);
/// assert_eq!(change.range.end.character, 9);
/// ```
pub fn compute_text_change(old: &str, new: &str) -> Option<LspTextChange> {
    if old == new {
        return None;
    }

    let old_chars: Vec<char> = old.chars().collect();
    let new_chars: Vec<char> = new.chars().collect();
    let old_len = old_chars.len();
    let new_len = new_chars.len();

    let mut prefix = 0;
    while prefix < old_len && prefix < new_len && old_chars[prefix] == new_chars[prefix] {
        prefix += 1;
    }

    let mut suffix = 0;
    while suffix < old_len.saturating_sub(prefix)
        && suffix < new_len.saturating_sub(prefix)
        && old_chars[old_len - 1 - suffix] == new_chars[new_len - 1 - suffix]
    {
        suffix += 1;
    }

    let removed_len = old_len.saturating_sub(prefix + suffix);
    let inserted: String = new_chars[prefix..new_len.saturating_sub(suffix)].iter().collect();

    let start = position_for_char_index(old, prefix);
    let end = position_for_char_index(old, prefix + removed_len);

    Some(LspTextChange {
        range: LspRange { start, end },
        text: inserted,
    })
}

/// Converts a character index into a line/character position.
fn position_for_char_index(text: &str, target_index: usize) -> LspPosition {
    let mut line: u32 = 0;
    let mut character: u32 = 0;
    for (index, ch) in text.chars().enumerate() {
        if index == target_index {
            return LspPosition { line, character };
        }
        if ch == '\n' {
            line = line.saturating_add(1);
            character = 0;
        } else {
            character = character.saturating_add(1);
        }
    }

    LspPosition { line, character }
}

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

    fn symbol(name: &str, start: u32, end: u32, children: Vec<LspSymbol>) -> LspSymbol {
        LspSymbol {
            name: name.to_string(),
            range: LspRange {
                start: LspPosition {
                    line: start,
                    character: 0,
                },
                end: LspPosition {
                    line: end,
                    character: 0,
                },
            },
            children,
        }
    }

    #[test]
    fn test_symbol_path_at_picks_matching_sibling() {
        let symbols = vec![
            symbol("first", 0, 3, Vec::new()),
            symbol("second", 5, 9, vec![symbol("inner", 6, 7, Vec::new())]),
        ];
        assert_eq!(symbol_path_at(&symbols, 6), ["second", "inner"]);
        assert_eq!(symbol_path_at(&symbols, 9), ["second"]);
        assert_eq!(symbol_path_at(&symbols, 2), ["first"]);
    }

    #[test]
    fn test_symbol_path_at_between_symbols_is_empty() {
        let symbols = vec![
            symbol("first", 0, 3, Vec::new()),
            symbol("second", 5, 9, Vec::new()),
        ];
        assert!(symbol_path_at(&symbols, 4).is_empty());
        assert!(symbol_path_at(&[], 0).is_empty());
    }

    #[test]
    fn test_compute_text_change_none_when_equal() {
        let change = compute_text_change("abc", "abc");
        assert!(change.is_none());
    }

    #[test]
    fn test_compute_text_change_insertion() {
        let change = compute_text_change("abc", "abXc");
        assert!(change.is_some());
        if let Some(change) = change {
            assert_eq!(change.text, "X");
            assert_eq!(
                change.range.start,
                LspPosition {
                    line: 0,
                    character: 2
                }
            );
            assert_eq!(
                change.range.end,
                LspPosition {
                    line: 0,
                    character: 2
                }
            );
        }
    }

    #[test]
    fn test_compute_text_change_deletion_across_lines() {
        let change = compute_text_change("a\nbc", "a\nc");
        assert!(change.is_some());
        if let Some(change) = change {
            assert_eq!(change.text, "");
            assert_eq!(
                change.range.start,
                LspPosition {
                    line: 1,
                    character: 0
                }
            );
            assert_eq!(
                change.range.end,
                LspPosition {
                    line: 1,
                    character: 1
                }
            );
        }
    }

    #[test]
    fn test_position_for_char_index_end_of_text() {
        let pos = position_for_char_index("a\nb", 3);
        assert_eq!(
            pos,
            LspPosition {
                line: 1,
                character: 1
            }
        );
    }
}