|
|
|
@ -1,6 +1,11 @@
|
|
|
|
|
use clap::ArgMatches as Args;
|
|
|
|
|
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::{
|
|
|
|
|
borrow::Cow,
|
|
|
|
@ -31,14 +36,16 @@ const OFFSET: u16 = 6; // 5 linenr + 1 gutter
|
|
|
|
|
|
|
|
|
|
type Terminal = tui::Terminal<CrosstermBackend<std::io::Stdout>>;
|
|
|
|
|
|
|
|
|
|
static EX: smol::Executor = smol::Executor::new();
|
|
|
|
|
|
|
|
|
|
const BASE_WIDTH: u16 = 30;
|
|
|
|
|
|
|
|
|
|
pub struct Application {
|
|
|
|
|
pub struct Application<'a> {
|
|
|
|
|
editor: Editor,
|
|
|
|
|
prompt: Option<Prompt>,
|
|
|
|
|
terminal: Renderer,
|
|
|
|
|
|
|
|
|
|
keymap: Keymaps,
|
|
|
|
|
executor: &'a smol::Executor<'a>,
|
|
|
|
|
lsp: helix_lsp::Client,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct Renderer {
|
|
|
|
@ -235,7 +242,7 @@ impl Renderer {
|
|
|
|
|
.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
|
|
|
|
|
if !prompt.completion.is_empty() {
|
|
|
|
|
// TODO: find out better way of clearing individual lines of the screen
|
|
|
|
@ -254,7 +261,7 @@ impl Renderer {
|
|
|
|
|
}
|
|
|
|
|
self.surface.set_style(
|
|
|
|
|
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() {
|
|
|
|
|
let color = if prompt.completion_selection_index.is_some()
|
|
|
|
@ -330,8 +337,8 @@ impl Renderer {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Application {
|
|
|
|
|
pub fn new(mut args: Args) -> Result<Self, Error> {
|
|
|
|
|
impl<'a> Application<'a> {
|
|
|
|
|
pub fn new(mut args: Args, executor: &'a smol::Executor<'a>) -> Result<Self, Error> {
|
|
|
|
|
let terminal = Renderer::new()?;
|
|
|
|
|
let mut editor = Editor::new();
|
|
|
|
|
|
|
|
|
@ -339,11 +346,18 @@ impl Application {
|
|
|
|
|
editor.open(file, terminal.size)?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let lsp = helix_lsp::Client::start(&executor, "rust-analyzer", &[]);
|
|
|
|
|
|
|
|
|
|
let mut app = Self {
|
|
|
|
|
editor,
|
|
|
|
|
terminal,
|
|
|
|
|
// TODO; move to state
|
|
|
|
|
prompt: None,
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
keymap: keymap::default(),
|
|
|
|
|
executor,
|
|
|
|
|
lsp,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Ok(app)
|
|
|
|
@ -361,7 +375,7 @@ impl Application {
|
|
|
|
|
if prompt.should_close {
|
|
|
|
|
self.prompt = None;
|
|
|
|
|
} 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) {
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
@ -384,8 +404,24 @@ impl Application {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
use futures_util::{select, FutureExt};
|
|
|
|
|
select! {
|
|
|
|
|
event = reader.next().fuse() => {
|
|
|
|
|
self.handle_terminal_events(event).await
|
|
|
|
|
}
|
|
|
|
|
notification = self.lsp.incoming.next().fuse() => {
|
|
|
|
|
self.handle_lsp_notification(notification).await
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn handle_terminal_events(
|
|
|
|
|
&mut self,
|
|
|
|
|
event: Option<Result<Event, crossterm::ErrorKind>>,
|
|
|
|
|
) {
|
|
|
|
|
// Handle key events
|
|
|
|
|
match reader.next().await {
|
|
|
|
|
match event {
|
|
|
|
|
Some(Ok(Event::Resize(width, height))) => {
|
|
|
|
|
self.terminal.resize(width, height);
|
|
|
|
|
|
|
|
|
@ -413,7 +449,7 @@ impl Application {
|
|
|
|
|
// TODO: handle count other than 1
|
|
|
|
|
match view.state.mode() {
|
|
|
|
|
Mode::Insert => {
|
|
|
|
|
if let Some(command) = keymap[&Mode::Insert].get(&keys) {
|
|
|
|
|
if let Some(command) = self.keymap[&Mode::Insert].get(&keys) {
|
|
|
|
|
command(view, 1);
|
|
|
|
|
} else if let KeyEvent {
|
|
|
|
|
code: KeyCode::Char(c),
|
|
|
|
@ -481,7 +517,7 @@ impl Application {
|
|
|
|
|
self.prompt = Some(prompt);
|
|
|
|
|
|
|
|
|
|
// HAXX: special casing for command mode
|
|
|
|
|
} else if let Some(command) = keymap[&Mode::Normal].get(&keys) {
|
|
|
|
|
} else if let Some(command) = self.keymap[&Mode::Normal].get(&keys) {
|
|
|
|
|
command(view, 1);
|
|
|
|
|
|
|
|
|
|
// TODO: simplistic ensure cursor in view for now
|
|
|
|
@ -489,7 +525,7 @@ impl Application {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
mode => {
|
|
|
|
|
if let Some(command) = keymap[&mode].get(&keys) {
|
|
|
|
|
if let Some(command) = self.keymap[&mode].get(&keys) {
|
|
|
|
|
command(view, 1);
|
|
|
|
|
|
|
|
|
|
// TODO: simplistic ensure cursor in view for now
|
|
|
|
@ -502,8 +538,15 @@ impl Application {
|
|
|
|
|
}
|
|
|
|
|
Some(Ok(Event::Mouse(_))) => (), // unhandled
|
|
|
|
|
Some(Err(x)) => panic!(x),
|
|
|
|
|
None => break,
|
|
|
|
|
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!(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|