Allow opening of non-existant files, fixes #434

This commit is contained in:
Jeremy Soller 2025-10-14 09:58:44 -06:00
parent d795b1fca4
commit bce1d2aebc
No known key found for this signature in database
GPG key ID: 670FDFB5428E05CA
3 changed files with 130 additions and 146 deletions

View file

@ -31,7 +31,7 @@ use std::{
any::TypeId,
collections::HashMap,
env, fs, io,
path::{Path, PathBuf},
path::{self, Path, PathBuf},
process,
sync::{Mutex, OnceLock},
};
@ -648,10 +648,13 @@ impl App {
Some(path) => {
let canonical = match fs::canonicalize(&path) {
Ok(ok) => ok,
Err(err) => {
log::error!("failed to canonicalize {:?}: {}", path, err);
return None;
}
Err(err) => match path::absolute(&path) {
Ok(ok) => ok,
Err(_) => {
log::error!("failed to canonicalize {:?}: {}", path, err);
return None;
}
},
};
//TODO: allow files to be open multiple times

View file

@ -10,8 +10,8 @@ use notify::Watcher;
use regex::Regex;
use std::{
fs,
io::Write,
path::PathBuf,
io::{self, Write},
path::{self, PathBuf},
process::{Command, Stdio},
sync::{Arc, Mutex},
};
@ -101,20 +101,30 @@ impl EditorTab {
let mut editor = self.editor.lock().unwrap();
let mut font_system = font_system().write().unwrap();
let mut editor = editor.borrow_with(font_system.raw());
match editor.load_text(&path, self.attrs.clone()) {
let absolute = match fs::canonicalize(&path) {
Ok(ok) => ok,
Err(err) => match path::absolute(&path) {
Ok(ok) => ok,
Err(_) => {
log::error!("failed to canonicalize {:?}: {}", path, err);
path
}
},
};
match editor.load_text(&absolute, self.attrs.clone()) {
Ok(()) => {
log::info!("opened {:?}", path);
self.path_opt = match fs::canonicalize(&path) {
Ok(ok) => Some(ok),
Err(err) => {
log::error!("failed to canonicalize {:?}: {}", path, err);
Some(path)
}
};
log::info!("opened {:?}", absolute);
self.path_opt = Some(absolute);
}
Err(err) => {
log::error!("failed to open {:?}: {}", path, err);
self.path_opt = None;
if err.kind() == io::ErrorKind::NotFound {
log::warn!("opened non-existant file {:?}", absolute);
self.path_opt = Some(absolute);
editor.set_changed(true);
} else {
log::error!("failed to open {:?}: {}", absolute, err);
self.path_opt = None;
}
}
}
}