on master help sign in
ui: Render command mode doc text if available.
d24844bBlaž Hrastnik
helix-term/src/commands.rs+11−1
@@ -948,7 +948,7 @@ pub fn command_mode(cx: &mut Context) {
948 // TODO: completion items should have a info section that would get displayed in
948 // TODO: completion items should have a info section that would get displayed in
949 // a popup above the prompt when items are tabbed over
949 // a popup above the prompt when items are tabbed over
950
950
951− let prompt = Prompt::new(
951+ let mut prompt = Prompt::new(
952 ":".to_owned(),
952 ":".to_owned(),
953 |input: &str| {
953 |input: &str| {
954 // we use .this over split_ascii_whitespace() because we care about empty segments
954 // we use .this over split_ascii_whitespace() because we care about empty segments
@@ -1002,6 +1002,16 @@ pub fn command_mode(cx: &mut Context) {
1002 };
1002 };
1003 },
1003 },
1004 );
1004 );
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+
1005 cx.push_layer(Box::new(prompt));
1015 cx.push_layer(Box::new(prompt));
1006}
1016}
1007
1017
helix-term/src/ui/prompt.rs+57−16
@@ -1,4 +1,5 @@
1use crate::compositor::{Component, Compositor, Context, EventResult};
1use crate::compositor::{Component, Compositor, Context, EventResult};
2+use crate::ui;
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>>,
17}
19}
18
20
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),
43 }
46 }
44 }
47 }
45
48
@@ -81,8 +84,8 @@ impl Prompt {
81 if self.completion.is_empty() {
84 if self.completion.is_empty() {
82 return;
85 return;
83 }
86 }
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);
86
89
87 let (range, item) = &self.completion[index];
90 let (range, item) = &self.completion[index];
88
91
@@ -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)
93 }
96 }
94 pub fn exit_selection(&mut self) {
97 pub fn exit_selection(&mut self) {
95− self.completion_selection_index = None;
98+ self.selection = None;
96 }
99 }
97}
100}
98
101
@@ -105,18 +108,22 @@ use tui::{
105const BASE_WIDTH: u16 = 30;
108const BASE_WIDTH: u16 = 30;
106
109
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
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);
117
115
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(
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;
120 let background = theme.get("ui.statusline");
127 let background = theme.get("ui.statusline");
121
128
122 for y in area.top()..area.bottom() {
129 for y in area.top()..area.bottom() {
@@ -127,8 +134,11 @@ impl Prompt {
127 }
134 }
128 }
135 }
129
136
137+ let mut row = 0;
138+ let mut col = 0;
139+
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))
133 } else {
143 } else {
134 text_color
144 text_color
@@ -150,6 +160,37 @@ impl Prompt {
150 }
160 }
151 }
161 }
152 }
162 }
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+
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 {
240 }
281 }
241
282
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)
244 }
285 }
245
286
246 fn cursor_position(&self, area: Rect, editor: &Editor) -> Option<Position> {
287 fn cursor_position(&self, area: Rect, editor: &Editor) -> Option<Position> {