prevent opening the same file multiple times

This commit is contained in:
Crispy 2023-03-11 22:48:38 +01:00
parent c4f0d85e24
commit f17a78f088
2 changed files with 23 additions and 8 deletions

View file

@ -93,6 +93,10 @@ impl Editor {
"untitled".into() "untitled".into()
} }
pub fn path(&self) -> Option<&PathBuf> {
self.path.as_ref()
}
pub fn has_unsaved_changes(&self) -> bool { pub fn has_unsaved_changes(&self) -> bool {
self.unsaved_changes self.unsaved_changes
} }
@ -240,7 +244,7 @@ impl Editor {
self.cursor.line -= 1; self.cursor.line -= 1;
self.cursor.column = self.current_line().len(); self.cursor.column = self.current_line().len();
} }
self.scroll_to_cursor() self.scroll_to_cursor();
} }
fn move_right(&mut self) { fn move_right(&mut self) {
@ -250,7 +254,7 @@ impl Editor {
self.cursor.line += 1; self.cursor.line += 1;
self.cursor.column = 0; self.cursor.column = 0;
} }
self.scroll_to_cursor() self.scroll_to_cursor();
} }
fn move_up(&mut self, lines: usize) { fn move_up(&mut self, lines: usize) {

View file

@ -169,13 +169,24 @@ impl Navigator {
env::set_current_dir(&self.path).unwrap(); env::set_current_dir(&self.path).unwrap();
self.selected = self.editors.len(); self.selected = self.editors.len();
} else if path.is_file() { } else if path.is_file() {
if let Some(editor) = let path = path.canonicalize().unwrap();
Editor::open_file(self.clipboard.clone(), path.canonicalize().unwrap()) self.selected = self.editors.len();
{ for (i, editor) in self.editors.iter().enumerate() {
self.selected = self.editors.len(); if editor.path() == Some(&path) {
self.editors.push(editor); self.selected = i;
self.open_selected(); break;
}
} }
if self.selected == self.editors.len() {
if let Some(editor) =
Editor::open_file(self.clipboard.clone(), path.canonicalize().unwrap())
{
self.editors.push(editor);
} else {
return;
}
}
self.open_selected();
} }
} }
} }