2020-12-13 05:57:28 +01:00
|
|
|
use crate::compositor::{Component, Compositor, Context, EventResult};
|
2021-05-08 10:33:06 +02:00
|
|
|
use crate::ui;
|
2020-12-11 10:25:09 +01:00
|
|
|
use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
|
2020-12-13 05:29:34 +01:00
|
|
|
use helix_core::Position;
|
2021-03-22 04:40:07 +01:00
|
|
|
use helix_view::{Editor, Theme};
|
2021-03-21 06:13:49 +01:00
|
|
|
use std::{borrow::Cow, ops::RangeFrom};
|
2020-10-09 22:55:45 +02:00
|
|
|
|
2021-03-22 05:16:56 +01:00
|
|
|
pub type Completion = (RangeFrom<usize>, Cow<'static, str>);
|
|
|
|
|
2020-10-09 22:55:45 +02:00
|
|
|
pub struct Prompt {
|
2021-03-18 06:48:42 +01:00
|
|
|
prompt: String,
|
2020-10-16 09:58:26 +02:00
|
|
|
pub line: String,
|
2021-03-18 06:48:42 +01:00
|
|
|
cursor: usize,
|
2021-03-22 05:16:56 +01:00
|
|
|
completion: Vec<Completion>,
|
2021-05-08 10:33:06 +02:00
|
|
|
selection: Option<usize>,
|
2021-03-22 05:16:56 +01:00
|
|
|
completion_fn: Box<dyn FnMut(&str) -> Vec<Completion>>,
|
2020-12-15 11:29:56 +01:00
|
|
|
callback_fn: Box<dyn FnMut(&mut Editor, &str, PromptEvent)>,
|
2021-05-08 10:33:06 +02:00
|
|
|
pub doc_fn: Box<dyn Fn(&str) -> Option<&'static str>>,
|
2020-12-15 11:29:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq)]
|
|
|
|
pub enum PromptEvent {
|
|
|
|
/// The prompt input has been updated.
|
|
|
|
Update,
|
|
|
|
/// Validate and finalize the change.
|
|
|
|
Validate,
|
|
|
|
/// Abort the change, reverting to the initial state.
|
|
|
|
Abort,
|
2020-10-09 22:55:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Prompt {
|
2020-10-15 12:08:01 +02:00
|
|
|
pub fn new(
|
2020-10-16 09:58:26 +02:00
|
|
|
prompt: String,
|
2021-03-22 05:16:56 +01:00
|
|
|
mut completion_fn: impl FnMut(&str) -> Vec<Completion> + 'static,
|
2020-12-15 11:29:56 +01:00
|
|
|
callback_fn: impl FnMut(&mut Editor, &str, PromptEvent) + 'static,
|
2021-05-06 10:20:00 +02:00
|
|
|
) -> Self {
|
|
|
|
Self {
|
2020-10-16 09:58:26 +02:00
|
|
|
prompt,
|
|
|
|
line: String::new(),
|
|
|
|
cursor: 0,
|
2020-10-24 13:36:34 +02:00
|
|
|
completion: completion_fn(""),
|
2021-05-08 10:33:06 +02:00
|
|
|
selection: None,
|
2020-10-15 12:08:01 +02:00
|
|
|
completion_fn: Box::new(completion_fn),
|
|
|
|
callback_fn: Box::new(callback_fn),
|
2021-05-08 10:33:06 +02:00
|
|
|
doc_fn: Box::new(|_| None),
|
2020-10-15 12:08:01 +02:00
|
|
|
}
|
2020-10-09 22:55:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn insert_char(&mut self, c: char) {
|
2020-10-16 09:58:26 +02:00
|
|
|
self.line.insert(self.cursor, c);
|
|
|
|
self.cursor += 1;
|
2020-10-20 23:02:02 +02:00
|
|
|
self.completion = (self.completion_fn)(&self.line);
|
2020-10-24 14:06:10 +02:00
|
|
|
self.exit_selection();
|
2020-10-09 22:55:45 +02:00
|
|
|
}
|
2020-10-13 00:23:48 +02:00
|
|
|
|
2020-10-13 19:10:50 +02:00
|
|
|
pub fn move_char_left(&mut self) {
|
2020-12-14 06:12:54 +01:00
|
|
|
self.cursor = self.cursor.saturating_sub(1)
|
2020-10-13 18:57:55 +02:00
|
|
|
}
|
|
|
|
|
2020-10-13 19:10:50 +02:00
|
|
|
pub fn move_char_right(&mut self) {
|
2020-10-16 09:58:26 +02:00
|
|
|
if self.cursor < self.line.len() {
|
|
|
|
self.cursor += 1;
|
2020-10-13 18:57:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 19:10:50 +02:00
|
|
|
pub fn move_start(&mut self) {
|
2020-10-16 09:58:26 +02:00
|
|
|
self.cursor = 0;
|
2020-10-13 19:10:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn move_end(&mut self) {
|
2020-10-16 09:58:26 +02:00
|
|
|
self.cursor = self.line.len();
|
2020-10-13 19:10:50 +02:00
|
|
|
}
|
|
|
|
|
2020-10-13 18:57:55 +02:00
|
|
|
pub fn delete_char_backwards(&mut self) {
|
2020-10-16 09:58:26 +02:00
|
|
|
if self.cursor > 0 {
|
|
|
|
self.line.remove(self.cursor - 1);
|
|
|
|
self.cursor -= 1;
|
2020-10-20 23:02:02 +02:00
|
|
|
self.completion = (self.completion_fn)(&self.line);
|
|
|
|
}
|
2020-10-24 14:06:10 +02:00
|
|
|
self.exit_selection();
|
2020-10-20 23:02:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn change_completion_selection(&mut self) {
|
2020-11-13 00:07:21 +01:00
|
|
|
if self.completion.is_empty() {
|
|
|
|
return;
|
2020-11-02 10:41:27 +01:00
|
|
|
}
|
2021-05-08 10:33:06 +02:00
|
|
|
let index = self.selection.map_or(0, |i| i + 1) % self.completion.len();
|
|
|
|
self.selection = Some(index);
|
2021-03-01 10:02:31 +01:00
|
|
|
|
2021-03-21 06:13:49 +01:00
|
|
|
let (range, item) = &self.completion[index];
|
2021-03-01 10:02:31 +01:00
|
|
|
|
2021-03-21 06:13:49 +01:00
|
|
|
self.line.replace_range(range.clone(), item);
|
2021-03-01 10:02:31 +01:00
|
|
|
|
|
|
|
self.move_end();
|
|
|
|
// TODO: recalculate completion when completion item is accepted, (Enter)
|
2020-10-24 14:06:10 +02:00
|
|
|
}
|
|
|
|
pub fn exit_selection(&mut self) {
|
2021-05-08 10:33:06 +02:00
|
|
|
self.selection = None;
|
2020-10-13 18:57:55 +02:00
|
|
|
}
|
2020-12-11 10:25:09 +01:00
|
|
|
}
|
|
|
|
|
2020-12-13 04:23:50 +01:00
|
|
|
use tui::{
|
|
|
|
buffer::Buffer as Surface,
|
|
|
|
layout::Rect,
|
|
|
|
style::{Color, Modifier, Style},
|
|
|
|
};
|
|
|
|
|
|
|
|
const BASE_WIDTH: u16 = 30;
|
|
|
|
|
|
|
|
impl Prompt {
|
2021-05-08 10:33:06 +02:00
|
|
|
pub fn render_prompt(&self, area: Rect, surface: &mut Surface, cx: &mut Context) {
|
|
|
|
let theme = &cx.editor.theme;
|
2021-05-07 10:38:25 +02:00
|
|
|
let text_color = theme.get("ui.text.focus");
|
2020-12-13 04:23:50 +01:00
|
|
|
// completion
|
|
|
|
|
2021-05-08 10:33:06 +02:00
|
|
|
let max_col = area.width / BASE_WIDTH;
|
|
|
|
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,
|
|
|
|
);
|
|
|
|
|
|
|
|
if !self.completion.is_empty() {
|
|
|
|
let area = completion_area;
|
2021-05-07 10:31:28 +02:00
|
|
|
let background = theme.get("ui.statusline");
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2020-12-13 04:23:50 +01:00
|
|
|
}
|
2021-05-07 10:31:28 +02:00
|
|
|
|
2021-05-08 10:33:06 +02:00
|
|
|
let mut row = 0;
|
|
|
|
let mut col = 0;
|
|
|
|
|
2021-03-21 06:13:49 +01:00
|
|
|
for (i, (_range, completion)) in self.completion.iter().enumerate() {
|
2021-05-08 10:33:06 +02:00
|
|
|
let color = if Some(i) == self.selection {
|
2021-01-08 08:31:19 +01:00
|
|
|
Style::default().bg(Color::Rgb(104, 60, 232))
|
2020-12-13 04:23:50 +01:00
|
|
|
} else {
|
2020-12-13 05:35:30 +01:00
|
|
|
text_color
|
2020-12-13 04:23:50 +01:00
|
|
|
};
|
|
|
|
surface.set_stringn(
|
2021-05-07 10:31:28 +02:00
|
|
|
area.x + 1 + col * BASE_WIDTH,
|
|
|
|
area.y + row,
|
2021-03-21 06:13:49 +01:00
|
|
|
&completion,
|
2020-12-13 04:23:50 +01:00
|
|
|
BASE_WIDTH as usize - 1,
|
|
|
|
color,
|
|
|
|
);
|
|
|
|
row += 1;
|
2021-05-07 10:31:28 +02:00
|
|
|
if row > area.height - 1 {
|
2020-12-13 04:23:50 +01:00
|
|
|
row = 0;
|
|
|
|
col += 1;
|
|
|
|
}
|
|
|
|
if col > max_col {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-08 10:33:06 +02:00
|
|
|
|
|
|
|
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,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-12-14 07:58:03 +01:00
|
|
|
let line = area.height - 1;
|
2020-12-13 04:23:50 +01:00
|
|
|
// render buffer text
|
2020-12-17 10:08:16 +01:00
|
|
|
surface.set_string(area.x, area.y + line, &self.prompt, text_color);
|
|
|
|
surface.set_string(
|
|
|
|
area.x + self.prompt.len() as u16,
|
|
|
|
area.y + line,
|
|
|
|
&self.line,
|
|
|
|
text_color,
|
|
|
|
);
|
2020-12-13 04:23:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-11 10:25:09 +01:00
|
|
|
impl Component for Prompt {
|
|
|
|
fn handle_event(&mut self, event: Event, cx: &mut Context) -> EventResult {
|
|
|
|
let event = match event {
|
|
|
|
Event::Key(event) => event,
|
2020-12-17 10:08:16 +01:00
|
|
|
Event::Resize(..) => return EventResult::Consumed(None),
|
2020-12-11 10:25:09 +01:00
|
|
|
_ => return EventResult::Ignored,
|
|
|
|
};
|
2020-10-13 18:57:55 +02:00
|
|
|
|
2020-12-21 08:23:05 +01:00
|
|
|
let close_fn = EventResult::Consumed(Some(Box::new(
|
|
|
|
|compositor: &mut Compositor, editor: &mut Editor| {
|
|
|
|
// remove the layer
|
|
|
|
compositor.pop();
|
|
|
|
},
|
|
|
|
)));
|
2020-12-14 07:59:48 +01:00
|
|
|
|
2020-12-11 10:25:09 +01:00
|
|
|
match event {
|
2021-03-01 10:02:31 +01:00
|
|
|
// char or shift char
|
2020-10-13 00:23:48 +02:00
|
|
|
KeyEvent {
|
|
|
|
code: KeyCode::Char(c),
|
2021-04-06 12:45:14 +02:00
|
|
|
modifiers: KeyModifiers::NONE,
|
|
|
|
}
|
|
|
|
| KeyEvent {
|
|
|
|
code: KeyCode::Char(c),
|
|
|
|
modifiers: KeyModifiers::SHIFT,
|
2020-12-15 11:29:56 +01:00
|
|
|
} => {
|
|
|
|
self.insert_char(c);
|
|
|
|
(self.callback_fn)(cx.editor, &self.line, PromptEvent::Update);
|
|
|
|
}
|
2020-10-13 00:23:48 +02:00
|
|
|
KeyEvent {
|
|
|
|
code: KeyCode::Esc, ..
|
2020-12-13 05:57:28 +01:00
|
|
|
} => {
|
2020-12-15 11:29:56 +01:00
|
|
|
(self.callback_fn)(cx.editor, &self.line, PromptEvent::Abort);
|
2020-12-14 07:59:48 +01:00
|
|
|
return close_fn;
|
2020-12-13 05:57:28 +01:00
|
|
|
}
|
2020-10-13 18:57:55 +02:00
|
|
|
KeyEvent {
|
|
|
|
code: KeyCode::Right,
|
|
|
|
..
|
2020-10-13 19:10:50 +02:00
|
|
|
} => self.move_char_right(),
|
2020-10-13 18:57:55 +02:00
|
|
|
KeyEvent {
|
|
|
|
code: KeyCode::Left,
|
|
|
|
..
|
2020-10-13 19:10:50 +02:00
|
|
|
} => self.move_char_left(),
|
|
|
|
KeyEvent {
|
|
|
|
code: KeyCode::Char('e'),
|
|
|
|
modifiers: KeyModifiers::CONTROL,
|
|
|
|
} => self.move_end(),
|
|
|
|
KeyEvent {
|
|
|
|
code: KeyCode::Char('a'),
|
|
|
|
modifiers: KeyModifiers::CONTROL,
|
|
|
|
} => self.move_start(),
|
2020-10-13 18:57:55 +02:00
|
|
|
KeyEvent {
|
|
|
|
code: KeyCode::Backspace,
|
2020-10-24 14:06:10 +02:00
|
|
|
modifiers: KeyModifiers::NONE,
|
2020-12-15 11:29:56 +01:00
|
|
|
} => {
|
|
|
|
self.delete_char_backwards();
|
|
|
|
(self.callback_fn)(cx.editor, &self.line, PromptEvent::Update);
|
|
|
|
}
|
2020-10-15 12:08:01 +02:00
|
|
|
KeyEvent {
|
|
|
|
code: KeyCode::Enter,
|
|
|
|
..
|
2020-12-14 07:59:48 +01:00
|
|
|
} => {
|
2020-12-15 11:29:56 +01:00
|
|
|
(self.callback_fn)(cx.editor, &self.line, PromptEvent::Validate);
|
2020-12-14 07:59:48 +01:00
|
|
|
return close_fn;
|
|
|
|
}
|
2020-10-19 16:16:00 +02:00
|
|
|
KeyEvent {
|
|
|
|
code: KeyCode::Tab, ..
|
2020-10-20 23:02:02 +02:00
|
|
|
} => self.change_completion_selection(),
|
2020-10-24 14:06:10 +02:00
|
|
|
KeyEvent {
|
|
|
|
code: KeyCode::Char('q'),
|
|
|
|
modifiers: KeyModifiers::CONTROL,
|
|
|
|
} => self.exit_selection(),
|
2020-10-13 00:23:48 +02:00
|
|
|
_ => (),
|
2020-12-11 10:25:09 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
EventResult::Consumed(None)
|
|
|
|
}
|
|
|
|
|
2020-12-13 04:23:50 +01:00
|
|
|
fn render(&self, area: Rect, surface: &mut Surface, cx: &mut Context) {
|
2021-05-08 10:33:06 +02:00
|
|
|
self.render_prompt(area, surface, cx)
|
2020-10-13 00:23:48 +02:00
|
|
|
}
|
2020-12-13 05:29:34 +01:00
|
|
|
|
2021-02-09 07:40:30 +01:00
|
|
|
fn cursor_position(&self, area: Rect, editor: &Editor) -> Option<Position> {
|
2020-12-13 05:29:34 +01:00
|
|
|
Some(Position::new(
|
2020-12-14 07:58:03 +01:00
|
|
|
area.height as usize,
|
|
|
|
area.x as usize + self.prompt.len() + self.cursor,
|
2020-12-13 05:29:34 +01:00
|
|
|
))
|
|
|
|
}
|
2020-10-09 22:55:45 +02:00
|
|
|
}
|