| 1 |
use std::sync::Arc; |
| 2 |
|
| 3 |
use arc_swap::ArcSwap; |
| 4 |
use helix_core::syntax::{self, OverlayHighlights}; |
| 5 |
use helix_view::graphics::{Margin, Rect, Style}; |
| 6 |
use helix_view::input::Event; |
| 7 |
use tui::buffer::Buffer; |
| 8 |
use tui::layout::Alignment; |
| 9 |
use tui::text::Text; |
| 10 |
use tui::widgets::{BorderType, Paragraph, Widget, Wrap}; |
| 11 |
|
| 12 |
use crate::compositor::{Component, Compositor, Context, EventResult}; |
| 13 |
|
| 14 |
use crate::alt; |
| 15 |
use crate::ui::Markdown; |
| 16 |
|
| 17 |
use crate::ui::Popup; |
| 18 |
|
| 19 |
pub struct Signature { |
| 20 |
pub signature: String, |
| 21 |
pub signature_doc: Option<String>, |
| 22 |
|
| 23 |
pub active_param_range: Option<(usize, usize)>, |
| 24 |
} |
| 25 |
|
| 26 |
pub struct SignatureHelp { |
| 27 |
language: String, |
| 28 |
config_loader: Arc<ArcSwap<syntax::Loader>>, |
| 29 |
active_signature: usize, |
| 30 |
lsp_signature: Option<usize>, |
| 31 |
signatures: Vec<Signature>, |
| 32 |
} |
| 33 |
|
| 34 |
impl SignatureHelp { |
| 35 |
pub const ID: &'static str = "signature-help"; |
| 36 |
|
| 37 |
pub fn new( |
| 38 |
language: String, |
| 39 |
config_loader: Arc<ArcSwap<syntax::Loader>>, |
| 40 |
active_signature: usize, |
| 41 |
lsp_signature: Option<usize>, |
| 42 |
signatures: Vec<Signature>, |
| 43 |
) -> Self { |
| 44 |
Self { |
| 45 |
language, |
| 46 |
config_loader, |
| 47 |
active_signature, |
| 48 |
lsp_signature, |
| 49 |
signatures, |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
pub fn active_signature(&self) -> usize { |
| 54 |
self.active_signature |
| 55 |
} |
| 56 |
|
| 57 |
pub fn lsp_signature(&self) -> Option<usize> { |
| 58 |
self.lsp_signature |
| 59 |
} |
| 60 |
|
| 61 |
pub fn visible_popup(compositor: &mut Compositor) -> Option<&mut Popup<Self>> { |
| 62 |
compositor.find_id::<Popup<Self>>(Self::ID) |
| 63 |
} |
| 64 |
|
| 65 |
fn signature_index(&self) -> String { |
| 66 |
format!("({}/{})", self.active_signature + 1, self.signatures.len()) |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
impl Component for SignatureHelp { |
| 71 |
fn handle_event(&mut self, event: &Event, _cx: &mut Context) -> EventResult { |
| 72 |
let Event::Key(event) = event else { |
| 73 |
return EventResult::Ignored(None); |
| 74 |
}; |
| 75 |
|
| 76 |
if self.signatures.len() <= 1 { |
| 77 |
return EventResult::Ignored(None); |
| 78 |
} |
| 79 |
|
| 80 |
match event { |
| 81 |
alt!('p') => { |
| 82 |
self.active_signature = self |
| 83 |
.active_signature |
| 84 |
.checked_sub(1) |
| 85 |
.unwrap_or(self.signatures.len() - 1); |
| 86 |
EventResult::Consumed(None) |
| 87 |
} |
| 88 |
alt!('n') => { |
| 89 |
self.active_signature = (self.active_signature + 1) % self.signatures.len(); |
| 90 |
EventResult::Consumed(None) |
| 91 |
} |
| 92 |
_ => EventResult::Ignored(None), |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
fn render(&mut self, area: Rect, surface: &mut Buffer, cx: &mut Context) { |
| 97 |
let margin = Margin::all(1); |
| 98 |
let area = area.inner(margin); |
| 99 |
|
| 100 |
let signature = self |
| 101 |
.signatures |
| 102 |
.get(self.active_signature) |
| 103 |
.unwrap_or_else(|| &self.signatures[0]); |
| 104 |
|
| 105 |
let active_param_span = signature.active_param_range.map(|(start, end)| { |
| 106 |
let highlight = cx |
| 107 |
.editor |
| 108 |
.theme |
| 109 |
.find_highlight_exact("ui.selection") |
| 110 |
.unwrap(); |
| 111 |
OverlayHighlights::single(highlight, start..end) |
| 112 |
}); |
| 113 |
|
| 114 |
let signature = self |
| 115 |
.signatures |
| 116 |
.get(self.active_signature) |
| 117 |
.unwrap_or_else(|| &self.signatures[0]); |
| 118 |
|
| 119 |
let sig_text = crate::ui::markdown::highlighted_code_block( |
| 120 |
signature.signature.as_str(), |
| 121 |
&self.language, |
| 122 |
Some(&cx.editor.theme), |
| 123 |
&self.config_loader.load(), |
| 124 |
active_param_span, |
| 125 |
); |
| 126 |
|
| 127 |
if self.signatures.len() > 1 { |
| 128 |
let signature_index = self.signature_index(); |
| 129 |
let text = Text::from(signature_index); |
| 130 |
let paragraph = Paragraph::new(&text).alignment(Alignment::Right); |
| 131 |
paragraph.render(area.with_height(1).clip_right(1), surface); |
| 132 |
} |
| 133 |
|
| 134 |
let sig_text_para = Paragraph::new(&sig_text) |
| 135 |
.wrap(Wrap { trim: false }) |
| 136 |
.scroll((cx.scroll.unwrap_or_default() as u16, 0)); |
| 137 |
let (_, sig_text_height) = sig_text_para.required_size(area.width); |
| 138 |
let sig_text_area = area.with_height(sig_text_height.min(area.height)); |
| 139 |
let sig_text_area = sig_text_area.intersection(surface.area); |
| 140 |
sig_text_para.render(sig_text_area, surface); |
| 141 |
|
| 142 |
if signature.signature_doc.is_none() { |
| 143 |
return; |
| 144 |
} |
| 145 |
|
| 146 |
let sep_style = Style::default(); |
| 147 |
let borders = BorderType::line_symbols(BorderType::Plain); |
| 148 |
for x in sig_text_area.left()..sig_text_area.right() { |
| 149 |
if let Some(cell) = surface.get_mut(x, sig_text_area.bottom()) { |
| 150 |
cell.set_symbol(borders.horizontal).set_style(sep_style); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
let sig_doc = match &signature.signature_doc { |
| 155 |
None => return, |
| 156 |
Some(doc) => Markdown::new(doc.clone(), Arc::clone(&self.config_loader)), |
| 157 |
}; |
| 158 |
let sig_doc = sig_doc.parse(Some(&cx.editor.theme)); |
| 159 |
let sig_doc_area = area |
| 160 |
.clip_top(sig_text_area.height + 2) |
| 161 |
.clip_bottom(u16::from(cx.editor.popup_border())); |
| 162 |
let sig_doc_para = Paragraph::new(&sig_doc) |
| 163 |
.wrap(Wrap { trim: false }) |
| 164 |
.scroll((cx.scroll.unwrap_or_default() as u16, 0)); |
| 165 |
sig_doc_para.render(sig_doc_area, surface); |
| 166 |
} |
| 167 |
|
| 168 |
fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> { |
| 169 |
const PADDING: u16 = 2; |
| 170 |
const SEPARATOR_HEIGHT: u16 = 1; |
| 171 |
|
| 172 |
let signature = self |
| 173 |
.signatures |
| 174 |
.get(self.active_signature) |
| 175 |
.unwrap_or_else(|| &self.signatures[0]); |
| 176 |
|
| 177 |
let max_text_width = viewport.0.saturating_sub(PADDING).clamp(10, 120); |
| 178 |
|
| 179 |
let signature_text = crate::ui::markdown::highlighted_code_block( |
| 180 |
signature.signature.as_str(), |
| 181 |
&self.language, |
| 182 |
None, |
| 183 |
&self.config_loader.load(), |
| 184 |
None, |
| 185 |
); |
| 186 |
let sig_text_para = Paragraph::new(&signature_text).wrap(Wrap { trim: false }); |
| 187 |
let (sig_width, sig_height) = sig_text_para.required_size(max_text_width); |
| 188 |
|
| 189 |
let (width, height) = match signature.signature_doc { |
| 190 |
Some(ref doc) => { |
| 191 |
let doc_md = Markdown::new(doc.clone(), Arc::clone(&self.config_loader)); |
| 192 |
let doc_text = doc_md.parse(None); |
| 193 |
let (doc_width, doc_height) = |
| 194 |
crate::ui::text::required_size(&doc_text, max_text_width); |
| 195 |
( |
| 196 |
sig_width.max(doc_width), |
| 197 |
sig_height + SEPARATOR_HEIGHT + doc_height, |
| 198 |
) |
| 199 |
} |
| 200 |
None => (sig_width, sig_height), |
| 201 |
}; |
| 202 |
|
| 203 |
let sig_index_width = if self.signatures.len() > 1 { |
| 204 |
self.signature_index().len() + 1 |
| 205 |
} else { |
| 206 |
0 |
| 207 |
}; |
| 208 |
|
| 209 |
Some((width + PADDING + sig_index_width as u16, height + PADDING)) |
| 210 |
} |
| 211 |
} |