@@ -19,7 +19,7 @@ use std::{
1919
2020use tokio::time::{sleep, Duration, Instant, Sleep};
2121
22−use anyhow::Error;
22+use anyhow::{bail, Context, Error};
2323
2424pub use helix_core::diagnostic::Severity;
2525pub use helix_core::register::Registers;
@@ -188,8 +188,8 @@ pub enum Action {
188188impl Editor {
189189 pub fn new(
190190 mut area: Rect,
191− themes: Arc<theme::Loader>,
192− config_loader: Arc<syntax::Loader>,
191+ theme_loader: Arc<theme::Loader>,
192+ syn_loader: Arc<syntax::Loader>,
193193 config: Config,
194194 ) -> Self {
195195 let language_servers = helix_lsp::Registry::new();
@@ -199,15 +199,14 @@ impl Editor {
199199
200200 Self {
201201 tree: Tree::new(area),
202− // Safety: 1 is non-zero
203202 next_document_id: DocumentId::default(),
204203 documents: BTreeMap::new(),
205204 count: None,
206205 selected_register: None,
207− theme: themes.default(),
206+ theme: theme_loader.default(),
208207 language_servers,
209− syn_loader: config_loader,
210− theme_loader: themes,
208+ syn_loader,
209+ theme_loader,
211210 registers: Registers::default(),
212211 clipboard_provider: get_clipboard_provider(),
213212 status_msg: None,
@@ -264,7 +263,6 @@ impl Editor {
264263 }
265264
266265 pub fn set_theme_from_name(&mut self, theme: &str) -> anyhow::Result<()> {
267− use anyhow::Context;
268266 let theme = self
269267 .theme_loader
270268 .load(theme.as_ref())
@@ -343,23 +341,22 @@ impl Editor {
343341 }
344342 Action::Load => {
345343 let view_id = view!(self).id;
346− if let Some(doc) = self.document_mut(id) {
347− if doc.selections().is_empty() {
348− doc.selections.insert(view_id, Selection::point(0));
349− }
344+ let doc = self.documents.get_mut(&id).unwrap();
345+ if doc.selections().is_empty() {
346+ doc.selections.insert(view_id, Selection::point(0));
350347 }
351348 return;
352349 }
353− Action::HorizontalSplit => {
354− let view = View::new(id);
355− let view_id = self.tree.split(view, Layout::Horizontal);
356− // initialize selection for view
357− let doc = self.documents.get_mut(&id).unwrap();
358− doc.selections.insert(view_id, Selection::point(0));
359− }
360− Action::VerticalSplit => {
350+ Action::HorizontalSplit | Action::VerticalSplit => {
361351 let view = View::new(id);
362− let view_id = self.tree.split(view, Layout::Vertical);
352+ let view_id = self.tree.split(
353+ view,
354+ match action {
355+ Action::HorizontalSplit => Layout::Horizontal,
356+ Action::VerticalSplit => Layout::Vertical,
357+ _ => unreachable!(),
358+ },
359+ );
363360 // initialize selection for view
364361 let doc = self.documents.get_mut(&id).unwrap();
365362 doc.selections.insert(view_id, Selection::point(0));
@@ -397,11 +394,7 @@ impl Editor {
397394
398395 pub fn open(&mut self, path: PathBuf, action: Action) -> Result<DocumentId, Error> {
399396 let path = helix_core::path::get_canonicalized_path(&path)?;
400−
401− let id = self
402− .documents()
403− .find(|doc| doc.path() == Some(&path))
404− .map(|doc| doc.id);
397+ let id = self.document_by_path(&path).map(|doc| doc.id);
405398
406399 let id = if let Some(id) = id {
407400 id
@@ -463,11 +456,11 @@ impl Editor {
463456 pub fn close_document(&mut self, doc_id: DocumentId, force: bool) -> anyhow::Result<()> {
464457 let doc = match self.documents.get(&doc_id) {
465458 Some(doc) => doc,
466− None => anyhow::bail!("document does not exist"),
459+ None => bail!("document does not exist"),
467460 };
468461
469462 if !force && doc.is_modified() {
470− anyhow::bail!(
463+ bail!(
471464 "buffer {:?} is modified",
472465 doc.relative_path()
473466 .map(|path| path.to_string_lossy().to_string())
@@ -500,7 +493,7 @@ impl Editor {
500493 // If the document we removed was visible in all views, we will have no more views. We don't
501494 // want to close the editor just for a simple buffer close, so we need to create a new view
502495 // containing either an existing document, or a brand new document.
503− if self.tree.views().peekable().peek().is_none() {
496+ if self.tree.views().next().is_none() {
504497 let doc_id = self
505498 .documents
506499 .iter()
@@ -585,8 +578,7 @@ impl Editor {
585578 }
586579
587580 pub fn cursor(&self) -> (Option<Position>, CursorKind) {
588− let view = view!(self);
589− let doc = &self.documents[&view.doc];
581+ let (view, doc) = current_ref!(self);
590582 let cursor = doc
591583 .selection(view.id)
592584 .primary()