aboutsummaryrefslogtreecommitdiff
path: root/helix-view
diff options
context:
space:
mode:
authorBlaž Hrastnik2021-04-06 10:02:22 +0000
committerBlaž Hrastnik2021-04-06 10:02:22 +0000
commit91462af546619740c93181b88a7908e481e6d6ab (patch)
tree24ba1aa03f68eda502c6831ff6f4d89dd1e0dfac /helix-view
parentf00cb15137fdff72c2f08c4c6f43bfa96933433f (diff)
Allow starting hx without a file. A new blank file will be created.
Diffstat (limited to 'helix-view')
-rw-r--r--helix-view/src/editor.rs92
1 files changed, 52 insertions, 40 deletions
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<DocumentId, Error> {
- 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<DocumentId, Error> {
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<DocumentId, Error> {
+ 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<DocumentId, Error> {
+ 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