project_search: Add ability to search only for opened files (#16580)

Any suggestion?

Release Notes:
  - add ability for project search only for opend files.

---------

Co-authored-by: Conrad Irwin <conrad.irwin@gmail.com>
This commit is contained in:
CharlesChen0823 2024-08-29 11:41:29 +08:00 committed by GitHub
parent 403fdd6018
commit 400e503d9b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 192 additions and 20 deletions

View file

@ -30,6 +30,7 @@ pub struct SearchInputs {
query: Arc<str>,
files_to_include: PathMatcher,
files_to_exclude: PathMatcher,
buffers: Option<Vec<Model<Buffer>>>,
}
impl SearchInputs {
@ -42,6 +43,9 @@ impl SearchInputs {
pub fn files_to_exclude(&self) -> &PathMatcher {
&self.files_to_exclude
}
pub fn buffers(&self) -> &Option<Vec<Model<Buffer>>> {
&self.buffers
}
}
#[derive(Clone, Debug)]
pub enum SearchQuery {
@ -73,6 +77,7 @@ impl SearchQuery {
include_ignored: bool,
files_to_include: PathMatcher,
files_to_exclude: PathMatcher,
buffers: Option<Vec<Model<Buffer>>>,
) -> Result<Self> {
let query = query.to_string();
let search = AhoCorasickBuilder::new()
@ -82,6 +87,7 @@ impl SearchQuery {
query: query.into(),
files_to_exclude,
files_to_include,
buffers,
};
Ok(Self::Text {
search: Arc::new(search),
@ -100,6 +106,7 @@ impl SearchQuery {
include_ignored: bool,
files_to_include: PathMatcher,
files_to_exclude: PathMatcher,
buffers: Option<Vec<Model<Buffer>>>,
) -> Result<Self> {
let mut query = query.to_string();
let initial_query = Arc::from(query.as_str());
@ -120,6 +127,7 @@ impl SearchQuery {
query: initial_query,
files_to_exclude,
files_to_include,
buffers,
};
Ok(Self::Regex {
regex,
@ -141,6 +149,7 @@ impl SearchQuery {
message.include_ignored,
deserialize_path_matches(&message.files_to_include)?,
deserialize_path_matches(&message.files_to_exclude)?,
None,
)
} else {
Self::text(
@ -150,6 +159,7 @@ impl SearchQuery {
message.include_ignored,
deserialize_path_matches(&message.files_to_include)?,
deserialize_path_matches(&message.files_to_exclude)?,
None,
)
}
}
@ -163,6 +173,7 @@ impl SearchQuery {
message.include_ignored,
deserialize_path_matches(&message.files_to_include)?,
deserialize_path_matches(&message.files_to_exclude)?,
None, // search opened only don't need search remote
)
} else {
Self::text(
@ -172,6 +183,7 @@ impl SearchQuery {
message.include_ignored,
deserialize_path_matches(&message.files_to_include)?,
deserialize_path_matches(&message.files_to_exclude)?,
None, // search opened only don't need search remote
)
}
}
@ -420,6 +432,14 @@ impl SearchQuery {
self.as_inner().files_to_exclude()
}
pub fn buffers(&self) -> Option<&Vec<Model<Buffer>>> {
self.as_inner().buffers.as_ref()
}
pub fn is_opened_only(&self) -> bool {
self.as_inner().buffers.is_some()
}
pub fn filters_path(&self) -> bool {
!(self.files_to_exclude().sources().is_empty()
&& self.files_to_include().sources().is_empty())