diff --git a/README.md b/README.md index b1a26f2..b998400 100644 --- a/README.md +++ b/README.md @@ -49,8 +49,8 @@ definition and diagnostics when they're installed (`clangd`, `rust-analyzer`, `p `typescript-language-server`, `bash-language-server`, `lua-language-server`). Completions appear as you type and with `Ctrl+Space`. With no working language server they come from -words in the file. `~/.cargo/bin/rust-analyzer` is only a rustup placeholder until you run -`rustup component add rust-analyzer`. +words in the file. On rustup, `~/.cargo/bin/rust-analyzer` is only a placeholder until you run +`rustup component add rust-analyzer`, and a project's first completions wait while it indexes. Colours follow your desktop (Caelestia's scheme or the KDE accent), or a colour you pick, and change live. diff --git a/src/lsp.rs b/src/lsp.rs index 66be58b..78a66e0 100644 --- a/src/lsp.rs +++ b/src/lsp.rs @@ -264,8 +264,21 @@ pub fn parse_hover(res: &Value) -> Option { Value::Array(a) => a.iter().filter_map(|x| x.as_str().map(str::to_string).or_else(|| x.get("value")?.as_str().map(str::to_string))).collect::>().join("\n"), v => v.get("value")?.as_str()?.to_string(), }; - let t = t.trim(); - (!t.is_empty()).then(|| t.to_string()) + let t = plain_hover(&t); + (!t.is_empty()).then_some(t) +} + +/// Servers send markdown whatever is asked for. The card shows plain text, so the code fences go +/// and a `---` rule becomes a blank line. +fn plain_hover(t: &str) -> String { + let lines: Vec<&str> = t.lines().filter(|l| !l.trim_start().starts_with("```")).map(|l| if l.trim() == "---" { "" } else { l }).collect(); + let mut out: Vec<&str> = vec![]; + for l in lines { + if !(l.trim().is_empty() && out.last().is_none_or(|p| p.trim().is_empty())) { + out.push(l); + } + } + out.join("\n").trim().to_string() } pub fn parse_definition(res: &Value) -> Option { @@ -292,6 +305,8 @@ mod tests { let v = parse_completion(&c); assert_eq!((v[0].label.as_str(), v[0].insert.as_str()), ("push", "push")); assert_eq!(parse_hover(&json!({"contents": {"kind": "markdown", "value": "fn a()"}})).unwrap(), "fn a()"); + let md = "```rust\nt\n```\n\n```rust\nfn helper(n: u32) -> u32\n```\n\n---\n\nDocs here"; + assert_eq!(parse_hover(&json!({"contents": {"kind": "markdown", "value": md}})).unwrap(), "t\n\nfn helper(n: u32) -> u32\n\nDocs here"); let d = parse_diags(&json!({"diagnostics": [{"range": {"start": {"line": 3, "character": 1}, "end": {"line": 3, "character": 4}}, "severity": 2, "message": "m"}]})); assert_eq!(d[0].line, 3); assert_eq!(d[0].severity, 2); diff --git a/tests/features.rs b/tests/features.rs index 0d73ec1..b775338 100644 --- a/tests/features.rs +++ b/tests/features.rs @@ -4,7 +4,7 @@ use damson::app::App; use damson::ui::commands::Cmd; -use eframe::egui::{self, vec2, Event, Key, Modifiers}; +use eframe::egui::{vec2, Event, Key, Modifiers}; use egui_kittest::Harness; use std::path::{Path, PathBuf}; use std::time::{Duration, Instant}; @@ -188,6 +188,39 @@ fn rust_analyzer_live() { key(&mut h, Key::F12, Modifiers::NONE); wait(&mut h, 30, "go to definition", |h| pos(h).0 == 0); wait(&mut h, 60, "type error shows", |h| h.state().problem_totals().0 > 0); + + caret(&mut h, 7, 13); + key(&mut h, Key::I, CTRL); + wait(&mut h, 20, "hover", |h| h.state().hover.is_some()); + let t = h.state().hover.as_ref().unwrap().0.clone(); + println!(" hover: {:?}", t.lines().filter(|l| !l.trim().is_empty()).take(3).collect::>()); + assert!(t.contains("fn helper") && !t.contains("```"), "{t}"); + key(&mut h, Key::Escape, Modifiers::NONE); + + // typing after the dot brings the list up, and it narrows; Enter takes the top match + caret(&mut h, 6, 6); + typed(&mut h, "pu"); + wait(&mut h, 15, "members while typing", |h| labels(h).iter().any(|l| l.starts_with("push"))); + println!(" v.pu: {:?}", &labels(&h)[..labels(&h).len().min(4)]); + let want = labels(&h).iter().position(|l| l.starts_with("push")).expect("push is listed"); + for _ in 0..want { + key(&mut h, Key::ArrowDown, Modifiers::NONE); + } + settle(&mut h, 2); + println!(" selected: {:?}", h.state().complete.as_ref().map(|c| c.items[c.sel].label.clone())); + key(&mut h, Key::Enter, Modifiers::NONE); + settle(&mut h, 3); + println!(" accepted: {:?}", text(&h).lines().nth(6)); + assert!(text(&h).lines().nth(6).unwrap().trim_start().starts_with("v.push"), "{}", text(&h)); + + // a path after :: + caret(&mut h, 7, 0); + key(&mut h, Key::Enter, Modifiers::NONE); + caret(&mut h, 7, 0); + typed(&mut h, " let m = std::co"); + wait(&mut h, 15, "paths while typing", |h| labels(h).iter().any(|l| l.starts_with("collections"))); + + key(&mut h, Key::Escape, Modifiers::NONE); std::fs::remove_dir_all(dir).ok(); }