In-file highlighting, tweaked grouping

This commit is contained in:
Simon Cambier
2022-04-18 18:17:25 +02:00
parent 41203a11da
commit edeada032a
5 changed files with 54 additions and 46 deletions

View File

@@ -1,20 +1,24 @@
<script lang="ts">
import CmpInput from "./CmpInput.svelte"
import CmpNoteInternalResult from "./CmpInfileResult.svelte"
import { surroundLen, type ResultNote, type SearchMatch } from "./globals"
import { excerptAfter, type ResultNote, type SearchMatch } from "./globals"
import { resultNotes } from "./stores"
import type { SearchResult } from "minisearch"
import { stringsToRegex } from "./utils"
$: firstResult = $resultNotes[0]
$: matches = firstResult?.matches ?? []
$: groupedMatches = (() => {
const groups = getGroups()
return groups.map((group) => ({
match: "",
offset: (group.first()!.offset + group.last()!.offset) / 2,
}))
})()
let matches: SearchMatch[] = []
let groupedOffsets: number[] = []
$: note = $resultNotes[0]
$: {
if (note) {
matches = note.matches
const groups = getGroups()
groupedOffsets = groups.map((group) =>
Math.round((group.first()!.offset + group.last()!.offset) / 2)
)
console.log(groups)
console.log(groupedOffsets)
}
}
/**
* Group together close
@@ -23,7 +27,7 @@ function getGroups(): SearchMatch[][] {
const groups: SearchMatch[][] = []
let lastOffset = -1
while (true) {
const group = getGroupedMatches(matches, lastOffset, surroundLen)
const group = getGroupedMatches(matches, lastOffset, excerptAfter)
if (!group.length) break
lastOffset = group.last()!.offset
groups.push(group)
@@ -36,7 +40,7 @@ function getGroupedMatches(
offsetFrom: number,
maxLen: number
): SearchMatch[] {
const first = matches.find((m) => m.offset >= offsetFrom)
const first = matches.find((m) => m.offset > offsetFrom)
if (!first) return []
return matches.filter(
(m) => m.offset > offsetFrom && m.offset <= first.offset + maxLen
@@ -55,22 +59,23 @@ function onInputEnter(event: CustomEvent<ResultNote>): void {
<div class="modal-content">
<div class="prompt-results">
{#if groupedMatches.length}
{#each groupedMatches as match}
<CmpNoteInternalResult {match} />
{#if groupedOffsets.length && note}
{#each groupedOffsets as offset}
<CmpNoteInternalResult {offset} {note} />
{/each}
{:else}
We found 0 result for your search here.
{/if}
</div>
<div class="prompt-instructions">
<div class="prompt-instruction">
<span class="prompt-instruction-command">↑↓</span><span>to navigate</span>
</div>
<div class="prompt-instruction">
<span class="prompt-instruction-command"></span><span>to open</span>
</div>
<!-- <div class="prompt-instruction">
</div>
<div class="prompt-instructions">
<div class="prompt-instruction">
<span class="prompt-instruction-command">↑↓</span><span>to navigate</span>
</div>
<div class="prompt-instruction">
<span class="prompt-instruction-command"></span><span>to open</span>
</div>
<!-- <div class="prompt-instruction">
<span class="prompt-instruction-command">ctrl ↵</span>
<span>to open in a new pane</span>
</div>
@@ -78,8 +83,7 @@ function onInputEnter(event: CustomEvent<ResultNote>): void {
<span class="prompt-instruction-command">shift ↵</span>
<span>to create</span>
</div> -->
<div class="prompt-instruction">
<span class="prompt-instruction-command">esc</span><span>to dismiss</span>
</div>
<div class="prompt-instruction">
<span class="prompt-instruction-command">esc</span><span>to dismiss</span>
</div>
</div>