Added file creation

This commit is contained in:
Joshua Barretto 2025-09-24 14:45:25 +01:00
parent ed2eece91b
commit af403b9099
4 changed files with 23 additions and 4 deletions

View file

@ -37,6 +37,7 @@ pub enum Action {
OpenFinder(Option<String>), // Open the finder, with the given default query
SwitchBuffer(BufferId), // Switch the current pane to the given buffer
OpenFile(PathBuf, usize), // Open the file (on the given line) and switch the current pane to it
CreateFile(PathBuf), // Create a new file and switch the current pane to it
CommandStart(&'static str), // Start a new command
GotoLine(isize), // Go to the specified file line
BeginSearch(String), // Request to begin a search with the given needle

View file

@ -878,6 +878,11 @@ impl TryFrom<Args> for State {
}
impl State {
pub fn create_file(&mut self, path: PathBuf) -> Result<BufferId, Error> {
self.open_or_get(path.clone())
.or_else(|_| Ok(self.buffers.insert(Buffer::from_file(path)?)))
}
pub fn open_or_get(&mut self, path: PathBuf) -> Result<BufferId, Error> {
let true_path = path.canonicalize()?;
if let Some((buffer_id, _)) = self.buffers.iter().find(|(_, b)| {

View file

@ -119,6 +119,15 @@ impl Element for Doc {
Action::Show(Some(format!("Could not open file")), format!("{err}")).into(),
))),
},
Some(Action::CreateFile(path)) => match state.create_file(path) {
Ok(buffer_id) => {
self.switch_buffer(state, buffer_id);
Ok(Resp::handled(None))
}
Err(err) => Ok(Resp::handled(Some(
Action::Show(Some(format!("Could not create file")), format!("{err}")).into(),
))),
},
Some(Action::Save) => {
let event = buffer.save().err().map(|err| {
Action::Show(Some("Could not save file".to_string()), err.to_string()).into()

View file

@ -397,11 +397,15 @@ impl Element<()> for Opener {
}
_ => match self.options.handle(state, event).map(Resp::into_ended) {
// Selecting a directory enters the directory
Ok(Some(file)) if matches!(file.kind, FileKind::Dir) => {
self.set_string(&format!("{}/", file.path.display()));
Ok(Resp::handled(None))
Ok(Some(file)) => match file.kind {
FileKind::Dir => {
self.set_string(&format!("{}/", file.path.display()));
Ok(Resp::handled(None))
},
FileKind::File => Ok(Resp::end(Some(Action::OpenFile(file.path, 0).into()))),
FileKind::New => Ok(Resp::end(Some(Action::CreateFile(file.path).into()))),
FileKind::Unknown => Ok(Resp::handled(None)),
}
Ok(Some(file)) => Ok(Resp::end(Some(Action::OpenFile(file.path, 0).into()))),
Ok(None) => Ok(Resp::handled(None)),
Err(event) => {
let res = self