Hold a reference to executor on the Editor type.

This commit is contained in:
Blaž Hrastnik 2021-03-16 23:14:51 +09:00
parent 8dc0b18e35
commit 51c15da3c3
5 changed files with 18 additions and 20 deletions

View file

@ -36,11 +36,11 @@ impl Application {
pub fn new(mut args: Args, executor: &'static smol::Executor<'static>) -> Result<Self, Error> { pub fn new(mut args: Args, executor: &'static smol::Executor<'static>) -> Result<Self, Error> {
let mut compositor = Compositor::new()?; let mut compositor = Compositor::new()?;
let size = compositor.size(); let size = compositor.size();
let mut editor = Editor::new(size); let mut editor = Editor::new(executor, size);
let files = args.values_of_t::<PathBuf>("files").unwrap(); let files = args.values_of_t::<PathBuf>("files").unwrap();
for file in files { for file in files {
editor.open(file, executor)?; editor.open(file)?;
} }
compositor.push(Box::new(ui::EditorView::new())); compositor.push(Box::new(ui::EditorView::new()));

View file

@ -28,7 +28,6 @@ use helix_lsp::lsp;
pub struct Context<'a> { pub struct Context<'a> {
pub count: usize, pub count: usize,
pub editor: &'a mut Editor, pub editor: &'a mut Editor,
pub executor: &'static smol::Executor<'static>,
pub callback: Option<crate::compositor::Callback>, pub callback: Option<crate::compositor::Callback>,
pub on_next_key_callback: Option<Box<dyn FnOnce(&mut Context, KeyEvent)>>, pub on_next_key_callback: Option<Box<dyn FnOnce(&mut Context, KeyEvent)>>,
@ -658,7 +657,6 @@ const COMMAND_LIST: &[&str] = &["write", "open", "quit"];
// 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(cx: &mut Context) { pub fn command_mode(cx: &mut Context) {
let executor = cx.executor;
let prompt = Prompt::new( let prompt = Prompt::new(
":".to_owned(), ":".to_owned(),
|input: &str| { |input: &str| {
@ -695,11 +693,11 @@ pub fn command_mode(cx: &mut Context) {
match *parts.as_slice() { match *parts.as_slice() {
["q"] | ["quit"] => { ["q"] | ["quit"] => {
editor.close(editor.view().id, executor); editor.close(editor.view().id);
// editor.should_close = true, // editor.should_close = true,
} }
["o", path] | ["open", path] => { ["o", path] | ["open", path] => {
editor.open(path.into(), executor); editor.open(path.into());
} }
["w"] | ["write"] => { ["w"] | ["write"] => {
// TODO: non-blocking via save() command // TODO: non-blocking via save() command
@ -713,7 +711,7 @@ pub fn command_mode(cx: &mut Context) {
cx.push_layer(Box::new(prompt)); cx.push_layer(Box::new(prompt));
} }
pub fn file_picker(cx: &mut Context) { pub fn file_picker(cx: &mut Context) {
let picker = ui::file_picker("./", cx.executor); let picker = ui::file_picker("./");
cx.push_layer(Box::new(picker)); cx.push_layer(Box::new(picker));
} }
@ -851,15 +849,13 @@ pub fn exit_select_mode(cx: &mut Context) {
} }
fn goto(cx: &mut Context, locations: Vec<lsp::Location>) { fn goto(cx: &mut Context, locations: Vec<lsp::Location>) {
let executor = cx.executor;
let doc = cx.doc(); let doc = cx.doc();
doc.mode = Mode::Normal; doc.mode = Mode::Normal;
match locations.as_slice() { match locations.as_slice() {
[location] => { [location] => {
cx.editor cx.editor.open(PathBuf::from(location.uri.path()));
.open(PathBuf::from(location.uri.path()), cx.executor);
let doc = cx.doc(); let doc = cx.doc();
let definition_pos = location.range.start; let definition_pos = location.range.start;
let new_pos = helix_lsp::util::lsp_pos_to_pos(doc.text().slice(..), definition_pos); let new_pos = helix_lsp::util::lsp_pos_to_pos(doc.text().slice(..), definition_pos);
@ -875,7 +871,7 @@ fn goto(cx: &mut Context, locations: Vec<lsp::Location>) {
format!("{}:{}", file, line).into() format!("{}:{}", file, line).into()
}, },
move |editor: &mut Editor, item| { move |editor: &mut Editor, item| {
editor.open(PathBuf::from(item.uri.path()), &executor); editor.open(PathBuf::from(item.uri.path()));
let mut doc = &mut editor.view_mut().doc; let mut doc = &mut editor.view_mut().doc;
let definition_pos = item.range.start; let definition_pos = item.range.start;
let new_pos = let new_pos =
@ -1274,7 +1270,7 @@ pub fn save(cx: &mut Context) {
// Spawns an async task to actually do the saving. This way we prevent blocking. // Spawns an async task to actually do the saving. This way we prevent blocking.
// TODO: handle save errors somehow? // TODO: handle save errors somehow?
cx.executor.spawn(cx.doc().save()).detach(); cx.editor.executor.spawn(cx.doc().save()).detach();
} }
pub fn completion(cx: &mut Context) { pub fn completion(cx: &mut Context) {

View file

@ -436,7 +436,6 @@ impl Component for EditorView {
let mode = view.doc.mode(); let mode = view.doc.mode();
let mut cxt = commands::Context { let mut cxt = commands::Context {
executor: cx.executor,
editor: &mut cx.editor, editor: &mut cx.editor,
count: 1, count: 1,
callback: None, callback: None,

View file

@ -73,7 +73,7 @@ pub fn regex_prompt(
} }
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
pub fn file_picker(root: &str, ex: &'static smol::Executor) -> Picker<PathBuf> { pub fn file_picker(root: &str) -> Picker<PathBuf> {
use ignore::Walk; use ignore::Walk;
// TODO: determine root based on git root // TODO: determine root based on git root
let files = Walk::new(root).filter_map(|entry| match entry { let files = Walk::new(root).filter_map(|entry| match entry {
@ -98,7 +98,7 @@ pub fn file_picker(root: &str, ex: &'static smol::Executor) -> Picker<PathBuf> {
path.strip_prefix("./").unwrap().to_str().unwrap().into() path.strip_prefix("./").unwrap().to_str().unwrap().into()
}, },
move |editor: &mut Editor, path: &PathBuf| { move |editor: &mut Editor, path: &PathBuf| {
editor.open(path.into(), ex); editor.open(path.into());
}, },
) )
} }

View file

@ -14,10 +14,11 @@ pub struct Editor {
pub count: Option<usize>, pub count: Option<usize>,
pub theme: Theme, pub theme: Theme,
pub language_servers: helix_lsp::Registry, pub language_servers: helix_lsp::Registry,
pub executor: &'static smol::Executor<'static>,
} }
impl Editor { impl Editor {
pub fn new(mut area: tui::layout::Rect) -> Self { pub fn new(executor: &'static smol::Executor<'static>, mut area: tui::layout::Rect) -> Self {
let theme = Theme::default(); let theme = Theme::default();
let language_servers = helix_lsp::Registry::new(); let language_servers = helix_lsp::Registry::new();
@ -29,10 +30,11 @@ impl Editor {
count: None, count: None,
theme, theme,
language_servers, language_servers,
executor,
} }
} }
pub fn open(&mut self, path: PathBuf, executor: &smol::Executor) -> Result<(), Error> { pub fn open(&mut self, path: PathBuf) -> Result<(), Error> {
let existing_view = self let existing_view = self
.tree .tree
.views() .views()
@ -49,7 +51,7 @@ impl Editor {
let language_server = doc let language_server = doc
.language .language
.as_ref() .as_ref()
.and_then(|language| self.language_servers.get(language, &executor)); .and_then(|language| self.language_servers.get(language, self.executor));
if let Some(language_server) = language_server { if let Some(language_server) = language_server {
doc.set_language_server(Some(language_server.clone())); doc.set_language_server(Some(language_server.clone()));
@ -74,17 +76,18 @@ impl Editor {
Ok(()) Ok(())
} }
pub fn close(&mut self, id: Key, executor: &smol::Executor) { pub fn close(&mut self, id: Key) {
let view = self.tree.get(self.tree.focus); let view = self.tree.get(self.tree.focus);
// get around borrowck issues // get around borrowck issues
let language_servers = &mut self.language_servers; let language_servers = &mut self.language_servers;
let executor = self.executor;
let doc = &view.doc; let doc = &view.doc;
let language_server = doc let language_server = doc
.language .language
.as_ref() .as_ref()
.and_then(|language| language_servers.get(language, &executor)); .and_then(|language| language_servers.get(language, executor));
if let Some(language_server) = language_server { if let Some(language_server) = language_server {
smol::block_on(language_server.text_document_did_close(doc.identifier())).unwrap(); smol::block_on(language_server.text_document_did_close(doc.identifier())).unwrap();