Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prefix case for incrementFileName #50003

Merged
merged 4 commits into from
May 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 21 additions & 4 deletions src/vs/workbench/parts/files/electron-browser/fileActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1058,11 +1058,23 @@ function findValidPasteFileTarget(targetFolder: ExplorerItem, fileToPaste: { res
return candidate;
}

function incrementFileName(name: string, isFolder: boolean): string {
export function incrementFileName(name: string, isFolder: boolean): string {
const separators = '[\\.\\-_]';

// file.1.txt=>file.2.txt
if (!isFolder && name.match(/(.*\.)(\d+)(\..*)$/)) {
return name.replace(/(.*\.)(\d+)(\..*)$/, (match, g1?, g2?, g3?) => { return g1 + (parseInt(g2) + 1) + g3; });
let suffixFileRegex = RegExp('(.*' + separators + ')(\\d+)(\\..*)$');
if (!isFolder && name.match(suffixFileRegex)) {
return name.replace(suffixFileRegex, (match, g1?, g2?, g3?) => {
return g1 + strings.pad(parseInt(g2) + 1, g2.length) + 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?) => {
return strings.pad(parseInt(g1) + 1, g1.length) + g2 + g3;
});
}

// file.txt=>file.1.txt
Expand All @@ -1073,7 +1085,12 @@ function incrementFileName(name: string, isFolder: boolean): string {

// folder.1=>folder.2
if (isFolder && name.match(/(\d+)$/)) {
return name.replace(/(\d+)$/, (match: string, ...groups: any[]) => { return String(parseInt(groups[0]) + 1); });
return name.replace(/(\d+)$/, (match: string, ...groups: any[]) => { return strings.pad(parseInt(groups[0]) + 1, groups[0].length); });
}

// 1.folder=>2.folder
if (isFolder && name.match(/^(\d+)/)) {
return name.replace(/^(\d+)/, (match: string, ...groups: any[]) => { return strings.pad(parseInt(groups[0]) + 1, groups[0].length); });
}

// file/folder=>file.1/folder.1
Expand Down
133 changes: 133 additions & 0 deletions src/vs/workbench/parts/files/test/electron-browser/fileActions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

'use strict';

import * as assert from 'assert';
import { incrementFileName } from 'vs/workbench/parts/files/electron-browser/fileActions';

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');
});

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.1.js';
const result = incrementFileName(name, false);
assert.strictEqual(result, 'test.2.js');
});

test('Increment file name with suffix version with trailing zeros', function () {
const name = 'test.001.js';
const result = incrementFileName(name, false);
assert.strictEqual(result, 'test.002.js');
});

test('Increment file name with suffix version with trailing zeros, changing length', function () {
const name = 'test.009.js';
const result = incrementFileName(name, false);
assert.strictEqual(result, 'test.010.js');
});

test('Increment file name with suffix version with `-` as separator', function () {
const name = 'test-1.js';
const result = incrementFileName(name, false);
assert.strictEqual(result, 'test-2.js');
});

test('Increment file name with suffix version with `-` as separator, trailing zeros', function () {
const name = 'test-001.js';
const result = incrementFileName(name, false);
assert.strictEqual(result, 'test-002.js');
});

test('Increment file name with suffix version with `-` as separator, trailing zeros, changnig length', function () {
const name = 'test-099.js';
const result = incrementFileName(name, false);
assert.strictEqual(result, 'test-100.js');
});

test('Increment file name with suffix version with `_` as separator', function () {
const name = 'test_1.js';
const result = incrementFileName(name, false);
assert.strictEqual(result, 'test_2.js');
});

test('Increment folder name with suffix version', function () {
const name = 'test.1';
const result = incrementFileName(name, true);
assert.strictEqual(result, 'test.2');
});

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 folder name with suffix version with `-` as separator', function () {
const name = 'test-1';
const result = incrementFileName(name, true);
assert.strictEqual(result, 'test-2');
});

test('Increment folder name with suffix version with `_` as separator', function () {
const name = 'test_1';
const result = incrementFileName(name, true);
assert.strictEqual(result, 'test_2');
});

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 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 suffix version', function () {
const name = '1.test';
const result = incrementFileName(name, true);
assert.strictEqual(result, '2.test');
});

test('Increment folder name with suffix version, trailing zeros', function () {
const name = '001.test';
const result = incrementFileName(name, true);
assert.strictEqual(result, '002.test');
});

test('Increment folder name with suffix version with `-` as separator', function () {
const name = '1-test';
const result = incrementFileName(name, true);
assert.strictEqual(result, '2-test');
});

});