@@ -104,6 +104,10 @@ pub fn normalize_path(path: &Path) -> PathBuf {
104104 ret
105105}
106106
107+pub fn canonicalize_path(path: &Path) -> std::io::Result<PathBuf> {
108+ std::env::current_dir().map(|current_dir| normalize_path(¤t_dir.join(path)))
109+}
110+
107111use helix_lsp::lsp;
108112use url::Url;
109113
@@ -133,13 +137,14 @@ impl Document {
133137
134138 // TODO: async fn?
135139 pub fn load(path: PathBuf) -> Result<Self, Error> {
136− use std::{env, 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))?;
140+ use std::{fs::File, io::BufReader};
141141
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+ };
143148
144149 let mut doc = Self::new(doc);
145150 // set the path and try detecting the language
@@ -192,6 +197,13 @@ impl Document {
192197
193198 async move {
194199 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+ }
195207 let mut file = File::create(path).await?;
196208
197209 // write all the rope chunks to file
@@ -220,16 +232,11 @@ impl Document {
220232 }
221233
222234 pub fn set_path(&mut self, path: &Path) -> Result<(), std::io::Error> {
223− // canonicalize path to absolute value
224− let current_dir = std::env::current_dir()?;
225− let path = normalize_path(¤t_dir.join(path));
226−
227− if let Some(parent) = path.parent() {
228− // TODO: return error as necessary
229− if parent.exists() {
230− self.path = Some(path);
231− }
232− }
235+ let path = canonicalize_path(path)?;
236+
237+ // if parent doesn't exist we still want to open the document
238+ // and error out when document is saved
239+ self.path = Some(path);
233240
234241 // try detecting the language based on filepath
235242 self.detect_language();