From 84efd7b5dc15d14d5a5c31f22137513f62025f1a Mon Sep 17 00:00:00 2001 From: Ramya Achutha Rao Date: Wed, 19 Jul 2017 17:52:29 -0700 Subject: [PATCH] Support offset when numbering Microsoft/vscode#30078 --- lib/numbering.js | 4 ++-- lib/utils.js | 14 ++++++++++++-- test/numbering.js | 5 +++++ 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/numbering.js b/lib/numbering.js index b96e2fb..02fb377 100644 --- a/lib/numbering.js +++ b/lib/numbering.js @@ -103,8 +103,8 @@ function getNumberingRanges(str) { * @return {String} */ function replaceNumberingRanges(str, ranges, value) { - const replaced = replaceRanges(str, ranges, token => { - let _value = String(value); + const replaced = replaceRanges(str, ranges, (token, offset) => { + let _value = String(value + offset); // pad values for multiple numbering tokens, e.g. 3 for $$$ becomes 003 while (_value.length < token.length) { _value = '0' + _value; diff --git a/lib/utils.js b/lib/utils.js index a2be5d5..3a40604 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -45,9 +45,19 @@ export function replaceRanges(str, ranges, value) { for (let i = ranges.length - 1; i >= 0; i--) { const r = ranges[i]; + let offset = 0; + let offsetLength = 0; + if (str.substr(r[0] + r[1], 1) === '@'){ + const matches = str.substr(r[0] + r[1] + 1).match(/^(\d+)/); + if (matches) { + offsetLength = matches[1].length + 1; + offset = parseInt(matches[1]) - 1; + } + } + str = str.substring(0, r[0]) - + (typeof value === 'function' ? value(str.substr(r[0], r[1])) : value) - + str.substring(r[0] + r[1]); + + (typeof value === 'function' ? value(str.substr(r[0], r[1]), offset) : value) + + str.substring(r[0] + r[1] + offsetLength); } return str; diff --git a/test/numbering.js b/test/numbering.js index a16d749..2af5432 100644 --- a/test/numbering.js +++ b/test/numbering.js @@ -41,4 +41,9 @@ describe('Item numbering', () => { const tree = numbering(parse('[!foo="bar$"]*2')); assert(tree.firstChild.getAttribute('foo').options.implied); }); + + it('add offset to numbering', () => { + assert.equal(expand('span.item$@3*2'), ''); + assert.equal(expand('span.item$$@3*2'), ''); + }); });