on master help sign in
Simplify some code in editor.rs
6e62c3dBlaž Hrastnik
helix-term/src/application.rs+1−1
@@ -265,7 +265,7 @@ impl Application {
265 use crate::commands::{insert::idle_completion, Context};
265 use crate::commands::{insert::idle_completion, Context};
266 use helix_view::document::Mode;
266 use helix_view::document::Mode;
267
267
268− if doc_mut!(self.editor).mode != Mode::Insert || !self.config.editor.auto_completion {
268+ if doc!(self.editor).mode != Mode::Insert || !self.config.editor.auto_completion {
269 return;
269 return;
270 }
270 }
271 let editor_view = self
271 let editor_view = self
helix-view/src/editor.rs+23−31
@@ -19,7 +19,7 @@ use std::{
19
19
20use tokio::time::{sleep, Duration, Instant, Sleep};
20use tokio::time::{sleep, Duration, Instant, Sleep};
21
21
22−use anyhow::Error;
22+use anyhow::{bail, Context, Error};
23
23
24pub use helix_core::diagnostic::Severity;
24pub use helix_core::diagnostic::Severity;
25pub use helix_core::register::Registers;
25pub use helix_core::register::Registers;
@@ -188,8 +188,8 @@ pub enum Action {
188impl Editor {
188impl Editor {
189 pub fn new(
189 pub fn new(
190 mut area: Rect,
190 mut area: Rect,
191− themes: Arc<theme::Loader>,
191+ theme_loader: Arc<theme::Loader>,
192− config_loader: Arc<syntax::Loader>,
192+ syn_loader: Arc<syntax::Loader>,
193 config: Config,
193 config: Config,
194 ) -> Self {
194 ) -> Self {
195 let language_servers = helix_lsp::Registry::new();
195 let language_servers = helix_lsp::Registry::new();
@@ -199,15 +199,14 @@ impl Editor {
199
199
200 Self {
200 Self {
201 tree: Tree::new(area),
201 tree: Tree::new(area),
202− // Safety: 1 is non-zero
203 next_document_id: DocumentId::default(),
202 next_document_id: DocumentId::default(),
204 documents: BTreeMap::new(),
203 documents: BTreeMap::new(),
205 count: None,
204 count: None,
206 selected_register: None,
205 selected_register: None,
207− theme: themes.default(),
206+ theme: theme_loader.default(),
208 language_servers,
207 language_servers,
209− syn_loader: config_loader,
208+ syn_loader,
210− theme_loader: themes,
209+ theme_loader,
211 registers: Registers::default(),
210 registers: Registers::default(),
212 clipboard_provider: get_clipboard_provider(),
211 clipboard_provider: get_clipboard_provider(),
213 status_msg: None,
212 status_msg: None,
@@ -264,7 +263,6 @@ impl Editor {
264 }
263 }
265
264
266 pub fn set_theme_from_name(&mut self, theme: &str) -> anyhow::Result<()> {
265 pub fn set_theme_from_name(&mut self, theme: &str) -> anyhow::Result<()> {
267− use anyhow::Context;
268 let theme = self
266 let theme = self
269 .theme_loader
267 .theme_loader
270 .load(theme.as_ref())
268 .load(theme.as_ref())
@@ -343,23 +341,22 @@ impl Editor {
343 }
341 }
344 Action::Load => {
342 Action::Load => {
345 let view_id = view!(self).id;
343 let view_id = view!(self).id;
346− if let Some(doc) = self.document_mut(id) {
344+ let doc = self.documents.get_mut(&id).unwrap();
347− if doc.selections().is_empty() {
345+ if doc.selections().is_empty() {
348− doc.selections.insert(view_id, Selection::point(0));
346+ doc.selections.insert(view_id, Selection::point(0));
349− }
350 }
347 }
351 return;
348 return;
352 }
349 }
353− Action::HorizontalSplit => {
350+ Action::HorizontalSplit | Action::VerticalSplit => {
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 => {
361 let view = View::new(id);
351 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+ );
363 // initialize selection for view
360 // initialize selection for view
364 let doc = self.documents.get_mut(&id).unwrap();
361 let doc = self.documents.get_mut(&id).unwrap();
365 doc.selections.insert(view_id, Selection::point(0));
362 doc.selections.insert(view_id, Selection::point(0));
@@ -397,11 +394,7 @@ impl Editor {
397
394
398 pub fn open(&mut self, path: PathBuf, action: Action) -> Result<DocumentId, Error> {
395 pub fn open(&mut self, path: PathBuf, action: Action) -> Result<DocumentId, Error> {
399 let path = helix_core::path::get_canonicalized_path(&path)?;
396 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);
405
398
406 let id = if let Some(id) = id {
399 let id = if let Some(id) = id {
407 id
400 id
@@ -463,11 +456,11 @@ impl Editor {
463 pub fn close_document(&mut self, doc_id: DocumentId, force: bool) -> anyhow::Result<()> {
456 pub fn close_document(&mut self, doc_id: DocumentId, force: bool) -> anyhow::Result<()> {
464 let doc = match self.documents.get(&doc_id) {
457 let doc = match self.documents.get(&doc_id) {
465 Some(doc) => doc,
458 Some(doc) => doc,
466− None => anyhow::bail!("document does not exist"),
459+ None => bail!("document does not exist"),
467 };
460 };
468
461
469 if !force && doc.is_modified() {
462 if !force && doc.is_modified() {
470− anyhow::bail!(
463+ bail!(
471 "buffer {:?} is modified",
464 "buffer {:?} is modified",
472 doc.relative_path()
465 doc.relative_path()
473 .map(|path| path.to_string_lossy().to_string())
466 .map(|path| path.to_string_lossy().to_string())
@@ -500,7 +493,7 @@ impl Editor {
500 // If the document we removed was visible in all views, we will have no more views. We don't
493 // If the document we removed was visible in all views, we will have no more views. We don't
501 // want to close the editor just for a simple buffer close, so we need to create a new view
494 // want to close the editor just for a simple buffer close, so we need to create a new view
502 // containing either an existing document, or a brand new document.
495 // 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() {
504 let doc_id = self
497 let doc_id = self
505 .documents
498 .documents
506 .iter()
499 .iter()
@@ -585,8 +578,7 @@ impl Editor {
585 }
578 }
586
579
587 pub fn cursor(&self) -> (Option<Position>, CursorKind) {
580 pub fn cursor(&self) -> (Option<Position>, CursorKind) {
588− let view = view!(self);
581+ let (view, doc) = current_ref!(self);
589− let doc = &self.documents[&view.doc];
590 let cursor = doc
582 let cursor = doc
591 .selection(view.id)
583 .selection(view.id)
592 .primary()
584 .primary()