feat: smartcase match workspace/symbol query - #884
Conversation
if query contains uppercase, do case-sensitive match; otherwise, ignore case
Summary of ChangesHello @phanen, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the workspace symbol search functionality by introducing a 'smartcase' matching mechanism. This improvement allows users to perform more intuitive searches: queries containing uppercase letters will trigger a case-sensitive match, while queries composed entirely of lowercase letters will result in a case-insensitive match. This change aims to provide a more flexible and user-friendly search experience for workspace symbols. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a 'smart case' matching for workspace symbol search, which is a great feature. The implementation is straightforward. I've left one comment with a suggestion to refactor the matching logic for better performance and to avoid redundant computations within loops. This involves creating a small helper struct to manage the query state.
| fn match_symbol(text: &str, query: &str) -> bool { | ||
| if query.chars().any(|c| c.is_uppercase()) { | ||
| text.contains(query) | ||
| } else { | ||
| text.to_lowercase().contains(&query.to_lowercase()) | ||
| } | ||
| } |
There was a problem hiding this comment.
This function is called inside loops in add_global_variable_symbols and add_type_symbols. This means that query.chars().any(|c| c.is_uppercase()) and query.to_lowercase() are executed on every iteration, which is inefficient as the query does not change inside the loops.
Additionally, text.to_lowercase() allocates a new string on every call in the case-insensitive branch, which could impact performance when searching through many symbols.
To improve this, I suggest introducing a struct that pre-processes the query. This avoids redundant work inside the loops and encapsulates the matching logic cleanly. Here's a possible refactoring:
First, you could define a SymbolMatcher struct to replace match_symbol:
struct SymbolMatcher {
query: String,
query_lower: String,
case_sensitive: bool,
}
impl SymbolMatcher {
fn new(query: String) -> Self {
let case_sensitive = query.chars().any(|c| c.is_uppercase());
let query_lower = if case_sensitive { String::new() } else { query.to_lowercase() };
Self { query, query_lower, case_sensitive }
}
fn is_match(&self, text: &str) -> bool {
if self.case_sensitive {
text.contains(&self.query)
} else {
// Note: text.to_lowercase() still allocates. For further optimization,
// a custom case-insensitive search or a crate like `caseless` could be used.
text.to_lowercase().contains(&self.query_lower)
}
}
}Then, you would use it in build_workspace_symbols and pass the matcher to the other functions:
// In build_workspace_symbols
let matcher = SymbolMatcher::new(query);
add_global_variable_symbols(&mut symbols, compilation, &matcher, &cancel_token)?;
add_type_symbols(&mut symbols, compilation, &matcher, &cancel_token)?;
// In add_global_variable_symbols (signature changed to accept &SymbolMatcher)
if matcher.is_match(decl.get_name()) {
// ...
}This refactoring would make the matching logic more efficient by processing the query only once.
There was a problem hiding this comment.
I think this AI modification makes a lot of sense.
if query contains uppercase, do case-sensitive match; otherwise, ignore case