add some doc, tweak command names

This commit is contained in:
Johannes Rieken
2019-07-23 09:59:41 +02:00
parent 31062c4634
commit 1740bcdbdb
3 changed files with 48 additions and 23 deletions

View File

@ -1 +1,18 @@
# FileSystem Consumer
# FileSystem Usage Sample
This is a sample extension that shows how to use the `vscode.workspace.fs` API. It is a series of commands, all prefixed with *FS*, that demonstrate file system capabilities and how to derive file-uris from existing uris.
### Derive new paths with `path.posix`
Throughout this sample [`path.posix`](https://nodejs.org/dist/latest-v10.x/docs/api/path.html#path_path_posix) is being used. This is important because uri paths are always slash-separated (`/`) and because the backslash (`\`) can be a valid file name. For more details see: https://nodejs.org/dist/latest-v10.x/docs/api/path.html#path_windows_vs_posix
# How it works, what it shows?
- The extension registers different commands that use the `workspace.fs`-API.
- Registers command and the corresponding activation events via `package.json`
# How to run locally
* `npm run compile` to start the compiler in watch mode
* open this folder in VS Code and press `F5`

View File

@ -15,27 +15,27 @@
"Other"
],
"activationEvents": [
"onCommand:fsConsume/findJS",
"onCommand:fsConsume/sumSizes",
"onCommand:fsConsume/readWriteFile"
"onCommand:fs/findJS",
"onCommand:fs/sumSizes",
"onCommand:fs/readWriteFile"
],
"main": "./out/src/extension",
"contributes": {
"commands": [
{
"command": "fsConsume/findJS",
"command": "fs/findJS",
"title": "Find JavaScript",
"category": "ConsumeFS"
"category": "FS"
},
{
"command": "fsConsume/sumSizes",
"command": "fs/sumSizes",
"title": "Sum file sizes in folder",
"category": "ConsumeFS"
"category": "FS"
},
{
"command": "fsConsume/readWriteFile",
"command": "fs/readWriteFile",
"title": "Read and Write file",
"category": "ConsumeFS"
"category": "FS"
}
]
},

View File

@ -1,15 +1,18 @@
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
'use strict';
import * as vscode from 'vscode';
import { posix } from 'path';
import { StringDecoder } from 'string_decoder';
export function activate(context: vscode.ExtensionContext) {
// Sample 1 - Check if for a TypeScript file a JavaScript files exists
// * use `uri.with` to derive a new uri without loosing things like scheme, authority, query, or fragment
// * use `path.posix` because uris use slashes to separate paths, e.g. backslash is a valid file name character
vscode.commands.registerCommand('fsConsume/findJS', async function () {
// Command #1 - Check if for a TypeScript-file a JavaScript-file exists
// * 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'
) {
@ -27,8 +30,10 @@ export function activate(context: vscode.ExtensionContext) {
}
});
// Sample 2 - Sum up the size of all files inside a folder (none recursive)
vscode.commands.registerCommand('fsConsume/sumSizes', async function () {
// Command #2 - Compute total size of files in a folder
// * shows how to read a directory
// * shows how retrieve metadata for a file
vscode.commands.registerCommand('fs/sumSizes', async function () {
async function sizeOfAllFilesInFolder(folder: vscode.Uri) {
let sum = 0;
@ -52,22 +57,25 @@ export function activate(context: vscode.ExtensionContext) {
}
});
// Sample 3 - Create a Uint8Array from a string and reverse
vscode.commands.registerCommand('fsConsume/readWriteFile', async function () {
// Command #3 - Write and read a file
// * shows how to derive a new file-uri from a folder-uri
// * shows how to convert a string into a typed array and back
vscode.commands.registerCommand('fs/readWriteFile', async function () {
if (!vscode.workspace.workspaceFolders) {
return vscode.window.showInformationMessage('No folder or workspace opened');
}
const writeStr = '1€ is 1.12$ is 0.9£';
const writeData = Buffer.from(writeStr);
const writeData = Buffer.from(writeStr, 'utf8');
const folderUri = vscode.workspace.workspaceFolders[0].uri;
const fileUri = folderUri.with({ path: posix.join(folderUri.path, 'test.txt') });
const folder = vscode.workspace.workspaceFolders[0].uri;
const fileUri = folder.with({ path: posix.join(folder.path, 'test.txt') });
await vscode.workspace.fs.writeFile(fileUri, writeData);
const readData = await vscode.workspace.fs.readFile(fileUri);
const readStr = new StringDecoder().end(Buffer.from(readData));
const readStr = Buffer.from(readData).toString('utf8');
vscode.window.showInformationMessage(readStr);
});