on master help sign in
ui: Render command mode doc text if available.
commitd24844b73db61e50e1d6fed898d3ff2ba8731356 authorBlaž Hrastnik <blaz@mxxn.io> dateSat 8 May 2021, 17:33 (5 years ago) parentd4d5e88
2 files changed+68−17
helix-term/src/commands.rs+11−1 helix-term/src/ui/prompt.rs+57−16
helix-term/src/commands.rs+11−1
@@ -948,7 +948,7 @@ pub fn command_mode(cx: &mut Context) {
948948 // TODO: completion items should have a info section that would get displayed in
949949 // a popup above the prompt when items are tabbed over
950950
951− let prompt = Prompt::new(
951+ let mut prompt = Prompt::new(
952952 ":".to_owned(),
953953 |input: &str| {
954954 // we use .this over split_ascii_whitespace() because we care about empty segments
@@ -1002,6 +1002,16 @@ pub fn command_mode(cx: &mut Context) {
10021002 };
10031003 },
10041004 );
1005+ prompt.doc_fn = Box::new(|input: &str| {
1006+ let part = input.split(' ').next().unwrap_or_default();
1007+
1008+ if let Some(cmd::Command { doc, .. }) = cmd::COMMANDS.get(part) {
1009+ return Some(doc);
1010+ }
1011+
1012+ None
1013+ });
1014+
10051015 cx.push_layer(Box::new(prompt));
10061016}
10071017
helix-term/src/ui/prompt.rs+57−16
@@ -1,4 +1,5 @@
11use crate::compositor::{Component, Compositor, Context, EventResult};
2+use crate::ui;
23use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
34use helix_core::Position;
45use helix_view::{Editor, Theme};
@@ -11,9 +12,10 @@ pub struct Prompt {
1112 pub line: String,
1213 cursor: usize,
1314 completion: Vec<Completion>,
14− completion_selection_index: Option<usize>,
15+ selection: Option<usize>,
1516 completion_fn: Box<dyn FnMut(&str) -> Vec<Completion>>,
1617 callback_fn: Box<dyn FnMut(&mut Editor, &str, PromptEvent)>,
18+ pub doc_fn: Box<dyn Fn(&str) -> Option<&'static str>>,
1719}
1820
1921#[derive(PartialEq)]
@@ -37,9 +39,10 @@ impl Prompt {
3739 line: String::new(),
3840 cursor: 0,
3941 completion: completion_fn(""),
40− completion_selection_index: None,
42+ selection: None,
4143 completion_fn: Box::new(completion_fn),
4244 callback_fn: Box::new(callback_fn),
45+ doc_fn: Box::new(|_| None),
4346 }
4447 }
4548
@@ -81,8 +84,8 @@ impl Prompt {
8184 if self.completion.is_empty() {
8285 return;
8386 }
84− let index = self.completion_selection_index.map_or(0, |i| i + 1) % self.completion.len();
85− self.completion_selection_index = Some(index);
87+ let index = self.selection.map_or(0, |i| i + 1) % self.completion.len();
88+ self.selection = Some(index);
8689
8790 let (range, item) = &self.completion[index];
8891
@@ -92,7 +95,7 @@ impl Prompt {
9295 // TODO: recalculate completion when completion item is accepted, (Enter)
9396 }
9497 pub fn exit_selection(&mut self) {
95− self.completion_selection_index = None;
98+ self.selection = None;
9699 }
97100}
98101
@@ -105,18 +108,22 @@ use tui::{
105108const BASE_WIDTH: u16 = 30;
106109
107110impl 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;
109113 let text_color = theme.get("ui.text.focus");
110114 // completion
111− if !self.completion.is_empty() {
112− // TODO: find out better way of clearing individual lines of the screen
113− let mut row = 0;
114− let mut col = 0;
115− let max_col = area.width / BASE_WIDTH;
116− let col_height = ((self.completion.len() as u16 + max_col - 1) / max_col);
117115
118− // completion box area
119− let area = Rect::new(area.x, area.height - col_height - 2, area.width, col_height);
116+ let max_col = area.width / BASE_WIDTH;
117+ let height = ((self.completion.len() as u16 + max_col - 1) / max_col);
118+ let completion_area = Rect::new(
119+ area.x,
120+ (area.height - height).saturating_sub(1),
121+ area.width,
122+ height,
123+ );
124+
125+ if !self.completion.is_empty() {
126+ let area = completion_area;
120127 let background = theme.get("ui.statusline");
121128
122129 for y in area.top()..area.bottom() {
@@ -127,8 +134,11 @@ impl Prompt {
127134 }
128135 }
129136
137+ let mut row = 0;
138+ let mut col = 0;
139+
130140 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 {
132142 Style::default().bg(Color::Rgb(104, 60, 232))
133143 } else {
134144 text_color
@@ -150,6 +160,37 @@ impl Prompt {
150160 }
151161 }
152162 }
163+
164+ if let Some(doc) = (self.doc_fn)(&self.line) {
165+ let text = ui::Text::new(doc.to_string());
166+
167+ let area = Rect::new(
168+ completion_area.x + 0,
169+ completion_area.y - 3,
170+ completion_area.width,
171+ 3,
172+ );
173+
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);
178+ cell.reset();
179+ cell.set_style(background);
180+ }
181+ }
182+
183+ use tui::layout::Margin;
184+ text.render(
185+ area.inner(&Margin {
186+ vertical: 1,
187+ horizontal: 1,
188+ }),
189+ surface,
190+ cx,
191+ );
192+ }
193+
153194 let line = area.height - 1;
154195 // render buffer text
155196 surface.set_string(area.x, area.y + line, &self.prompt, text_color);
@@ -240,7 +281,7 @@ impl Component for Prompt {
240281 }
241282
242283 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)
244285 }
245286
246287 fn cursor_position(&self, area: Rect, editor: &Editor) -> Option<Position> {