| 1 |
use std::sync::Arc; |
| 2 |
|
| 3 |
use arc_swap::ArcSwap; |
| 4 |
use helix_core::syntax; |
| 5 |
use helix_lsp::lsp; |
| 6 |
use helix_view::graphics::{Margin, Rect, Style}; |
| 7 |
use helix_view::input::Event; |
| 8 |
use tui::buffer::Buffer; |
| 9 |
use tui::widgets::{BorderType, Paragraph, Widget, Wrap}; |
| 10 |
|
| 11 |
use crate::compositor::{Component, Context, EventResult}; |
| 12 |
|
| 13 |
use crate::alt; |
| 14 |
use crate::ui::Markdown; |
| 15 |
|
| 16 |
pub struct Hover { |
| 17 |
active_index: usize, |
| 18 |
contents: Vec<(Option<Markdown>, Markdown)>, |
| 19 |
} |
| 20 |
|
| 21 |
impl Hover { |
| 22 |
pub const ID: &'static str = "hover"; |
| 23 |
|
| 24 |
pub fn new( |
| 25 |
hovers: Vec<(String, lsp::Hover)>, |
| 26 |
config_loader: Arc<ArcSwap<syntax::Loader>>, |
| 27 |
) -> Self { |
| 28 |
let n_hovers = hovers.len(); |
| 29 |
let contents = hovers |
| 30 |
.into_iter() |
| 31 |
.enumerate() |
| 32 |
.map(|(idx, (server_name, hover))| { |
| 33 |
let header = (n_hovers > 1).then(|| { |
| 34 |
Markdown::new( |
| 35 |
format!("**[{}/{}] {}**", idx + 1, n_hovers, server_name), |
| 36 |
config_loader.clone(), |
| 37 |
) |
| 38 |
}); |
| 39 |
let body = Markdown::new( |
| 40 |
hover_contents_to_string(hover.contents), |
| 41 |
config_loader.clone(), |
| 42 |
); |
| 43 |
(header, body) |
| 44 |
}) |
| 45 |
.collect(); |
| 46 |
|
| 47 |
Self { |
| 48 |
active_index: usize::default(), |
| 49 |
contents, |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
fn has_header(&self) -> bool { |
| 54 |
self.contents.len() > 1 |
| 55 |
} |
| 56 |
|
| 57 |
fn content(&self) -> &(Option<Markdown>, Markdown) { |
| 58 |
&self.contents[self.active_index] |
| 59 |
} |
| 60 |
|
| 61 |
fn set_index(&mut self, index: usize) { |
| 62 |
assert!((0..self.contents.len()).contains(&index)); |
| 63 |
self.active_index = index; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
const PADDING_HORIZONTAL: u16 = 2; |
| 68 |
const PADDING_TOP: u16 = 1; |
| 69 |
const PADDING_BOTTOM: u16 = 1; |
| 70 |
const HEADER_HEIGHT: u16 = 1; |
| 71 |
const SEPARATOR_HEIGHT: u16 = 1; |
| 72 |
|
| 73 |
impl Component for Hover { |
| 74 |
fn render(&mut self, area: Rect, surface: &mut Buffer, cx: &mut Context) { |
| 75 |
let margin = Margin::all(1); |
| 76 |
let area = area.inner(margin); |
| 77 |
|
| 78 |
let (header, contents) = self.content(); |
| 79 |
|
| 80 |
|
| 81 |
if let Some(header) = header { |
| 82 |
|
| 83 |
let header = header.parse(Some(&cx.editor.theme)); |
| 84 |
let header = Paragraph::new(&header); |
| 85 |
header.render(area.with_height(HEADER_HEIGHT), surface); |
| 86 |
|
| 87 |
|
| 88 |
let sep_style = Style::default(); |
| 89 |
let borders = BorderType::line_symbols(BorderType::Plain); |
| 90 |
for x in area.left()..area.right() { |
| 91 |
if let Some(cell) = surface.get_mut(x, area.top() + HEADER_HEIGHT) { |
| 92 |
cell.set_symbol(borders.horizontal).set_style(sep_style); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
let contents = contents.parse(Some(&cx.editor.theme)); |
| 99 |
let contents_area = area.clip_top(if self.has_header() { |
| 100 |
HEADER_HEIGHT + SEPARATOR_HEIGHT |
| 101 |
} else { |
| 102 |
0 |
| 103 |
}); |
| 104 |
let contents_para = Paragraph::new(&contents) |
| 105 |
.wrap(Wrap { trim: false }) |
| 106 |
.scroll((cx.scroll.unwrap_or_default() as u16, 0)); |
| 107 |
contents_para.render(contents_area, surface); |
| 108 |
} |
| 109 |
|
| 110 |
fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> { |
| 111 |
let max_text_width = viewport.0.saturating_sub(PADDING_HORIZONTAL).clamp(10, 120); |
| 112 |
|
| 113 |
let (header, contents) = self.content(); |
| 114 |
|
| 115 |
let header_width = header |
| 116 |
.as_ref() |
| 117 |
.map(|header| { |
| 118 |
let header = header.parse(None); |
| 119 |
let (width, _height) = crate::ui::text::required_size(&header, max_text_width); |
| 120 |
width |
| 121 |
}) |
| 122 |
.unwrap_or_default(); |
| 123 |
|
| 124 |
let contents = contents.parse(None); |
| 125 |
let (content_width, content_height) = |
| 126 |
crate::ui::text::required_size(&contents, max_text_width); |
| 127 |
|
| 128 |
let width = PADDING_HORIZONTAL + header_width.max(content_width); |
| 129 |
let height = if self.has_header() { |
| 130 |
PADDING_TOP + HEADER_HEIGHT + SEPARATOR_HEIGHT + content_height + PADDING_BOTTOM |
| 131 |
} else { |
| 132 |
PADDING_TOP + content_height + PADDING_BOTTOM |
| 133 |
}; |
| 134 |
|
| 135 |
Some((width, height)) |
| 136 |
} |
| 137 |
|
| 138 |
fn handle_event(&mut self, event: &Event, _ctx: &mut Context) -> EventResult { |
| 139 |
let Event::Key(event) = event else { |
| 140 |
return EventResult::Ignored(None); |
| 141 |
}; |
| 142 |
|
| 143 |
match event { |
| 144 |
alt!('p') => { |
| 145 |
let index = self |
| 146 |
.active_index |
| 147 |
.checked_sub(1) |
| 148 |
.unwrap_or(self.contents.len() - 1); |
| 149 |
self.set_index(index); |
| 150 |
EventResult::Consumed(None) |
| 151 |
} |
| 152 |
alt!('n') => { |
| 153 |
self.set_index((self.active_index + 1) % self.contents.len()); |
| 154 |
EventResult::Consumed(None) |
| 155 |
} |
| 156 |
_ => EventResult::Ignored(None), |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
fn hover_contents_to_string(contents: lsp::HoverContents) -> String { |
| 162 |
fn marked_string_to_markdown(contents: lsp::MarkedString) -> String { |
| 163 |
match contents { |
| 164 |
lsp::MarkedString::String(contents) => contents, |
| 165 |
lsp::MarkedString::LanguageString(string) => { |
| 166 |
if string.language == "markdown" { |
| 167 |
string.value |
| 168 |
} else { |
| 169 |
format!("```{}\n{}\n```", string.language, string.value) |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
match contents { |
| 175 |
lsp::HoverContents::Scalar(contents) => marked_string_to_markdown(contents), |
| 176 |
lsp::HoverContents::Array(contents) => contents |
| 177 |
.into_iter() |
| 178 |
.map(marked_string_to_markdown) |
| 179 |
.collect::<Vec<_>>() |
| 180 |
.join("\n\n"), |
| 181 |
lsp::HoverContents::Markup(contents) => contents.value, |
| 182 |
} |
| 183 |
} |