优化功能

This commit is contained in:
2026-07-10 17:44:50 +08:00
parent fed4110565
commit de5bd2fa01
6 changed files with 46 additions and 215 deletions

View File

@@ -334,23 +334,14 @@ fn rename_entry(path: String, new_name: String) -> Result<String, String> {
fn search_files(
workspace: String,
keyword: String,
case_sensitive: bool,
use_regex: bool,
whole_word: bool,
extensions: Vec<String>,
) -> Result<Vec<FileSearchResult>, String> {
if keyword.is_empty() {
return Ok(Vec::new());
}
let pattern = if use_regex {
keyword.clone()
} else {
regex::escape(&keyword)
};
let pattern = regex::escape(&keyword);
let mut builder = regex::RegexBuilder::new(&pattern);
builder.case_insensitive(!case_sensitive);
builder.case_insensitive(true);
let re = builder
.build()
.map_err(|e| format!("正则表达式无效: {}", e))?;
@@ -362,8 +353,6 @@ fn search_files(
workspace_path,
workspace_path,
&re,
whole_word,
&extensions,
&mut results,
)?;
@@ -375,8 +364,6 @@ fn walk_dir(
base: &std::path::Path,
dir: &std::path::Path,
re: &Regex,
whole_word: bool,
extensions: &[String],
results: &mut Vec<FileSearchResult>,
) -> Result<(), String> {
let entries = fs::read_dir(dir).map_err(|e| format!("无法读取目录: {}", e))?;
@@ -389,14 +376,14 @@ fn walk_dir(
let path = entry.path();
if path.is_dir() {
walk_dir(base, &path, re, whole_word, extensions, results)?;
walk_dir(base, &path, re, results)?;
} else if path.is_file() {
let ext = path
.extension()
.and_then(|e| e.to_str())
.unwrap_or("")
.to_lowercase();
if !extensions.iter().any(|e| e == &ext) {
if ext != "md" {
continue;
}
@@ -409,59 +396,41 @@ fn walk_dir(
let mut matches: Vec<SearchMatch> = Vec::new();
for (i, line) in lines.iter().enumerate() {
let has_match = if whole_word {
re.find_iter(line).any(|m| {
let start = m.start();
let end = m.end();
let prev_char = line[..start]
.chars()
.last()
.map(|c| !c.is_alphanumeric() && c != '_')
.unwrap_or(true);
let next_char = line[end..]
.chars()
.next()
.map(|c| !c.is_alphanumeric() && c != '_')
.unwrap_or(true);
prev_char && next_char
})
} else {
re.is_match(line)
};
if !re.is_match(line) {
continue;
}
if has_match {
let column = re
.find(line)
.map(|m| line[..m.start()].chars().count() + 1)
.unwrap_or(1);
let column = re
.find(line)
.map(|m| line[..m.start()].chars().count() + 1)
.unwrap_or(1);
let context_before: Vec<String> = (0..i)
.rev()
.take(2)
.map(|j| lines[j].to_string())
.collect::<Vec<_>>()
.into_iter()
.rev()
.collect();
let context_before: Vec<String> = (0..i)
.rev()
.take(2)
.map(|j| lines[j].to_string())
.collect::<Vec<_>>()
.into_iter()
.rev()
.collect();
let context_after: Vec<String> = lines
.iter()
.skip(i + 1)
.take(2)
.map(|s| s.to_string())
.collect();
let context_after: Vec<String> = lines
.iter()
.skip(i + 1)
.take(2)
.map(|s| s.to_string())
.collect();
matches.push(SearchMatch {
line: i + 1,
column,
line_text: line.to_string(),
context_before,
context_after,
});
matches.push(SearchMatch {
line: i + 1,
column,
line_text: line.to_string(),
context_before,
context_after,
});
if matches.len() >= 100 {
break;
}
if matches.len() >= 100 {
break;
}
}