|
|
@ -1,6 +1,11 @@
|
|
|
|
use clap::ArgMatches as Args;
|
|
|
|
use clap::ArgMatches as Args;
|
|
|
|
use helix_core::{indent::TAB_WIDTH, state::Mode, syntax::HighlightEvent, Position, Range, State};
|
|
|
|
use helix_core::{indent::TAB_WIDTH, state::Mode, syntax::HighlightEvent, Position, Range, State};
|
|
|
|
use helix_view::{commands, keymap, prompt::Prompt, Editor, Theme, View};
|
|
|
|
use helix_view::{
|
|
|
|
|
|
|
|
commands,
|
|
|
|
|
|
|
|
keymap::{self, Keymaps},
|
|
|
|
|
|
|
|
prompt::Prompt,
|
|
|
|
|
|
|
|
Editor, Theme, View,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
use std::{
|
|
|
|
use std::{
|
|
|
|
borrow::Cow,
|
|
|
|
borrow::Cow,
|
|
|
@ -31,14 +36,16 @@ const OFFSET: u16 = 6; // 5 linenr + 1 gutter
|
|
|
|
|
|
|
|
|
|
|
|
type Terminal = tui::Terminal<CrosstermBackend<std::io::Stdout>>;
|
|
|
|
type Terminal = tui::Terminal<CrosstermBackend<std::io::Stdout>>;
|
|
|
|
|
|
|
|
|
|
|
|
static EX: smol::Executor = smol::Executor::new();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const BASE_WIDTH: u16 = 30;
|
|
|
|
const BASE_WIDTH: u16 = 30;
|
|
|
|
|
|
|
|
|
|
|
|
pub struct Application {
|
|
|
|
pub struct Application<'a> {
|
|
|
|
editor: Editor,
|
|
|
|
editor: Editor,
|
|
|
|
prompt: Option<Prompt>,
|
|
|
|
prompt: Option<Prompt>,
|
|
|
|
terminal: Renderer,
|
|
|
|
terminal: Renderer,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
keymap: Keymaps,
|
|
|
|
|
|
|
|
executor: &'a smol::Executor<'a>,
|
|
|
|
|
|
|
|
lsp: helix_lsp::Client,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct Renderer {
|
|
|
|
struct Renderer {
|
|
|
@ -235,7 +242,7 @@ 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, view: &View, prompt: &Prompt) {
|
|
|
|
pub fn render_prompt(&mut self, view: &View, prompt: &Prompt, theme: &Theme) {
|
|
|
|
// completion
|
|
|
|
// completion
|
|
|
|
if !prompt.completion.is_empty() {
|
|
|
|
if !prompt.completion.is_empty() {
|
|
|
|
// TODO: find out better way of clearing individual lines of the screen
|
|
|
|
// TODO: find out better way of clearing individual lines of the screen
|
|
|
@ -254,7 +261,7 @@ impl Renderer {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.surface.set_style(
|
|
|
|
self.surface.set_style(
|
|
|
|
Rect::new(0, self.size.1 - col_height - 2, self.size.0, col_height),
|
|
|
|
Rect::new(0, self.size.1 - col_height - 2, self.size.0, col_height),
|
|
|
|
view.theme.get("ui.statusline"),
|
|
|
|
theme.get("ui.statusline"),
|
|
|
|
);
|
|
|
|
);
|
|
|
|
for (i, command) in prompt.completion.iter().enumerate() {
|
|
|
|
for (i, command) in prompt.completion.iter().enumerate() {
|
|
|
|
let color = if prompt.completion_selection_index.is_some()
|
|
|
|
let color = if prompt.completion_selection_index.is_some()
|
|
|
@ -330,8 +337,8 @@ impl Renderer {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Application {
|
|
|
|
impl<'a> Application<'a> {
|
|
|
|
pub fn new(mut args: Args) -> Result<Self, Error> {
|
|
|
|
pub fn new(mut args: Args, executor: &'a smol::Executor<'a>) -> Result<Self, Error> {
|
|
|
|
let terminal = Renderer::new()?;
|
|
|
|
let terminal = Renderer::new()?;
|
|
|
|
let mut editor = Editor::new();
|
|
|
|
let mut editor = Editor::new();
|
|
|
|
|
|
|
|
|
|
|
@ -339,11 +346,18 @@ impl Application {
|
|
|
|
editor.open(file, terminal.size)?;
|
|
|
|
editor.open(file, terminal.size)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let lsp = helix_lsp::Client::start(&executor, "rust-analyzer", &[]);
|
|
|
|
|
|
|
|
|
|
|
|
let mut app = Self {
|
|
|
|
let mut app = Self {
|
|
|
|
editor,
|
|
|
|
editor,
|
|
|
|
terminal,
|
|
|
|
terminal,
|
|
|
|
// TODO; move to state
|
|
|
|
// TODO; move to state
|
|
|
|
prompt: None,
|
|
|
|
prompt: None,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
keymap: keymap::default(),
|
|
|
|
|
|
|
|
executor,
|
|
|
|
|
|
|
|
lsp,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Ok(app)
|
|
|
|
Ok(app)
|
|
|
@ -361,7 +375,7 @@ impl Application {
|
|
|
|
if prompt.should_close {
|
|
|
|
if prompt.should_close {
|
|
|
|
self.prompt = None;
|
|
|
|
self.prompt = None;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
self.terminal.render_prompt(view, prompt);
|
|
|
|
self.terminal.render_prompt(view, prompt, theme_ref);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -375,7 +389,13 @@ impl Application {
|
|
|
|
|
|
|
|
|
|
|
|
pub async fn event_loop(&mut self) {
|
|
|
|
pub async fn event_loop(&mut self) {
|
|
|
|
let mut reader = EventStream::new();
|
|
|
|
let mut reader = EventStream::new();
|
|
|
|
let keymap = keymap::default();
|
|
|
|
|
|
|
|
|
|
|
|
// initialize lsp
|
|
|
|
|
|
|
|
let res = self.lsp.initialize().await;
|
|
|
|
|
|
|
|
let res = self
|
|
|
|
|
|
|
|
.lsp
|
|
|
|
|
|
|
|
.text_document_did_open(&self.editor.view().unwrap().state)
|
|
|
|
|
|
|
|
.await;
|
|
|
|
|
|
|
|
|
|
|
|
self.render();
|
|
|
|
self.render();
|
|
|
|
|
|
|
|
|
|
|
@ -384,126 +404,149 @@ impl Application {
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Handle key events
|
|
|
|
use futures_util::{select, FutureExt};
|
|
|
|
match reader.next().await {
|
|
|
|
select! {
|
|
|
|
Some(Ok(Event::Resize(width, height))) => {
|
|
|
|
event = reader.next().fuse() => {
|
|
|
|
self.terminal.resize(width, height);
|
|
|
|
self.handle_terminal_events(event).await
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
notification = self.lsp.incoming.next().fuse() => {
|
|
|
|
|
|
|
|
self.handle_lsp_notification(notification).await
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: simplistic ensure cursor in view for now
|
|
|
|
pub async fn handle_terminal_events(
|
|
|
|
// TODO: loop over views
|
|
|
|
&mut self,
|
|
|
|
if let Some(view) = self.editor.view_mut() {
|
|
|
|
event: Option<Result<Event, crossterm::ErrorKind>>,
|
|
|
|
view.size = self.terminal.size;
|
|
|
|
) {
|
|
|
|
view.ensure_cursor_in_view()
|
|
|
|
// Handle key events
|
|
|
|
};
|
|
|
|
match event {
|
|
|
|
|
|
|
|
Some(Ok(Event::Resize(width, height))) => {
|
|
|
|
|
|
|
|
self.terminal.resize(width, height);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: simplistic ensure cursor in view for now
|
|
|
|
|
|
|
|
// TODO: loop over views
|
|
|
|
|
|
|
|
if let Some(view) = self.editor.view_mut() {
|
|
|
|
|
|
|
|
view.size = self.terminal.size;
|
|
|
|
|
|
|
|
view.ensure_cursor_in_view()
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.render();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(Ok(Event::Key(event))) => {
|
|
|
|
|
|
|
|
// if there's a prompt, it takes priority
|
|
|
|
|
|
|
|
if let Some(prompt) = &mut self.prompt {
|
|
|
|
|
|
|
|
self.prompt
|
|
|
|
|
|
|
|
.as_mut()
|
|
|
|
|
|
|
|
.unwrap()
|
|
|
|
|
|
|
|
.handle_input(event, &mut self.editor);
|
|
|
|
|
|
|
|
|
|
|
|
self.render();
|
|
|
|
self.render();
|
|
|
|
}
|
|
|
|
} else if let Some(view) = self.editor.view_mut() {
|
|
|
|
Some(Ok(Event::Key(event))) => {
|
|
|
|
let keys = vec![event];
|
|
|
|
// if there's a prompt, it takes priority
|
|
|
|
// TODO: sequences (`gg`)
|
|
|
|
if let Some(prompt) = &mut self.prompt {
|
|
|
|
// TODO: handle count other than 1
|
|
|
|
self.prompt
|
|
|
|
match view.state.mode() {
|
|
|
|
.as_mut()
|
|
|
|
Mode::Insert => {
|
|
|
|
.unwrap()
|
|
|
|
if let Some(command) = self.keymap[&Mode::Insert].get(&keys) {
|
|
|
|
.handle_input(event, &mut self.editor);
|
|
|
|
command(view, 1);
|
|
|
|
|
|
|
|
} else if let KeyEvent {
|
|
|
|
self.render();
|
|
|
|
code: KeyCode::Char(c),
|
|
|
|
} else if let Some(view) = self.editor.view_mut() {
|
|
|
|
..
|
|
|
|
let keys = vec![event];
|
|
|
|
} = event
|
|
|
|
// TODO: sequences (`gg`)
|
|
|
|
{
|
|
|
|
// TODO: handle count other than 1
|
|
|
|
commands::insert::insert_char(view, c);
|
|
|
|
match view.state.mode() {
|
|
|
|
|
|
|
|
Mode::Insert => {
|
|
|
|
|
|
|
|
if let Some(command) = keymap[&Mode::Insert].get(&keys) {
|
|
|
|
|
|
|
|
command(view, 1);
|
|
|
|
|
|
|
|
} else if let KeyEvent {
|
|
|
|
|
|
|
|
code: KeyCode::Char(c),
|
|
|
|
|
|
|
|
..
|
|
|
|
|
|
|
|
} = event
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
commands::insert::insert_char(view, c);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
view.ensure_cursor_in_view();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Mode::Normal => {
|
|
|
|
view.ensure_cursor_in_view();
|
|
|
|
if let &[KeyEvent {
|
|
|
|
}
|
|
|
|
code: KeyCode::Char(':'),
|
|
|
|
Mode::Normal => {
|
|
|
|
..
|
|
|
|
if let &[KeyEvent {
|
|
|
|
}] = keys.as_slice()
|
|
|
|
code: KeyCode::Char(':'),
|
|
|
|
{
|
|
|
|
..
|
|
|
|
let prompt = Prompt::new(
|
|
|
|
}] = keys.as_slice()
|
|
|
|
":".to_owned(),
|
|
|
|
{
|
|
|
|
|_input: &str| {
|
|
|
|
let prompt = Prompt::new(
|
|
|
|
// TODO: i need this duplicate list right now to avoid borrow checker issues
|
|
|
|
":".to_owned(),
|
|
|
|
let command_list = vec![
|
|
|
|
|_input: &str| {
|
|
|
|
String::from("q"),
|
|
|
|
// TODO: i need this duplicate list right now to avoid borrow checker issues
|
|
|
|
String::from("aaa"),
|
|
|
|
let command_list = vec![
|
|
|
|
String::from("bbb"),
|
|
|
|
String::from("q"),
|
|
|
|
String::from("ccc"),
|
|
|
|
String::from("aaa"),
|
|
|
|
String::from("ddd"),
|
|
|
|
String::from("bbb"),
|
|
|
|
String::from("eee"),
|
|
|
|
String::from("ccc"),
|
|
|
|
String::from("averylongcommandaverylongcommandaverylongcommandaverylongcommandaverylongcommand"),
|
|
|
|
String::from("ddd"),
|
|
|
|
String::from("q"),
|
|
|
|
String::from("eee"),
|
|
|
|
String::from("aaa"),
|
|
|
|
String::from("averylongcommandaverylongcommandaverylongcommandaverylongcommandaverylongcommand"),
|
|
|
|
String::from("bbb"),
|
|
|
|
String::from("q"),
|
|
|
|
String::from("ccc"),
|
|
|
|
String::from("aaa"),
|
|
|
|
String::from("ddd"),
|
|
|
|
String::from("bbb"),
|
|
|
|
String::from("eee"),
|
|
|
|
String::from("ccc"),
|
|
|
|
String::from("q"),
|
|
|
|
String::from("ddd"),
|
|
|
|
String::from("aaa"),
|
|
|
|
String::from("eee"),
|
|
|
|
String::from("bbb"),
|
|
|
|
String::from("q"),
|
|
|
|
String::from("ccc"),
|
|
|
|
String::from("aaa"),
|
|
|
|
String::from("ddd"),
|
|
|
|
String::from("bbb"),
|
|
|
|
String::from("eee"),
|
|
|
|
String::from("ccc"),
|
|
|
|
String::from("q"),
|
|
|
|
String::from("ddd"),
|
|
|
|
String::from("aaa"),
|
|
|
|
String::from("eee"),
|
|
|
|
String::from("bbb"),
|
|
|
|
String::from("q"),
|
|
|
|
String::from("ccc"),
|
|
|
|
String::from("aaa"),
|
|
|
|
String::from("ddd"),
|
|
|
|
String::from("bbb"),
|
|
|
|
String::from("eee"),
|
|
|
|
String::from("ccc"),
|
|
|
|
String::from("q"),
|
|
|
|
String::from("ddd"),
|
|
|
|
String::from("aaa"),
|
|
|
|
String::from("eee"),
|
|
|
|
String::from("bbb"),
|
|
|
|
String::from("q"),
|
|
|
|
String::from("ccc"),
|
|
|
|
String::from("aaa"),
|
|
|
|
String::from("ddd"),
|
|
|
|
String::from("bbb"),
|
|
|
|
String::from("eee"),
|
|
|
|
String::from("ccc"),
|
|
|
|
];
|
|
|
|
String::from("ddd"),
|
|
|
|
command_list
|
|
|
|
String::from("eee"),
|
|
|
|
.into_iter()
|
|
|
|
];
|
|
|
|
.filter(|command| command.contains(_input))
|
|
|
|
command_list
|
|
|
|
.collect()
|
|
|
|
.into_iter()
|
|
|
|
}, // completion
|
|
|
|
.filter(|command| command.contains(_input))
|
|
|
|
|editor: &mut Editor, input: &str| match input {
|
|
|
|
.collect()
|
|
|
|
"q" => editor.should_close = true,
|
|
|
|
}, // completion
|
|
|
|
_ => (),
|
|
|
|
|editor: &mut Editor, input: &str| match input {
|
|
|
|
},
|
|
|
|
"q" => editor.should_close = true,
|
|
|
|
);
|
|
|
|
_ => (),
|
|
|
|
|
|
|
|
},
|
|
|
|
self.prompt = Some(prompt);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// HAXX: special casing for command mode
|
|
|
|
self.prompt = Some(prompt);
|
|
|
|
} else if let Some(command) = keymap[&Mode::Normal].get(&keys) {
|
|
|
|
|
|
|
|
command(view, 1);
|
|
|
|
// HAXX: special casing for command mode
|
|
|
|
|
|
|
|
} else if let Some(command) = self.keymap[&Mode::Normal].get(&keys) {
|
|
|
|
// TODO: simplistic ensure cursor in view for now
|
|
|
|
command(view, 1);
|
|
|
|
view.ensure_cursor_in_view();
|
|
|
|
|
|
|
|
}
|
|
|
|
// TODO: simplistic ensure cursor in view for now
|
|
|
|
|
|
|
|
view.ensure_cursor_in_view();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mode => {
|
|
|
|
}
|
|
|
|
if let Some(command) = keymap[&mode].get(&keys) {
|
|
|
|
mode => {
|
|
|
|
command(view, 1);
|
|
|
|
if let Some(command) = self.keymap[&mode].get(&keys) {
|
|
|
|
|
|
|
|
command(view, 1);
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: simplistic ensure cursor in view for now
|
|
|
|
// TODO: simplistic ensure cursor in view for now
|
|
|
|
view.ensure_cursor_in_view();
|
|
|
|
view.ensure_cursor_in_view();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.render();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.render();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(Ok(Event::Mouse(_))) => (), // unhandled
|
|
|
|
|
|
|
|
Some(Err(x)) => panic!(x),
|
|
|
|
|
|
|
|
None => break,
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(Ok(Event::Mouse(_))) => (), // unhandled
|
|
|
|
|
|
|
|
Some(Err(x)) => panic!(x),
|
|
|
|
|
|
|
|
None => panic!(),
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub async fn handle_lsp_notification(&mut self, notification: Option<helix_lsp::Notification>) {
|
|
|
|
|
|
|
|
use helix_lsp::Notification;
|
|
|
|
|
|
|
|
match notification {
|
|
|
|
|
|
|
|
Some(Notification::PublishDiagnostics(params)) => unimplemented!("{:?}", params),
|
|
|
|
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|