@@ -104,6 +104,10 @@ pub fn normalize_path(path: &Path) -> PathBuf {
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)))
107use helix_lsp::lsp;
111use helix_lsp::lsp;
108use url::Url;
112use url::Url;
@@ -133,13 +137,14 @@ impl Document {
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()?;
139− let file = File::open(path.clone()).context(format!("unable to open {:?}", path))?;
140− let doc = Rope::from_reader(BufReader::new(file))?;
142− // TODO: create if not found
142+ let doc = if !path.exists() {
145+ let file = File::open(&path).context(format!("unable to open {:?}", path))?;
146+ Rope::from_reader(BufReader::new(file))?
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 {
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",
195 let mut file = File::create(path).await?;
207 let mut file = File::create(path).await?;
197 // write all the rope chunks to file
209 // write all the rope chunks to file
@@ -220,16 +232,11 @@ impl Document {
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(¤t_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);
234 // try detecting the language based on filepath
241 // try detecting the language based on filepath
235 self.detect_language();
242 self.detect_language();