-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileOps.js
More file actions
92 lines (84 loc) · 3.06 KB
/
Copy pathfileOps.js
File metadata and controls
92 lines (84 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*---------------------------------------------------------------------------------------------
* LevelCode — Notepad++ Pack
* Feature: Duplicate file/folder from the Explorer context menu (and Cmd+D in the tree).
*
* Mirrors Finder/Notepad++ "Duplicate": creates "name copy.ext" next to the original,
* with collision-safe numbering ("name copy 2.ext"). Works on files and folders
* (folders copy recursively) and on multi-selection.
*--------------------------------------------------------------------------------------------*/
// @ts-check
'use strict';
const vscode = require('vscode');
const path = require('path');
/** @param {string} fsPath */
async function pathExists(fsPath) {
try {
await vscode.workspace.fs.stat(vscode.Uri.file(fsPath));
return true;
} catch {
return false;
}
}
/**
* Build a non-colliding duplicate path: "base copy.ext", then "base copy 2.ext", …
* @param {string} srcFsPath
*/
async function nextCopyPath(srcFsPath) {
const dir = path.dirname(srcFsPath);
const ext = path.extname(srcFsPath); // '' for dotfiles like .env
const base = path.basename(srcFsPath, ext);
let candidate = path.join(dir, `${base} copy${ext}`);
let n = 2;
while (await pathExists(candidate)) {
candidate = path.join(dir, `${base} copy ${n}${ext}`);
n++;
}
return candidate;
}
/**
* @param {vscode.Uri|undefined} clickedUri
* @param {vscode.Uri[]|undefined} selectedUris
*/
async function duplicate(clickedUri, selectedUris) {
// Explorer passes (clickedResource, [allSelectedResources]). Fall back to the
// active editor's file when invoked without an explorer target.
let targets = (selectedUris && selectedUris.length) ? selectedUris
: (clickedUri ? [clickedUri] : []);
if (targets.length === 0 && vscode.window.activeTextEditor) {
targets = [vscode.window.activeTextEditor.document.uri];
}
targets = targets.filter(u => u && u.scheme === 'file');
if (targets.length === 0) {
vscode.window.showInformationMessage('LevelCode: nothing to duplicate (select a file or folder in the Explorer).');
return;
}
/** @type {vscode.Uri|undefined} */
let lastNew;
for (const src of targets) {
try {
const target = vscode.Uri.file(await nextCopyPath(src.fsPath));
await vscode.workspace.fs.copy(src, target, { overwrite: false });
lastNew = target;
} catch (e) {
vscode.window.showErrorMessage(
`LevelCode: could not duplicate ${path.basename(src.fsPath)}: ${e && e.message ? e.message : String(e)}`
);
}
}
if (lastNew) {
// Select the new item in the Explorer so it's easy to rename next.
try { await vscode.commands.executeCommand('revealInExplorer', lastNew); } catch { /* noop */ }
const label = path.basename(lastNew.fsPath);
vscode.window.setStatusBarMessage(
`$(files) LevelCode: duplicated → ${label}${targets.length > 1 ? ` (+${targets.length - 1} more)` : ''}`,
2500
);
}
}
/** @param {vscode.ExtensionContext} context */
function registerFileOps(context) {
context.subscriptions.push(
vscode.commands.registerCommand('levelcode.file.duplicate', duplicate)
);
}
module.exports = { registerFileOps };