@@ -1,4 +1,5 @@
1use crate::compositor::{Component, Compositor, Context, EventResult};
1use crate::compositor::{Component, Compositor, Context, EventResult};
2use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
3use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
3use helix_core::Position;
4use helix_core::Position;
4use helix_view::{Editor, Theme};
5use helix_view::{Editor, Theme};
@@ -11,9 +12,10 @@ pub struct Prompt {
11 pub line: String,
12 pub line: String,
12 cursor: usize,
13 cursor: usize,
13 completion: Vec<Completion>,
14 completion: Vec<Completion>,
14− completion_selection_index: Option<usize>,
15+ selection: Option<usize>,
15 completion_fn: Box<dyn FnMut(&str) -> Vec<Completion>>,
16 completion_fn: Box<dyn FnMut(&str) -> Vec<Completion>>,
16 callback_fn: Box<dyn FnMut(&mut Editor, &str, PromptEvent)>,
17 callback_fn: Box<dyn FnMut(&mut Editor, &str, PromptEvent)>,
18+ pub doc_fn: Box<dyn Fn(&str) -> Option<&'static str>>,
19#[derive(PartialEq)]
21#[derive(PartialEq)]
@@ -37,9 +39,10 @@ impl Prompt {
37 line: String::new(),
39 line: String::new(),
38 cursor: 0,
40 cursor: 0,
39 completion: completion_fn(""),
41 completion: completion_fn(""),
40− completion_selection_index: None,
42+ selection: None,
41 completion_fn: Box::new(completion_fn),
43 completion_fn: Box::new(completion_fn),
42 callback_fn: Box::new(callback_fn),
44 callback_fn: Box::new(callback_fn),
45+ doc_fn: Box::new(|_| None),
@@ -81,8 +84,8 @@ impl Prompt {
81 if self.completion.is_empty() {
84 if self.completion.is_empty() {
84− let index = self.completion_selection_index.map_or(0, |i| i + 1) % self.completion.len();
87+ let index = self.selection.map_or(0, |i| i + 1) % self.completion.len();
85− self.completion_selection_index = Some(index);
88+ self.selection = Some(index);
87 let (range, item) = &self.completion[index];
90 let (range, item) = &self.completion[index];
@@ -92,7 +95,7 @@ impl Prompt {
92 // TODO: recalculate completion when completion item is accepted, (Enter)
95 // TODO: recalculate completion when completion item is accepted, (Enter)
94 pub fn exit_selection(&mut self) {
97 pub fn exit_selection(&mut self) {
95− self.completion_selection_index = None;
98+ self.selection = None;
@@ -105,18 +108,22 @@ use tui::{
105const BASE_WIDTH: u16 = 30;
108const BASE_WIDTH: u16 = 30;
107impl Prompt {
110impl Prompt {
108− pub fn render_prompt(&self, area: Rect, surface: &mut Surface, theme: &Theme) {
111+ pub fn render_prompt(&self, area: Rect, surface: &mut Surface, cx: &mut Context) {
112+ let theme = &cx.editor.theme;
109 let text_color = theme.get("ui.text.focus");
113 let text_color = theme.get("ui.text.focus");
110 // completion
114 // completion
111− if !self.completion.is_empty() {
112− // TODO: find out better way of clearing individual lines of the screen
115− let max_col = area.width / BASE_WIDTH;
116− let col_height = ((self.completion.len() as u16 + max_col - 1) / max_col);
118− // completion box area
116+ let max_col = area.width / BASE_WIDTH;
119− let area = Rect::new(area.x, area.height - col_height - 2, area.width, col_height);
117+ let height = ((self.completion.len() as u16 + max_col - 1) / max_col);
118+ let completion_area = Rect::new(
120+ (area.height - height).saturating_sub(1),
125+ if !self.completion.is_empty() {
126+ let area = completion_area;
120 let background = theme.get("ui.statusline");
127 let background = theme.get("ui.statusline");
122 for y in area.top()..area.bottom() {
129 for y in area.top()..area.bottom() {
@@ -127,8 +134,11 @@ impl Prompt {
130 for (i, (_range, completion)) in self.completion.iter().enumerate() {
140 for (i, (_range, completion)) in self.completion.iter().enumerate() {
131− let color = if Some(i) == self.completion_selection_index {
141+ let color = if Some(i) == self.selection {
132 Style::default().bg(Color::Rgb(104, 60, 232))
142 Style::default().bg(Color::Rgb(104, 60, 232))
134 text_color
144 text_color
@@ -150,6 +160,37 @@ impl Prompt {
164+ if let Some(doc) = (self.doc_fn)(&self.line) {
165+ let text = ui::Text::new(doc.to_string());
167+ let area = Rect::new(
168+ completion_area.x + 0,
169+ completion_area.y - 3,
170+ completion_area.width,
174+ let background = theme.get("ui.window");
175+ for y in area.top()..area.bottom() {
176+ for x in area.left()..area.right() {
177+ let cell = surface.get_mut(x, y);
179+ cell.set_style(background);
183+ use tui::layout::Margin;
185+ area.inner(&Margin {
153 let line = area.height - 1;
194 let line = area.height - 1;
154 // render buffer text
195 // render buffer text
155 surface.set_string(area.x, area.y + line, &self.prompt, text_color);
196 surface.set_string(area.x, area.y + line, &self.prompt, text_color);
@@ -240,7 +281,7 @@ impl Component for Prompt {
242 fn render(&self, area: Rect, surface: &mut Surface, cx: &mut Context) {
283 fn render(&self, area: Rect, surface: &mut Surface, cx: &mut Context) {
243− self.render_prompt(area, surface, &cx.editor.theme)
284+ self.render_prompt(area, surface, cx)
246 fn cursor_position(&self, area: Rect, editor: &Editor) -> Option<Position> {
287 fn cursor_position(&self, area: Rect, editor: &Editor) -> Option<Position> {