Skip to content

Commit

Permalink
Gracefully handle accept in open mode without results
Browse files Browse the repository at this point in the history
  • Loading branch information
jmacdonald committed Nov 15, 2024
1 parent 44ee981 commit 35b68e5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/commands/search_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ pub fn accept(app: &mut Application) -> Result {
(selection.command)(app)?;
}
Mode::Open(ref mut mode) => {
if mode.selection().is_none() {
bail!("No buffer selected");
}

for DisplayablePath(path) in mode.selections() {
let syntax_definition = app
.preferences
Expand Down
25 changes: 25 additions & 0 deletions src/models/application/modes/open/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ impl OpenMode {
}

pub fn selections(&self) -> Vec<&DisplayablePath> {
if self.selection().is_none() {
return Vec::new();
}

let mut selections: Vec<&DisplayablePath> = self
.marked_results
.iter()
Expand Down Expand Up @@ -389,6 +393,27 @@ mod tests {
assert_eq!(mode.pinned_query(), "");
}

#[test]
fn selections_works_without_results() {
let path = env::current_dir().expect("can't get current directory/path");
let mut workspace = Workspace::new(&path, None).unwrap();
let config = SearchSelectConfig::default();
let mut mode = OpenMode::new(path.clone(), config.clone());
let (sender, receiver) = channel();

// Populate the index
mode.reset(&mut workspace, None, sender, config).unwrap();
if let Ok(Event::OpenModeIndexComplete(index)) = receiver.recv() {
mode.set_index(index);
}

mode.query().push_str("non-existent");
mode.search();

let selections: Vec<&DisplayablePath> = mode.selections().iter().copied().collect();
assert_eq!(selections, Vec::<&DisplayablePath>::new());
}

#[test]
fn selections_returns_current_selection() {
let path = env::current_dir().expect("can't get current directory/path");
Expand Down

0 comments on commit 35b68e5

Please sign in to comment.