diff --git a/src/domain/desktop/stateRoles.test.ts b/src/domain/desktop/stateRoles.test.ts index fa2a05aded..92cda6e71e 100644 --- a/src/domain/desktop/stateRoles.test.ts +++ b/src/domain/desktop/stateRoles.test.ts @@ -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". diff --git a/src/domain/desktop/stateRoles.ts b/src/domain/desktop/stateRoles.ts index b86e8abd64..0c97d6936d 100644 --- a/src/domain/desktop/stateRoles.ts +++ b/src/domain/desktop/stateRoles.ts @@ -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;