1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
//! LSP Signature Help (parameter hints): a floating one-line popup showing the
//! active call's signature with the current parameter bolded, anchored to the
//! cursor. Auto-triggered when typing `(` or `,` inside a call; dismissed on
//! `)` or Esc. Purely informational — it never captures editing keys.
use std::path::PathBuf;
use ratatui::{
buffer::Buffer,
layout::Rect,
style::{Color, Modifier, Style},
text::{Line, Span},
widgets::{Block, Borders, Clear, Paragraph, Widget},
};
use crate::lsp::manager::SignatureInfo;
const MIN_WIDTH: u16 = 20;
const MAX_WIDTH: u16 = 100;
pub struct SignatureHelpPopup {
pub signatures: Vec<SignatureInfo>,
pub active_signature: usize,
pub anchor: (u16, u16),
pub path: PathBuf,
pub request_id: u64,
/// Black theme: gradient border instead of the legacy bright-blue. Set by
/// the app before render from `popup_gradient`.
pub theme: crate::theme::Theme,
}
impl SignatureHelpPopup {
pub fn new(
signatures: Vec<SignatureInfo>,
active_signature: usize,
anchor: (u16, u16),
path: PathBuf,
request_id: u64,
) -> Self {
Self {
signatures,
active_signature,
anchor,
path,
request_id,
theme: crate::theme::Theme::default(),
}
}
pub fn is_empty(&self) -> bool {
self.signatures.is_empty()
}
fn active(&self) -> Option<&SignatureInfo> {
self.signatures.get(self.active_signature)
}
/// The popup rect, anchored to the cursor. Prefers sitting ABOVE the caret
/// (VS Code's parameter hints), so a completion popup below the caret and
/// the signature above don't fight for the same row; flips below only when
/// there's no room above.
pub fn area_for(&self, viewport: Rect) -> Rect {
let label_w = self
.active()
.map(|s| s.label.chars().count() as u16)
.unwrap_or(MIN_WIDTH);
// +1 each side of padding, +2 border.
let width = label_w.saturating_add(4).clamp(MIN_WIDTH, MAX_WIDTH);
let height = 3; // one content row + top/bottom border
let (cx, cy) = self.anchor;
let mut x = cx;
if x.saturating_add(width) > viewport.right() {
x = viewport.right().saturating_sub(width);
}
// Back inside the pane's own left edge. The overflow clamp above pushes
// a wide signature left without a floor, and the width clamp below
// cannot repair it: `x - viewport.x` saturates to 0 once x is left of
// the pane, so the popup kept its full width and painted over the
// sidebar (or the other split). Same clamp `hover_popup` already has.
if x < viewport.x {
x = viewport.x;
}
// Above the caret if it fits, else below.
let y = if cy >= viewport.y + height {
cy - height
} else {
cy.saturating_add(1)
};
Rect {
x,
y,
width: width.min(viewport.width.saturating_sub(x.saturating_sub(viewport.x))),
height: height.min(viewport.height.saturating_sub(y.saturating_sub(viewport.y))),
}
}
}
impl Widget for &SignatureHelpPopup {
fn render(self, area: Rect, buf: &mut Buffer) {
let Some(sig) = self.active() else {
return;
};
// Split the label into (before, active param, after) so the active
// parameter renders bold-and-bright while the rest stays dim.
let base = Style::default().fg(self.theme.ui(Color::Rgb(0xc8, 0xce, 0xda)));
let active = Style::default()
.fg(self.theme.ui(Color::Rgb(0xff, 0xff, 0xff)))
.add_modifier(Modifier::BOLD);
let chars: Vec<char> = sig.label.chars().collect();
let spans: Vec<Span<'static>> = match sig.active_param {
Some((s, e)) if s < e && e <= chars.len() => {
let take = |r: std::ops::Range<usize>| chars[r].iter().collect::<String>();
vec![
Span::styled(format!(" {}", take(0..s)), base),
Span::styled(take(s..e), active),
Span::styled(format!("{} ", take(e..chars.len())), base),
]
}
_ => vec![Span::styled(format!(" {} ", sig.label), base)],
};
// A "2 of 3" counter when the server offers overloads, like VS Code.
let mut title_spans = Vec::new();
if self.signatures.len() > 1 {
title_spans.push(Span::styled(
format!(" {}/{} ", self.active_signature + 1, self.signatures.len()),
Style::default().fg(self.theme.ui(Color::Rgb(0x88, 0xc0, 0xd0))),
));
}
let block = Block::default()
.borders(Borders::ALL)
.border_style(Style::default().fg(self.theme.ui(Color::Rgb(0x4e, 0x9a, 0xff))))
.title(Line::from(title_spans))
.style(Style::default().bg(self.theme.ui(Color::Rgb(0x1e, 0x21, 0x2a))));
Widget::render(Clear, area, buf);
Widget::render(Paragraph::new(Line::from(spans)).block(block), area, buf);
if self.theme.framed_overlays() {
crate::widgets::surface::paint_overlay_frame(buf, area, self.theme);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn sig(label: &str, active: Option<(usize, usize)>) -> SignatureInfo {
SignatureInfo {
label: label.to_string(),
active_param: active,
}
}
/// The overflow clamp pushed `x` left to fit a wide signature, but nothing
/// clamped it back to the pane's own left edge — and the trailing width
/// clamp cannot repair it, since `x - viewport.x` saturates to 0 once `x`
/// is left of the pane. With the sidebar open (or in a split) the popup
/// painted over the Explorer, outside the editor entirely. `hover_popup`
/// has the missing clamp; this one, with MAX_WIDTH 100 against the
/// completion popup's 60, is the one wide enough to hit it.
#[test]
fn a_wide_signature_stays_inside_its_pane() {
let label = format!("fn {}(a: i32, b: i32)", "x".repeat(80)); // 97 chars
let p = SignatureHelpPopup::new(
vec![sig(&label, Some((3, 5)))],
0,
(60, 20),
PathBuf::from("f.rs"),
1,
);
// 120-col terminal with the Explorer sidebar open.
let vp = Rect {
x: 37,
y: 1,
width: 83,
height: 40,
};
let r = p.area_for(vp);
assert!(
r.x >= vp.x,
"popup must not start left of its pane (x {} < {})",
r.x,
vp.x
);
assert!(
r.right() <= vp.right(),
"and must not run past its right edge ({} > {})",
r.right(),
vp.right()
);
}
#[test]
fn area_prefers_above_the_caret() {
let p = SignatureHelpPopup::new(
vec![sig("fn foo(a: i32, b: i32)", Some((7, 13)))],
0,
(10, 20),
PathBuf::from("f.rs"),
1,
);
let vp = Rect {
x: 0,
y: 0,
width: 120,
height: 40,
};
let r = p.area_for(vp);
// height 3, caret row 20 → sits at 17 (above).
assert_eq!(r.y, 17);
}
#[test]
fn area_flips_below_when_no_room_above() {
let p = SignatureHelpPopup::new(
vec![sig("fn foo(a: i32)", Some((7, 13)))],
0,
(5, 1),
PathBuf::from("f.rs"),
1,
);
let vp = Rect {
x: 0,
y: 0,
width: 120,
height: 40,
};
let r = p.area_for(vp);
// caret row 1 (< height 3) → drop below to row 2.
assert_eq!(r.y, 2);
}
#[test]
fn renders_active_parameter_in_bold() {
use ratatui::buffer::Buffer;
let p = SignatureHelpPopup::new(
vec![sig("foo(a, b)", Some((4, 5)))], // bold the "a"
0,
(0, 5),
PathBuf::from("f.rs"),
1,
);
let area = Rect {
x: 0,
y: 0,
width: 20,
height: 3,
};
let mut buf = Buffer::empty(area);
(&p).render(area, &mut buf);
// Find the 'a' cell on the content row and assert it's bold.
let row = 1;
let mut found_bold_a = false;
for x in area.x..area.right() {
let cell = &buf[(x, row)];
if cell.symbol() == "a" && cell.modifier.contains(Modifier::BOLD) {
found_bold_a = true;
}
}
assert!(found_bold_a, "active parameter 'a' must render bold");
}
}