From 91462af546619740c93181b88a7908e481e6d6ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Tue, 6 Apr 2021 19:02:22 +0900 Subject: [PATCH] Allow starting hx without a file. A new blank file will be created. --- helix-term/src/application.rs | 11 +++-- helix-term/src/main.rs | 2 +- helix-view/src/editor.rs | 92 ++++++++++++++++++++--------------- 3 files changed, 60 insertions(+), 45 deletions(-) diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index 53fd086b6..396bd5650 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -45,14 +45,17 @@ pub struct Application { impl Application { pub fn new(mut args: Args, executor: &'static smol::Executor<'static>) -> Result { + use helix_view::editor::Action; let mut compositor = Compositor::new()?; let size = compositor.size(); let mut editor = Editor::new(executor, size); - let files = args.values_of_t::("files").unwrap(); - for file in files { - use helix_view::editor::Action; - editor.open(file, Action::HorizontalSplit)?; + if let Ok(files) = args.values_of_t::("files") { + for file in files { + editor.open(file, Action::HorizontalSplit)?; + } + } else { + editor.new_file(Action::HorizontalSplit)?; } compositor.push(Box::new(ui::EditorView::new())); diff --git a/helix-term/src/main.rs b/helix-term/src/main.rs index 337929477..a2ea59933 100644 --- a/helix-term/src/main.rs +++ b/helix-term/src/main.rs @@ -56,7 +56,7 @@ fn main() { .arg( Arg::new("files") .about("Sets the input file to use") - .required(true) + .required(false) .multiple(true) .index(1), ) diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index a4ce2aca4..531571f7c 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -51,46 +51,7 @@ fn _refresh(&mut self) { } } - pub fn open(&mut self, path: PathBuf, action: Action) -> Result { - let id = self - .documents() - .find(|doc| doc.path() == Some(&path)) - .map(|doc| doc.id); - - let id = if let Some(id) = id { - id - } else { - let mut doc = Document::load(path, self.theme.scopes())?; - - // try to find a language server based on the language name - let language_server = doc - .language - .as_ref() - .and_then(|language| self.language_servers.get(language, self.executor)); - - if let Some(language_server) = language_server { - doc.set_language_server(Some(language_server.clone())); - - let language_id = doc - .language() - .and_then(|s| s.split('.').last()) // source.rust - .map(ToOwned::to_owned) - .unwrap_or_default(); - - smol::block_on(language_server.text_document_did_open( - doc.url().unwrap(), - doc.version(), - doc.text(), - language_id, - )) - .unwrap(); - } - - let id = self.documents.insert(doc); - self.documents[id].id = id; - id - }; - + fn _open(&mut self, id: DocumentId, action: Action) -> Result { use crate::tree::Layout; use helix_core::Selection; match action { @@ -134,6 +95,57 @@ pub fn open(&mut self, path: PathBuf, action: Action) -> Result Result { + use helix_core::Rope; + let doc = Document::new(Rope::from("\n")); + let id = self.documents.insert(doc); + self.documents[id].id = id; + self._open(id, action) + } + + pub fn open(&mut self, path: PathBuf, action: Action) -> Result { + let id = self + .documents() + .find(|doc| doc.path() == Some(&path)) + .map(|doc| doc.id); + + let id = if let Some(id) = id { + id + } else { + let mut doc = Document::load(path, self.theme.scopes())?; + + // try to find a language server based on the language name + let language_server = doc + .language + .as_ref() + .and_then(|language| self.language_servers.get(language, self.executor)); + + if let Some(language_server) = language_server { + doc.set_language_server(Some(language_server.clone())); + + let language_id = doc + .language() + .and_then(|s| s.split('.').last()) // source.rust + .map(ToOwned::to_owned) + .unwrap_or_default(); + + smol::block_on(language_server.text_document_did_open( + doc.url().unwrap(), + doc.version(), + doc.text(), + language_id, + )) + .unwrap(); + } + + let id = self.documents.insert(doc); + self.documents[id].id = id; + id + }; + + self._open(id, action) + } + pub fn close(&mut self, id: ViewId) { let view = self.tree.get(self.tree.focus); // get around borrowck issues