Allow starting hx without a file. A new blank file will be created.

pull/11/head
Blaž Hrastnik 4 years ago
parent f00cb15137
commit 91462af546

@ -45,14 +45,17 @@ pub struct Application {
impl Application { impl Application {
pub fn new(mut args: Args, executor: &'static smol::Executor<'static>) -> Result<Self, Error> { pub fn new(mut args: Args, executor: &'static smol::Executor<'static>) -> Result<Self, Error> {
use helix_view::editor::Action;
let mut compositor = Compositor::new()?; let mut compositor = Compositor::new()?;
let size = compositor.size(); let size = compositor.size();
let mut editor = Editor::new(executor, size); let mut editor = Editor::new(executor, size);
let files = args.values_of_t::<PathBuf>("files").unwrap(); if let Ok(files) = args.values_of_t::<PathBuf>("files") {
for file in files { for file in files {
use helix_view::editor::Action; editor.open(file, Action::HorizontalSplit)?;
editor.open(file, Action::HorizontalSplit)?; }
} else {
editor.new_file(Action::HorizontalSplit)?;
} }
compositor.push(Box::new(ui::EditorView::new())); compositor.push(Box::new(ui::EditorView::new()));

@ -56,7 +56,7 @@ fn main() {
.arg( .arg(
Arg::new("files") Arg::new("files")
.about("Sets the input file to use") .about("Sets the input file to use")
.required(true) .required(false)
.multiple(true) .multiple(true)
.index(1), .index(1),
) )

@ -51,46 +51,7 @@ impl Editor {
} }
} }
pub fn open(&mut self, path: PathBuf, action: Action) -> Result<DocumentId, Error> { fn _open(&mut self, id: DocumentId, 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
};
use crate::tree::Layout; use crate::tree::Layout;
use helix_core::Selection; use helix_core::Selection;
match action { match action {
@ -134,6 +95,57 @@ impl Editor {
Ok(id) 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) { pub fn close(&mut self, id: ViewId) {
let view = self.tree.get(self.tree.focus); let view = self.tree.get(self.tree.focus);
// get around borrowck issues // get around borrowck issues

Loading…
Cancel
Save