From 27aed73abbf28f37b9fe4150d8f08ccfd52276f7 Mon Sep 17 00:00:00 2001 From: Daniel <101683475+Koranir@users.noreply.github.com> Date: Mon, 18 Dec 2023 21:54:16 +1100 Subject: [PATCH] Update lsp.rs --- helix-term/src/commands/lsp.rs | 92 ++++++++-------------------------- 1 file changed, 21 insertions(+), 71 deletions(-) diff --git a/helix-term/src/commands/lsp.rs b/helix-term/src/commands/lsp.rs index b6f06aa1c..df9ad3b00 100644 --- a/helix-term/src/commands/lsp.rs +++ b/helix-term/src/commands/lsp.rs @@ -1636,79 +1636,29 @@ fn compute_inlay_hints_for_view( } } - fn collect_to_map<'a>( - doc_text: &helix_core::Rope, - hints: &'a Vec, - ) -> HashMap> { - let mut v = HashMap::new(); - for hint in hints { - let char_idx = hint.char_idx; - let char_pos = helix_core::coords_at_pos(doc_text.byte_slice(..), char_idx); - let char_index = helix_core::line_ending::line_end_char_index( - &doc_text.slice(..), - char_pos.row, - ); - - let refm = match v.get_mut(&char_index) { - Some(l) => l, - None => { - v.insert(char_index, helix_core::SmallVec::<[&str; 1]>::new()); - v.get_mut(&char_index).unwrap() - } + let vimmise_inlays = |hints: &mut Vec, prefix| { + hints.iter_mut().map(|it| { + it.char_idx = helix_core::line_ending::line_end_char_index(&doc_text.slice(..), doc_text.char_to_line(it.char_idx)); + (it.char_idx, it) + }).fold(HashMap::>::new(), |mut map, (id, annot)| { + match map.get_mut(&id) { + Some(v) => v.push(annot), + None => {map.insert(id, vec![annot]);} }; + map + }).into_iter().for_each(|(_, mut v)| { + v[0].text.insert_str(0, prefix); + v.iter_mut().for_each(|s| s.text.push_str(",")); + let last = v.last_mut().unwrap(); + last.text.truncate(last.text.len() - 1); + }); + }; - refm.push(hint.text.as_str()); - } - v - } - - fn inlay_map_to_vec( - map: HashMap>, - type_hint_prefix: &str, - ) -> Vec { - map.into_iter() - .map(|(char_index, hints)| { - let type_hint_midfix = ","; - let type_hint_suffix = ""; - - let mut type_built_string = String::with_capacity( - hints - .iter() - .map(|it| it.len() + type_hint_midfix.len()) - .sum::() - + type_hint_suffix.len() - + type_hint_prefix.len(), - ); - - type_built_string.push_str(type_hint_prefix); - for hint in hints { - type_built_string.push_str(hint); - type_built_string.push_str(type_hint_midfix); - } - for _ in 0..type_hint_midfix.len() { - type_built_string.pop(); - } - type_built_string.push_str(type_hint_suffix); - - InlineAnnotation::new(0, type_built_string) - }) - .collect() - } - - let type_hint_line_index_map = collect_to_map(doc_text, &type_inlay_hints); - let param_hint_line_index_map = collect_to_map(doc_text, ¶meter_inlay_hints); - - type_inlay_hints = inlay_map_to_vec(type_hint_line_index_map, " > "); - parameter_inlay_hints = inlay_map_to_vec(param_hint_line_index_map, " < "); - - padding_after_inlay_hints = type_inlay_hints - .iter() - .map(|it| InlineAnnotation::new(it.char_idx, " ")) - .collect(); - padding_before_inlay_hints = parameter_inlay_hints - .iter() - .map(|it| InlineAnnotation::new(it.char_idx, " ")) - .collect(); + vimmise_inlays(&mut type_inlay_hints, " => "); + vimmise_inlays(&mut parameter_inlay_hints, " <= "); + vimmise_inlays(&mut other_inlay_hints, " == "); + padding_after_inlay_hints = vec![]; + padding_before_inlay_hints = vec![]; doc.set_inlay_hints( view_id,