Skip to content

API Request: Function for showing an "Open File" dialog that returns the selected path #13807

Closed

Description

@daviwil

Is it possible to add an API function which allows an extension to show an "Open File" dialog which doesn't cause VS Code to open the selected file but instead returns the file path? I'd like to allow the user to easily pick the location of a PowerShell executable to use for their editing session.

I've looked around for a while to find a way to do this without the need for a new API but couldn't find a solution. If you know of a better method, I'd be more than happy to use that :)

Thanks!

Activity

  1. added
    feature-request
    on Oct 17, 2016
  2. RainingChain commented on Mar 5, 2017

    @RainingChain

    So is there any way to do that right now?

    Only way I found is to tell the user to open the file and place it in the second column. Then I can get the file with this code:

     let col2Editor = vscode.window.visibleTextEditors.find(editor => editor.viewColumn === vscode.ViewColumn.Two);
     let filename = col2Editor && col2Editor.document.fileName;
    

    I'm sure there's a cleaner way to do this...

  3. emackey commented on Apr 12, 2017

    @emackey

    I'd like this feature as well. I'm writing an extension that wants to allow the user to select a binary file on disk (one that wouldn't normally open in vscode) and encode it into a data-uri string, and embed it into the current text document. So, I need a "file open" dialog that returns the full path & filename.

  4. jrieken commented on Aug 15, 2017

    @jrieken
    Contributor

    Chris Dias (@chrisdias) Please provide some details about your use-case/scenario

  5. chrisdias commented on Aug 17, 2017

    @chrisdias
    Contributor

    In a nutshell, I want to be able to write an extension that will scaffold out a "new project". I want to prompt the user for a new folder, I'll then scaffold code into that folder, and then I will (re) open Code on that folder, or add as a new root folder.

    So, I need:

    • An API that let's me prompt for a new folder

    • An API for opening or reopening VS Code on a folder. To accomplish this today, I have to invoke the vscode.openFolder command, see the awesome open new instance extension:

    // The explorer/context menu contribution receives the URI to the file/folder
        let cmd1 = vscode.commands.registerCommand('extension.openNewInstance', (e: vscode.Uri) => {
            vscode.commands.executeCommand("vscode.openFolder", e, true);
        });
    • An API for adding a new root level work space folder
  6. daviwil commented on Aug 17, 2017

    @daviwil
    ContributorAuthor

    Incidentally the PowerShell extension also needs this API for project templating UI, so it'd be awesome to have this :)

  7. jrieken commented on Aug 17, 2017

    @jrieken
    Contributor

    Ok, you want a lot more then a file picker dialog. Step by step...

    An API that let's me prompt for a new folder

    I assume that is outside the current workspace folders and you'd expect the native OS file picker, right? It's unclear to me if picking a file inside the workspace should use our Cmd+P picker... In general there are a few options to this but nothing dramatic.

    An API for opening or reopening VS Code on a folder. To accomplish this today, I have to invoke the vscode.openFolder command, see the awesome open new instance extension:

    That is a lot more and I am not sure the implication are clear to everyone here. When we open a new or different folder a different extension host comes to life and your current extension host might be shut down (when replacing the current window). In any case you won't be able to talk to that "other" extension host.

    An API for adding a new root level work space folder

    Similar to above. For API-stability reasons we restart the extension host when going from 0-1, 1-N, (1-0, N-1) workspace folders. That also means that your extension is getting restarted and that you need to keep your state somewhere else.

  8. chrisdias commented on Aug 17, 2017

    @chrisdias
    Contributor

    I assume that is outside the current workspace folder

    Yes, that would be my general expectation where the user would pick. However, it seems reasonable that they could pick a folder inside the workspace. Whether or not I reload the workbench seems to be something that is then up to the extension author.

    you'd expect the native OS file picker

    Yes, very much. I want the easy ability to switch drives, add a new folder, etc. all provided by the OS pickers today.

    your current extension host might be shut down

    In my scenario I'm perfectly fine with this.

    I can live with using the vscode.openFolder command for now, but having a real API for this feels cleaner. For adding a new root level workspace folder i'm assuming there will be a similar vscode.addFolderToWorkspace command I could leverage?

  9. jrieken commented on Aug 18, 2017

    @jrieken
    Contributor

    Yes, very much. I want the easy ability to switch drives, add a new folder, etc. all provided by the OS pickers today.

    Check, it's pretty much exposing what electron offers to us

    I can live with using the vscode.openFolder command for now, but having a real API for this feels cleaner.

    Yes and no. To some extend command identifiers are API because folks reference them from the keybindings.json-file and therefore shouldn't change. When exposing this via blessed API I am afraid that people will be confused about the lifecycle... It's rare that you call an method (other than exit) to shutdown yourself.

    For adding a new root level workspace folder i'm assuming there will be a similar vscode.addFolderToWorkspace command I could leverage?

    Unsure, it has been discussed before. But it's the same concern, having an API that restarts everything feels wrong...

  10. chrisdias commented on Aug 18, 2017

    @chrisdias
    Contributor

    Sorry, maybe I wasn't clear. I can live with using this to reload VS Code:

            vscode.commands.executeCommand("vscode.openFolder", e, true);

    So I guess the last question is, how will an extension add or remove a root level workspace?

  11. jrieken commented on Aug 21, 2017

    @jrieken
    Contributor

    So I guess the last question is, how will an extension add or remove a root level workspace?

    Unsure, it has been discussed before. It's the same problem, when going from 0-1, 1-N a restart is required.

  12. 15 remaining items

  13. stef-levesque commented on Sep 22, 2017

    @stef-levesque

    Johannes Rieken (@jrieken) thanks for the feature 👍

    Although, maybe i'm doing something wrong, but I can't get a SaveDialog with anything other dans 'Untitled' in the Save As input box (on Mac). Even with a valid defaultUri.

    const filepath = getPhysicalPath(d.uri);
    const defaultUri = vscode.Uri.file(filepath);
    const option: vscode.SaveDialogOptions = { defaultUri, filters: {} };
    
    vscode.window.showSaveDialog( option ).then(fileUri => {
        console.log(fileUri ? fileUri.toString() : 'undefined');
    });

    I'm on 1.17.0-insider (f7962f0)

  14. jrieken commented on Sep 22, 2017

    @jrieken
    Contributor

    Hm, it works for me on master. You mean undefined is always the result instead of a meaning full fileUri?

  15. stef-levesque commented on Sep 22, 2017

    @stef-levesque

    No, I mean I can't pre-fill the SaveDialog with a default file.

  16. stef-levesque commented on Sep 22, 2017

    @stef-levesque

    Hum, but it works if I name it defaultResource. The Insider might be more behind that I though of...

  17. jrieken commented on Sep 22, 2017

    @jrieken
    Contributor

    Yeah, Insiders is behind, the code-sign infrastructure gets an overhaul. In the stable API we have named this thing defaultUri instead of defaultResource but that change was yesterday...

  18. stef-levesque commented on Sep 22, 2017

    @stef-levesque

    Thanks, sry for the false-alarm then :)

  19. locked and limited conversation to collaborators on Nov 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

apifeature-requeston-testplanrelease-notes

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions