From 66d04b116168b123a6309633883fa61b88278d22 Mon Sep 17 00:00:00 2001 From: vycdev2 Date: Sun, 9 Aug 2026 23:53:40 +0000 Subject: [PATCH] fix: correct repeated capture ranges --- CHANGELOG.md | 1 + src/utils/regex.ts | 9 +++++---- test/regex.spec.ts | 10 ++++++++++ 3 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 test/regex.spec.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 72caf8d..525b51a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ### Version 5.3.0 (Unreleased) +- Fixed decoration ranges so repeated captured text highlights the final captured segment instead of an earlier duplicate. - Fixed dependency indexing so one unreadable or removed Todo file no longer hides dependencies from other workspace files. - Fixed embedded todo parsing for file paths that start with a number. - Fixed date-sorted archiving so consecutive finished todos are ordered by their own completion dates. diff --git a/src/utils/regex.ts b/src/utils/regex.ts index 1578d39..55e9957 100644 --- a/src/utils/regex.ts +++ b/src/utils/regex.ts @@ -1,7 +1,5 @@ /* IMPORT */ -import * as _ from 'lodash'; - /* REGEX */ const Regex = { @@ -21,8 +19,11 @@ const Regex = { match2range(match: RegExpMatchArray) { const first = match[0], - last = _.findLast(match, (txt) => txt && txt.length) as string, //TSC - start = match.index + first.indexOf(last), + last = match + .slice() + .reverse() + .find((txt) => !!(txt && txt.length)) as string, //TSC + start = match.index + first.lastIndexOf(last), end = start + last.length; return { start, end }; diff --git a/test/regex.spec.ts b/test/regex.spec.ts new file mode 100644 index 0000000..c32dd48 --- /dev/null +++ b/test/regex.spec.ts @@ -0,0 +1,10 @@ +import { expect } from 'chai'; +import Regex from '../src/utils/regex'; + +describe('Regex utilities', () => { + it('ranges the final capture when its text repeats earlier in the full match', () => { + const match = /(foo)(foo)/.exec('foofoo')!; + + expect(Regex.match2range(match)).to.deep.equal({ start: 3, end: 6 }); + }); +});