feat(sql-library): show matching line snippets with jump in search
This commit is contained in:
1 parent
eeeb67e62c
commit
51f9dfc854
19 files changed
+686
-106
No files matched your search
@@ -10,6 +10,8 @@ import CustomContextMenu, { type ContextMenuItem as CtxMenuItem } from "@/compon
|
||||
import ConnectionTreeSelect from "@/components/connection/ConnectionTreeSelect.vue";
|
||||
import HelpTooltip from "@/components/ui/tooltip/HelpTooltip.vue";
|
||||
import LightTooltip from "@/components/ui/LightTooltip.vue";
|
||||
import SqlLibrarySearchSnippet from "@/components/layout/SqlLibrarySearchSnippet.vue";
|
||||
import type { SavedSqlLineMatch } from "@/lib/savedSql/savedSqlSearch";
|
||||
import { useToast } from "@/composables/useToast";
|
||||
import { isTauriRuntime } from "@/lib/backend/tauriRuntime";
|
||||
import * as api from "@/lib/backend/api";
|
||||
@@ -57,6 +59,12 @@ const activeConnectionIds = computed(() => new Set(connectionStore.connections.m
|
||||
const searchText = ref("");
|
||||
const searchQuery = computed(() => searchText.value.trim().toLowerCase());
|
||||
|
||||
watch(searchText, (text) => {
|
||||
if (text.trim()) {
|
||||
void savedSqlStore.ensureAllFilesLoaded();
|
||||
}
|
||||
});
|
||||
|
||||
// Sort mode: "folder" (default tree structure) or "date" (flat list by update date)
|
||||
const sortMode = ref<"folder" | "date">("folder");
|
||||
|
||||
@@ -875,16 +883,27 @@ async function moveFilesToFolder(fileIds: string[], folderId?: string) {
|
||||
}
|
||||
}
|
||||
|
||||
async function openFile(file: SavedSqlFile, targetMode?: SavedSqlOpenTargetMode) {
|
||||
async function openFile(file: SavedSqlFile, targetMode?: SavedSqlOpenTargetMode, reveal?: { line: number; column?: number }) {
|
||||
if (suppressNextRowClick.value) return;
|
||||
const loadedFile = await savedSqlStore.ensureFileContent(file.id);
|
||||
if (!loadedFile) return;
|
||||
const tabId = queryStore.openSavedSql(loadedFile, { targetMode });
|
||||
const tabId = queryStore.openSavedSql(loadedFile, { targetMode, reveal });
|
||||
const openedConnectionId = queryStore.tabs.find((tab) => tab.id === tabId)?.connectionId ?? loadedFile.connectionId;
|
||||
if (openedConnectionId) connectionStore.activeConnectionId = openedConnectionId;
|
||||
void savedSqlStore.recordFileUsage(loadedFile.id);
|
||||
}
|
||||
|
||||
function handleMatchClick(file: SavedSqlFile, match: SavedSqlLineMatch) {
|
||||
if (suppressNextRowClick.value) return;
|
||||
const currentIndex = allSelectableItems.value.findIndex((item) => item.type === "file" && item.id === file.id);
|
||||
if (currentIndex >= 0) {
|
||||
clearSelection();
|
||||
lastClickedItemIndex.value = currentIndex;
|
||||
setActiveItem(file.id, "file");
|
||||
}
|
||||
openFile(file, undefined, { line: match.lineNumber, column: match.column });
|
||||
}
|
||||
|
||||
function handleFileClick(file: SavedSqlFile, event: MouseEvent) {
|
||||
if (suppressNextRowClick.value) return;
|
||||
|
||||
@@ -1356,7 +1375,16 @@ function showDropInside(targetId: string) {
|
||||
<div class="border-b shrink-0 px-2 py-1">
|
||||
<div class="relative">
|
||||
<Search class="absolute left-2 top-1/2 -translate-y-1/2 h-3 w-3 text-muted-foreground" />
|
||||
<input data-sql-library-search v-model="searchText" autocapitalize="off" autocorrect="off" spellcheck="false" class="w-full h-6 pl-7 pr-6 text-[13px] rounded border border-border bg-background focus:outline-none focus:ring-1 focus:ring-ring" :placeholder="t('grid.search')" />
|
||||
<input
|
||||
data-sql-library-search
|
||||
v-model="searchText"
|
||||
@focus="savedSqlStore.ensureAllFilesLoaded()"
|
||||
autocapitalize="off"
|
||||
autocorrect="off"
|
||||
spellcheck="false"
|
||||
class="w-full h-6 pl-7 pr-6 text-[13px] rounded border border-border bg-background focus:outline-none focus:ring-1 focus:ring-ring"
|
||||
:placeholder="t('grid.search')"
|
||||
/>
|
||||
<button v-if="searchText" type="button" class="absolute right-1.5 top-1/2 -translate-y-1/2 text-muted-foreground hover:text-foreground" @click="searchText = ''">
|
||||
<X class="h-3 w-3" />
|
||||
</button>
|
||||
@@ -1422,36 +1450,38 @@ function showDropInside(targetId: string) {
|
||||
</LightTooltip>
|
||||
</div>
|
||||
|
||||
<div
|
||||
v-else
|
||||
class="relative flex cursor-default items-center gap-1 px-2 py-1.5 text-[13px] group"
|
||||
:class="[fileRowClass(item.item.id), isDraggingItem(item.item.id) ? 'opacity-50' : '']"
|
||||
@mousedown="handleDragMouseDown($event, item.item.id, 'file')"
|
||||
@click="handleFileClick(item.item, $event)"
|
||||
@contextmenu.capture="contextTarget = item.item"
|
||||
@contextmenu.prevent="
|
||||
contextTarget = item.item;
|
||||
onContextMenu($event);
|
||||
"
|
||||
>
|
||||
<FileText class="h-3.5 w-3.5 text-blue-400 shrink-0" />
|
||||
<span v-if="isFileDirty(item.item)" aria-hidden="true" class="dirty-sql-library-marker">*</span>
|
||||
<template v-if="isRenamingFile(item.item.id)">
|
||||
<input
|
||||
:ref="setRenameInputRef"
|
||||
v-model="renameValue"
|
||||
data-no-drag="true"
|
||||
class="min-w-0 flex-1 rounded border border-primary/50 bg-transparent px-1 text-[13px] outline-none"
|
||||
@keydown.enter.prevent="confirmRename"
|
||||
@keydown.escape.prevent="cancelRename"
|
||||
@blur="confirmRename"
|
||||
@mousedown.stop
|
||||
@click.stop
|
||||
/>
|
||||
</template>
|
||||
<span v-else class="dbx-sql-library-drag-label min-w-0 flex-1 truncate" :title="fileTitleLabel(item.item)" :style="fileTitleStyle(item.item)">{{ item.item.name }}</span>
|
||||
<span class="min-w-0 max-w-[45%] shrink truncate text-[13px]" :class="fileMetaClass(item.item.id)" :title="getConnectionLabel(item.item.connectionId)">[{{ getConnectionLabel(item.item.connectionId) }}]</span>
|
||||
</div>
|
||||
<template v-else>
|
||||
<div
|
||||
class="relative flex cursor-default items-center gap-1 px-2 py-1.5 text-[13px] group"
|
||||
:class="[fileRowClass(item.item.id), isDraggingItem(item.item.id) ? 'opacity-50' : '']"
|
||||
@mousedown="handleDragMouseDown($event, item.item.id, 'file')"
|
||||
@click="handleFileClick(item.item, $event)"
|
||||
@contextmenu.capture="contextTarget = item.item"
|
||||
@contextmenu.prevent="
|
||||
contextTarget = item.item;
|
||||
onContextMenu($event);
|
||||
"
|
||||
>
|
||||
<FileText class="h-3.5 w-3.5 text-blue-400 shrink-0" />
|
||||
<span v-if="isFileDirty(item.item)" aria-hidden="true" class="dirty-sql-library-marker">*</span>
|
||||
<template v-if="isRenamingFile(item.item.id)">
|
||||
<input
|
||||
:ref="setRenameInputRef"
|
||||
v-model="renameValue"
|
||||
data-no-drag="true"
|
||||
class="min-w-0 flex-1 rounded border border-primary/50 bg-transparent px-1 text-[13px] outline-none"
|
||||
@keydown.enter.prevent="confirmRename"
|
||||
@keydown.escape.prevent="cancelRename"
|
||||
@blur="confirmRename"
|
||||
@mousedown.stop
|
||||
@click.stop
|
||||
/>
|
||||
</template>
|
||||
<span v-else class="dbx-sql-library-drag-label min-w-0 flex-1 truncate" :title="fileTitleLabel(item.item)" :style="fileTitleStyle(item.item)">{{ item.item.name }}</span>
|
||||
<span class="min-w-0 max-w-[45%] shrink truncate text-[13px]" :class="fileMetaClass(item.item.id)" :title="getConnectionLabel(item.item.connectionId)">[{{ getConnectionLabel(item.item.connectionId) }}]</span>
|
||||
</div>
|
||||
<SqlLibrarySearchSnippet v-if="searchQuery" :file="item.item" :query="searchQuery" @select-match="(match) => handleMatchClick(item.item, match)" />
|
||||
</template>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1507,41 +1537,43 @@ function showDropInside(targetId: string) {
|
||||
</LightTooltip>
|
||||
</div>
|
||||
|
||||
<div
|
||||
v-else
|
||||
class="relative flex cursor-default items-center gap-1 py-1.5 pr-2 text-[13px] group"
|
||||
:style="{ paddingLeft: `${8 + row.depth * 16}px` }"
|
||||
:class="[fileRowClass(row.file.id), isDraggingItem(row.file.id) ? 'opacity-50' : '']"
|
||||
@mousedown="handleDragMouseDown($event, row.file.id, 'file')"
|
||||
@mousemove="updateDropTarget($event, row.file.id, 'file')"
|
||||
@mouseleave="clearDropTarget(row.file.id)"
|
||||
@click="handleFileClick(row.file, $event)"
|
||||
@contextmenu.capture="contextTarget = row.file"
|
||||
@contextmenu.prevent="
|
||||
contextTarget = row.file;
|
||||
onContextMenu($event);
|
||||
"
|
||||
>
|
||||
<div v-if="showDropBefore(row.file.id)" class="absolute left-2 right-2 top-0 border-t-2 border-primary" />
|
||||
<div v-if="showDropAfter(row.file.id)" class="absolute left-2 right-2 bottom-0 border-b-2 border-primary" />
|
||||
<FileText class="h-3.5 w-3.5 text-blue-400 shrink-0" />
|
||||
<span v-if="isFileDirty(row.file)" aria-hidden="true" class="dirty-sql-library-marker">*</span>
|
||||
<template v-if="isRenamingFile(row.file.id)">
|
||||
<input
|
||||
:ref="setRenameInputRef"
|
||||
v-model="renameValue"
|
||||
data-no-drag="true"
|
||||
class="min-w-0 flex-1 rounded border border-primary/50 bg-transparent px-1 text-[13px] outline-none"
|
||||
@keydown.enter.prevent="confirmRename"
|
||||
@keydown.escape.prevent="cancelRename"
|
||||
@blur="confirmRename"
|
||||
@mousedown.stop
|
||||
@click.stop
|
||||
/>
|
||||
</template>
|
||||
<span v-else class="dbx-sql-library-drag-label min-w-0 flex-1 truncate" :title="fileTitleLabel(row.file)" :style="fileTitleStyle(row.file)">{{ row.file.name }}</span>
|
||||
<span class="min-w-0 max-w-[45%] shrink truncate text-[13px]" :class="fileMetaClass(row.file.id)" :title="getConnectionLabel(row.file.connectionId)">[{{ getConnectionLabel(row.file.connectionId) }}]</span>
|
||||
</div>
|
||||
<template v-else>
|
||||
<div
|
||||
class="relative flex cursor-default items-center gap-1 py-1.5 pr-2 text-[13px] group"
|
||||
:style="{ paddingLeft: `${8 + row.depth * 16}px` }"
|
||||
:class="[fileRowClass(row.file.id), isDraggingItem(row.file.id) ? 'opacity-50' : '']"
|
||||
@mousedown="handleDragMouseDown($event, row.file.id, 'file')"
|
||||
@mousemove="updateDropTarget($event, row.file.id, 'file')"
|
||||
@mouseleave="clearDropTarget(row.file.id)"
|
||||
@click="handleFileClick(row.file, $event)"
|
||||
@contextmenu.capture="contextTarget = row.file"
|
||||
@contextmenu.prevent="
|
||||
contextTarget = row.file;
|
||||
onContextMenu($event);
|
||||
"
|
||||
>
|
||||
<div v-if="showDropBefore(row.file.id)" class="absolute left-2 right-2 top-0 border-t-2 border-primary" />
|
||||
<div v-if="showDropAfter(row.file.id)" class="absolute left-2 right-2 bottom-0 border-b-2 border-primary" />
|
||||
<FileText class="h-3.5 w-3.5 text-blue-400 shrink-0" />
|
||||
<span v-if="isFileDirty(row.file)" aria-hidden="true" class="dirty-sql-library-marker">*</span>
|
||||
<template v-if="isRenamingFile(row.file.id)">
|
||||
<input
|
||||
:ref="setRenameInputRef"
|
||||
v-model="renameValue"
|
||||
data-no-drag="true"
|
||||
class="min-w-0 flex-1 rounded border border-primary/50 bg-transparent px-1 text-[13px] outline-none"
|
||||
@keydown.enter.prevent="confirmRename"
|
||||
@keydown.escape.prevent="cancelRename"
|
||||
@blur="confirmRename"
|
||||
@mousedown.stop
|
||||
@click.stop
|
||||
/>
|
||||
</template>
|
||||
<span v-else class="dbx-sql-library-drag-label min-w-0 flex-1 truncate" :title="fileTitleLabel(row.file)" :style="fileTitleStyle(row.file)">{{ row.file.name }}</span>
|
||||
<span class="min-w-0 max-w-[45%] shrink truncate text-[13px]" :class="fileMetaClass(row.file.id)" :title="getConnectionLabel(row.file.connectionId)">[{{ getConnectionLabel(row.file.connectionId) }}]</span>
|
||||
</div>
|
||||
<SqlLibrarySearchSnippet v-if="searchQuery" :file="row.file" :query="searchQuery" :depth="row.depth" @select-match="(match) => handleMatchClick(row.file, match)" />
|
||||
</template>
|
||||
</div>
|
||||
|
||||
<div v-if="visibleFiles.length > 0 || dragState.draggedType === 'file'">
|
||||
@@ -1554,41 +1586,42 @@ function showDropInside(targetId: string) {
|
||||
>
|
||||
{{ t("sqlLibrary.unfiled") }}
|
||||
</div>
|
||||
<div
|
||||
v-for="file in visibleFiles"
|
||||
:key="file.id"
|
||||
class="relative flex cursor-default items-center gap-1 px-2 py-1.5 text-[13px] group"
|
||||
:class="[fileRowClass(file.id), isDraggingItem(file.id) ? 'opacity-50' : '']"
|
||||
@mousedown="handleDragMouseDown($event, file.id, 'file')"
|
||||
@mousemove="updateDropTarget($event, file.id, 'file')"
|
||||
@mouseleave="clearDropTarget(file.id)"
|
||||
@click="handleFileClick(file, $event)"
|
||||
@contextmenu.capture="contextTarget = file"
|
||||
@contextmenu.prevent="
|
||||
contextTarget = file;
|
||||
onContextMenu($event);
|
||||
"
|
||||
>
|
||||
<div v-if="showDropBefore(file.id)" class="absolute left-2 right-2 top-0 border-t-2 border-primary" />
|
||||
<div v-if="showDropAfter(file.id)" class="absolute left-2 right-2 bottom-0 border-b-2 border-primary" />
|
||||
<FileText class="h-3.5 w-3.5 text-blue-400 shrink-0" />
|
||||
<span v-if="isFileDirty(file)" aria-hidden="true" class="dirty-sql-library-marker">*</span>
|
||||
<template v-if="isRenamingFile(file.id)">
|
||||
<input
|
||||
:ref="setRenameInputRef"
|
||||
v-model="renameValue"
|
||||
data-no-drag="true"
|
||||
class="min-w-0 flex-1 rounded border border-primary/50 bg-transparent px-1 text-[13px] outline-none"
|
||||
@keydown.enter.prevent="confirmRename"
|
||||
@keydown.escape.prevent="cancelRename"
|
||||
@blur="confirmRename"
|
||||
@mousedown.stop
|
||||
@click.stop
|
||||
/>
|
||||
</template>
|
||||
<span v-else class="dbx-sql-library-drag-label min-w-0 flex-1 truncate" :title="fileTitleLabel(file)" :style="fileTitleStyle(file)">{{ file.name }}</span>
|
||||
<span class="min-w-0 max-w-[45%] shrink truncate text-[13px]" :class="fileMetaClass(file.id)" :title="getConnectionLabel(file.connectionId)">[{{ getConnectionLabel(file.connectionId) }}]</span>
|
||||
</div>
|
||||
<template v-for="file in visibleFiles" :key="file.id">
|
||||
<div
|
||||
class="relative flex cursor-default items-center gap-1 px-2 py-1.5 text-[13px] group"
|
||||
:class="[fileRowClass(file.id), isDraggingItem(file.id) ? 'opacity-50' : '']"
|
||||
@mousedown="handleDragMouseDown($event, file.id, 'file')"
|
||||
@mousemove="updateDropTarget($event, file.id, 'file')"
|
||||
@mouseleave="clearDropTarget(file.id)"
|
||||
@click="handleFileClick(file, $event)"
|
||||
@contextmenu.capture="contextTarget = file"
|
||||
@contextmenu.prevent="
|
||||
contextTarget = file;
|
||||
onContextMenu($event);
|
||||
"
|
||||
>
|
||||
<div v-if="showDropBefore(file.id)" class="absolute left-2 right-2 top-0 border-t-2 border-primary" />
|
||||
<div v-if="showDropAfter(file.id)" class="absolute left-2 right-2 bottom-0 border-b-2 border-primary" />
|
||||
<FileText class="h-3.5 w-3.5 text-blue-400 shrink-0" />
|
||||
<span v-if="isFileDirty(file)" aria-hidden="true" class="dirty-sql-library-marker">*</span>
|
||||
<template v-if="isRenamingFile(file.id)">
|
||||
<input
|
||||
:ref="setRenameInputRef"
|
||||
v-model="renameValue"
|
||||
data-no-drag="true"
|
||||
class="min-w-0 flex-1 rounded border border-primary/50 bg-transparent px-1 text-[13px] outline-none"
|
||||
@keydown.enter.prevent="confirmRename"
|
||||
@keydown.escape.prevent="cancelRename"
|
||||
@blur="confirmRename"
|
||||
@mousedown.stop
|
||||
@click.stop
|
||||
/>
|
||||
</template>
|
||||
<span v-else class="dbx-sql-library-drag-label min-w-0 flex-1 truncate" :title="fileTitleLabel(file)" :style="fileTitleStyle(file)">{{ file.name }}</span>
|
||||
<span class="min-w-0 max-w-[45%] shrink truncate text-[13px]" :class="fileMetaClass(file.id)" :title="getConnectionLabel(file.connectionId)">[{{ getConnectionLabel(file.connectionId) }}]</span>
|
||||
</div>
|
||||
<SqlLibrarySearchSnippet v-if="searchQuery" :file="file" :query="searchQuery" @select-match="(match) => handleMatchClick(file, match)" />
|
||||
</template>
|
||||
</div>
|
||||
</div>
|
||||
<!-- End tree structure -->
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, ref } from "vue";
|
||||
import { useI18n } from "vue-i18n";
|
||||
import type { SavedSqlFile } from "@/types/database";
|
||||
import { findSavedSqlSearchMatches, formatMatchTooltip, type SavedSqlLineMatch } from "@/lib/savedSql/savedSqlSearch";
|
||||
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
file: SavedSqlFile;
|
||||
query: string;
|
||||
depth?: number;
|
||||
}>(),
|
||||
{
|
||||
depth: 0,
|
||||
},
|
||||
);
|
||||
|
||||
const emit = defineEmits<{
|
||||
selectMatch: [match: SavedSqlLineMatch];
|
||||
}>();
|
||||
|
||||
const { t } = useI18n();
|
||||
const expanded = ref(false);
|
||||
|
||||
const searchResult = computed(() => {
|
||||
if (!props.query || !props.file.sql) {
|
||||
return { matches: [], totalMatches: 0 };
|
||||
}
|
||||
return findSavedSqlSearchMatches(props.file.sql, props.query, {
|
||||
maxMatches: expanded.value ? 100 : 3,
|
||||
});
|
||||
});
|
||||
|
||||
const matches = computed(() => searchResult.value.matches);
|
||||
const totalMatches = computed(() => searchResult.value.totalMatches);
|
||||
const hasHiddenMatches = computed(() => totalMatches.value > 3);
|
||||
|
||||
function toggleExpanded() {
|
||||
expanded.value = !expanded.value;
|
||||
}
|
||||
|
||||
function handleSnippetClick(match: SavedSqlLineMatch) {
|
||||
emit("selectMatch", match);
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div v-if="matches.length > 0" class="dbx-sql-library-snippets mb-1 mt-0.5 space-y-0.5 select-none" :style="{ paddingLeft: `${24 + depth * 16}px` }" data-no-drag="true" @mousedown.stop>
|
||||
<div
|
||||
v-for="match in matches"
|
||||
:key="match.lineNumber"
|
||||
class="group/snippet flex cursor-pointer items-center gap-1.5 rounded py-0.5 pr-2 font-mono text-[11px] text-muted-foreground transition-colors hover:bg-accent/60 hover:text-foreground"
|
||||
:title="formatMatchTooltip(match)"
|
||||
@click.stop="handleSnippetClick(match)"
|
||||
>
|
||||
<span class="shrink-0 text-[10px] text-muted-foreground/70 select-none"> L{{ match.lineNumber }} </span>
|
||||
<span class="min-w-0 flex-1 truncate">
|
||||
<template v-for="(seg, idx) in match.segments" :key="idx">
|
||||
<mark v-if="seg.matched" class="rounded-[2px] bg-amber-300/80 px-0.5 text-foreground dark:bg-amber-500/40">{{ seg.text }}</mark>
|
||||
<span v-else>{{ seg.text }}</span>
|
||||
</template>
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div v-if="hasHiddenMatches" class="pt-0.5 text-[11px]">
|
||||
<button type="button" class="text-[11px] font-normal text-primary hover:underline hover:text-primary/80 select-none" @click.stop="toggleExpanded">
|
||||
{{ expanded ? t("sqlLibrary.collapseMatches") : t("sqlLibrary.moreMatches", { count: totalMatches - 3 }) }}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
+131
@@ -0,0 +1,131 @@
|
||||
// @vitest-environment happy-dom
|
||||
|
||||
import { createApp, nextTick, type App } from "vue";
|
||||
import { createPinia, setActivePinia } from "pinia";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import i18n from "@/i18n";
|
||||
import type { SavedSqlFile } from "@/types/database";
|
||||
import SqlLibrarySearchSnippet from "../SqlLibrarySearchSnippet.vue";
|
||||
|
||||
const mounted: Array<{ app: App; host: HTMLElement }> = [];
|
||||
|
||||
function sampleFile(sql: string): SavedSqlFile {
|
||||
return {
|
||||
id: "test-file-1",
|
||||
connectionId: "conn-1",
|
||||
name: "users_query.sql",
|
||||
database: "main",
|
||||
sql,
|
||||
sqlLoaded: true,
|
||||
createdAt: "2026-10-01T00:00:00Z",
|
||||
updatedAt: "2026-10-01T00:00:00Z",
|
||||
};
|
||||
}
|
||||
|
||||
function mountSnippet(file: SavedSqlFile, query: string, onSelectMatch?: (match: any) => void) {
|
||||
setActivePinia(createPinia());
|
||||
const host = document.createElement("div");
|
||||
document.body.appendChild(host);
|
||||
const app = createApp(SqlLibrarySearchSnippet, {
|
||||
file,
|
||||
query,
|
||||
onSelectMatch,
|
||||
});
|
||||
app.use(i18n);
|
||||
app.mount(host);
|
||||
mounted.push({ app, host });
|
||||
return host;
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
for (const { app, host } of mounted.splice(0)) {
|
||||
app.unmount();
|
||||
host.remove();
|
||||
}
|
||||
});
|
||||
|
||||
describe("SqlLibrarySearchSnippet", () => {
|
||||
it("renders nothing when there are no matches", async () => {
|
||||
const host = mountSnippet(sampleFile("SELECT 1;"), "orders");
|
||||
await nextTick();
|
||||
|
||||
const snippetContainer = host.querySelector(".dbx-sql-library-snippets");
|
||||
expect(snippetContainer).toBeNull();
|
||||
});
|
||||
|
||||
it("renders matching line with line number and highlight mark", async () => {
|
||||
const sql = ["-- comments", "SELECT id, username FROM users", "WHERE active = true;"].join("\n");
|
||||
|
||||
const host = mountSnippet(sampleFile(sql), "users");
|
||||
await nextTick();
|
||||
|
||||
const container = host.querySelector(".dbx-sql-library-snippets");
|
||||
expect(container).not.toBeNull();
|
||||
|
||||
const snippetRows = host.querySelectorAll(".group\\/snippet");
|
||||
expect(snippetRows).toHaveLength(1);
|
||||
|
||||
const lineBadge = snippetRows[0].querySelector("span");
|
||||
expect(lineBadge?.textContent?.trim()).toBe("L2");
|
||||
|
||||
const mark = snippetRows[0].querySelector("mark");
|
||||
expect(mark).not.toBeNull();
|
||||
expect(mark?.textContent).toBe("users");
|
||||
|
||||
// Tooltip should contain multi-line context
|
||||
const titleAttr = snippetRows[0].getAttribute("title");
|
||||
expect(titleAttr).toContain("1 | -- comments");
|
||||
expect(titleAttr).toContain("2 | SELECT id, username FROM users");
|
||||
expect(titleAttr).toContain("3 | WHERE active = true;");
|
||||
});
|
||||
|
||||
it("emits selectMatch with line and column on click", async () => {
|
||||
const sql = "SELECT * FROM orders WHERE status = 'pending';";
|
||||
const onSelectMatch = vi.fn();
|
||||
const host = mountSnippet(sampleFile(sql), "orders", onSelectMatch);
|
||||
await nextTick();
|
||||
|
||||
const snippetRow = host.querySelector<HTMLElement>(".group\\/snippet");
|
||||
expect(snippetRow).not.toBeNull();
|
||||
snippetRow!.click();
|
||||
await nextTick();
|
||||
|
||||
expect(onSelectMatch).toHaveBeenCalledTimes(1);
|
||||
expect(onSelectMatch).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
lineNumber: 1,
|
||||
column: 15,
|
||||
lineText: "SELECT * FROM orders WHERE status = 'pending';",
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("limits to 3 matches initially and expands on button click", async () => {
|
||||
const sql = ["SELECT 1 FROM tbl;", "SELECT 2 FROM tbl;", "SELECT 3 FROM tbl;", "SELECT 4 FROM tbl;", "SELECT 5 FROM tbl;"].join("\n");
|
||||
|
||||
const host = mountSnippet(sampleFile(sql), "SELECT");
|
||||
await nextTick();
|
||||
|
||||
let snippetRows = host.querySelectorAll(".group\\/snippet");
|
||||
expect(snippetRows).toHaveLength(3);
|
||||
|
||||
const toggleButton = host.querySelector<HTMLButtonElement>("button");
|
||||
expect(toggleButton).not.toBeNull();
|
||||
expect(toggleButton?.textContent).toContain("2");
|
||||
|
||||
// Click to expand
|
||||
toggleButton!.click();
|
||||
await nextTick();
|
||||
|
||||
snippetRows = host.querySelectorAll(".group\\/snippet");
|
||||
expect(snippetRows).toHaveLength(5);
|
||||
expect(toggleButton?.textContent?.trim()).toBe(i18n.global.t("sqlLibrary.collapseMatches"));
|
||||
|
||||
// Click to collapse
|
||||
toggleButton!.click();
|
||||
await nextTick();
|
||||
|
||||
snippetRows = host.querySelectorAll(".group\\/snippet");
|
||||
expect(snippetRows).toHaveLength(3);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user