mirror of https://github.com/helix-editor/helix
commit
2e12fc9a7c
|
@ -34,6 +34,8 @@ type Terminal = tui::Terminal<CrosstermBackend<std::io::Stdout>>;
|
||||||
|
|
||||||
static EX: smol::Executor = smol::Executor::new();
|
static EX: smol::Executor = smol::Executor::new();
|
||||||
|
|
||||||
|
const BASE_WIDTH: u16 = 30;
|
||||||
|
|
||||||
pub struct Application {
|
pub struct Application {
|
||||||
editor: Editor,
|
editor: Editor,
|
||||||
prompt: Option<Prompt>,
|
prompt: Option<Prompt>,
|
||||||
|
@ -235,7 +237,52 @@ impl Renderer {
|
||||||
.set_string(1, self.size.1 - 2, mode, self.text_color);
|
.set_string(1, self.size.1 - 2, mode, self.text_color);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn render_prompt(&mut self, prompt: &Prompt) {
|
pub fn render_prompt(&mut self, view: &View, prompt: &Prompt) {
|
||||||
|
// completion
|
||||||
|
if !prompt.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 = self.size.0 / BASE_WIDTH;
|
||||||
|
let col_height = ((prompt.completion.len() as u16 + max_col - 1) / max_col);
|
||||||
|
|
||||||
|
for i in (3..col_height + 3) {
|
||||||
|
self.surface.set_string(
|
||||||
|
0,
|
||||||
|
self.size.1 - i as u16,
|
||||||
|
" ".repeat(self.size.0 as usize),
|
||||||
|
self.text_color,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
self.surface.set_style(
|
||||||
|
Rect::new(0, self.size.1 - col_height - 2, self.size.0, col_height),
|
||||||
|
view.theme.get("ui.statusline"),
|
||||||
|
);
|
||||||
|
for (i, command) in prompt.completion.iter().enumerate() {
|
||||||
|
let color = if prompt.completion_selection_index.is_some()
|
||||||
|
&& i == prompt.completion_selection_index.unwrap()
|
||||||
|
{
|
||||||
|
Style::default().bg(Color::Rgb(104, 060, 232))
|
||||||
|
} else {
|
||||||
|
self.text_color
|
||||||
|
};
|
||||||
|
self.surface.set_stringn(
|
||||||
|
1 + col * BASE_WIDTH,
|
||||||
|
self.size.1 - col_height - 2 + row,
|
||||||
|
&command,
|
||||||
|
BASE_WIDTH as usize - 1,
|
||||||
|
color,
|
||||||
|
);
|
||||||
|
row += 1;
|
||||||
|
if row > col_height - 1 {
|
||||||
|
row = 0;
|
||||||
|
col += 1;
|
||||||
|
}
|
||||||
|
if col > max_col {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
// render buffer text
|
// render buffer text
|
||||||
self.surface
|
self.surface
|
||||||
.set_string(1, self.size.1 - 1, &prompt.prompt, self.text_color);
|
.set_string(1, self.size.1 - 1, &prompt.prompt, self.text_color);
|
||||||
|
@ -309,10 +356,13 @@ impl Application {
|
||||||
|
|
||||||
if let Some(view) = &mut self.editor.view {
|
if let Some(view) = &mut self.editor.view {
|
||||||
self.terminal.render_view(view, viewport);
|
self.terminal.render_view(view, viewport);
|
||||||
}
|
if let Some(prompt) = &self.prompt {
|
||||||
|
if prompt.should_close {
|
||||||
if let Some(prompt) = &self.prompt {
|
self.prompt = None;
|
||||||
self.terminal.render_prompt(prompt);
|
} else {
|
||||||
|
self.terminal.render_prompt(view, prompt);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.terminal.draw();
|
self.terminal.draw();
|
||||||
|
@ -383,7 +433,46 @@ impl Application {
|
||||||
{
|
{
|
||||||
let prompt = Prompt::new(
|
let prompt = Prompt::new(
|
||||||
":".to_owned(),
|
":".to_owned(),
|
||||||
|_input: &str| None, // completion
|
|_input: &str| {
|
||||||
|
// TODO: i need this duplicate list right now to avoid borrow checker issues
|
||||||
|
let command_list = vec![
|
||||||
|
String::from("q"),
|
||||||
|
String::from("aaa"),
|
||||||
|
String::from("bbb"),
|
||||||
|
String::from("ccc"),
|
||||||
|
String::from("ddd"),
|
||||||
|
String::from("eee"),
|
||||||
|
String::from("averylongcommandaverylongcommandaverylongcommandaverylongcommandaverylongcommand"),
|
||||||
|
String::from("q"),
|
||||||
|
String::from("aaa"),
|
||||||
|
String::from("bbb"),
|
||||||
|
String::from("ccc"),
|
||||||
|
String::from("ddd"),
|
||||||
|
String::from("eee"),
|
||||||
|
String::from("q"),
|
||||||
|
String::from("aaa"),
|
||||||
|
String::from("bbb"),
|
||||||
|
String::from("ccc"),
|
||||||
|
String::from("ddd"),
|
||||||
|
String::from("eee"),
|
||||||
|
String::from("q"),
|
||||||
|
String::from("aaa"),
|
||||||
|
String::from("bbb"),
|
||||||
|
String::from("ccc"),
|
||||||
|
String::from("ddd"),
|
||||||
|
String::from("eee"),
|
||||||
|
String::from("q"),
|
||||||
|
String::from("aaa"),
|
||||||
|
String::from("bbb"),
|
||||||
|
String::from("ccc"),
|
||||||
|
String::from("ddd"),
|
||||||
|
String::from("eee"),
|
||||||
|
];
|
||||||
|
command_list
|
||||||
|
.into_iter()
|
||||||
|
.filter(|command| command.contains(_input))
|
||||||
|
.collect()
|
||||||
|
}, // completion
|
||||||
|editor: &mut Editor, input: &str| match input {
|
|editor: &mut Editor, input: &str| match input {
|
||||||
"q" => editor.should_close = true,
|
"q" => editor.should_close = true,
|
||||||
_ => (),
|
_ => (),
|
||||||
|
|
|
@ -307,23 +307,12 @@ pub fn append_mode(view: &mut View, _count: usize) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn command_mode(_view: &mut View, _count: usize) {
|
|
||||||
use crate::Editor;
|
|
||||||
|
|
||||||
let prompt = Prompt::new(
|
|
||||||
":".to_owned(),
|
|
||||||
|_input: &str| None, // completion
|
|
||||||
|editor: &mut Editor, input: &str| match input {
|
|
||||||
"q" => editor.should_close = true,
|
|
||||||
_ => (),
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
// set_prompt(prompt)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: I, A, o and O can share a lot of the primitives.
|
// TODO: I, A, o and O can share a lot of the primitives.
|
||||||
|
|
||||||
|
pub fn command_mode(_view: &mut View, _count: usize) {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
|
||||||
// calculate line numbers for each selection range
|
// calculate line numbers for each selection range
|
||||||
fn selection_lines(state: &State) -> Vec<usize> {
|
fn selection_lines(state: &State) -> Vec<usize> {
|
||||||
let mut lines = state
|
let mut lines = state
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
use crate::commands;
|
use crate::Editor;
|
||||||
use crate::{Editor, View};
|
|
||||||
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
|
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
|
||||||
use std::string::String;
|
use std::string::String;
|
||||||
|
|
||||||
|
@ -7,20 +6,26 @@ pub struct Prompt {
|
||||||
pub prompt: String,
|
pub prompt: String,
|
||||||
pub line: String,
|
pub line: String,
|
||||||
pub cursor: usize,
|
pub cursor: usize,
|
||||||
completion_fn: Box<dyn FnMut(&str) -> Option<Vec<&str>>>,
|
pub completion: Vec<String>,
|
||||||
|
pub should_close: bool,
|
||||||
|
pub completion_selection_index: Option<usize>,
|
||||||
|
completion_fn: Box<dyn FnMut(&str) -> Vec<String>>,
|
||||||
callback_fn: Box<dyn FnMut(&mut Editor, &str)>,
|
callback_fn: Box<dyn FnMut(&mut Editor, &str)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Prompt {
|
impl Prompt {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
prompt: String,
|
prompt: String,
|
||||||
completion_fn: impl FnMut(&str) -> Option<Vec<&str>> + 'static,
|
mut completion_fn: impl FnMut(&str) -> Vec<String> + 'static,
|
||||||
callback_fn: impl FnMut(&mut Editor, &str) + 'static,
|
callback_fn: impl FnMut(&mut Editor, &str) + 'static,
|
||||||
) -> Prompt {
|
) -> Prompt {
|
||||||
Prompt {
|
Prompt {
|
||||||
prompt,
|
prompt,
|
||||||
line: String::new(),
|
line: String::new(),
|
||||||
cursor: 0,
|
cursor: 0,
|
||||||
|
completion: completion_fn(""),
|
||||||
|
should_close: false,
|
||||||
|
completion_selection_index: 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),
|
||||||
}
|
}
|
||||||
|
@ -29,10 +34,12 @@ impl Prompt {
|
||||||
pub fn insert_char(&mut self, c: char) {
|
pub fn insert_char(&mut self, c: char) {
|
||||||
self.line.insert(self.cursor, c);
|
self.line.insert(self.cursor, c);
|
||||||
self.cursor += 1;
|
self.cursor += 1;
|
||||||
|
self.completion = (self.completion_fn)(&self.line);
|
||||||
|
self.exit_selection();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn move_char_left(&mut self) {
|
pub fn move_char_left(&mut self) {
|
||||||
if self.cursor > 1 {
|
if self.cursor > 0 {
|
||||||
self.cursor -= 1;
|
self.cursor -= 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -55,7 +62,22 @@ impl Prompt {
|
||||||
if self.cursor > 0 {
|
if self.cursor > 0 {
|
||||||
self.line.remove(self.cursor - 1);
|
self.line.remove(self.cursor - 1);
|
||||||
self.cursor -= 1;
|
self.cursor -= 1;
|
||||||
|
self.completion = (self.completion_fn)(&self.line);
|
||||||
}
|
}
|
||||||
|
self.exit_selection();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn change_completion_selection(&mut self) {
|
||||||
|
if self.completion.is_empty() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let index =
|
||||||
|
self.completion_selection_index.map(|i| i + 1).unwrap_or(0) % self.completion.len();
|
||||||
|
self.completion_selection_index = Some(index);
|
||||||
|
self.line = self.completion[index].clone();
|
||||||
|
}
|
||||||
|
pub fn exit_selection(&mut self) {
|
||||||
|
self.completion_selection_index = None;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_input(&mut self, key_event: KeyEvent, editor: &mut Editor) {
|
pub fn handle_input(&mut self, key_event: KeyEvent, editor: &mut Editor) {
|
||||||
|
@ -66,7 +88,7 @@ impl Prompt {
|
||||||
} => self.insert_char(c),
|
} => self.insert_char(c),
|
||||||
KeyEvent {
|
KeyEvent {
|
||||||
code: KeyCode::Esc, ..
|
code: KeyCode::Esc, ..
|
||||||
} => unimplemented!("Exit prompt!"),
|
} => self.should_close = true,
|
||||||
KeyEvent {
|
KeyEvent {
|
||||||
code: KeyCode::Right,
|
code: KeyCode::Right,
|
||||||
..
|
..
|
||||||
|
@ -85,12 +107,19 @@ impl Prompt {
|
||||||
} => self.move_start(),
|
} => self.move_start(),
|
||||||
KeyEvent {
|
KeyEvent {
|
||||||
code: KeyCode::Backspace,
|
code: KeyCode::Backspace,
|
||||||
..
|
modifiers: KeyModifiers::NONE,
|
||||||
} => self.delete_char_backwards(),
|
} => self.delete_char_backwards(),
|
||||||
KeyEvent {
|
KeyEvent {
|
||||||
code: KeyCode::Enter,
|
code: KeyCode::Enter,
|
||||||
..
|
..
|
||||||
} => (self.callback_fn)(editor, &self.line),
|
} => (self.callback_fn)(editor, &self.line),
|
||||||
|
KeyEvent {
|
||||||
|
code: KeyCode::Tab, ..
|
||||||
|
} => self.change_completion_selection(),
|
||||||
|
KeyEvent {
|
||||||
|
code: KeyCode::Char('q'),
|
||||||
|
modifiers: KeyModifiers::CONTROL,
|
||||||
|
} => self.exit_selection(),
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue