fix(desktop): classify complete filename boundary code points

This commit is contained in:
ShuaixinHuang authored and Ysopwnable committed 2026-11-21 20:22:43 +01:00
1 parent d07d28b533
commit d9f2fd365d
2 files changed
+24 -25

No files matched your search

+18 -18
View File
@@ -139,24 +139,24 @@ describe("stateReasons", () => {
expect(reasons("/home/ada/Documents/today.tx")).toEqual([]);
});
it.each(["𐐀notes.txt", "notes.txt𐐀"])(
"does not attribute an opened file to a title naming %s",
(name) => {
const path = "/home/ada/notes.txt";
const facts: DesktopRoleFacts = {
home: "/home/ada",
stateDirectories: [],
stateTails: [],
selectedRoots: [],
selections: [],
titles: [`${name} - Editor`],
openedFiles: new Set([path]),
};
expect(stateReasons(facts, path, [], false)).toEqual([
"opened_by_application",
]);
},
);
it.each([
["𐐀notes.txt", ["opened_by_application"]],
["notes.txt𐐀", ["opened_by_application"]],
["📄notes.txt", ["opened_by_application", "named_in_window_title"]],
["notes.txt📄", ["opened_by_application", "named_in_window_title"]],
])("attributes an opened file using the boundary in %s", (name, reasons) => {
const path = "/home/ada/notes.txt";
const facts: DesktopRoleFacts = {
home: "/home/ada",
stateDirectories: [],
stateTails: [],
selectedRoots: [],
selections: [],
titles: [`${name} - Editor`],
openedFiles: new Set([path]),
};
expect(stateReasons(facts, path, [], false)).toEqual(reasons);
});
it("names a file by its whole name or path, never by a folder segment, a cache or a dconf database", () => {
// CherryTree shows the document's folder, whose path holds the segment "user".
+6 -7
View File
@@ -204,8 +204,6 @@ export const roleFacts = (
};
const FILENAME_CHARACTER = /[\p{L}\p{M}\p{N}._~-]/u;
const BEFORE_FILENAME = new RegExp(`${FILENAME_CHARACTER.source}$`, "u");
const AFTER_FILENAME = new RegExp(`^${FILENAME_CHARACTER.source}`, "u");
/** Home folders whose files a title never names as a document: caches, and dconf's databases. */
const NEVER_NAMED = [".cache", ".config/dconf"];
@@ -221,13 +219,14 @@ const spelledIn = (title: string, spelling: string, bare = false): boolean => {
at >= 0;
at = title.indexOf(spelling, at + 1)
) {
const before = title[at - 1];
const end = at + spelling.length;
// A neighboring Unicode code point can occupy two UTF-16 code units.
// Inspect one complete neighboring code point on each side of the match.
const before = Array.from(title.slice(Math.max(0, at - 2), at)).at(-1);
const after = Array.from(title.slice(end, end + 2))[0];
if (
!BEFORE_FILENAME.test(title.slice(Math.max(0, at - 2), at)) &&
!AFTER_FILENAME.test(title.slice(end, end + 2)) &&
title[end] !== "/" &&
(before === undefined || !FILENAME_CHARACTER.test(before)) &&
(after === undefined || !FILENAME_CHARACTER.test(after)) &&
after !== "/" &&
!(bare && before === "/")
)
return true;