feat(search): add global file content search in QuickOpen
This commit is contained in:
1 parent
5278594ed2
commit
04c6e67bbd
31 files changed
+1509
-16
No files matched your search
@@ -427,6 +427,9 @@ export const inspectExternalSqlFile = forward("inspectExternalSqlFile");
|
||||
export const writeExternalSqlFile = forward("writeExternalSqlFile");
|
||||
export const saveExternalSqlFile = forward("saveExternalSqlFile");
|
||||
export const listSqlFilesInFolder = forward("listSqlFilesInFolder");
|
||||
export const globalSearch = forward("globalSearch");
|
||||
export const loadGlobalSearchSettings = forward("loadGlobalSearchSettings");
|
||||
export const saveGlobalSearchSettings = forward("saveGlobalSearchSettings");
|
||||
export const createSqlFileInFolder = forward("createSqlFileInFolder");
|
||||
export const renameSqlFileInFolder = forward("renameSqlFileInFolder");
|
||||
export const deleteSqlFileInFolder = forward("deleteSqlFileInFolder");
|
||||
@@ -1066,6 +1069,9 @@ export type {
|
||||
QueryResultExportRequest,
|
||||
AgentEvent,
|
||||
SqlFileEntry,
|
||||
GlobalSearchRequest,
|
||||
GlobalSearchMatch,
|
||||
GlobalSearchSettings,
|
||||
} from "@/lib/backend/tauri";
|
||||
|
||||
// MQTT
|
||||
|
||||
@@ -2543,6 +2543,47 @@ export async function deleteSqlFileInFolder(_rootPath: string, _filePath: string
|
||||
throw new Error("Managing SQL files in folders is only available in the desktop app");
|
||||
}
|
||||
|
||||
export interface GlobalSearchRequest {
|
||||
roots: string[];
|
||||
query: string;
|
||||
extensions?: string[];
|
||||
caseSensitive?: boolean;
|
||||
useRegex?: boolean;
|
||||
wholeWord?: boolean;
|
||||
limit?: number;
|
||||
}
|
||||
|
||||
export interface GlobalSearchMatch {
|
||||
path: string;
|
||||
fileName: string;
|
||||
line: number;
|
||||
column: number;
|
||||
matchText: string;
|
||||
lineText: string;
|
||||
}
|
||||
|
||||
export async function globalSearch(_request: GlobalSearchRequest): Promise<GlobalSearchMatch[]> {
|
||||
throw new Error("Global content search is only available in the desktop app");
|
||||
}
|
||||
|
||||
export interface GlobalSearchSettings {
|
||||
roots: string[];
|
||||
extensions: string[];
|
||||
}
|
||||
|
||||
export async function loadGlobalSearchSettings(): Promise<GlobalSearchSettings | null> {
|
||||
try {
|
||||
const raw = globalThis.localStorage?.getItem("dbx-global-search-settings-disk");
|
||||
return raw ? (JSON.parse(raw) as GlobalSearchSettings) : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function saveGlobalSearchSettings(settings: GlobalSearchSettings): Promise<void> {
|
||||
globalThis.localStorage?.setItem("dbx-global-search-settings-disk", JSON.stringify(settings));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Data Transfer
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@@ -789,6 +789,19 @@ export async function saveEditorSettings(settings: unknown): Promise<void> {
|
||||
return invoke("save_editor_settings", { settings });
|
||||
}
|
||||
|
||||
export interface GlobalSearchSettings {
|
||||
roots: string[];
|
||||
extensions: string[];
|
||||
}
|
||||
|
||||
export function loadGlobalSearchSettings(): Promise<GlobalSearchSettings | null> {
|
||||
return invoke("load_global_search_settings");
|
||||
}
|
||||
|
||||
export function saveGlobalSearchSettings(settings: GlobalSearchSettings): Promise<void> {
|
||||
return invoke("save_global_search_settings", { settings });
|
||||
}
|
||||
|
||||
export interface BackgroundImageInfo {
|
||||
storedPath: string;
|
||||
fileName: string;
|
||||
@@ -1079,6 +1092,31 @@ export async function deleteSqlFileInFolder(rootPath: string, filePath: string):
|
||||
return invoke("delete_sql_file_in_folder", { rootPath, filePath });
|
||||
}
|
||||
|
||||
export interface GlobalSearchRequest {
|
||||
roots: string[];
|
||||
query: string;
|
||||
extensions?: string[];
|
||||
caseSensitive?: boolean;
|
||||
useRegex?: boolean;
|
||||
wholeWord?: boolean;
|
||||
limit?: number;
|
||||
}
|
||||
|
||||
export interface GlobalSearchMatch {
|
||||
path: string;
|
||||
fileName: string;
|
||||
/** 1-based line number. */
|
||||
line: number;
|
||||
/** 1-based char column within the line (for CodeMirror). */
|
||||
column: number;
|
||||
matchText: string;
|
||||
lineText: string;
|
||||
}
|
||||
|
||||
export async function globalSearch(request: GlobalSearchRequest): Promise<GlobalSearchMatch[]> {
|
||||
return invoke("global_search", { request });
|
||||
}
|
||||
|
||||
// --- AI Conversations ---
|
||||
|
||||
export interface AiChatMessage {
|
||||
|
||||
@@ -325,6 +325,10 @@ export function isQuickOpenShortcut(event: ShortcutLikeEvent, shortcuts?: Partia
|
||||
return matchesShortcut(event, actionShortcut("quickOpen", shortcuts));
|
||||
}
|
||||
|
||||
export function isGlobalSearchShortcut(event: ShortcutLikeEvent, shortcuts?: Partial<ShortcutSettings>): boolean {
|
||||
return matchesShortcut(event, actionShortcut("globalSearch", shortcuts));
|
||||
}
|
||||
|
||||
export function isNavigateTabHistoryBackShortcut(event: ShortcutLikeEvent, shortcuts?: Partial<ShortcutSettings>, platform = globalThis.navigator?.platform || ""): boolean {
|
||||
return matchesShortcut(event, actionShortcut("navigateTabHistoryBack", shortcuts, platform), platform);
|
||||
}
|
||||
|
||||
@@ -46,6 +46,7 @@ export type ShortcutActionId =
|
||||
| "closeOtherTabs"
|
||||
| "focusSearch"
|
||||
| "quickOpen"
|
||||
| "globalSearch"
|
||||
| "toggleAiPanel"
|
||||
| "navigateTabHistoryBack"
|
||||
| "navigateTabHistoryForward"
|
||||
@@ -410,6 +411,12 @@ export const SHORTCUT_DEFINITIONS: ShortcutDefinition[] = [
|
||||
scope: "global",
|
||||
defaultShortcut: "Mod+P",
|
||||
},
|
||||
{
|
||||
id: "globalSearch",
|
||||
labelKey: "settings.shortcutGlobalSearch",
|
||||
scope: "global",
|
||||
defaultShortcut: "Mod+Alt+F",
|
||||
},
|
||||
{
|
||||
id: "toggleAiPanel",
|
||||
labelKey: "settings.shortcutToggleAiPanel",
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
save: vi.fn().mockResolvedValue(undefined),
|
||||
load: vi.fn().mockResolvedValue(null),
|
||||
sqlFolders: vi.fn((): string[] => []),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/backend/api", () => ({
|
||||
saveGlobalSearchSettings: mocks.save,
|
||||
loadGlobalSearchSettings: mocks.load,
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/sqlFile/sqlFileFolders", () => ({
|
||||
getSqlFileFolderPaths: () => mocks.sqlFolders(),
|
||||
}));
|
||||
|
||||
class MemoryStorage {
|
||||
private map = new Map<string, string>();
|
||||
getItem(key: string): string | null {
|
||||
return this.map.get(key) ?? null;
|
||||
}
|
||||
setItem(key: string, value: string): void {
|
||||
this.map.set(key, String(value));
|
||||
}
|
||||
removeItem(key: string): void {
|
||||
this.map.delete(key);
|
||||
}
|
||||
clear(): void {
|
||||
this.map.clear();
|
||||
}
|
||||
}
|
||||
|
||||
const { DEFAULT_GLOBAL_SEARCH_EXTENSIONS, getGlobalSearchExtensions, getGlobalSearchRoots, saveGlobalSearchExtensions, saveGlobalSearchRoots, composeGlobalSearchRoots, globalSearchSettingsVersion, initializeGlobalSearchSettings } = await import("../globalSearchSettings");
|
||||
|
||||
describe("globalSearchSettings", () => {
|
||||
beforeEach(() => {
|
||||
(globalThis as { localStorage?: Storage }).localStorage = new MemoryStorage() as unknown as Storage;
|
||||
mocks.save.mockClear();
|
||||
mocks.load.mockReset().mockResolvedValue(null);
|
||||
mocks.sqlFolders.mockReset().mockReturnValue([]);
|
||||
});
|
||||
|
||||
it("defaults extensions to sql", () => {
|
||||
expect(getGlobalSearchExtensions()).toEqual(DEFAULT_GLOBAL_SEARCH_EXTENSIONS);
|
||||
expect(getGlobalSearchExtensions()).toEqual(["sql"]);
|
||||
});
|
||||
|
||||
it("normalizes extensions (dot prefix, case, whitespace, dedupe) and persists to disk", () => {
|
||||
const versionBefore = globalSearchSettingsVersion.value;
|
||||
saveGlobalSearchExtensions([" .SQL ", "Txt", "sql", ""]);
|
||||
expect(getGlobalSearchExtensions()).toEqual(["sql", "txt"]);
|
||||
expect(globalSearchSettingsVersion.value).toBe(versionBefore + 1);
|
||||
expect(mocks.save).toHaveBeenCalledTimes(1);
|
||||
expect(mocks.save).toHaveBeenCalledWith({ roots: [], extensions: ["sql", "txt"] });
|
||||
});
|
||||
|
||||
it("falls back to default extensions when the list becomes empty", () => {
|
||||
saveGlobalSearchExtensions([" ", "."]);
|
||||
expect(getGlobalSearchExtensions()).toEqual(["sql"]);
|
||||
});
|
||||
|
||||
it("saves roots, dropping blank entries, and persists to disk", () => {
|
||||
const versionBefore = globalSearchSettingsVersion.value;
|
||||
saveGlobalSearchRoots([" D:\\work\\sql ", "", "E:\\docs"]);
|
||||
expect(getGlobalSearchRoots()).toEqual(["D:\\work\\sql", "E:\\docs"]);
|
||||
expect(globalSearchSettingsVersion.value).toBe(versionBefore + 1);
|
||||
expect(mocks.save).toHaveBeenCalledTimes(1);
|
||||
expect(mocks.save).toHaveBeenCalledWith({ roots: ["D:\\work\\sql", "E:\\docs"], extensions: ["sql"] });
|
||||
});
|
||||
|
||||
it("composes SQL folders plus extra roots without duplicates", () => {
|
||||
mocks.sqlFolders.mockReturnValue(["D:\\sql", "D:\\shared"]);
|
||||
saveGlobalSearchRoots(["D:\\shared", "E:\\extra", " "]);
|
||||
expect(composeGlobalSearchRoots()).toEqual(["D:\\sql", "D:\\shared", "E:\\extra"]);
|
||||
});
|
||||
|
||||
it("hydrates roots and extensions from the disk store", async () => {
|
||||
mocks.load.mockResolvedValue({
|
||||
roots: ["D:\\disk-root"],
|
||||
extensions: [".sql", "txt"],
|
||||
});
|
||||
await initializeGlobalSearchSettings();
|
||||
expect(getGlobalSearchRoots()).toEqual(["D:\\disk-root"]);
|
||||
expect(getGlobalSearchExtensions()).toEqual(["sql", "txt"]);
|
||||
// Hydration re-persists the normalized values back to disk.
|
||||
expect(mocks.save).toHaveBeenCalledTimes(2);
|
||||
expect(mocks.save).toHaveBeenLastCalledWith({ roots: ["D:\\disk-root"], extensions: ["sql", "txt"] });
|
||||
});
|
||||
|
||||
it("keeps current values when the disk store is empty", async () => {
|
||||
saveGlobalSearchRoots(["D:\\keep"]);
|
||||
await initializeGlobalSearchSettings();
|
||||
expect(getGlobalSearchRoots()).toEqual(["D:\\keep"]);
|
||||
});
|
||||
|
||||
it("ignores malformed disk payloads", async () => {
|
||||
saveGlobalSearchExtensions(["sql"]);
|
||||
mocks.load.mockResolvedValue({ roots: "not-an-array", extensions: 42 });
|
||||
await initializeGlobalSearchSettings();
|
||||
expect(getGlobalSearchRoots()).toEqual([]);
|
||||
expect(getGlobalSearchExtensions()).toEqual(["sql"]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,111 @@
|
||||
import { ref } from "vue";
|
||||
import { safeLocalStorageGet, safeLocalStorageSet } from "@/lib/backend/safeStorage";
|
||||
import { getSqlFileFolderPaths } from "@/lib/sqlFile/sqlFileFolders";
|
||||
import * as api from "@/lib/backend/api";
|
||||
|
||||
const ROOTS_STORAGE_KEY = "dbx-global-search-roots";
|
||||
const EXTENSIONS_STORAGE_KEY = "dbx-global-search-extensions";
|
||||
export const DEFAULT_GLOBAL_SEARCH_EXTENSIONS = ["sql"];
|
||||
|
||||
/**
|
||||
* Shared reactive version counter — bumped whenever global-search settings change.
|
||||
* useQuickOpen watches this to know when to re-run the current search.
|
||||
*/
|
||||
export const globalSearchSettingsVersion = ref(0);
|
||||
|
||||
/** Additional search root directories (on top of the configured SQL file folders). */
|
||||
export function getGlobalSearchRoots(): string[] {
|
||||
try {
|
||||
const raw = safeLocalStorageGet(ROOTS_STORAGE_KEY);
|
||||
if (!raw) return [];
|
||||
const parsed = JSON.parse(raw);
|
||||
return Array.isArray(parsed) ? parsed.filter((p): p is string => typeof p === "string") : [];
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/** File extensions to search (normalized, without a leading dot). */
|
||||
export function getGlobalSearchExtensions(): string[] {
|
||||
try {
|
||||
const raw = safeLocalStorageGet(EXTENSIONS_STORAGE_KEY);
|
||||
if (raw) {
|
||||
const parsed = JSON.parse(raw);
|
||||
if (Array.isArray(parsed)) {
|
||||
const normalized = parsed
|
||||
.filter((ext): ext is string => typeof ext === "string")
|
||||
.map((ext) => ext.trim().replace(/^\.+/, "").toLowerCase())
|
||||
.filter((ext) => ext.length > 0);
|
||||
if (normalized.length > 0) return normalized;
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// fall through to default
|
||||
}
|
||||
return [...DEFAULT_GLOBAL_SEARCH_EXTENSIONS];
|
||||
}
|
||||
|
||||
export function saveGlobalSearchRoots(roots: string[]): void {
|
||||
const normalized = [...new Set(roots.map((root) => root.trim()).filter((root) => root.length > 0))];
|
||||
safeLocalStorageSet(ROOTS_STORAGE_KEY, JSON.stringify(normalized));
|
||||
globalSearchSettingsVersion.value++;
|
||||
void persistGlobalSearchSettings();
|
||||
}
|
||||
|
||||
export function saveGlobalSearchExtensions(extensions: string[]): void {
|
||||
const normalized = extensions.map((ext) => ext.trim().replace(/^\.+/, "").toLowerCase()).filter((ext) => ext.length > 0);
|
||||
if (normalized.length === 0) {
|
||||
safeLocalStorageSet(EXTENSIONS_STORAGE_KEY, JSON.stringify(DEFAULT_GLOBAL_SEARCH_EXTENSIONS));
|
||||
} else {
|
||||
safeLocalStorageSet(EXTENSIONS_STORAGE_KEY, JSON.stringify([...new Set(normalized)]));
|
||||
}
|
||||
globalSearchSettingsVersion.value++;
|
||||
void persistGlobalSearchSettings();
|
||||
}
|
||||
|
||||
/** Persist the current roots + extensions to disk (Tauri) for durable storage. */
|
||||
async function persistGlobalSearchSettings(): Promise<void> {
|
||||
try {
|
||||
await api.saveGlobalSearchSettings({
|
||||
roots: getGlobalSearchRoots(),
|
||||
extensions: getGlobalSearchExtensions(),
|
||||
});
|
||||
} catch (error) {
|
||||
// Web/browser mode may not expose persistence; ignore.
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Hydrate the localStorage-backed settings from the disk store once at startup.
|
||||
* Prefers configured SQL-file folders, so this only seeds extra roots/extensions.
|
||||
*/
|
||||
export async function initializeGlobalSearchSettings(): Promise<void> {
|
||||
try {
|
||||
const disk = await api.loadGlobalSearchSettings();
|
||||
if (!disk) return;
|
||||
if (Array.isArray(disk.roots) && disk.roots.length > 0) {
|
||||
saveGlobalSearchRoots(disk.roots);
|
||||
}
|
||||
if (Array.isArray(disk.extensions) && disk.extensions.length > 0) {
|
||||
saveGlobalSearchExtensions(disk.extensions);
|
||||
}
|
||||
} catch {
|
||||
// No disk store (pure web mode) — keep current values.
|
||||
}
|
||||
}
|
||||
|
||||
// Seed from disk once on import; later saves also write back to disk.
|
||||
void initializeGlobalSearchSettings();
|
||||
|
||||
/** Union of the configured SQL file folders and the extra global-search roots. */
|
||||
export function composeGlobalSearchRoots(): string[] {
|
||||
const seen = new Set<string>();
|
||||
const combined: string[] = [];
|
||||
for (const root of [...getSqlFileFolderPaths(), ...getGlobalSearchRoots()]) {
|
||||
const normalized = root.trim();
|
||||
if (!normalized || seen.has(normalized)) continue;
|
||||
seen.add(normalized);
|
||||
combined.push(normalized);
|
||||
}
|
||||
return combined;
|
||||
}
|
||||
Reference in new issue
Block a user