use crate::buffer::TextBuffer;
use crate::canvas_editor::CodeEditor;
use crate::canvas_editor::lsp;
impl CodeEditor {
pub fn attach_lsp(
&mut self,
mut client: Box<dyn lsp::LspClient>,
document: lsp::LspDocument,
) {
if !self.lsp_enabled {
return;
}
let (document, text) =
open_lsp_document(client.as_mut(), &self.buffer, document);
self.lsp_client = Some(client);
self.lsp_document = Some(document);
self.reset_lsp_shadow_state(text);
}
pub fn lsp_open_document(&mut self, document: lsp::LspDocument) {
let Some(client) = self.lsp_client.as_mut() else { return };
if let Some(current) = self.lsp_document.as_ref() {
client.did_close(current);
}
let (document, text) =
open_lsp_document(client.as_mut(), &self.buffer, document);
self.lsp_document = Some(document);
self.reset_lsp_shadow_state(text);
}
fn reset_lsp_shadow_state(&mut self, text: String) {
self.lsp_shadow_text = text;
self.lsp_shadow_is_current = true;
self.update_lsp_synced_extent();
self.lsp_edit_snapshot = None;
self.lsp_pending_changes.clear();
}
fn with_lsp<R>(
&mut self,
f: impl FnOnce(&mut dyn lsp::LspClient, &lsp::LspDocument) -> R,
) -> Option<R> {
let client = self.lsp_client.as_deref_mut()?;
let document = self.lsp_document.as_ref()?;
Some(f(client, document))
}
fn with_lsp_mut_document<R>(
&mut self,
f: impl FnOnce(&mut dyn lsp::LspClient, &mut lsp::LspDocument) -> R,
) -> Option<R> {
let client = self.lsp_client.as_deref_mut()?;
let document = self.lsp_document.as_mut()?;
Some(f(client, document))
}
pub fn detach_lsp(&mut self) {
self.with_lsp(|client, document| client.did_close(document));
self.lsp_client = None;
self.lsp_document = None;
self.lsp_shadow_text = String::new();
self.lsp_shadow_is_current = true;
self.lsp_synced_line_count = 1;
self.lsp_synced_last_line_len = 0;
self.lsp_edit_snapshot = None;
self.lsp_pending_changes.clear();
}
pub fn lsp_did_save(&mut self) {
if self.lsp_client.is_none() || self.lsp_document.is_none() {
return;
}
let text = self.buffer.to_string();
self.with_lsp(|client, document| client.did_save(document, &text));
}
pub fn lsp_request_hover(&mut self) {
let position = self.lsp_position_from_cursor();
self.with_lsp(|client, document| {
client.request_hover(document, position);
});
}
pub fn lsp_request_hover_at(&mut self, point: iced::Point) -> bool {
let Some(position) = self.lsp_position_from_point(point) else {
return false;
};
self.with_lsp(|client, document| {
client.request_hover(document, position);
})
.is_some()
}
pub fn lsp_request_hover_at_position(
&mut self,
position: lsp::LspPosition,
) -> bool {
self.with_lsp(|client, document| {
client.request_hover(document, position);
})
.is_some()
}
pub fn lsp_position_at_point(
&self,
point: iced::Point,
) -> Option<lsp::LspPosition> {
self.lsp_position_from_point(point)
}
pub fn lsp_hover_anchor_at_point(
&self,
point: iced::Point,
) -> Option<(lsp::LspPosition, iced::Point)> {
let (line, col) = self.calculate_cursor_from_point(point)?;
let line_content = self.buffer.line(line);
let anchor_col = Self::word_start_in_line(line_content, col);
let anchor_point =
self.point_from_position(line, anchor_col).unwrap_or(point);
let line = u32::try_from(line).unwrap_or(u32::MAX);
let character = u32::try_from(anchor_col).unwrap_or(u32::MAX);
Some((lsp::LspPosition { line, character }, anchor_point))
}
pub fn lsp_request_completion(&mut self) {
let position = self.lsp_position_from_cursor();
self.with_lsp(|client, document| {
client.request_completion(document, position);
});
}
pub fn lsp_flush_pending_changes(&mut self) {
if self.lsp_pending_changes.is_empty() {
return;
}
if self.lsp_client.is_none() || self.lsp_document.is_none() {
return;
}
let changes = std::mem::take(&mut self.lsp_pending_changes);
self.with_lsp_mut_document(|client, document| {
document.version = document.version.saturating_add(1);
client.did_change(document, &changes);
});
}
pub fn set_lsp_auto_flush(&mut self, auto_flush: bool) {
self.lsp_auto_flush = auto_flush;
}
fn lsp_position_from_cursor(&self) -> lsp::LspPosition {
let pos = self.cursors.primary_position();
let line = u32::try_from(pos.0).unwrap_or(u32::MAX);
let character = u32::try_from(pos.1).unwrap_or(u32::MAX);
lsp::LspPosition { line, character }
}
fn lsp_position_from_point(
&self,
point: iced::Point,
) -> Option<lsp::LspPosition> {
let (line, col) = self.calculate_cursor_from_point(point)?;
let line = u32::try_from(line).unwrap_or(u32::MAX);
let character = u32::try_from(col).unwrap_or(u32::MAX);
Some(lsp::LspPosition { line, character })
}
pub(crate) fn enqueue_lsp_change(&mut self) {
if self.lsp_document.is_none() {
return;
}
let new_text = self.buffer.to_string();
let change = if self.lsp_shadow_is_current {
lsp::compute_text_change(&self.lsp_shadow_text, &new_text)
} else {
let end_line = self.lsp_synced_line_count.saturating_sub(1);
Some(lsp::LspTextChange {
range: lsp::LspRange {
start: lsp::LspPosition { line: 0, character: 0 },
end: lsp::LspPosition {
line: u32::try_from(end_line).unwrap_or(u32::MAX),
character: u32::try_from(self.lsp_synced_last_line_len)
.unwrap_or(u32::MAX),
},
},
text: new_text.clone(),
})
};
if let Some(change) = change {
self.lsp_pending_changes.push(change);
}
self.lsp_shadow_text = new_text;
self.lsp_shadow_is_current = true;
self.update_lsp_synced_extent();
if self.lsp_auto_flush {
self.lsp_flush_pending_changes();
}
}
pub(crate) fn enqueue_incremental_lsp_change(&mut self) {
if self.lsp_document.is_none() {
self.lsp_edit_snapshot = None;
return;
}
let Some(snapshot) = self.lsp_edit_snapshot.take() else {
self.enqueue_lsp_change();
return;
};
let new_line_count = self.buffer.line_count();
let start_line =
snapshot.start_line.min(new_line_count.saturating_sub(1));
let new_end_exclusive = if new_line_count >= snapshot.old_line_count {
snapshot
.old_end_exclusive
.saturating_add(new_line_count - snapshot.old_line_count)
.min(new_line_count)
} else {
snapshot
.old_end_exclusive
.saturating_sub(snapshot.old_line_count - new_line_count)
.max(start_line.saturating_add(1))
.min(new_line_count)
};
let text =
self.buffer.line_range_to_string(start_line, new_end_exclusive);
self.lsp_pending_changes.push(lsp::LspTextChange {
range: lsp::LspRange {
start: lsp::LspPosition {
line: u32::try_from(snapshot.start_line)
.unwrap_or(u32::MAX),
character: 0,
},
end: snapshot.old_end,
},
text,
});
self.lsp_shadow_text = String::new();
self.lsp_shadow_is_current = false;
self.update_lsp_synced_extent();
if self.lsp_auto_flush {
self.lsp_flush_pending_changes();
}
}
fn update_lsp_synced_extent(&mut self) {
self.lsp_synced_line_count = self.buffer.line_count();
self.lsp_synced_last_line_len =
self.buffer.line_len(self.lsp_synced_line_count.saturating_sub(1));
}
pub fn set_lsp_enabled(&mut self, enabled: bool) {
self.lsp_enabled = enabled;
if !enabled {
self.detach_lsp();
}
}
pub fn lsp_enabled(&self) -> bool {
self.lsp_enabled
}
pub fn lsp_request_definition(&mut self) {
let position = self.lsp_position_from_cursor();
self.with_lsp(|client, document| {
client.request_definition(document, position);
});
}
pub fn lsp_request_definition_at(&mut self, point: iced::Point) -> bool {
let Some(position) = self.lsp_position_from_point(point) else {
return false;
};
self.with_lsp(|client, document| {
client.request_definition(document, position);
})
.is_some()
}
}
fn open_lsp_document(
client: &mut dyn lsp::LspClient,
buffer: &TextBuffer,
mut document: lsp::LspDocument,
) -> (lsp::LspDocument, String) {
document.version = 1;
let text = buffer.to_string();
client.did_open(&document, &text);
(document, text)
}
#[cfg(test)]
mod tests {
use std::cell::RefCell;
use std::rc::Rc;
use crate::canvas_editor::Message;
use super::*;
#[derive(Default)]
struct TestLspClient {
changes: Rc<RefCell<Vec<Vec<lsp::LspTextChange>>>>,
}
impl lsp::LspClient for TestLspClient {
fn did_change(
&mut self,
_document: &lsp::LspDocument,
changes: &[lsp::LspTextChange],
) {
self.changes.borrow_mut().push(changes.to_vec());
}
}
#[derive(Default)]
struct RecordingLspClient {
calls: Rc<RefCell<Vec<String>>>,
}
impl lsp::LspClient for RecordingLspClient {
fn did_open(&mut self, _document: &lsp::LspDocument, _text: &str) {
self.calls.borrow_mut().push("did_open".to_string());
}
fn did_close(&mut self, _document: &lsp::LspDocument) {
self.calls.borrow_mut().push("did_close".to_string());
}
fn did_save(&mut self, _document: &lsp::LspDocument, _text: &str) {
self.calls.borrow_mut().push("did_save".to_string());
}
fn request_hover(
&mut self,
_document: &lsp::LspDocument,
_position: lsp::LspPosition,
) {
self.calls.borrow_mut().push("request_hover".to_string());
}
fn did_change(
&mut self,
_document: &lsp::LspDocument,
_changes: &[lsp::LspTextChange],
) {
self.calls.borrow_mut().push("did_change".to_string());
}
}
#[test]
fn test_enqueue_lsp_change_auto_flush() {
let changes = Rc::new(RefCell::new(Vec::new()));
let client = TestLspClient { changes: Rc::clone(&changes) };
let mut editor = CodeEditor::new("hello", "rs");
editor.attach_lsp(
Box::new(client),
lsp::LspDocument::new("file:///test.rs", "rust"),
);
editor.set_lsp_auto_flush(true);
editor.buffer.insert_char(0, 5, '!');
editor.enqueue_lsp_change();
let changes = changes.borrow();
assert_eq!(changes.len(), 1);
assert_eq!(changes[0].len(), 1);
let change = &changes[0][0];
assert_eq!(change.text, "!");
assert_eq!(change.range.start.line, 0);
assert_eq!(change.range.start.character, 5);
assert_eq!(change.range.end.line, 0);
assert_eq!(change.range.end.character, 5);
}
#[test]
fn test_editor_update_sends_bounded_incremental_lsp_change() {
let changes = Rc::new(RefCell::new(Vec::new()));
let client = TestLspClient { changes: Rc::clone(&changes) };
let content = (0..10)
.map(|line| format!("line{line}"))
.collect::<Vec<_>>()
.join("\n");
let mut editor = CodeEditor::new(&content, "rs");
editor.attach_lsp(
Box::new(client),
lsp::LspDocument::new("file:///large.rs", "rust"),
);
editor.request_focus();
editor.has_canvas_focus = true;
editor.focus_locked = false;
editor.cursors.primary_mut().position = (5, 2);
let _ = editor.update(&Message::CharacterInput('X'));
let changes = changes.borrow();
assert_eq!(changes.len(), 1);
assert_eq!(changes[0].len(), 1);
let change = &changes[0][0];
assert_eq!(change.range.start.line, 4);
assert_eq!(change.range.start.character, 0);
assert_eq!(change.range.end.line, 7);
assert_eq!(change.range.end.character, 0);
assert_eq!(change.text, "line4\nliXne5\nline6\n");
assert!(!editor.lsp_shadow_is_current);
assert!(editor.lsp_shadow_text.is_empty());
}
#[test]
fn test_open_lsp_document_stamps_version_and_sends_did_open() {
let calls = Rc::new(RefCell::new(Vec::new()));
let mut client = RecordingLspClient { calls: Rc::clone(&calls) };
let buffer = TextBuffer::new("hello");
let document = lsp::LspDocument::new("file:///test.rs", "rust");
let (document, text) =
open_lsp_document(&mut client, &buffer, document);
assert_eq!(document.version, 1);
assert_eq!(text, "hello");
assert_eq!(calls.borrow().as_slice(), ["did_open"]);
}
#[test]
fn test_with_lsp_runs_closure_when_client_and_document_present() {
let calls = Rc::new(RefCell::new(Vec::new()));
let client = RecordingLspClient { calls: Rc::clone(&calls) };
let mut editor = CodeEditor::new("hello", "rs");
editor.attach_lsp(
Box::new(client),
lsp::LspDocument::new("file:///test.rs", "rust"),
);
calls.borrow_mut().clear();
let ran = editor.with_lsp(|client, document| {
client.did_save(document, "hello");
});
assert!(ran.is_some());
assert_eq!(calls.borrow().as_slice(), ["did_save"]);
}
#[test]
fn test_with_lsp_returns_none_when_client_absent() {
let mut editor = CodeEditor::new("hello", "rs");
let ran = editor.with_lsp(|_client, _document| {});
assert!(ran.is_none());
}
#[test]
fn test_lsp_did_save_sends_current_buffer_contents() {
let calls = Rc::new(RefCell::new(Vec::new()));
let client = RecordingLspClient { calls: Rc::clone(&calls) };
let mut editor = CodeEditor::new("hello", "rs");
editor.attach_lsp(
Box::new(client),
lsp::LspDocument::new("file:///test.rs", "rust"),
);
calls.borrow_mut().clear();
editor.lsp_did_save();
assert_eq!(calls.borrow().as_slice(), ["did_save"]);
}
#[test]
fn test_lsp_did_save_is_a_noop_without_an_attached_client() {
let mut editor = CodeEditor::new("hello", "rs");
editor.lsp_did_save(); }
#[test]
fn test_with_lsp_returns_none_when_document_absent() {
let calls = Rc::new(RefCell::new(Vec::new()));
let client = RecordingLspClient { calls: Rc::clone(&calls) };
let mut editor = CodeEditor::new("hello", "rs");
editor.attach_lsp(
Box::new(client),
lsp::LspDocument::new("file:///test.rs", "rust"),
);
editor.lsp_document = None;
let ran = editor.with_lsp(|_client, _document| {});
assert!(ran.is_none());
}
#[test]
#[allow(clippy::unwrap_used)]
fn test_with_lsp_mut_document_allows_version_bump() {
let calls = Rc::new(RefCell::new(Vec::new()));
let client = RecordingLspClient { calls: Rc::clone(&calls) };
let mut editor = CodeEditor::new("hello", "rs");
editor.attach_lsp(
Box::new(client),
lsp::LspDocument::new("file:///test.rs", "rust"),
);
editor.with_lsp_mut_document(|_client, document| {
document.version = document.version.saturating_add(1);
});
assert_eq!(editor.lsp_document.as_ref().unwrap().version, 2);
}
#[test]
fn test_lsp_flush_pending_changes_preserves_queue_when_no_client_attached()
{
let mut editor = CodeEditor::new("hello", "rs");
editor.lsp_pending_changes.push(lsp::LspTextChange {
range: lsp::LspRange {
start: lsp::LspPosition { line: 0, character: 0 },
end: lsp::LspPosition { line: 0, character: 0 },
},
text: "x".to_string(),
});
editor.lsp_flush_pending_changes();
assert_eq!(editor.lsp_pending_changes.len(), 1);
}
}