diff --git a/src/editor.rs b/src/editor.rs index 3242cdb..7587bb8 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -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) { diff --git a/src/main.rs b/src/main.rs index 69ea7e1..8d9f59a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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(); } } }