mirror of
https://github.com/microsoft/vscode-extension-samples.git
synced 2026-06-13 07:10:26 +08:00
tweak commands
This commit is contained in:
@ -15,7 +15,7 @@
|
||||
"Other"
|
||||
],
|
||||
"activationEvents": [
|
||||
"onCommand:fs/findJS",
|
||||
"onCommand:fs/openJS",
|
||||
"onCommand:fs/sumSizes",
|
||||
"onCommand:fs/readWriteFile"
|
||||
],
|
||||
@ -23,13 +23,13 @@
|
||||
"contributes": {
|
||||
"commands": [
|
||||
{
|
||||
"command": "fs/findJS",
|
||||
"title": "Find JavaScript",
|
||||
"command": "fs/openJS",
|
||||
"title": "Open JavaScript-file for TypeScript-file...",
|
||||
"category": "FS"
|
||||
},
|
||||
{
|
||||
"command": "fs/sumSizes",
|
||||
"title": "Sum file sizes in folder",
|
||||
"title": "Show folder information for current file",
|
||||
"category": "FS"
|
||||
},
|
||||
{
|
||||
|
||||
@ -9,12 +9,13 @@ import { posix } from 'path';
|
||||
|
||||
export function activate(context: vscode.ExtensionContext) {
|
||||
|
||||
// Command #1 - Check if for a TypeScript-file a JavaScript-file exists
|
||||
// Command #1 - Check and show a JavaScript-file for a TypeScript-file
|
||||
// * shows how to derive a new uri from an existing uri
|
||||
// * shows how to check for existence of a file
|
||||
vscode.commands.registerCommand('fs/findJS', async function () {
|
||||
if (!vscode.window.activeTextEditor ||
|
||||
posix.extname(vscode.window.activeTextEditor.document.uri.path) !== '.ts'
|
||||
vscode.commands.registerCommand('fs/openJS', async function () {
|
||||
if (
|
||||
!vscode.window.activeTextEditor
|
||||
|| posix.extname(vscode.window.activeTextEditor.document.uri.path) !== '.ts'
|
||||
) {
|
||||
return vscode.window.showInformationMessage('Open a TypeScript file first');
|
||||
}
|
||||
@ -22,9 +23,10 @@ export function activate(context: vscode.ExtensionContext) {
|
||||
const tsUri = vscode.window.activeTextEditor.document.uri;
|
||||
const jsPath = posix.join(tsUri.path, '..', posix.basename(tsUri.path, '.ts') + '.js');
|
||||
const jsUri = tsUri.with({ path: jsPath });
|
||||
|
||||
try {
|
||||
await vscode.workspace.fs.stat(jsUri);
|
||||
vscode.window.showInformationMessage(`${jsUri.toString(true)} file does exist`);
|
||||
vscode.window.showTextDocument(jsUri, { viewColumn: vscode.ViewColumn.Beside });
|
||||
} catch {
|
||||
vscode.window.showInformationMessage(`${jsUri.toString(true)} file does *not* exist`);
|
||||
}
|
||||
@ -33,28 +35,34 @@ export function activate(context: vscode.ExtensionContext) {
|
||||
// Command #2 - Compute total size of files in a folder
|
||||
// * shows how to read a directory
|
||||
// * shows how retrieve metadata for a file
|
||||
// * create an untitled document that shows count and total of files
|
||||
vscode.commands.registerCommand('fs/sumSizes', async function () {
|
||||
|
||||
async function sizeOfAllFilesInFolder(folder: vscode.Uri) {
|
||||
let sum = 0;
|
||||
async function countAndTotalOfFilesInFolder(folder: vscode.Uri): Promise<{ total: number, count: number }> {
|
||||
let total = 0;
|
||||
let count = 0;
|
||||
for (const [name, type] of await vscode.workspace.fs.readDirectory(folder)) {
|
||||
if (type === vscode.FileType.File) {
|
||||
const filePath = posix.join(folder.path, name);
|
||||
const stat = await vscode.workspace.fs.stat(folder.with({ path: filePath }));
|
||||
sum += stat.size;
|
||||
total += stat.size;
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
return { total, count };
|
||||
}
|
||||
|
||||
if (!vscode.workspace.workspaceFolders) {
|
||||
return vscode.window.showInformationMessage('No folder or workspace opened');
|
||||
if (!vscode.window.activeTextEditor) {
|
||||
return vscode.window.showInformationMessage('Open a file first');
|
||||
}
|
||||
|
||||
for (const folder of vscode.workspace.workspaceFolders) {
|
||||
const total = await sizeOfAllFilesInFolder(folder.uri);
|
||||
vscode.window.showInformationMessage(`${total} bytes in ${folder.uri.toString(true)}`);
|
||||
}
|
||||
const fileUri = vscode.window.activeTextEditor.document.uri;
|
||||
const folderPath = posix.dirname(fileUri.path);
|
||||
const folderUri = fileUri.with({ path: folderPath });
|
||||
|
||||
const info = await countAndTotalOfFilesInFolder(folderUri);
|
||||
const doc = await vscode.workspace.openTextDocument({ content: `${info.count} files in ${folderUri.toString(true)} with a total of ${info.total} bytes` });
|
||||
vscode.window.showTextDocument(doc, { viewColumn: vscode.ViewColumn.Beside });
|
||||
});
|
||||
|
||||
// Command #3 - Write and read a file
|
||||
@ -76,7 +84,9 @@ export function activate(context: vscode.ExtensionContext) {
|
||||
|
||||
const readData = await vscode.workspace.fs.readFile(fileUri);
|
||||
const readStr = Buffer.from(readData).toString('utf8');
|
||||
|
||||
vscode.window.showInformationMessage(readStr);
|
||||
vscode.window.showTextDocument(fileUri);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user