//! Code assistance from the language server (M3.4): `lsp.completion`, //! `lsp.hover` and `lsp.signatureHelp`. //! //! The mechanism, off the engine lock: //! 1. Under the lock the command checks the buffer and the position, runs //! the LSP pump once (so a buffer opened a moment ago is opened on the //! server first) and queues a closure with [`crate::lsp::submit`]. That //! is all the lock is held for. //! 2. The closure runs on the session thread after every `didChange` //! queued so far, so the server answers for the text the caller sees. //! It sends the request with [`Server::request_with`]; the reply //! callback (on a client thread, still off the lock) converts the //! answer into Lutra's shape, so even a 20,000-item list is never //! converted under the lock, and sends it into the channel of a //! [`Pending`] (M3.5's mechanism, `crate::command`). //! 3. The command returns [`Outcome::pending`]. The host releases the //! engine lock, waits ([`Pending::wait`]) and resolves the answer //! ([`crate::Engine::resolve`], via [`crate::run_waiting`]); the desktop //! runs these commands through the `engine_execute_waiting` IPC, on a //! worker thread, so the window never waits either. //! //! Without a session (no project, toolchain not ready, server failed) the //! commands fail with `failed`; a server that does not offer the feature, //! and a buffer that is not PHP, give the empty result. Server answers are //! untrusted: every list and string is capped, unknown shapes become the //! empty result. use std::path::Path; use std::time::Duration; use lutra_lsp::types::position_to_json; use lutra_lsp::uri::path_to_uri; use lutra_lsp::{LspError, Range, Server}; use lutra_text::{ColumnUnit, Position}; use serde::Deserialize; use serde_json::{Map, Value, json}; use super::params::parse; use crate::buffers::BufferId; use crate::{Command, CommandSpec, EngineError, Outcome, Pending, Registry, Reply, State}; /// Most completion items returned; a longer list is cut and marked /// `incomplete`, so the editor asks again as the user types. pub const MAX_ITEMS: usize = 500; /// Most items of a server answer looked at (the rest is dropped unread). const MAX_SCANNED: usize = 20_000; /// Longest label, detail or sort text kept, in bytes. const MAX_SHORT: usize = 1024; /// Longest documentation, insert text or hover kept, in bytes. const MAX_LONG: usize = 64 * 1024; /// Most additional text edits (imports) kept per item. const MAX_EXTRA_EDITS: usize = 32; /// Most signatures, and parameters per signature, kept. const MAX_SIGNATURES: usize = 32; /// How long the server gets to answer. pub const REQUEST_TIMEOUT: Duration = Duration::from_secs(10); /// How long a host waits for the result: the request timeout plus slack /// for the queue in front of it ([`Pending::wait`] adds one more second). const WAIT_TIMEOUT: Duration = Duration::from_secs(11); /// Registers the commands of this module. /// /// # Errors /// [`EngineError::Internal`] if an id is already taken. pub fn register(registry: &mut Registry) -> Result<(), EngineError> { registry.register(Box::new(Completion))?; registry.register(Box::new(Hover))?; registry.register(Box::new(SignatureHelp))?; Ok(()) } /// A position in params: 0-based line, UTF-16 column. #[derive(Debug, Clone, Copy, Deserialize)] #[serde(deny_unknown_fields)] struct PositionParam { /// Line. line: usize, /// Column in UTF-16 code units. column: usize, } /// Params of `lsp.completion`. #[derive(Deserialize)] #[serde(deny_unknown_fields, rename_all = "camelCase")] struct CompletionParams { /// Buffer id. id: BufferId, /// The cursor. position: PositionParam, /// The character that triggered the completion, if typing did. #[serde(default)] trigger_character: Option, } /// Params of `lsp.hover` and `lsp.signatureHelp`. #[derive(Deserialize)] #[serde(deny_unknown_fields)] struct AtParams { /// Buffer id. id: BufferId, /// The position. position: PositionParam, } /// `lsp.completion`: completion items at a position. pub struct Completion; impl Command for Completion { fn spec(&self) -> &CommandSpec { &CommandSpec { id: "lsp.completion", label: "Complete Code", menu_path: None, default_shortcut: None, params: "{ id: number, position: { line, column }, triggerCharacter?: string }; returns { items: [{ label, kind, detail?, documentation? (markdown), insertText, textEdit?: { range: { start: { line, column }, end }, newText }, additionalTextEdits?: [{ range, newText }], sortText?, filterText?, deprecated? }], incomplete } with at most 500 items sorted by sortText; incomplete: ask again as the user types; columns are UTF-16; kind: text|method|function|constructor|field|variable|class|interface|module|property|unit|value|enum|keyword|snippet|color|file|reference|folder|enumMember|constant|struct|event|operator|typeParameter; fails without a running language server", } } fn enabled(&self, state: &State) -> bool { state.has_project() } fn run(&self, state: &mut State, params: Value) -> Result { let command = self.spec().id; let p: CompletionParams = parse(command, params)?; let trigger = p.trigger_character.filter(|c| c.chars().count() == 1); let empty = json!({ "items": [], "incomplete": false }); ask( state, Ask { command, id: p.id, position: p.position, method: "textDocument/completion", provider: "completionProvider", empty, context: Some(match trigger { Some(character) => json!({"triggerKind": 2, "triggerCharacter": character}), None => json!({"triggerKind": 1}), }), convert: completion_result, }, ) } } /// `lsp.hover`: documentation of the symbol at a position. pub struct Hover; impl Command for Hover { fn spec(&self) -> &CommandSpec { &CommandSpec { id: "lsp.hover", label: "Show Quick Documentation", menu_path: None, default_shortcut: None, params: "{ id: number, position: { line, column } }; returns { contents: string (markdown, empty when there is nothing), range?: { start: { line, column }, end } }; columns are UTF-16; fails without a running language server", } } fn enabled(&self, state: &State) -> bool { state.has_project() } fn run(&self, state: &mut State, params: Value) -> Result { let command = self.spec().id; let p: AtParams = parse(command, params)?; ask( state, Ask { command, id: p.id, position: p.position, method: "textDocument/hover", provider: "hoverProvider", empty: json!({ "contents": "" }), context: None, convert: hover_result, }, ) } } /// `lsp.signatureHelp`: the signatures of the call around a position. pub struct SignatureHelp; impl Command for SignatureHelp { fn spec(&self) -> &CommandSpec { &CommandSpec { id: "lsp.signatureHelp", label: "Show Parameter Info", menu_path: None, default_shortcut: None, params: "{ id: number, position: { line, column } }; returns { signatures: [{ label, documentation? (markdown), parameters: [{ label: [start, end] (UTF-16 offsets into the signature label) | null, documentation? }], activeParameter? }], activeSignature, activeParameter }, signatures empty outside a call; fails without a running language server", } } fn enabled(&self, state: &State) -> bool { state.has_project() } fn run(&self, state: &mut State, params: Value) -> Result { let command = self.spec().id; let p: AtParams = parse(command, params)?; ask( state, Ask { command, id: p.id, position: p.position, method: "textDocument/signatureHelp", provider: "signatureHelpProvider", empty: json!({ "signatures": [], "activeSignature": 0, "activeParameter": 0 }), context: None, convert: signature_result, }, ) } } /// One request to the language server, as the commands describe it. struct Ask { /// The command id, for errors. command: &'static str, /// The buffer. id: BufferId, /// The position in it. position: PositionParam, /// The LSP method. method: &'static str, /// The server capability that must be present. provider: &'static str, /// The result when there is nothing to ask. empty: Value, /// The request's `context` member, if any. context: Option, /// Converts the server's answer into the command's result. convert: fn(&Value) -> Value, } /// Validates the request, queues it on the session (see the module docs) /// and returns the pending result. /// /// # Errors /// [`EngineError::Failed`] for an unknown buffer or without a language /// server session; [`EngineError::InvalidParams`] for a position outside /// the buffer or inside a surrogate pair. fn ask(state: &mut State, ask: Ask) -> Result { let Ask { command, id, position, method, provider, empty, context, convert, } = ask; let open = state .buffers .get(id) .ok_or_else(|| EngineError::Failed(format!("{command}: no buffer {id}")))?; let at = Position::new(position.line, position.column); open.buffer .position_to_offset(at, ColumnUnit::Utf16) .map_err(|e| EngineError::invalid_params(command, format!("position: {e}")))?; if !is_php(&open.path) { return Ok(Outcome::value(empty)); } let Some(root) = state .project .as_ref() .map(|w| w.project.root().to_path_buf()) else { return Ok(Outcome::value(empty)); }; let path = root.join(&open.path); let uri = path_to_uri(&path).map_err(|e| EngineError::Failed(format!("{command}: {e}")))?; let mut request = json!({ "textDocument": { "uri": uri }, "position": position_to_json(at), }); if let (Some(context), Some(object)) = (context, request.as_object_mut()) { object.insert("context".into(), context); } crate::lsp::pump(state); let (sender, receiver) = std::sync::mpsc::channel::>(); let queued = crate::lsp::submit(state, move |server: &Server| { let supported = server .capabilities() .is_none_or(|caps| caps.supports(provider)); if !supported { let _ = sender.send(Ok(empty)); return; } let reply = sender.clone(); let sent = server.request_with( method, request, REQUEST_TIMEOUT, Box::new(move |result: Result| { let _ = reply.send(result.map(|value| convert(&value))); }), ); if let Err(e) = sent { let _ = sender.send(Err(e)); } }); if !queued { return Err(EngineError::Failed(format!( "{command}: no PHP language server is running" ))); } Ok(Outcome::pending(Pending::new( receiver, WAIT_TIMEOUT, move |_state: &mut State, reply: Reply| finish(command, reply), ))) } /// The command's value from the converted answer. /// /// # Errors /// [`EngineError::Failed`] for a server error, and when no answer came /// (the session ended or the wait timed out). fn finish(command: &str, reply: Reply) -> Result { match reply { Some(Ok(value)) => Ok(value), Some(Err(e)) => Err(EngineError::Failed(format!( "{command}: language server: {e}" ))), None => Err(EngineError::Failed(format!( "{command}: the PHP language server did not answer" ))), } } /// Whether a project-relative path is PHP, as the document sync decides. fn is_php(path: &Path) -> bool { path.extension().and_then(|e| e.to_str()).is_some_and(|e| { ["php", "phtml", "inc"] .iter() .any(|x| e.eq_ignore_ascii_case(x)) }) } /// The LSP `CompletionItemKind` names, from 1. const KINDS: [&str; 25] = [ "text", "method", "function", "constructor", "field", "variable", "class", "interface", "module", "property", "unit", "value", "enum", "keyword", "snippet", "color", "file", "reference", "folder", "enumMember", "constant", "struct", "event", "operator", "typeParameter", ]; /// The name of a completion item kind; `text` for missing or unknown ones. fn kind_name(kind: Option<&Value>) -> &'static str { kind.and_then(Value::as_u64) .and_then(|k| usize::try_from(k).ok()) .and_then(|k| k.checked_sub(1)) .and_then(|k| KINDS.get(k)) .copied() .unwrap_or("text") } /// Converts a `textDocument/completion` answer (`CompletionItem[]`, /// `CompletionList` or `null`). Items are sorted by `sortText`, then /// label, and cut at [`MAX_ITEMS`]. pub fn completion_result(value: &Value) -> Value { let (list, mut incomplete) = match value { Value::Array(items) => (items.as_slice(), false), Value::Object(object) => ( object .get("items") .and_then(Value::as_array) .map_or(&[][..], Vec::as_slice), object .get("isIncomplete") .and_then(Value::as_bool) .unwrap_or(false), ), _ => (&[][..], false), }; if list.len() > MAX_SCANNED { incomplete = true; } let mut items: Vec<(String, String, Value)> = list .iter() .take(MAX_SCANNED) .filter_map(completion_item) .collect(); items.sort_by(|a, b| a.0.cmp(&b.0).then_with(|| a.1.cmp(&b.1))); if items.len() > MAX_ITEMS { items.truncate(MAX_ITEMS); incomplete = true; } let items: Vec = items.into_iter().map(|(_, _, item)| item).collect(); json!({ "items": items, "incomplete": incomplete }) } /// Converts one `CompletionItem`; `None` without a label. /// /// # Returns /// The sort key, the label and the item. fn completion_item(item: &Value) -> Option<(String, String, Value)> { let label = item.get("label").and_then(Value::as_str)?; if label.is_empty() { return None; } let label = cap(label, MAX_SHORT); let snippet = item.get("insertTextFormat").and_then(Value::as_u64) == Some(2); let plain = |text: &str| { if snippet { cap(&snippet_to_text(text), MAX_LONG) } else { cap(text, MAX_LONG) } }; let mut out = Map::new(); out.insert("label".into(), Value::from(label.clone())); out.insert("kind".into(), Value::from(kind_name(item.get("kind")))); if let Some(detail) = detail_of(item) { out.insert("detail".into(), Value::from(detail)); } if let Some(doc) = item.get("documentation").and_then(markup) { out.insert("documentation".into(), Value::from(doc)); } let text_edit = item.get("textEdit").and_then(|edit| { let range = edit .get("range") .or_else(|| edit.get("insert")) .and_then(Range::from_json)?; let new_text = edit.get("newText").and_then(Value::as_str)?; Some((range, plain(new_text))) }); let insert_text = match (&text_edit, item.get("insertText").and_then(Value::as_str)) { (Some((_, new_text)), _) => new_text.clone(), (None, Some(text)) => plain(text), (None, None) => label.clone(), }; out.insert("insertText".into(), Value::from(insert_text)); if let Some((range, new_text)) = text_edit { out.insert("textEdit".into(), edit_json(range, new_text)); } let extra: Vec = item .get("additionalTextEdits") .and_then(Value::as_array) .map(|edits| { edits .iter() .filter_map(|edit| { let range = edit.get("range").and_then(Range::from_json)?; let new_text = edit.get("newText").and_then(Value::as_str)?; Some(edit_json(range, cap(new_text, MAX_LONG))) }) .take(MAX_EXTRA_EDITS) .collect() }) .unwrap_or_default(); if !extra.is_empty() { out.insert("additionalTextEdits".into(), Value::from(extra)); } let sort_text = item .get("sortText") .and_then(Value::as_str) .map(|s| cap(s, MAX_SHORT)); if let Some(sort) = &sort_text { out.insert("sortText".into(), Value::from(sort.clone())); } if let Some(filter) = item.get("filterText").and_then(Value::as_str) { out.insert("filterText".into(), Value::from(cap(filter, MAX_SHORT))); } let deprecated = item.get("deprecated").and_then(Value::as_bool) == Some(true) || item .get("tags") .and_then(Value::as_array) .is_some_and(|tags| tags.iter().any(|t| t.as_u64() == Some(1))); if deprecated { out.insert("deprecated".into(), Value::Bool(true)); } let key = sort_text.unwrap_or_else(|| label.clone()); Some((key, label, Value::Object(out))) } /// The item's detail; the label details stand in when it has none. fn detail_of(item: &Value) -> Option { if let Some(detail) = item.get("detail").and_then(Value::as_str) && !detail.is_empty() { return Some(cap(detail, MAX_SHORT)); } let details = item.get("labelDetails")?; let parts: Vec<&str> = ["detail", "description"] .iter() .filter_map(|k| details.get(*k).and_then(Value::as_str)) .filter(|s| !s.is_empty()) .collect(); (!parts.is_empty()).then(|| cap(&parts.join(" "), MAX_SHORT)) } /// A text edit as Lutra's protocol writes it. fn edit_json(range: Range, new_text: String) -> Value { json!({ "range": range_json(range), "newText": new_text }) } /// A range with `{line, column}` positions. fn range_json(range: Range) -> Value { json!({ "start": { "line": range.start.line, "column": range.start.column }, "end": { "line": range.end.line, "column": range.end.column }, }) } /// Converts a `textDocument/hover` answer into `{contents, range?}`. pub fn hover_result(value: &Value) -> Value { let contents = match value.get("contents") { Some(Value::Array(parts)) => parts .iter() .take(MAX_SIGNATURES) .filter_map(marked) .filter(|s| !s.trim().is_empty()) .collect::>() .join("\n\n"), Some(part) => marked(part).unwrap_or_default(), None => String::new(), }; let mut out = Map::new(); out.insert("contents".into(), Value::from(cap(&contents, MAX_LONG))); if let Some(range) = value.get("range").and_then(Range::from_json) { out.insert("range".into(), range_json(range)); } Value::Object(out) } /// One hover part as markdown: a `MarkupContent`, a markdown string or a /// `{language, value}` code block. fn marked(part: &Value) -> Option { match part { Value::String(text) => Some(text.clone()), Value::Object(object) if object.contains_key("language") => { let language = object.get("language").and_then(Value::as_str)?; let code = object.get("value").and_then(Value::as_str)?; let language: String = language .chars() .filter(|c| c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '+')) .collect(); Some(format!( "```{language}\n{}\n```", code.replace("```", "`\u{200b}``") )) } Value::Object(_) => markup(part), _ => None, } } /// Documentation as markdown: a `MarkupContent` or a plain string, whose /// markdown characters are escaped. fn markup(value: &Value) -> Option { let (kind, text) = match value { Value::String(text) => ("plaintext", text.as_str()), Value::Object(object) => ( object .get("kind") .and_then(Value::as_str) .unwrap_or("plaintext"), object.get("value").and_then(Value::as_str)?, ), _ => return None, }; if text.trim().is_empty() { return None; } let text = cap(text, MAX_LONG); Some(if kind == "markdown" { text } else { escape_markdown(&text) }) } /// Escapes the characters markdown gives a meaning, so plain text shows /// as written. pub fn escape_markdown(text: &str) -> String { let mut out = String::with_capacity(text.len()); for c in text.chars() { if "\\`*_{}[]()<>#+-.!|~".contains(c) { out.push('\\'); } out.push(c); } out } /// Converts a `textDocument/signatureHelp` answer. pub fn signature_result(value: &Value) -> Value { let signatures: Vec = value .get("signatures") .and_then(Value::as_array) .map(|list| { list.iter() .take(MAX_SIGNATURES) .filter_map(signature) .collect() }) .unwrap_or_default(); let index = |key: &str| value.get(key).and_then(Value::as_u64).unwrap_or(0); json!({ "signatures": signatures, "activeSignature": index("activeSignature"), "activeParameter": index("activeParameter"), }) } /// Converts one `SignatureInformation`; `None` without a label. fn signature(value: &Value) -> Option { let label = cap(value.get("label").and_then(Value::as_str)?, MAX_SHORT * 4); let length = label.encode_utf16().count(); let parameters: Vec = value .get("parameters") .and_then(Value::as_array) .map(|list| { list.iter() .take(MAX_SIGNATURES * 2) .map(|p| { let span = match p.get("label") { Some(Value::String(text)) if !text.is_empty() => { label.find(text.as_str()).map(|byte| { let start = label.get(..byte).unwrap_or("").encode_utf16().count(); (start, start + text.encode_utf16().count()) }) } Some(Value::Array(pair)) => { let at = |i: usize| { pair.get(i) .and_then(Value::as_u64) .and_then(|n| usize::try_from(n).ok()) }; match (at(0), at(1)) { (Some(a), Some(b)) if a <= b && b <= length => Some((a, b)), _ => None, } } _ => None, }; let mut out = Map::new(); out.insert( "label".into(), span.map_or(Value::Null, |(a, b)| json!([a, b])), ); if let Some(doc) = p.get("documentation").and_then(markup) { out.insert("documentation".into(), Value::from(doc)); } Value::Object(out) }) .collect() }) .unwrap_or_default(); let mut out = Map::new(); out.insert("label".into(), Value::from(label)); if let Some(doc) = value.get("documentation").and_then(markup) { out.insert("documentation".into(), Value::from(doc)); } out.insert("parameters".into(), Value::from(parameters)); if let Some(active) = value.get("activeParameter").and_then(Value::as_u64) { out.insert("activeParameter".into(), Value::from(active)); } Some(Value::Object(out)) } /// Turns snippet syntax into the text it inserts: tab stops (`$1`, /// `${2}`) vanish, placeholders (`${1:name}`) keep their text, choices /// (`${1|a,b|}`) their first option, escapes (`\$`, `\}`, `\\`) their /// character. `$` before anything else (a PHP variable) stays. pub fn snippet_to_text(snippet: &str) -> String { let mut out = String::with_capacity(snippet.len()); let chars: Vec = snippet.chars().collect(); let mut i = 0; let mut depth = 0usize; while let Some(&c) = chars.get(i) { let next = chars.get(i + 1).copied(); match c { '\\' if matches!(next, Some('$' | '}' | '\\' | ',' | '|')) => { if let Some(n) = next { out.push(n); } i += 2; } '$' if next.is_some_and(|n| n.is_ascii_digit()) => { i += 1; while chars.get(i).is_some_and(char::is_ascii_digit) { i += 1; } } '$' if next == Some('{') => { let mut j = i + 2; while chars.get(j).is_some_and(char::is_ascii_digit) { j += 1; } match chars.get(j) { Some(':') => { depth += 1; i = j + 1; } Some('|') => { depth += 1; i = j + 1; let mut first = true; while let Some(&ch) = chars.get(i) { if ch == '|' && chars.get(i + 1) == Some(&'}') { i += 1; break; } if ch == ',' { first = false; } else if first { out.push(ch); } i += 1; } } Some('}') => i = j + 1, _ => { out.push(c); i += 1; } } } '}' if depth > 0 => { depth -= 1; i += 1; } _ => { out.push(c); i += 1; } } } out } /// `text` cut to at most `max` bytes, on a character boundary. fn cap(text: &str, max: usize) -> String { if text.len() <= max { return text.to_string(); } let end = text .char_indices() .map(|(i, _)| i) .take_while(|&i| i <= max) .last() .unwrap_or(0); text.get(..end).unwrap_or("").to_string() } #[cfg(test)] mod tests { use serde_json::json; use super::{ MAX_ITEMS, cap, completion_result, escape_markdown, hover_result, kind_name, signature_result, snippet_to_text, }; #[test] fn kinds_map_to_names() { assert_eq!(kind_name(Some(&json!(2))), "method"); assert_eq!(kind_name(Some(&json!(25))), "typeParameter"); assert_eq!(kind_name(Some(&json!(0))), "text"); assert_eq!(kind_name(Some(&json!(99))), "text"); assert_eq!(kind_name(Some(&json!("x"))), "text"); assert_eq!(kind_name(None), "text"); } #[test] fn completion_reads_lists_and_arrays() { let list = completion_result(&json!({ "isIncomplete": true, "items": [ {"label": "b", "sortText": "2"}, {"label": "a", "sortText": "1", "kind": 7, "detail": "App\\A", "documentation": {"kind": "markdown", "value": "**A**"}}, {"label": ""}, {"nolabel": true} ] })); assert_eq!(list["incomplete"], true); let items = list["items"].as_array().unwrap(); assert_eq!(items.len(), 2); assert_eq!(items[0]["label"], "a"); assert_eq!(items[0]["kind"], "class"); assert_eq!(items[0]["documentation"], "**A**"); assert_eq!(items[0]["insertText"], "a"); let array = completion_result(&json!([{"label": "x", "insertText": "x()"}])); assert_eq!(array["incomplete"], false); assert_eq!(array["items"][0]["insertText"], "x()"); assert_eq!(completion_result(&json!(null))["items"], json!([])); assert_eq!(completion_result(&json!("junk"))["items"], json!([])); } #[test] fn completion_is_capped_and_marked_incomplete() { let items: Vec<_> = (0..MAX_ITEMS + 50) .map(|i| json!({"label": format!("item{i:04}")})) .collect(); let out = completion_result(&json!({"isIncomplete": false, "items": items})); assert_eq!(out["items"].as_array().unwrap().len(), MAX_ITEMS); assert_eq!(out["incomplete"], true); assert_eq!(out["items"][0]["label"], "item0000"); } #[test] fn text_edits_and_snippets_become_plain_text() { let out = completion_result(&json!([{ "label": "format", "insertTextFormat": 2, "textEdit": {"range": {"start": {"line": 1, "character": 6}, "end": {"line": 1, "character": 8}}, "newText": "format(${1:\\$pattern})$0"}, "additionalTextEdits": [{"range": {"start": {"line": 0, "character": 5}, "end": {"line": 0, "character": 5}}, "newText": "\nuse App\\Fmt;"}], "documentation": "a < b *c*", "tags": [1] }])); let item = &out["items"][0]; assert_eq!(item["insertText"], "format($pattern)"); assert_eq!(item["textEdit"]["newText"], "format($pattern)"); assert_eq!( item["textEdit"]["range"], json!({"start": {"line": 1, "column": 6}, "end": {"line": 1, "column": 8}}) ); assert_eq!(item["additionalTextEdits"][0]["newText"], "\nuse App\\Fmt;"); assert_eq!(item["documentation"], "a \\< b \\*c\\*"); assert_eq!(item["deprecated"], true); } #[test] fn label_details_stand_in_for_a_missing_detail() { let out = completion_result(&json!([{ "label": "f", "labelDetails": {"detail": "(int $a)", "description": "App"} }])); assert_eq!(out["items"][0]["detail"], "(int $a) App"); } #[test] fn snippets() { assert_eq!(snippet_to_text("foo($1, $2)$0"), "foo(, )"); assert_eq!(snippet_to_text("${1:a} ${2:${3:b}}"), "a b"); assert_eq!(snippet_to_text("${1|one,two|}"), "one"); assert_eq!(snippet_to_text("\\$this->x"), "$this->x"); assert_eq!(snippet_to_text("$this"), "$this"); assert_eq!(snippet_to_text("${x"), "${x"); assert_eq!(snippet_to_text("${12}a"), "a"); } #[test] fn hover_parts_become_markdown() { assert_eq!( hover_result(&json!({"contents": {"kind": "markdown", "value": "# T"}}))["contents"], "# T" ); let out = hover_result(&json!({ "contents": [{"language": "php", "value": "class A {}"}, "text", ""], "range": {"start": {"line": 0, "character": 1}, "end": {"line": 0, "character": 2}} })); assert_eq!(out["contents"], "```php\nclass A {}\n```\n\ntext"); assert_eq!(out["range"]["end"]["column"], 2); assert_eq!(hover_result(&json!(null)), json!({"contents": ""})); assert_eq!( hover_result(&json!({"contents": {"kind": "plaintext", "value": ""}}))["contents"], "\\" ); } #[test] fn signatures_get_utf16_parameter_spans() { let out = signature_result(&json!({ "signatures": [{ "label": "f(\u{1f600} $a, int $b)", "documentation": "doc", "parameters": [{"label": "\u{1f600} $a"}, {"label": [9, 15]}, {"label": [3, 99]}, {"label": "zz"}] }], "activeSignature": 0, "activeParameter": 1 })); let sig = &out["signatures"][0]; assert_eq!(sig["parameters"][0]["label"], json!([2, 7])); assert_eq!(sig["parameters"][1]["label"], json!([9, 15])); assert_eq!(sig["parameters"][2]["label"], json!(null)); assert_eq!(sig["parameters"][3]["label"], json!(null)); assert_eq!(out["activeParameter"], 1); assert_eq!(signature_result(&json!(null))["signatures"], json!([])); } #[test] fn caps_cut_on_character_boundaries() { assert_eq!(cap("äöü", 3), "ä"); assert_eq!(cap("abc", 10), "abc"); assert_eq!(escape_markdown("a_b"), "a\\_b"); } }