Skip to content

Commit

Permalink
Display default results in search/select modes
Browse files Browse the repository at this point in the history
It's worth noting that we'll immediately exit search/select modes (bypassing
normal mode) despite default results.
  • Loading branch information
jmacdonald committed Nov 14, 2024
1 parent a32d6a3 commit 7592582
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 52 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ regex = "1.10"
[dependencies]
app_dirs2 = "2.5"
scribe = "0.8"
bloodhound = "0.5"
bloodhound = "0.5.5"
luthor = "0.2"
fragment = "0.3"
regex = "1.10"
Expand Down
8 changes: 4 additions & 4 deletions src/commands/search_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,11 @@ pub fn pop_search_token(app: &mut Application) -> Result {

pub fn step_back(app: &mut Application) -> Result {
let selection_available = match app.mode {
Mode::Command(ref mut mode) => mode.results().count() > 0,
Mode::Command(ref mut mode) => mode.results().count() > 0 && !mode.query().is_empty(),
Mode::Open(ref mut mode) => mode.results().count() > 0 && !mode.query().is_empty(),
Mode::Theme(ref mut mode) => mode.results().count() > 0,
Mode::SymbolJump(ref mut mode) => mode.results().count() > 0,
Mode::Syntax(ref mut mode) => mode.results().count() > 0,
Mode::Theme(ref mut mode) => mode.results().count() > 0 && !mode.query().is_empty(),
Mode::SymbolJump(ref mut mode) => mode.results().count() > 0 && !mode.query().is_empty(),
Mode::Syntax(ref mut mode) => mode.results().count() > 0 && !mode.query().is_empty(),
_ => bail!("Can't pop search token outside of search select mode"),
};

Expand Down
25 changes: 16 additions & 9 deletions src/models/application/modes/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,20 @@ impl SearchSelectMode for CommandMode {
type Item = DisplayableCommand;

fn search(&mut self) {
let commands: Vec<&'static str> = self.commands.keys().copied().collect();

// Find the commands we're looking for using the query.
let results = fragment::matching::find(&self.input, &commands, self.config.max_results);
let results = if self.input.is_empty() {
self.commands
.iter()
.take(self.config.max_results)
.map(|(k, v)| DisplayableCommand {
description: *k,
command: *v,
})
.collect()
} else {
let commands: Vec<&'static str> = self.commands.keys().copied().collect();

// We don't care about the result objects; we just want
// the underlying commands. Map the collection to get these.
self.results = SelectableVec::new(
results
fragment::matching::find(&self.input, &commands, self.config.max_results)
.into_iter()
.filter_map(|result| {
self.commands
Expand All @@ -64,8 +69,10 @@ impl SearchSelectMode for CommandMode {
command: *command,
})
})
.collect(),
);
.collect()
};

self.results = SelectableVec::new(results);
}

fn query(&mut self) -> &mut String {
Expand Down
42 changes: 24 additions & 18 deletions src/models/application/modes/open/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,15 @@ impl OpenMode {
}

fn collection(&self) -> &SelectableVec<DisplayablePath> {
if self.input.is_empty() {
if self.input.is_empty() && self.buffers.len() > 1 {
&self.buffers
} else {
&self.results
}
}

fn collection_mut(&mut self) -> &mut SelectableVec<DisplayablePath> {
if self.input.is_empty() {
if self.input.is_empty() && self.buffers.len() > 1 {
&mut self.buffers
} else {
&mut self.results
Expand All @@ -185,18 +185,26 @@ impl SearchSelectMode for OpenMode {

fn search(&mut self) {
let results = if let OpenModeIndex::Complete(ref index) = self.index {
index
.find(
&format!(
"{} {}",
self.pinned_input.to_lowercase(),
self.input.to_lowercase()
),
self.config.max_results,
)
.into_iter()
.map(|path| DisplayablePath(path.to_path_buf()))
.collect()
if self.input.is_empty() {
index
.iter()
.take(self.config.max_results)
.map(|path| DisplayablePath(path.to_path_buf()))
.collect()
} else {
index
.find(
&format!(
"{} {}",
self.pinned_input.to_lowercase(),
self.input.to_lowercase()
),
self.config.max_results,
)
.into_iter()
.map(|path| DisplayablePath(path.to_path_buf()))
.collect()
}
} else {
vec![]
};
Expand Down Expand Up @@ -242,16 +250,14 @@ impl SearchSelectMode for OpenMode {
}

fn message(&mut self) -> Option<String> {
// When multiple buffers are open, we show them instead of query prompts
// Show open buffers in empty state if there are more than one
if self.buffers.len() > 1 && self.query().is_empty() {
return None;
}

if let OpenModeIndex::Indexing(ref path) = self.index {
Some(format!("Indexing {}", path.to_string_lossy()))
} else if self.pinned_query().is_empty() && self.query().is_empty() {
Some(String::from("Enter a search query to start."))
} else if self.results().count() == 0 {
} else if !self.query().is_empty() && self.results().count() == 0 {
Some(String::from("No matching entries found."))
} else {
None
Expand Down
4 changes: 1 addition & 3 deletions src/models/application/modes/search_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ pub trait SearchSelectMode: Display {
fn select_next(&mut self);
fn config(&self) -> &SearchSelectConfig;
fn message(&mut self) -> Option<String> {
if self.query().is_empty() {
Some(String::from("Enter a search query to start."))
} else if self.results().count() == 0 {
if !self.query().is_empty() && self.results().count() == 0 {
Some(String::from("No matching entries found."))
} else {
None
Expand Down
19 changes: 14 additions & 5 deletions src/models/application/modes/symbol_jump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,20 @@ impl SearchSelectMode for SymbolJumpMode {

fn search(&mut self) {
// Find the symbols we're looking for using the query.
let results = fragment::matching::find(&self.input, &self.symbols, self.config.max_results);

// We don't care about the result objects; we just want
// the underlying symbols. Map the collection to get these.
self.results = SelectableVec::new(results.into_iter().map(|r| r.clone()).collect());
let results = if self.input.is_empty() {
self.symbols
.iter()
.take(self.config.max_results)
.cloned()
.collect()
} else {
fragment::matching::find(&self.input, &self.symbols, self.config.max_results)
.into_iter()
.map(|i| i.clone())
.collect()
};

self.results = SelectableVec::new(results);
}

fn query(&mut self) -> &mut String {
Expand Down
20 changes: 14 additions & 6 deletions src/models/application/modes/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,20 @@ impl SearchSelectMode for SyntaxMode {

fn search(&mut self) {
// Find the themes we're looking for using the query.
let results =
fragment::matching::find(&self.input, &self.syntaxes, self.config.max_results);

// We don't care about the result objects; we just want
// the underlying symbols. Map the collection to get these.
self.results = SelectableVec::new(results.into_iter().map(|r| r.clone()).collect());
let results = if self.input.is_empty() {
self.syntaxes
.iter()
.take(self.config.max_results)
.cloned()
.collect()
} else {
fragment::matching::find(&self.input, &self.syntaxes, self.config.max_results)
.into_iter()
.map(|i| i.clone())
.collect()
};

self.results = SelectableVec::new(results);
}

fn query(&mut self) -> &mut String {
Expand Down
17 changes: 13 additions & 4 deletions src/models/application/modes/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,20 @@ impl SearchSelectMode for ThemeMode {

fn search(&mut self) {
// Find the themes we're looking for using the query.
let results = fragment::matching::find(&self.input, &self.themes, self.config.max_results);
let results = if self.input.is_empty() {
self.themes
.iter()
.take(self.config.max_results)
.cloned()
.collect()
} else {
fragment::matching::find(&self.input, &self.themes, self.config.max_results)
.into_iter()
.map(|i| i.clone())
.collect()
};

// We don't care about the result objects; we just want
// the underlying symbols. Map the collection to get these.
self.results = SelectableVec::new(results.into_iter().map(|r| r.clone()).collect());
self.results = SelectableVec::new(results);
}

fn query(&mut self) -> &mut String {
Expand Down

0 comments on commit 7592582

Please sign in to comment.