Fixed bugs

This commit is contained in:
2026-06-21 23:14:07 +02:00
parent 6a7c45ffbe
commit 5054185e0d
2 changed files with 28 additions and 18 deletions
+26 -8
View File
@@ -80,6 +80,18 @@ fn byte_offset_from_lsp(source: &str, line: u32, character: u32) -> Option<usize
} }
} }
/// Back up to the start of the current identifier/word so the marker isn't
/// injected in the middle of a token (which would break parsing).
fn snap_to_word_start(source: &str, offset: usize) -> usize {
let before = &source[..offset];
let trim = before
.chars()
.rev()
.take_while(|c| c.is_alphanumeric() || *c == '_')
.count();
offset - trim
}
fn find_marker_position(content: &str, marker: &str) -> Option<Position> { fn find_marker_position(content: &str, marker: &str) -> Option<Position> {
let idx = content.find(marker)?; let idx = content.find(marker)?;
let before = &content[..idx]; let before = &content[..idx];
@@ -149,6 +161,7 @@ impl Backend {
let marker = format!("__mist_mk{id:x}__"); let marker = format!("__mist_mk{id:x}__");
let offset = byte_offset_from_lsp(source, line, character)?; let offset = byte_offset_from_lsp(source, line, character)?;
let offset = snap_to_word_start(source, offset);
let mut modified = String::with_capacity(source.len() + marker.len() + 4); let mut modified = String::with_capacity(source.len() + marker.len() + 4);
modified.push_str(&source[..offset]); modified.push_str(&source[..offset]);
@@ -373,14 +386,19 @@ impl LanguageServer for Backend {
if let Some(root) = &*ws.lock().await { if let Some(root) = &*ws.lock().await {
let src_root = root.join("src"); let src_root = root.join("src");
if let Err(e) = ra.lock().await.initialize(root).await { // Hold the lock across both initialize + initialized so the
eprintln!("Failed to initialize rust-analyzer: {e}"); // editor's didOpen cannot race in-between and send notifications
return; // to rust-analyzer before it has received Initialized.
} {
let mut guard = ra.lock().await;
if let Err(e) = ra.lock().await.initialized().await { if let Err(e) = guard.initialize(root).await {
eprintln!("rust-analyzer initialized failed: {e}"); eprintln!("Failed to initialize rust-analyzer: {e}");
return; return;
}
if let Err(e) = guard.initialized().await {
eprintln!("rust-analyzer initialized failed: {e}");
return;
}
} }
let mut files = Vec::new(); let mut files = Vec::new();
+2 -10
View File
@@ -47,16 +47,8 @@ async fn send_lsp_message<W: AsyncWriteExt + Unpin>(
) -> std::io::Result<()> { ) -> std::io::Result<()> {
let payload = serde_json::to_string(value)?; let payload = serde_json::to_string(value)?;
let frame = format!("Content-Length: {}\r\n\r\n{}", payload.len(), payload); let frame = format!("Content-Length: {}\r\n\r\n{}", payload.len(), payload);
eprintln!("[send] writing {} bytes to child stdin", frame.len()); writer.write_all(frame.as_bytes()).await?;
if let Err(e) = writer.write_all(frame.as_bytes()).await { writer.flush().await?;
eprintln!("[send] write_all failed: {e}");
return Err(e);
}
if let Err(e) = writer.flush().await {
eprintln!("[send] flush failed: {e}");
return Err(e);
}
eprintln!("[send] write OK");
Ok(()) Ok(())
} }