mirror of
https://github.com/helix-editor/helix.git
synced 2024-11-22 17:36:19 +04:00
Simplify some code in editor.rs
This commit is contained in:
parent
67bf4250ca
commit
6e62c3de47
@ -265,7 +265,7 @@ pub fn handle_idle_timeout(&mut self) {
|
|||||||
use crate::commands::{insert::idle_completion, Context};
|
use crate::commands::{insert::idle_completion, Context};
|
||||||
use helix_view::document::Mode;
|
use helix_view::document::Mode;
|
||||||
|
|
||||||
if doc_mut!(self.editor).mode != Mode::Insert || !self.config.editor.auto_completion {
|
if doc!(self.editor).mode != Mode::Insert || !self.config.editor.auto_completion {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let editor_view = self
|
let editor_view = self
|
||||||
|
@ -19,7 +19,7 @@
|
|||||||
|
|
||||||
use tokio::time::{sleep, Duration, Instant, Sleep};
|
use tokio::time::{sleep, Duration, Instant, Sleep};
|
||||||
|
|
||||||
use anyhow::Error;
|
use anyhow::{bail, Context, Error};
|
||||||
|
|
||||||
pub use helix_core::diagnostic::Severity;
|
pub use helix_core::diagnostic::Severity;
|
||||||
pub use helix_core::register::Registers;
|
pub use helix_core::register::Registers;
|
||||||
@ -188,8 +188,8 @@ pub enum Action {
|
|||||||
impl Editor {
|
impl Editor {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
mut area: Rect,
|
mut area: Rect,
|
||||||
themes: Arc<theme::Loader>,
|
theme_loader: Arc<theme::Loader>,
|
||||||
config_loader: Arc<syntax::Loader>,
|
syn_loader: Arc<syntax::Loader>,
|
||||||
config: Config,
|
config: Config,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let language_servers = helix_lsp::Registry::new();
|
let language_servers = helix_lsp::Registry::new();
|
||||||
@ -199,15 +199,14 @@ pub fn new(
|
|||||||
|
|
||||||
Self {
|
Self {
|
||||||
tree: Tree::new(area),
|
tree: Tree::new(area),
|
||||||
// Safety: 1 is non-zero
|
|
||||||
next_document_id: DocumentId::default(),
|
next_document_id: DocumentId::default(),
|
||||||
documents: BTreeMap::new(),
|
documents: BTreeMap::new(),
|
||||||
count: None,
|
count: None,
|
||||||
selected_register: None,
|
selected_register: None,
|
||||||
theme: themes.default(),
|
theme: theme_loader.default(),
|
||||||
language_servers,
|
language_servers,
|
||||||
syn_loader: config_loader,
|
syn_loader,
|
||||||
theme_loader: themes,
|
theme_loader,
|
||||||
registers: Registers::default(),
|
registers: Registers::default(),
|
||||||
clipboard_provider: get_clipboard_provider(),
|
clipboard_provider: get_clipboard_provider(),
|
||||||
status_msg: None,
|
status_msg: None,
|
||||||
@ -264,7 +263,6 @@ pub fn set_theme(&mut self, theme: Theme) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_theme_from_name(&mut self, theme: &str) -> anyhow::Result<()> {
|
pub fn set_theme_from_name(&mut self, theme: &str) -> anyhow::Result<()> {
|
||||||
use anyhow::Context;
|
|
||||||
let theme = self
|
let theme = self
|
||||||
.theme_loader
|
.theme_loader
|
||||||
.load(theme.as_ref())
|
.load(theme.as_ref())
|
||||||
@ -343,23 +341,22 @@ pub fn switch(&mut self, id: DocumentId, action: Action) {
|
|||||||
}
|
}
|
||||||
Action::Load => {
|
Action::Load => {
|
||||||
let view_id = view!(self).id;
|
let view_id = view!(self).id;
|
||||||
if let Some(doc) = self.document_mut(id) {
|
let doc = self.documents.get_mut(&id).unwrap();
|
||||||
if doc.selections().is_empty() {
|
if doc.selections().is_empty() {
|
||||||
doc.selections.insert(view_id, Selection::point(0));
|
doc.selections.insert(view_id, Selection::point(0));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Action::HorizontalSplit => {
|
Action::HorizontalSplit | Action::VerticalSplit => {
|
||||||
let view = View::new(id);
|
let view = View::new(id);
|
||||||
let view_id = self.tree.split(view, Layout::Horizontal);
|
let view_id = self.tree.split(
|
||||||
// initialize selection for view
|
view,
|
||||||
let doc = self.documents.get_mut(&id).unwrap();
|
match action {
|
||||||
doc.selections.insert(view_id, Selection::point(0));
|
Action::HorizontalSplit => Layout::Horizontal,
|
||||||
}
|
Action::VerticalSplit => Layout::Vertical,
|
||||||
Action::VerticalSplit => {
|
_ => unreachable!(),
|
||||||
let view = View::new(id);
|
},
|
||||||
let view_id = self.tree.split(view, Layout::Vertical);
|
);
|
||||||
// initialize selection for view
|
// initialize selection for view
|
||||||
let doc = self.documents.get_mut(&id).unwrap();
|
let doc = self.documents.get_mut(&id).unwrap();
|
||||||
doc.selections.insert(view_id, Selection::point(0));
|
doc.selections.insert(view_id, Selection::point(0));
|
||||||
@ -397,11 +394,7 @@ pub fn new_file_from_stdin(&mut self, action: Action) -> Result<DocumentId, Erro
|
|||||||
|
|
||||||
pub fn open(&mut self, path: PathBuf, action: Action) -> Result<DocumentId, Error> {
|
pub fn open(&mut self, path: PathBuf, action: Action) -> Result<DocumentId, Error> {
|
||||||
let path = helix_core::path::get_canonicalized_path(&path)?;
|
let path = helix_core::path::get_canonicalized_path(&path)?;
|
||||||
|
let id = self.document_by_path(&path).map(|doc| doc.id);
|
||||||
let id = self
|
|
||||||
.documents()
|
|
||||||
.find(|doc| doc.path() == Some(&path))
|
|
||||||
.map(|doc| doc.id);
|
|
||||||
|
|
||||||
let id = if let Some(id) = id {
|
let id = if let Some(id) = id {
|
||||||
id
|
id
|
||||||
@ -463,11 +456,11 @@ pub fn close(&mut self, id: ViewId) {
|
|||||||
pub fn close_document(&mut self, doc_id: DocumentId, force: bool) -> anyhow::Result<()> {
|
pub fn close_document(&mut self, doc_id: DocumentId, force: bool) -> anyhow::Result<()> {
|
||||||
let doc = match self.documents.get(&doc_id) {
|
let doc = match self.documents.get(&doc_id) {
|
||||||
Some(doc) => doc,
|
Some(doc) => doc,
|
||||||
None => anyhow::bail!("document does not exist"),
|
None => bail!("document does not exist"),
|
||||||
};
|
};
|
||||||
|
|
||||||
if !force && doc.is_modified() {
|
if !force && doc.is_modified() {
|
||||||
anyhow::bail!(
|
bail!(
|
||||||
"buffer {:?} is modified",
|
"buffer {:?} is modified",
|
||||||
doc.relative_path()
|
doc.relative_path()
|
||||||
.map(|path| path.to_string_lossy().to_string())
|
.map(|path| path.to_string_lossy().to_string())
|
||||||
@ -500,7 +493,7 @@ pub fn close_document(&mut self, doc_id: DocumentId, force: bool) -> anyhow::Res
|
|||||||
// If the document we removed was visible in all views, we will have no more views. We don't
|
// If the document we removed was visible in all views, we will have no more views. We don't
|
||||||
// want to close the editor just for a simple buffer close, so we need to create a new view
|
// want to close the editor just for a simple buffer close, so we need to create a new view
|
||||||
// containing either an existing document, or a brand new document.
|
// containing either an existing document, or a brand new document.
|
||||||
if self.tree.views().peekable().peek().is_none() {
|
if self.tree.views().next().is_none() {
|
||||||
let doc_id = self
|
let doc_id = self
|
||||||
.documents
|
.documents
|
||||||
.iter()
|
.iter()
|
||||||
@ -585,8 +578,7 @@ pub fn document_by_path_mut<P: AsRef<Path>>(&mut self, path: P) -> Option<&mut D
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn cursor(&self) -> (Option<Position>, CursorKind) {
|
pub fn cursor(&self) -> (Option<Position>, CursorKind) {
|
||||||
let view = view!(self);
|
let (view, doc) = current_ref!(self);
|
||||||
let doc = &self.documents[&view.doc];
|
|
||||||
let cursor = doc
|
let cursor = doc
|
||||||
.selection(view.id)
|
.selection(view.id)
|
||||||
.primary()
|
.primary()
|
||||||
|
Loading…
Reference in New Issue
Block a user