diff --git a/src/vs/workbench/contrib/files/browser/fileActions.ts b/src/vs/workbench/contrib/files/browser/fileActions.ts index 4b0a0547dc94c..ca8d73b9e9257 100644 --- a/src/vs/workbench/contrib/files/browser/fileActions.ts +++ b/src/vs/workbench/contrib/files/browser/fileActions.ts @@ -8,6 +8,7 @@ import * as nls from 'vs/nls'; import * as types from 'vs/base/common/types'; import { isWindows, isLinux } from 'vs/base/common/platform'; import * as extpath from 'vs/base/common/extpath'; +import { extname, basename } from 'vs/base/common/path'; import * as resources from 'vs/base/common/resources'; import { URI } from 'vs/base/common/uri'; import { toErrorMessage } from 'vs/base/common/errorMessage'; @@ -344,70 +345,29 @@ export function findValidPasteFileTarget(targetFolder: ExplorerItem, fileToPaste } export function incrementFileName(name: string, isFolder: boolean): string { - const separators = '[\\.\\-_]'; - const maxNumber = Constants.MAX_SAFE_SMALL_INTEGER; - - // file.1.txt=>file.2.txt - let suffixFileRegex = RegExp('(.*' + separators + ')(\\d+)(\\..*)$'); - if (!isFolder && name.match(suffixFileRegex)) { - return name.replace(suffixFileRegex, (match, g1?, g2?, g3?) => { - let number = parseInt(g2); - return number < maxNumber - ? g1 + strings.pad(number + 1, g2.length) + g3 - : strings.format('{0}{1}.1{2}', g1, g2, g3); - }); - } - - // 1.file.txt=>2.file.txt - let prefixFileRegex = RegExp('(\\d+)(' + separators + '.*)(\\..*)$'); - if (!isFolder && name.match(prefixFileRegex)) { - return name.replace(prefixFileRegex, (match, g1?, g2?, g3?) => { - let number = parseInt(g1); - return number < maxNumber - ? strings.pad(number + 1, g1.length) + g2 + g3 - : strings.format('{0}{1}.1{2}', g1, g2, g3); - }); - } - - // 1.txt=>2.txt - let prefixFileNoNameRegex = RegExp('(\\d+)(\\..*)$'); - if (!isFolder && name.match(prefixFileNoNameRegex)) { - return name.replace(prefixFileNoNameRegex, (match, g1?, g2?) => { - let number = parseInt(g1); - return number < maxNumber - ? strings.pad(number + 1, g1.length) + g2 - : strings.format('{0}.1{1}', g1, g2); - }); - } - - // file.txt=>file.1.txt - const lastIndexOfDot = name.lastIndexOf('.'); - if (!isFolder && lastIndexOfDot >= 0) { - return strings.format('{0}.1{1}', name.substr(0, lastIndexOfDot), name.substr(lastIndexOfDot)); - } - - // folder.1=>folder.2 - if (isFolder && name.match(/(\d+)$/)) { - return name.replace(/(\d+)$/, (match: string, ...groups: any[]) => { - let number = parseInt(groups[0]); - return number < maxNumber - ? strings.pad(number + 1, groups[0].length) - : strings.format('{0}.1', groups[0]); - }); - } - - // 1.folder=>2.folder - if (isFolder && name.match(/^(\d+)/)) { - return name.replace(/^(\d+)(.*)$/, (match: string, ...groups: any[]) => { - let number = parseInt(groups[0]); - return number < maxNumber - ? strings.pad(number + 1, groups[0].length) + groups[1] - : strings.format('{0}{1}.1', groups[0], groups[1]); - }); - } - - // file/folder=>file.1/folder.1 - return strings.format('{0}.1', name); + let namePrefix = name; + let extSuffix = ''; + if (!isFolder) { + extSuffix = extname(name); + namePrefix = basename(name, extSuffix); + } + + // name copy 5(.txt) => name copy 6(.txt) + // name copy(.txt) => name copy 2(.txt) + const suffixRegex = /^(.+ copy)( \d+)?$/; + if (suffixRegex.test(namePrefix)) { + return namePrefix.replace(suffixRegex, (match, g1?, g2?) => { + let number = (g2 ? parseInt(g2) : 1); + return number === 0 + ? `${g1}` + : (number < Constants.MAX_SAFE_SMALL_INTEGER + ? `${g1} ${number + 1}` + : `${g1}${g2} copy`); + }) + extSuffix; + } + + // name(.txt) => name copy(.txt) + return `${namePrefix} copy${extSuffix}`; } // Global Compare with diff --git a/src/vs/workbench/contrib/files/test/electron-browser/fileActions.test.ts b/src/vs/workbench/contrib/files/test/electron-browser/fileActions.test.ts index dde3743e76673..d52b99bb2d907 100644 --- a/src/vs/workbench/contrib/files/test/electron-browser/fileActions.test.ts +++ b/src/vs/workbench/contrib/files/test/electron-browser/fileActions.test.ts @@ -11,157 +11,127 @@ suite('Files - Increment file name', () => { test('Increment file name without any version', function () { const name = 'test.js'; const result = incrementFileName(name, false); - assert.strictEqual(result, 'test.1.js'); + assert.strictEqual(result, 'test copy.js'); }); - test('Increment folder name without any version', function () { - const name = 'test'; - const result = incrementFileName(name, true); - assert.strictEqual(result, 'test.1'); + test('Increment file name with suffix version', function () { + const name = 'test copy.js'; + const result = incrementFileName(name, false); + assert.strictEqual(result, 'test copy 2.js'); }); - test('Increment file name with suffix version', function () { - const name = 'test.1.js'; + test('Increment file name with suffix version with leading zeros', function () { + const name = 'test copy 005.js'; const result = incrementFileName(name, false); - assert.strictEqual(result, 'test.2.js'); + assert.strictEqual(result, 'test copy 6.js'); }); - test('Increment file name with suffix version with trailing zeros', function () { - const name = 'test.001.js'; + test('Increment file name with suffix version, too big number', function () { + const name = 'test copy 9007199254740992.js'; const result = incrementFileName(name, false); - assert.strictEqual(result, 'test.002.js'); + assert.strictEqual(result, 'test copy 9007199254740992 copy.js'); }); - test('Increment file name with suffix version with trailing zeros, changing length', function () { - const name = 'test.009.js'; + test('Increment file name with just version in name', function () { + const name = 'copy.js'; const result = incrementFileName(name, false); - assert.strictEqual(result, 'test.010.js'); + assert.strictEqual(result, 'copy copy.js'); }); - test('Increment file name with suffix version with `-` as separator', function () { - const name = 'test-1.js'; + test('Increment file name with just version in name, v2', function () { + const name = 'copy 2.js'; const result = incrementFileName(name, false); - assert.strictEqual(result, 'test-2.js'); + assert.strictEqual(result, 'copy 2 copy.js'); }); - test('Increment file name with suffix version with `-` as separator, trailing zeros', function () { - const name = 'test-001.js'; + test('Increment file name without any extension or version', function () { + const name = 'test'; const result = incrementFileName(name, false); - assert.strictEqual(result, 'test-002.js'); + assert.strictEqual(result, 'test copy'); }); - test('Increment file name with suffix version with `-` as separator, trailing zeros, changnig length', function () { - const name = 'test-099.js'; + test('Increment file name without any extension or version, trailing dot', function () { + const name = 'test.'; const result = incrementFileName(name, false); - assert.strictEqual(result, 'test-100.js'); + assert.strictEqual(result, 'test copy.'); }); - test('Increment file name with suffix version with `_` as separator', function () { - const name = 'test_1.js'; + test('Increment file name without any extension or version, leading dot', function () { + const name = '.test'; const result = incrementFileName(name, false); - assert.strictEqual(result, 'test_2.js'); + assert.strictEqual(result, '.test copy'); }); - test('Increment folder name with suffix version', function () { - const name = 'test.1'; - const result = incrementFileName(name, true); - assert.strictEqual(result, 'test.2'); + test('Increment file name without any extension or version, leading dot v2', function () { + const name = '..test'; + const result = incrementFileName(name, false); + assert.strictEqual(result, '. copy.test'); }); - test('Increment folder name with suffix version, trailing zeros', function () { - const name = 'test.001'; - const result = incrementFileName(name, true); - assert.strictEqual(result, 'test.002'); + test('Increment file name without any extension but with suffix version', function () { + const name = 'test copy 5'; + const result = incrementFileName(name, false); + assert.strictEqual(result, 'test copy 6'); }); - test('Increment folder name with suffix version with `-` as separator', function () { - const name = 'test-1'; + test('Increment folder name without any version', function () { + const name = 'test'; const result = incrementFileName(name, true); - assert.strictEqual(result, 'test-2'); + assert.strictEqual(result, 'test copy'); }); - test('Increment folder name with suffix version with `_` as separator', function () { - const name = 'test_1'; + test('Increment folder name with suffix version', function () { + const name = 'test copy'; const result = incrementFileName(name, true); - assert.strictEqual(result, 'test_2'); + assert.strictEqual(result, 'test copy 2'); }); - test('Increment file name with suffix version, too big number', function () { - const name = 'test.9007199254740992.js'; - const result = incrementFileName(name, false); - assert.strictEqual(result, 'test.9007199254740992.1.js'); + test('Increment folder name with suffix version, leading zeros', function () { + const name = 'test copy 005'; + const result = incrementFileName(name, true); + assert.strictEqual(result, 'test copy 6'); }); test('Increment folder name with suffix version, too big number', function () { - const name = 'test.9007199254740992'; + const name = 'test copy 9007199254740992'; const result = incrementFileName(name, true); - assert.strictEqual(result, 'test.9007199254740992.1'); + assert.strictEqual(result, 'test copy 9007199254740992 copy'); }); - test('Increment file name with prefix version', function () { - const name = '1.test.js'; - const result = incrementFileName(name, false); - assert.strictEqual(result, '2.test.js'); - }); - - test('Increment file name with just version in name', function () { - const name = '1.js'; - const result = incrementFileName(name, false); - assert.strictEqual(result, '2.js'); - }); - - test('Increment file name with just version in name, too big number', function () { - const name = '9007199254740992.js'; - const result = incrementFileName(name, false); - assert.strictEqual(result, '9007199254740992.1.js'); - }); - - test('Increment file name with prefix version, trailing zeros', function () { - const name = '001.test.js'; - const result = incrementFileName(name, false); - assert.strictEqual(result, '002.test.js'); - }); - - test('Increment file name with prefix version with `-` as separator', function () { - const name = '1-test.js'; - const result = incrementFileName(name, false); - assert.strictEqual(result, '2-test.js'); - }); - - test('Increment file name with prefix version with `-` as separator', function () { - const name = '1_test.js'; - const result = incrementFileName(name, false); - assert.strictEqual(result, '2_test.js'); + test('Increment folder name with just version in name', function () { + const name = 'copy'; + const result = incrementFileName(name, true); + assert.strictEqual(result, 'copy copy'); }); - test('Increment file name with prefix version, too big number', function () { - const name = '9007199254740992.test.js'; - const result = incrementFileName(name, false); - assert.strictEqual(result, '9007199254740992.test.1.js'); + test('Increment folder name with just version in name, v2', function () { + const name = 'copy 2'; + const result = incrementFileName(name, true); + assert.strictEqual(result, 'copy 2 copy'); }); - test('Increment folder name with prefix version', function () { - const name = '1.test'; + test('Increment folder name "with extension" but without any version', function () { + const name = 'test.js'; const result = incrementFileName(name, true); - assert.strictEqual(result, '2.test'); + assert.strictEqual(result, 'test.js copy'); }); - test('Increment folder name with prefix version, too big number', function () { - const name = '9007199254740992.test'; + test('Increment folder name "with extension" and with suffix version', function () { + const name = 'test.js copy 5'; const result = incrementFileName(name, true); - assert.strictEqual(result, '9007199254740992.test.1'); + assert.strictEqual(result, 'test.js copy 6'); }); - test('Increment folder name with prefix version, trailing zeros', function () { - const name = '001.test'; + test('Increment file/folder name with suffix version, special case 1', function () { + const name = 'test copy 0'; const result = incrementFileName(name, true); - assert.strictEqual(result, '002.test'); + assert.strictEqual(result, 'test copy'); }); - test('Increment folder name with prefix version with `-` as separator', function () { - const name = '1-test'; + test('Increment file/folder name with suffix version, special case 2', function () { + const name = 'test copy 1'; const result = incrementFileName(name, true); - assert.strictEqual(result, '2-test'); + assert.strictEqual(result, 'test copy 2'); }); -}); \ No newline at end of file +});