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

View file

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