i try this to put a text sample into a new document create in the same command.
import * as vscode from 'vscode';
import * as fs from 'fs';
export function activate(context: vscode.ExtensionContext) {
let disposable_getSelectedFolder = vscode.commands.registerCommand('tima.getSelectedFolder', (uri:vscode.Uri) => {
let selectedPath = uri.fsPath;
let fileNameBase;
if(selectedPath){
const wsedit = new vscode.WorkspaceEdit();
fileNameBase = LastSubFolder(selectedPath);
wsedit.createFile(vscode.Uri.file(selectedPath+"\\"+fileNameBase+".cpp"),{ignoreIfExists:false} );
wsedit.createFile(vscode.Uri.file(selectedPath+"\\"+fileNameBase+".h"),{ignoreIfExists:false} );
vscode.workspace.applyEdit(wsedit);
}else{
vscode.window.showInformationMessage("selectedPath n'a pas été défini");
return; //TODO: Voir pour enregistrer un defaut à un endroit approprié.
}
//Write in source and header file
let sourceFile;
let textToSource = "#include <"+fileNameBase+".h>"; // The text to send in sourceFile.cpp
let fileToOpen = selectedPath+"\\"+fileNameBase+".cpp";
function LastSubFolder(words:String) {
var n = words.split("\\");
return n[n.length - 1];
}
if (!fs.existsSync(fileToOpen)) {
vscode.window.showInformationMessage(`le fichier n'existe pas`);
}
function writeInFile(file:String,text:string){
vscode.workspace.openTextDocument(fileToOpen).then((sourceFile: vscode.TextDocument) => {
vscode.window.showTextDocument(sourceFile ,1,false).then(e => {
e.edit(edit =>{
edit.insert(new vscode.Position(0,0),textToSource);
});
});
});
}
The files have been created, but the text hasn't been inserted. It's because the file has not yet been created when the send text command is executed. How can i resolve this please?
To resolved this, i used the callback of vscode.workspace.onDidCreateFiles
:
const wsedit = new vscode.WorkspaceEdit();
wsedit.createFile(vscode.Uri.file(selectedPath+"\\"+fileNameBase+".cpp"),{ignoreIfExists:false} );
wsedit.createFile(vscode.Uri.file(selectedPath+"\\"+fileNameBase+".h"),{ignoreIfExists:false} );
vscode.workspace.applyEdit(wsedit);
vscode.workspace.onDidCreateFiles(()=> {
vscode.window.showInformationMessage("Les fichiers sont en cours de création");
headerText(fileNameBase);
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.