on master help sign in
Create document if it doesn't exist on save
14f511dWojciech Kępka
helix-view/src/document.rs+23−16
@@ -104,6 +104,10 @@ pub fn normalize_path(path: &Path) -> PathBuf {
104 ret
104 ret
105}
105}
106
106
107+pub fn canonicalize_path(path: &Path) -> std::io::Result<PathBuf> {
108+ std::env::current_dir().map(|current_dir| normalize_path(&current_dir.join(path)))
109+}
110+
107use helix_lsp::lsp;
111use helix_lsp::lsp;
108use url::Url;
112use url::Url;
109
113
@@ -133,13 +137,14 @@ impl Document {
133
137
134 // TODO: async fn?
138 // TODO: async fn?
135 pub fn load(path: PathBuf) -> Result<Self, Error> {
139 pub fn load(path: PathBuf) -> Result<Self, Error> {
136− use std::{env, fs::File, io::BufReader};
140+ use std::{fs::File, io::BufReader};
137− let _current_dir = env::current_dir()?;
138−
139− let file = File::open(path.clone()).context(format!("unable to open {:?}", path))?;
140− let doc = Rope::from_reader(BufReader::new(file))?;
141
141
142− // TODO: create if not found
142+ let doc = if !path.exists() {
143+ Rope::from("\n")
144+ } else {
145+ let file = File::open(&path).context(format!("unable to open {:?}", path))?;
146+ Rope::from_reader(BufReader::new(file))?
147+ };
143
148
144 let mut doc = Self::new(doc);
149 let mut doc = Self::new(doc);
145 // set the path and try detecting the language
150 // set the path and try detecting the language
@@ -192,6 +197,13 @@ impl Document {
192
197
193 async move {
198 async move {
194 use tokio::{fs::File, io::AsyncWriteExt};
199 use tokio::{fs::File, io::AsyncWriteExt};
200+ if let Some(parent) = path.parent() {
201+ if !parent.exists() {
202+ return Err(Error::msg(
203+ "can't save file, parent directory does not exist",
204+ ));
205+ }
206+ }
195 let mut file = File::create(path).await?;
207 let mut file = File::create(path).await?;
196
208
197 // write all the rope chunks to file
209 // write all the rope chunks to file
@@ -220,16 +232,11 @@ impl Document {
220 }
232 }
221
233
222 pub fn set_path(&mut self, path: &Path) -> Result<(), std::io::Error> {
234 pub fn set_path(&mut self, path: &Path) -> Result<(), std::io::Error> {
223− // canonicalize path to absolute value
235+ let path = canonicalize_path(path)?;
224− let current_dir = std::env::current_dir()?;
236+
225− let path = normalize_path(&current_dir.join(path));
237+ // if parent doesn't exist we still want to open the document
226−
238+ // and error out when document is saved
227− if let Some(parent) = path.parent() {
228− // TODO: return error as necessary
229− if parent.exists() {
230− self.path = Some(path);
239+ self.path = Some(path);
231− }
232− }
233
240
234 // try detecting the language based on filepath
241 // try detecting the language based on filepath
235 self.detect_language();
242 self.detect_language();
helix-view/src/editor.rs+1−1
@@ -137,7 +137,7 @@ impl Editor {
137 }
137 }
138
138
139 pub fn open(&mut self, path: PathBuf, action: Action) -> Result<DocumentId, Error> {
139 pub fn open(&mut self, path: PathBuf, action: Action) -> Result<DocumentId, Error> {
140− let path = std::fs::canonicalize(path)?;
140+ let path = crate::document::canonicalize_path(&path)?;
141
141
142 let id = self
142 let id = self
143 .documents()
143 .documents()