Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ All notable changes to `sessionpipe-gui`. The format follows

## [Unreleased]

### Fixed

- Command palette matches containing a dotted capital İ keep the correct underline positions without throwing.

## [0.8.18] - 2026-12-04

### Fixed
Expand Down
57 changes: 57 additions & 0 deletions web/src/lib/fuzzy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// SPDX-License-Identifier: Apache-2.0
import { describe, expect, it } from "vitest";
import { fuzzy, fuzzyFields } from "./fuzzy";

/** The characters a match underlines. */
const underlined = (text: string, positions: number[]) => positions.map((p) => text[p]).join("");

describe("fuzzy", () => {
it("matches a subsequence case-insensitively and underlines it", () => {
const m = fuzzy("pay", "Payments");
expect(m?.positions).toEqual([0, 1, 2]);
expect(fuzzy("xyz", "Payments")).toBeNull();
expect(fuzzy("", "Payments")).toEqual({ score: 0, positions: [] });
});

// "İ" (U+0130) lowercases to "i" and a combining dot, two UTF-16 units where the text has
// one, so indexes into the lowered text ran one past the text's own characters.
it("underlines the right characters after a dotted capital İ", () => {
const text = "İstanbul payments";
const m = fuzzy("pay", text);
expect(m?.positions).toEqual([9, 10, 11]);
expect(underlined(text, m?.positions ?? [])).toBe("pay");
expect(fuzzy("x", "İX")?.positions).toEqual([1]);
});

it("does not throw on a match at the end of a text with a dotted capital İ", () => {
expect(fuzzy("s", "İstanbul payments")?.positions).toEqual([1]);
expect(fuzzy("ts", "İstanbul payments")?.positions).toEqual([15, 16]);
});

it("matches a dotted capital İ as an i, in the text and in the query", () => {
expect(fuzzy("ist", "İstanbul")?.positions).toEqual([0, 1, 2]);
expect(fuzzy("İst", "istanbul")?.positions).toEqual([0, 1, 2]);
expect(fuzzy("İst", "İstanbul")?.positions).toEqual([0, 1, 2]);
});

it("still lowers a word-final sigma in a text with a dotted capital İ", () => {
const text = "İzmir ΟΔΟΣ";
expect(fuzzy("οδος", text)?.positions).toEqual([6, 7, 8, 9]);
expect(fuzzy("ΟΔΟΣ", text)?.positions).toEqual([6, 7, 8, 9]);
});

it("keeps lower-casing text without an İ as it was", () => {
expect(fuzzy("k", "K")?.positions).toEqual([0]); // the Kelvin sign
expect(fuzzy("οδος", "ΟΔΟΣ")?.positions).toEqual([0, 1, 2, 3]); // the final sigma
expect(fuzzy("ss", "straße")).toBeNull(); // no case folding
});
});

describe("fuzzyFields", () => {
it("does not throw on a field with a dotted capital İ and underlines the right characters", () => {
const fields = ["Claude", "İzmir deployments"];
const m = fuzzyFields("ments", fields);
expect(m?.positions[0]).toEqual([]);
expect(underlined(fields[1] as string, m?.positions[1] ?? [])).toBe("ments");
});
});
10 changes: 8 additions & 2 deletions web/src/lib/fuzzy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ const BOUNDARY = /[\s\-_./:@#()[\]{}"'“”«»,;|]/;
const isUpper = (c: string) => c !== c.toLowerCase() && c === c.toUpperCase();
const isLower = (c: string) => c !== c.toUpperCase() && c === c.toLowerCase();

/** `s` in lower case and the same length, so an index into it is an index into `s`. The
* dotted capital İ (U+0130) is the one character whose lower case is longer: "i" and a
* combining dot, which would shift every index after it. It lowers to the "i" alone here;
* everything else, a word-final sigma included, lowers as the whole string does. */
const lowerSameLength = (s: string) => s.replace(/\u0130/g, "i").toLowerCase();

/** Whether `text[i]` begins a word. */
export function isWordStart(text: string, i: number): boolean {
if (i <= 0) return true;
Expand All @@ -35,8 +41,8 @@ export function isWordStart(text: string, i: number): boolean {
/** The best way `query` fits into `text` as a subsequence, or null when it does not.
* An empty query matches everything with a score of 0. */
export function fuzzy(query: string, text: string): FuzzyMatch | null {
const q = query.toLowerCase();
const t = text.toLowerCase();
const q = lowerSameLength(query);
const t = lowerSameLength(text);
const m = q.length;
const n = t.length;
if (m === 0) return { score: 0, positions: [] };
Expand Down