ui: Render command mode doc text if available.

pull/11/head
Blaž Hrastnik 4 years ago
parent d4d5e88ade
commit d24844b73d

@ -948,7 +948,7 @@ pub fn command_mode(cx: &mut Context) {
// TODO: completion items should have a info section that would get displayed in // TODO: completion items should have a info section that would get displayed in
// a popup above the prompt when items are tabbed over // a popup above the prompt when items are tabbed over
let prompt = Prompt::new( let mut prompt = Prompt::new(
":".to_owned(), ":".to_owned(),
|input: &str| { |input: &str| {
// we use .this over split_ascii_whitespace() because we care about empty segments // we use .this over split_ascii_whitespace() because we care about empty segments
@ -1002,6 +1002,16 @@ pub fn command_mode(cx: &mut Context) {
}; };
}, },
); );
prompt.doc_fn = Box::new(|input: &str| {
let part = input.split(' ').next().unwrap_or_default();
if let Some(cmd::Command { doc, .. }) = cmd::COMMANDS.get(part) {
return Some(doc);
}
None
});
cx.push_layer(Box::new(prompt)); cx.push_layer(Box::new(prompt));
} }

@ -1,4 +1,5 @@
use crate::compositor::{Component, Compositor, Context, EventResult}; use crate::compositor::{Component, Compositor, Context, EventResult};
use crate::ui;
use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers}; use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
use helix_core::Position; use helix_core::Position;
use helix_view::{Editor, Theme}; use helix_view::{Editor, Theme};
@ -11,9 +12,10 @@ pub struct Prompt {
pub line: String, pub line: String,
cursor: usize, cursor: usize,
completion: Vec<Completion>, completion: Vec<Completion>,
completion_selection_index: Option<usize>, selection: Option<usize>,
completion_fn: Box<dyn FnMut(&str) -> Vec<Completion>>, completion_fn: Box<dyn FnMut(&str) -> Vec<Completion>>,
callback_fn: Box<dyn FnMut(&mut Editor, &str, PromptEvent)>, callback_fn: Box<dyn FnMut(&mut Editor, &str, PromptEvent)>,
pub doc_fn: Box<dyn Fn(&str) -> Option<&'static str>>,
} }
#[derive(PartialEq)] #[derive(PartialEq)]
@ -37,9 +39,10 @@ impl Prompt {
line: String::new(), line: String::new(),
cursor: 0, cursor: 0,
completion: completion_fn(""), completion: completion_fn(""),
completion_selection_index: None, selection: None,
completion_fn: Box::new(completion_fn), completion_fn: Box::new(completion_fn),
callback_fn: Box::new(callback_fn), callback_fn: Box::new(callback_fn),
doc_fn: Box::new(|_| None),
} }
} }
@ -81,8 +84,8 @@ impl Prompt {
if self.completion.is_empty() { if self.completion.is_empty() {
return; return;
} }
let index = self.completion_selection_index.map_or(0, |i| i + 1) % self.completion.len(); let index = self.selection.map_or(0, |i| i + 1) % self.completion.len();
self.completion_selection_index = Some(index); self.selection = Some(index);
let (range, item) = &self.completion[index]; let (range, item) = &self.completion[index];
@ -92,7 +95,7 @@ impl Prompt {
// TODO: recalculate completion when completion item is accepted, (Enter) // TODO: recalculate completion when completion item is accepted, (Enter)
} }
pub fn exit_selection(&mut self) { pub fn exit_selection(&mut self) {
self.completion_selection_index = None; self.selection = None;
} }
} }
@ -105,18 +108,22 @@ use tui::{
const BASE_WIDTH: u16 = 30; const BASE_WIDTH: u16 = 30;
impl Prompt { impl Prompt {
pub fn render_prompt(&self, area: Rect, surface: &mut Surface, theme: &Theme) { pub fn render_prompt(&self, area: Rect, surface: &mut Surface, cx: &mut Context) {
let theme = &cx.editor.theme;
let text_color = theme.get("ui.text.focus"); let text_color = theme.get("ui.text.focus");
// completion // completion
if !self.completion.is_empty() {
// TODO: find out better way of clearing individual lines of the screen
let mut row = 0;
let mut col = 0;
let max_col = area.width / BASE_WIDTH; let max_col = area.width / BASE_WIDTH;
let col_height = ((self.completion.len() as u16 + max_col - 1) / max_col); let height = ((self.completion.len() as u16 + max_col - 1) / max_col);
let completion_area = Rect::new(
area.x,
(area.height - height).saturating_sub(1),
area.width,
height,
);
// completion box area if !self.completion.is_empty() {
let area = Rect::new(area.x, area.height - col_height - 2, area.width, col_height); let area = completion_area;
let background = theme.get("ui.statusline"); let background = theme.get("ui.statusline");
for y in area.top()..area.bottom() { for y in area.top()..area.bottom() {
@ -127,8 +134,11 @@ impl Prompt {
} }
} }
let mut row = 0;
let mut col = 0;
for (i, (_range, completion)) in self.completion.iter().enumerate() { for (i, (_range, completion)) in self.completion.iter().enumerate() {
let color = if Some(i) == self.completion_selection_index { let color = if Some(i) == self.selection {
Style::default().bg(Color::Rgb(104, 60, 232)) Style::default().bg(Color::Rgb(104, 60, 232))
} else { } else {
text_color text_color
@ -150,6 +160,37 @@ impl Prompt {
} }
} }
} }
if let Some(doc) = (self.doc_fn)(&self.line) {
let text = ui::Text::new(doc.to_string());
let area = Rect::new(
completion_area.x + 0,
completion_area.y - 3,
completion_area.width,
3,
);
let background = theme.get("ui.window");
for y in area.top()..area.bottom() {
for x in area.left()..area.right() {
let cell = surface.get_mut(x, y);
cell.reset();
cell.set_style(background);
}
}
use tui::layout::Margin;
text.render(
area.inner(&Margin {
vertical: 1,
horizontal: 1,
}),
surface,
cx,
);
}
let line = area.height - 1; let line = area.height - 1;
// render buffer text // render buffer text
surface.set_string(area.x, area.y + line, &self.prompt, text_color); surface.set_string(area.x, area.y + line, &self.prompt, text_color);
@ -240,7 +281,7 @@ impl Component for Prompt {
} }
fn render(&self, area: Rect, surface: &mut Surface, cx: &mut Context) { fn render(&self, area: Rect, surface: &mut Surface, cx: &mut Context) {
self.render_prompt(area, surface, &cx.editor.theme) self.render_prompt(area, surface, cx)
} }
fn cursor_position(&self, area: Rect, editor: &Editor) -> Option<Position> { fn cursor_position(&self, area: Rect, editor: &Editor) -> Option<Position> {

Loading…
Cancel
Save