on master help sign in
Allow starting hx without a file. A new blank file will be created.
91462afBlaž Hrastnik
helix-term/src/application.rs+7−4
@@ -45,14 +45,17 @@ pub struct Application {
45
45
46impl Application {
46impl Application {
47 pub fn new(mut args: Args, executor: &'static smol::Executor<'static>) -> Result<Self, Error> {
47 pub fn new(mut args: Args, executor: &'static smol::Executor<'static>) -> Result<Self, Error> {
48+ use helix_view::editor::Action;
48 let mut compositor = Compositor::new()?;
49 let mut compositor = Compositor::new()?;
49 let size = compositor.size();
50 let size = compositor.size();
50 let mut editor = Editor::new(executor, size);
51 let mut editor = Editor::new(executor, size);
51
52
52− let files = args.values_of_t::<PathBuf>("files").unwrap();
53+ if let Ok(files) = args.values_of_t::<PathBuf>("files") {
53− for file in files {
54+ for file in files {
54− use helix_view::editor::Action;
55− editor.open(file, Action::HorizontalSplit)?;
55+ editor.open(file, Action::HorizontalSplit)?;
56+ }
57+ } else {
58+ editor.new_file(Action::HorizontalSplit)?;
56 }
59 }
57
60
58 compositor.push(Box::new(ui::EditorView::new()));
61 compositor.push(Box::new(ui::EditorView::new()));
helix-term/src/main.rs+1−1
@@ -56,7 +56,7 @@ fn main() {
56 .arg(
56 .arg(
57 Arg::new("files")
57 Arg::new("files")
58 .about("Sets the input file to use")
58 .about("Sets the input file to use")
59− .required(true)
59+ .required(false)
60 .multiple(true)
60 .multiple(true)
61 .index(1),
61 .index(1),
62 )
62 )
helix-view/src/editor.rs+52−40
@@ -51,46 +51,7 @@ impl Editor {
51 }
51 }
52 }
52 }
53
53
54− pub fn open(&mut self, path: PathBuf, action: Action) -> Result<DocumentId, Error> {
54+ fn _open(&mut self, id: DocumentId, action: Action) -> Result<DocumentId, Error> {
55− let id = self
56− .documents()
57− .find(|doc| doc.path() == Some(&path))
58− .map(|doc| doc.id);
59−
60− let id = if let Some(id) = id {
61− id
62− } else {
63− let mut doc = Document::load(path, self.theme.scopes())?;
64−
65− // try to find a language server based on the language name
66− let language_server = doc
67− .language
68− .as_ref()
69− .and_then(|language| self.language_servers.get(language, self.executor));
70−
71− if let Some(language_server) = language_server {
72− doc.set_language_server(Some(language_server.clone()));
73−
74− let language_id = doc
75− .language()
76− .and_then(|s| s.split('.').last()) // source.rust
77− .map(ToOwned::to_owned)
78− .unwrap_or_default();
79−
80− smol::block_on(language_server.text_document_did_open(
81− doc.url().unwrap(),
82− doc.version(),
83− doc.text(),
84− language_id,
85− ))
86− .unwrap();
87− }
88−
89− let id = self.documents.insert(doc);
90− self.documents[id].id = id;
91− id
92− };
93−
94 use crate::tree::Layout;
55 use crate::tree::Layout;
95 use helix_core::Selection;
56 use helix_core::Selection;
96 match action {
57 match action {
@@ -134,6 +95,57 @@ impl Editor {
134 Ok(id)
95 Ok(id)
135 }
96 }
136
97
98+ pub fn new_file(&mut self, action: Action) -> Result<DocumentId, Error> {
99+ use helix_core::Rope;
100+ let doc = Document::new(Rope::from("\n"));
101+ let id = self.documents.insert(doc);
102+ self.documents[id].id = id;
103+ self._open(id, action)
104+ }
105+
106+ pub fn open(&mut self, path: PathBuf, action: Action) -> Result<DocumentId, Error> {
107+ let id = self
108+ .documents()
109+ .find(|doc| doc.path() == Some(&path))
110+ .map(|doc| doc.id);
111+
112+ let id = if let Some(id) = id {
113+ id
114+ } else {
115+ let mut doc = Document::load(path, self.theme.scopes())?;
116+
117+ // try to find a language server based on the language name
118+ let language_server = doc
119+ .language
120+ .as_ref()
121+ .and_then(|language| self.language_servers.get(language, self.executor));
122+
123+ if let Some(language_server) = language_server {
124+ doc.set_language_server(Some(language_server.clone()));
125+
126+ let language_id = doc
127+ .language()
128+ .and_then(|s| s.split('.').last()) // source.rust
129+ .map(ToOwned::to_owned)
130+ .unwrap_or_default();
131+
132+ smol::block_on(language_server.text_document_did_open(
133+ doc.url().unwrap(),
134+ doc.version(),
135+ doc.text(),
136+ language_id,
137+ ))
138+ .unwrap();
139+ }
140+
141+ let id = self.documents.insert(doc);
142+ self.documents[id].id = id;
143+ id
144+ };
145+
146+ self._open(id, action)
147+ }
148+
137 pub fn close(&mut self, id: ViewId) {
149 pub fn close(&mut self, id: ViewId) {
138 let view = self.tree.get(self.tree.focus);
150 let view = self.tree.get(self.tree.focus);
139 // get around borrowck issues
151 // get around borrowck issues