优化功能
This commit is contained in:
@@ -55,8 +55,8 @@
|
||||
},
|
||||
"topic_20260710-063053_353d579222d3b70b": {
|
||||
"stage": 3,
|
||||
"userTurns": 3,
|
||||
"userTurns": 4,
|
||||
"basisHash": "aaae120b1f33c2f2",
|
||||
"updatedAt": 1783668572932
|
||||
"updatedAt": 1783674217586
|
||||
}
|
||||
}
|
||||
@@ -9,5 +9,6 @@
|
||||
"topic_20260710-034033_6432ee6566f7b432": 1783654833341,
|
||||
"topic_20260710-034724_3c6fb29d8d0a4119": 1783655244536,
|
||||
"topic_20260710-054543_5e7c427932aecd7b": 1783662343716,
|
||||
"topic_20260710-063053_353d579222d3b70b": 1783665053391
|
||||
"topic_20260710-063053_353d579222d3b70b": 1783665053391,
|
||||
"topic_20260710-090003_60717bd90da5ec68": 1783674003004
|
||||
}
|
||||
@@ -47,5 +47,6 @@
|
||||
"topic_20260710-034033_6432ee6566f7b432": "auto",
|
||||
"topic_20260710-034724_3c6fb29d8d0a4119": "auto",
|
||||
"topic_20260710-054543_5e7c427932aecd7b": "auto",
|
||||
"topic_20260710-063053_353d579222d3b70b": "auto"
|
||||
"topic_20260710-063053_353d579222d3b70b": "auto",
|
||||
"topic_20260710-090003_60717bd90da5ec68": "auto"
|
||||
}
|
||||
@@ -47,5 +47,6 @@
|
||||
"topic_20260710-034033_6432ee6566f7b432": "我在点击目录树的时候新增文件夹和新建…",
|
||||
"topic_20260710-034724_3c6fb29d8d0a4119": "目录树中鼠标悬浮弹框dropmenu…",
|
||||
"topic_20260710-054543_5e7c427932aecd7b": "现在在markdown的编辑区域中…",
|
||||
"topic_20260710-063053_353d579222d3b70b": "这个应用里有一个全局搜索的功能,帮我…"
|
||||
"topic_20260710-063053_353d579222d3b70b": "这个应用里有一个全局搜索的功能,帮我…",
|
||||
"topic_20260710-090003_60717bd90da5ec68": "新的会话"
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -83,10 +83,6 @@ const MAX_SEARCH_HISTORY = 10;
|
||||
const SEARCH_DEBOUNCE_MS = 300;
|
||||
|
||||
const searchKeyword = ref("");
|
||||
const searchCaseSensitive = ref(false);
|
||||
const searchUseRegex = ref(false);
|
||||
const searchWholeWord = ref(false);
|
||||
const searchExtensions = ref<string[]>(["md"]);
|
||||
const searchResults = ref<FileSearchResult[]>([]);
|
||||
const searchLoading = ref(false);
|
||||
const searchError = ref("");
|
||||
@@ -95,21 +91,6 @@ const searchInputRef = ref<HTMLInputElement | null>(null);
|
||||
const searchFocusedFile = ref<string | null>(null);
|
||||
let searchDebounceTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
|
||||
const searchableTypes = [
|
||||
{ label: "Markdown (.md)", value: "md" },
|
||||
{ label: "纯文本 (.txt)", value: "txt" },
|
||||
{ label: "JSON (.json)", value: "json" },
|
||||
{ label: "YAML (.yaml/.yml)", value: "yaml" },
|
||||
{ label: "JavaScript (.js)", value: "js" },
|
||||
{ label: "TypeScript (.ts)", value: "ts" },
|
||||
{ label: "CSS (.css)", value: "css" },
|
||||
{ label: "HTML (.html)", value: "html" },
|
||||
{ label: "XML (.xml)", value: "xml" },
|
||||
];
|
||||
|
||||
const showExtensionDropdown = ref(false);
|
||||
const extensionDropdownRef = ref<HTMLElement | null>(null);
|
||||
|
||||
const totalMatchCount = computed(() => {
|
||||
return searchResults.value.reduce((sum, f) => sum + f.match_count, 0);
|
||||
});
|
||||
@@ -150,10 +131,6 @@ async function performSearch() {
|
||||
const results = await invoke<FileSearchResult[]>("search_files", {
|
||||
workspace: props.folderPath,
|
||||
keyword,
|
||||
caseSensitive: searchCaseSensitive.value,
|
||||
useRegex: searchUseRegex.value,
|
||||
wholeWord: searchWholeWord.value,
|
||||
extensions: searchExtensions.value,
|
||||
});
|
||||
searchResults.value = results;
|
||||
if (results.length === 0) {
|
||||
@@ -181,18 +158,6 @@ function clearSearch() {
|
||||
searchFocusedFile.value = null;
|
||||
}
|
||||
|
||||
function toggleExtension(ext: string) {
|
||||
const idx = searchExtensions.value.indexOf(ext);
|
||||
if (idx >= 0) {
|
||||
if (searchExtensions.value.length > 1) {
|
||||
searchExtensions.value.splice(idx, 1);
|
||||
}
|
||||
} else {
|
||||
searchExtensions.value = [...searchExtensions.value, ext];
|
||||
}
|
||||
triggerSearch();
|
||||
}
|
||||
|
||||
function handleSearchKeydown(e: KeyboardEvent) {
|
||||
if (e.key === "Escape") {
|
||||
clearSearch();
|
||||
@@ -231,13 +196,10 @@ function activateSearch() {
|
||||
|
||||
function highlightMatchText(text: string): string {
|
||||
const keyword = searchKeyword.value.trim();
|
||||
if (!keyword || searchUseRegex.value) {
|
||||
return escapeHtml(text);
|
||||
}
|
||||
if (!keyword) return escapeHtml(text);
|
||||
const escaped = keyword.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
try {
|
||||
const flags = searchCaseSensitive.value ? "g" : "gi";
|
||||
const re = new RegExp(`(${escaped})`, flags);
|
||||
const re = new RegExp(`(${escaped})`, "gi");
|
||||
return escapeHtml(text).replace(re, "<mark class='search-highlight'>$1</mark>");
|
||||
} catch {
|
||||
return escapeHtml(text);
|
||||
@@ -251,21 +213,7 @@ function escapeHtml(text: string): string {
|
||||
.replace(/>/g, ">");
|
||||
}
|
||||
|
||||
function closeExtensionDropdown(e: MouseEvent) {
|
||||
if (extensionDropdownRef.value && !extensionDropdownRef.value.contains(e.target as Node)) {
|
||||
showExtensionDropdown.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
watch(showExtensionDropdown, (open) => {
|
||||
if (open) {
|
||||
setTimeout(() => document.addEventListener("click", closeExtensionDropdown), 0);
|
||||
} else {
|
||||
document.removeEventListener("click", closeExtensionDropdown);
|
||||
}
|
||||
});
|
||||
|
||||
watch([searchKeyword, searchCaseSensitive, searchUseRegex, searchWholeWord], () => {
|
||||
watch(searchKeyword, () => {
|
||||
triggerSearch();
|
||||
});
|
||||
|
||||
@@ -703,64 +651,7 @@ function handleCreateRootFile() {
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<!-- 搜索选项 -->
|
||||
<div class="flex items-center gap-2 mt-2 flex-wrap">
|
||||
<button
|
||||
class="search-option-btn"
|
||||
:class="{ 'search-option-active': searchCaseSensitive }"
|
||||
title="区分大小写"
|
||||
@click="searchCaseSensitive = !searchCaseSensitive"
|
||||
>
|
||||
Aa
|
||||
</button>
|
||||
<button
|
||||
class="search-option-btn"
|
||||
:class="{ 'search-option-active': searchUseRegex }"
|
||||
title="正则表达式"
|
||||
@click="searchUseRegex = !searchUseRegex"
|
||||
>
|
||||
.*
|
||||
</button>
|
||||
<button
|
||||
class="search-option-btn"
|
||||
:class="{ 'search-option-active': searchWholeWord }"
|
||||
title="全词匹配"
|
||||
@click="searchWholeWord = !searchWholeWord"
|
||||
>
|
||||
<i class="ri-font-size"></i>
|
||||
</button>
|
||||
|
||||
<!-- 文件类型下拉 -->
|
||||
<div ref="extensionDropdownRef" class="relative">
|
||||
<button
|
||||
class="search-option-btn flex items-center gap-1"
|
||||
@click="showExtensionDropdown = !showExtensionDropdown"
|
||||
title="文件类型"
|
||||
>
|
||||
<span class="text-[10px]">.{{ searchExtensions.join(', .') }}</span>
|
||||
<i class="ri-arrow-down-s-line text-[9px]"></i>
|
||||
</button>
|
||||
<div
|
||||
v-if="showExtensionDropdown"
|
||||
class="search-ext-dropdown absolute top-full left-0 mt-1 bg-[var(--app-surface)] border border-[var(--app-border)] rounded-md shadow-lg z-30 py-1 min-w-[150px] max-h-52 overflow-y-auto"
|
||||
@click.stop
|
||||
>
|
||||
<label
|
||||
v-for="type in searchableTypes"
|
||||
:key="type.value"
|
||||
class="flex items-center gap-2 px-3 py-1.5 text-xs cursor-pointer hover:bg-[var(--app-hover)]"
|
||||
>
|
||||
<input
|
||||
type="checkbox"
|
||||
:checked="searchExtensions.includes(type.value)"
|
||||
class="w-3 h-3 accent-[var(--app-accent)]"
|
||||
@change="toggleExtension(type.value)"
|
||||
/>
|
||||
{{ type.label }}
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 搜索结果区域 -->
|
||||
@@ -776,7 +667,7 @@ function handleCreateRootFile() {
|
||||
</div>
|
||||
|
||||
<!-- Empty prompt / history -->
|
||||
<div v-else-if="!searchKeyword.trim()" class="flex flex-col py-4 px-1">
|
||||
<div v-else-if="!searchKeyword.trim()" class="flex flex-col px-1">
|
||||
<template v-if="searchHistory.length > 0">
|
||||
<p class="text-[10px] text-[var(--app-subtle)] px-2 mb-2 uppercase tracking-wider">最近搜索</p>
|
||||
<button
|
||||
@@ -1765,34 +1656,6 @@ function handleCreateRootFile() {
|
||||
|
||||
/* ── Search view ──────────────────────────────────────────────── */
|
||||
|
||||
.search-option-btn {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
height: 24px;
|
||||
min-width: 24px;
|
||||
padding: 0 6px;
|
||||
font-size: 11px;
|
||||
font-weight: 500;
|
||||
border-radius: 4px;
|
||||
border: 1px solid transparent;
|
||||
background: transparent;
|
||||
color: var(--app-subtle);
|
||||
cursor: pointer;
|
||||
transition: all 0.15s;
|
||||
}
|
||||
|
||||
.search-option-btn:hover {
|
||||
background: var(--app-hover);
|
||||
color: var(--app-text-soft);
|
||||
}
|
||||
|
||||
.search-option-active {
|
||||
background: color-mix(in srgb, var(--app-accent) 15%, transparent);
|
||||
color: var(--app-accent);
|
||||
border-color: color-mix(in srgb, var(--app-accent) 30%, transparent);
|
||||
}
|
||||
|
||||
.search-file-header {
|
||||
color: var(--app-text-soft);
|
||||
}
|
||||
@@ -1815,8 +1678,4 @@ function handleCreateRootFile() {
|
||||
.search-history-item {
|
||||
color: var(--app-text-soft);
|
||||
}
|
||||
|
||||
.search-ext-dropdown {
|
||||
backdrop-filter: blur(12px);
|
||||
}
|
||||
</style>
|
||||
|
||||
Reference in New Issue
Block a user