Parag Diwan
Parag Diwan

Reputation: 3956

VSCODE extension - How to show file save dialog

Is it possible in existing API, to show a dialog to user to save a file at custom location ?

Upvotes: 2

Views: 2362

Answers (3)

Mark
Mark

Reputation: 182711

There is a proposed extension api (saveEditor) implementing save and saveAs so this simple code works:

const activeUri = vscode.window.activeTextEditor.document.uri;

// const res = await vscode.workspace.save(activeUri);   // no dialog

// const res = await vscode.workspace.saveAs(activeUri);    // dialog to enter filename plus overwrite warning 

// opens a dialog to enter filename plus overwrite warning if necessary
const res = await vscode.workspace.saveAs(activeUri)
  .then(result => {
    console.log(result);  // result is the Uri if successful
}); 

This can be tested now in Insiders. Here is the resolved issue: Have workspace.save and workspace.saveAs methods that return the URI and the api:

/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

// https://github.com/microsoft/vscode/issues/178713

declare module 'vscode' {

    export namespace workspace {

        /**
         * Saves the editor identified by the given resource and returns the resulting resource or `undefined`
         * if save was not successful or no editor with the given resource was found.
         *
         * **Note** that an editor with the provided resource must be opened in order to be saved.
         *
         * @param uri the associated uri for the opened editor to save.
         * @return A thenable that resolves when the save operation has finished.
         */
        export function save(uri: Uri): Thenable<Uri | undefined>;

        /**
         * Saves the editor identified by the given resource to a new file name as provided by the user and
         * returns the resulting resource or `undefined` if save was not successful or cancelled or no editor
         * with the given resource was found.
         *
         * **Note** that an editor with the provided resource must be opened in order to be saved as.
         *
         * @param uri the associated uri for the opened editor to save as.
         * @return A thenable that resolves when the save-as operation has finished.
         */
        export function saveAs(uri: Uri): Thenable<Uri | undefined>;
    }
}

Upvotes: 2

TOPKAT
TOPKAT

Reputation: 8688

if you want custom path and data saving, I think the best you can do is:

vscode.window.showSaveDialog({...options}).then(fileInfos => {
    // here you can use fs to handle data saving
    fs.writeFileSync(fileInfos.path, yourData)
});

This will prompt the user to enter a saving location and store the user choice in the fileInfos response.

Here is the doc: https://code.visualstudio.com/api/references/vscode-api#SaveDialogOptions

Upvotes: 3

Matt Bierner
Matt Bierner

Reputation: 65593

Yes, your extension can use the workbench.action.files.saveAs command to bring up the save as dialog for the current file.

Upvotes: 0

Related Questions