Added file creation
This commit is contained in:
parent
ed2eece91b
commit
af403b9099
4 changed files with 23 additions and 4 deletions
|
|
@ -37,6 +37,7 @@ pub enum Action {
|
||||||
OpenFinder(Option<String>), // Open the finder, with the given default query
|
OpenFinder(Option<String>), // Open the finder, with the given default query
|
||||||
SwitchBuffer(BufferId), // Switch the current pane to the given buffer
|
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
|
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
|
CommandStart(&'static str), // Start a new command
|
||||||
GotoLine(isize), // Go to the specified file line
|
GotoLine(isize), // Go to the specified file line
|
||||||
BeginSearch(String), // Request to begin a search with the given needle
|
BeginSearch(String), // Request to begin a search with the given needle
|
||||||
|
|
|
||||||
|
|
@ -878,6 +878,11 @@ impl TryFrom<Args> for State {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl 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> {
|
pub fn open_or_get(&mut self, path: PathBuf) -> Result<BufferId, Error> {
|
||||||
let true_path = path.canonicalize()?;
|
let true_path = path.canonicalize()?;
|
||||||
if let Some((buffer_id, _)) = self.buffers.iter().find(|(_, b)| {
|
if let Some((buffer_id, _)) = self.buffers.iter().find(|(_, b)| {
|
||||||
|
|
|
||||||
|
|
@ -119,6 +119,15 @@ impl Element for Doc {
|
||||||
Action::Show(Some(format!("Could not open file")), format!("{err}")).into(),
|
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) => {
|
Some(Action::Save) => {
|
||||||
let event = buffer.save().err().map(|err| {
|
let event = buffer.save().err().map(|err| {
|
||||||
Action::Show(Some("Could not save file".to_string()), err.to_string()).into()
|
Action::Show(Some("Could not save file".to_string()), err.to_string()).into()
|
||||||
|
|
|
||||||
|
|
@ -397,11 +397,15 @@ impl Element<()> for Opener {
|
||||||
}
|
}
|
||||||
_ => match self.options.handle(state, event).map(Resp::into_ended) {
|
_ => match self.options.handle(state, event).map(Resp::into_ended) {
|
||||||
// Selecting a directory enters the directory
|
// Selecting a directory enters the directory
|
||||||
Ok(Some(file)) if matches!(file.kind, FileKind::Dir) => {
|
Ok(Some(file)) => match file.kind {
|
||||||
self.set_string(&format!("{}/", file.path.display()));
|
FileKind::Dir => {
|
||||||
Ok(Resp::handled(None))
|
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)),
|
Ok(None) => Ok(Resp::handled(None)),
|
||||||
Err(event) => {
|
Err(event) => {
|
||||||
let res = self
|
let res = self
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue