From 91462af546619740c93181b88a7908e481e6d6ab Mon Sep 17 00:00:00 2001 From: Blaž Hrastnik Date: Tue, 6 Apr 2021 19:02:22 +0900 Subject: Allow starting hx without a file. A new blank file will be created. --- helix-view/src/editor.rs | 92 +++++++++++++++++++++++++++--------------------- 1 file changed, 52 insertions(+), 40 deletions(-) (limited to 'helix-view') diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index a4ce2aca..531571f7 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -51,46 +51,7 @@ impl Editor { } } - 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 @@ impl Editor { Ok(id) } + pub fn new_file(&mut self, action: Action) -> 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 -- cgit v1.2.3-70-g09d2