Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
daviwil opened this issue Oct 15, 2016 · 26 comments
Assignees
Labels
api feature-request Request for new features or functionality on-testplan release-notes Release notes issues

Comments

@daviwil
Copy link
Contributor

daviwil commented Oct 15, 2016

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!

@RainingChain
Copy link

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...

@emackey
Copy link

emackey commented Apr 12, 2017

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.

@jrieken
Copy link
Member

jrieken commented Aug 15, 2017

@chrisdias Please provide some details about your use-case/scenario

@chrisdias
Copy link
Member

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

@daviwil
Copy link
Contributor Author

daviwil commented Aug 17, 2017

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

@jrieken
Copy link
Member

jrieken commented Aug 17, 2017

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.

@chrisdias
Copy link
Member

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?

@jrieken
Copy link
Member

jrieken commented Aug 18, 2017

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...

@chrisdias
Copy link
Member

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?

@jrieken
Copy link
Member

jrieken commented Aug 21, 2017

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.

jrieken added a commit that referenced this issue Aug 21, 2017
jrieken added a commit that referenced this issue Aug 22, 2017
@jrieken
Copy link
Member

jrieken commented Aug 22, 2017

This is the current proposed API:

	export interface OpenDialogOptions {
		uri?: Uri;
		openFiles?: boolean;
		openFolders?: boolean;
		openMany?: boolean;
	}

	export namespace window {

		export function showOpenDialog(options: OpenDialogOptions): Thenable<Uri[]>;
	}

jrieken added a commit that referenced this issue Aug 22, 2017
@jrieken
Copy link
Member

jrieken commented Aug 28, 2017

API is proposed, awaiting feedback, making it final in September

@jrieken jrieken modified the milestones: September 2017, August 2017 Aug 28, 2017
@stef-levesque
Copy link

Any way we could have the same for 'showSaveDialog' ?

Some of my extensions need a way to save/export into new files. I'm currently using showInputBox to get a file path, but a Save dialog would be way better :)

@jrieken
Copy link
Member

jrieken commented Sep 6, 2017

Any way we could have the same for 'showSaveDialog' ?

Yeah, I like. Will be similar to the open-api. I'll let know when I have something

@daviwil
Copy link
Contributor Author

daviwil commented Sep 6, 2017

Sorry for the delay, new API looks good to me! Looking forward to trying it out.

@thelink2012
Copy link

thelink2012 commented Sep 7, 2017 via email

jrieken added a commit that referenced this issue Sep 7, 2017
@jrieken
Copy link
Member

jrieken commented Sep 7, 2017

I have pushed a change that also adds a save dialog

	export interface OpenDialogOptions {
		defaultResource?: Uri;
		openLabel?: string;
		openFiles?: boolean;
		openFolders?: boolean;
		openMany?: boolean;
	}

	export interface SaveDialogOptions {
		defaultResource?: Uri;
		saveLabel?: string;
	}

	export namespace window {
		export function showOpenDialog(options: OpenDialogOptions): Thenable<Uri[]>;
		export function showSaveDialog(options: SaveDialogOptions): Thenable<Uri>;
	}

@emackey
Copy link

emackey commented Sep 7, 2017

Is it easy to add a filter list for these? Some File (*.sf)|*.sf|All Files (*.*)|*.*

@daviwil
Copy link
Contributor Author

daviwil commented Sep 7, 2017

@jrieken did you intend to use SaveDialogOptions on the showSaveDialog method? Thanks for adding that one also, this is going to be really helpful!

@jrieken
Copy link
Member

jrieken commented Sep 7, 2017

did you intend to use SaveDialogOptions on the showSaveDialog method?

Doh, I am a copy&paste programmer....

Is it easy to add a filter list for these? Some File (.sf)|.sf|All Files (.)|.

Yeah, it's not there yet but the underlying electron API has it. Unsure yet how it will look like, may some sort of object like { [title:string]:string[]} which makes filters: { 'Images': [*.png, *.gif], 'Text': [*.txt]}

jrieken added a commit that referenced this issue Sep 8, 2017
jrieken added a commit that referenced this issue Sep 21, 2017
jrieken added a commit that referenced this issue Sep 21, 2017
jrieken added a commit that referenced this issue Sep 21, 2017
@jrieken jrieken closed this as completed Sep 22, 2017
@stef-levesque
Copy link

@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)

@jrieken
Copy link
Member

jrieken commented Sep 22, 2017

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

@stef-levesque
Copy link

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

@stef-levesque
Copy link

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

@jrieken
Copy link
Member

jrieken commented Sep 22, 2017

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...

@stef-levesque
Copy link

Thanks, sry for the false-alarm then :)

@jrieken jrieken added the release-notes Release notes issues label Sep 29, 2017
@vscodebot vscodebot bot locked and limited conversation to collaborators Nov 17, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api feature-request Request for new features or functionality on-testplan release-notes Release notes issues
Projects
None yet
Development

No branches or pull requests

7 participants