Skip to content

Commit 733b438

Browse files
author
ZouYingjun
committed
实现跨文件全文搜索:默认保存目录范围 + QuickOpen 增强
1 parent 04793ec commit 733b438

12 files changed

Lines changed: 940 additions & 12 deletions

File tree

‎src-tauri/src/lib.rs‎

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,133 @@ fn delete_file(path: String) -> Result<(), String> {
155155
fs::remove_file(path).map_err(|e| e.to_string())
156156
}
157157

158+
#[derive(Serialize)]
159+
struct DirEntry {
160+
path: String,
161+
name: String,
162+
rel: String,
163+
size: u64,
164+
mtime_ms: u64,
165+
}
166+
167+
/// Skip these names during directory traversal (noise / VCS / cycles).
168+
const SKIP_NAMES: &[&str] = &["node_modules", ".git", ".svn", ".DS_Store", "target", "dist"];
169+
170+
/// Default text-like extensions (lowercase, no dot).
171+
const DEFAULT_TEXT_EXTS: &[&str] = &[
172+
"md", "markdown", "mdx", "txt", "log", "csv", "tsv", "json", "json5", "org", "rst",
173+
"html", "htm", "xml", "yml", "yaml", "toml", "ini", "cfg", "conf", "tex", "creole",
174+
];
175+
176+
/// Files larger than this are skipped during enumeration.
177+
const ENUM_BIG_FILE: u64 = 2_000_000;
178+
179+
/// Enumerate text files under `dir`. Skips hidden/VCS/junk, filters by extension,
180+
/// skips files larger than ~2MB and symlinks, caps the count, sorts by mtime desc.
181+
#[tauri::command]
182+
fn list_dir_files(
183+
dir: String,
184+
recursive: bool,
185+
max_files: Option<usize>,
186+
exts: Option<Vec<String>>,
187+
) -> Result<Vec<DirEntry>, String> {
188+
let root = std::path::Path::new(dir.trim_start_matches("file://"));
189+
if !root.is_dir() {
190+
return Err("not a directory".into());
191+
}
192+
let max = max_files.unwrap_or(1000);
193+
let allowed: Vec<String> = exts
194+
.map(|v| {
195+
v.into_iter()
196+
.map(|e| e.trim_start_matches('.').to_lowercase())
197+
.collect()
198+
})
199+
.unwrap_or_else(|| DEFAULT_TEXT_EXTS.iter().map(|s| s.to_string()).collect());
200+
let ext_set: std::collections::HashSet<String> = allowed.into_iter().collect();
201+
202+
let mut out: Vec<DirEntry> = Vec::new();
203+
// (base, current_dir)
204+
let mut stack: Vec<(std::path::PathBuf, std::path::PathBuf)> =
205+
vec![(root.to_path_buf(), root.to_path_buf())];
206+
while let Some((base, cur)) = stack.pop() {
207+
if out.len() >= max {
208+
break;
209+
}
210+
let entries = match fs::read_dir(&cur) {
211+
Ok(e) => e,
212+
Err(_) => continue,
213+
};
214+
for ent in entries {
215+
if out.len() >= max {
216+
break;
217+
}
218+
let ent = match ent {
219+
Ok(e) => e,
220+
Err(_) => continue,
221+
};
222+
let name = ent.file_name().to_string_lossy().to_string();
223+
// skip hidden and known junk
224+
if name.starts_with('.') || SKIP_NAMES.contains(&name.as_str()) {
225+
continue;
226+
}
227+
let ft = match ent.file_type() {
228+
Ok(t) => t,
229+
Err(_) => continue,
230+
};
231+
// Symlinks: skip (don't follow) to avoid cycles and dupes.
232+
if ft.is_symlink() {
233+
continue;
234+
}
235+
let path = ent.path();
236+
if ft.is_dir() {
237+
if recursive {
238+
stack.push((base.clone(), path));
239+
}
240+
continue;
241+
}
242+
if !ft.is_file() {
243+
continue;
244+
}
245+
let ext_ok = path
246+
.extension()
247+
.and_then(|e| e.to_str())
248+
.map(|e| ext_set.contains(&e.to_lowercase()))
249+
.unwrap_or(false);
250+
if !ext_ok {
251+
continue;
252+
}
253+
let meta = match fs::metadata(&path) {
254+
Ok(m) => m,
255+
Err(_) => continue,
256+
};
257+
if meta.len() > ENUM_BIG_FILE {
258+
continue;
259+
}
260+
let mtime_ms = meta
261+
.modified()
262+
.ok()
263+
.and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
264+
.map(|d| d.as_millis() as u64)
265+
.unwrap_or(0);
266+
let rel = path
267+
.strip_prefix(&base)
268+
.unwrap_or(&path)
269+
.to_string_lossy()
270+
.to_string();
271+
out.push(DirEntry {
272+
path: path.to_string_lossy().to_string(),
273+
name,
274+
rel,
275+
size: meta.len(),
276+
mtime_ms,
277+
});
278+
}
279+
}
280+
out.sort_by(|a, b| b.mtime_ms.cmp(&a.mtime_ms));
281+
out.truncate(max);
282+
Ok(out)
283+
}
284+
158285
#[tauri::command]
159286
fn write_file(
160287
path: String,
@@ -337,6 +464,7 @@ pub fn run() {
337464
file_mtime,
338465
rename_file,
339466
delete_file,
467+
list_dir_files,
340468
read_state,
341469
write_state,
342470
set_app_menu,

‎src/App.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { ContextMenu } from "@/components/ContextMenu";
2020
import { PageSetup } from "@/components/PageSetup";
2121
import { DiffDialog } from "@/components/DiffDialog";
2222
import { QuickOpen } from "@/components/QuickOpen";
23+
import { CrossSearch } from "@/components/CrossSearch";
2324
import { About, ConfirmDialog, GotoDialog, ReloadDialog, SessionNameDialog } from "@/components/Dialogs";
2425
import { HistoryPanel } from "@/components/HistoryPanel";
2526
import { ToastHost } from "@/components/ToastHost";
@@ -340,6 +341,7 @@ export function App() {
340341
)}
341342
<ContextMenu />
342343
<QuickOpen />
344+
<CrossSearch />
343345
<ConfirmDialog />
344346
<SessionNameDialog />
345347
<ReloadDialog />

0 commit comments

Comments
 (0)