このようなものが必要です。プロジェクト内にフォルダーを作成したら、たとえば React プロジェクトを作成します。そのフォルダーを選択して、これを実行する何らかのマクロまたは一連のアクションを実行できます。
たとえば、フォルダーの名前が の場合Component
、マクロが再生されると次のようになります。
何らかのスニペットのデフォルト コンテンツを含む、 という名前でファイルを作成しますComponent.js
。 何らかのComponent.styled.js
スニペットのデフォルト コンテンツを含む、 という名前でファイルを作成します。index.js
Content: を含む、 という名前でファイルを作成します。export { default } from './Component'
Component は実際にはフォルダーの名前です。
実際にどこから始めればよいのか全くわかりません...マクロを調べ、Visual Studio Code ユーザー スニペットの使用方法も知っていますが、これを正確にどのように実行できるかはわかりません。
ベストアンサー1
マクロ拡張を使用するとマクロコマンダーフォルダーとインクルード ファイル (必要に応じてデフォルトのコンテンツを含む) をかなり簡単に作成できます。ただし、基本的には settings.json に拡張機能を記述するため、少し複雑に見えますが、操作はスムーズです。拡張機能をインストールした後、次のコードを settings.json に入力します。
"macros": {
"createReactFolderAndFiles" : [
{
"javascript": [
"const newReactFolder = await window.showInputBox()", // get the component's name
// create a object to store various edits, including file creation, deletion and edits
"const we = new vscode.WorkspaceEdit();",
// get the workspace folder path + file:/// uri scheme
"const thisWorkspace = vscode.workspace.workspaceFolders[0].uri.toString();", // file:///c:/Users/Mark/OneDrive/Test Bed
// append the new folder name
"const uriBase = `${thisWorkspace}/${newReactFolder}`;", // file:///c:/Users/Mark/OneDrive/Test Bed/Test
// "await window.showInformationMessage(` ${uriBase}`)", // just handy to check on progress
// create uri's for the three files to be created
"let newUri1 = vscode.Uri.parse(`${uriBase}/index.js`);",
"let newUri2 = vscode.Uri.parse(`${uriBase}/${newReactFolder}.js`);",
"let newUri3 = vscode.Uri.parse(`${uriBase}/${newReactFolder}.styled.js`);",
"let newFiles = [ newUri1, newUri2, newUri3];",
"for (const newFile of newFiles) { let document = await we.createFile(newFile, { ignoreIfExists: false, overwrite: true });};",
"let styledContent = `first line flush left", // how to use template literals in this macro
"second line will be flush left",
" third line indented`;",
// insert text into the top of each file
"await we.insert(newUri1, new vscode.Position(0, 0), `export { default } from './${newReactFolder}'`);",
"await we.insert(newUri2, new vscode.Position(0, 0), `my ${newReactFolder}.js content`);",
"await we.insert(newUri3, new vscode.Position(0, 0), styledContent);",
"await vscode.workspace.applyEdit(we);", // apply all the edits: file creation and adding text
"for (const newFile of newFiles) { let document = await vscode.workspace.openTextDocument(newFile); await document.save(); };",
]
}
]
},
設定で vscode 拡張 API を使用できるようになります。このマクロを実行するには、キーバインディングを設定します。
{
"key": "alt+j",
"command": "macros.createReactFolderAndFiles"
},
最初にフォルダ名の入力ボックスが表示されます。フォルダ名を入力して を押しますEnter。
繰り返し使用したいデフォルトのコンテンツがある場合は、それを保存して、このマクロで使用するために取得することができます。デフォルトのコンテンツが同じワークスペースにあるとします/templates/defaultComponent.txt
。
これにより、次のコンテンツが取得されます。
"let defaultContent = await vscode.workspace.fs.readFile(vscode.Uri.parse(`${thisWorkspace}/templates/defaultComponent.txt`));",
それをファイルの 1 つに追加します。
"await we.insert(newUri3, new vscode.Position(0, 0), defaultContent.toString());",
デモでわかるように、何らかの理由で常に新しく作成されたファイルのいくつかが開かれますが、それを防ぐ方法はわかりません。
コードは複雑に見えますが、異なるファイル名やコンテンツに合わせて変更するのは簡単だと思います。