Using VSCode 1.61.2, I am making an extension that must open an empty, unsaved file in the editor.
The first part, without filling the document, is done like bellow, and works well:
vscode.workspace.openTextDocument({
language: "json",
content: "toto"
})
However, I would like the content to be filled with the content of a file located in the extension code source. The file is located at:
src/my-file.json
So far I have:
vscode.workspace.openTextDocument("src/my-file.json").then((file) => {
vscode.workspace.openTextDocument({
language: "json",
content: file.getText()
})
})
However, this throws:
rejected promise not handled within 1 second: Error: cannot open file:///src/my-file.json. Detail: Unable to read file '\src\my-file.json' (Error: Unable to resolve nonexistent file '\src\my-file.json')
I highly suspect the workspace
refers to the user workspace, thus the file does not exist, which explain the error.
However, if I am right, I did not find anything in the api that allows to read extension files.
How can I read my extension source files and write it into an empty unsaved file in the editor ?
CodePudding user response:
Create the path in the extension
let srcfolder = path.join(__dirname, 'src', 'my-file.json');
Or use the ExtensionContext that is the argument of the activate
function, use the member extensionUri
this is also web extension save, use vscode.workspace.fs
to read the file