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

Get selected file/folder in the explorer view #3553

Open
SamVerschueren opened this issue Feb 29, 2016 · 49 comments
Open

Get selected file/folder in the explorer view #3553

SamVerschueren opened this issue Feb 29, 2016 · 49 comments
Assignees
Labels
api feature-request Request for new features or functionality file-explorer Explorer widget issues
Milestone

Comments

@SamVerschueren
Copy link
Contributor

It would be nice if it was possible to retrieve the selected file/folder in the explorer view. The use case where I want to use it for is my vscode-yo extension to be able to run the generator in the selected directory. An API for this would be nice.

@bpasero
Copy link
Member

bpasero commented Mar 1, 2016

@jrieken fyi

@isidorn isidorn added the feature-request Request for new features or functionality label Mar 1, 2016
@stef-levesque
Copy link

This would be helpful for my vscode-hexdump extension, to retrieve selected file not compatible with TextDocument (i.e. png, jpg, ...). I wouldn't need to show an inputbox to ask for the file path everything :)

@riteshatsencha
Copy link

Second that. It will help us for the extension we are developing @sencha. Here is the link to Stack Overflow question on the same feature.

@fantacytyx
Copy link

Some extension need retrieve selected file/folder, such as TortoiseSVN.
If can get selected file/folder, even if not integrate SVN, also can be very convenient use SVN by contextmenu of explorer.

@jrieken
Copy link
Member

jrieken commented Sep 27, 2016

Maybe the menus contribution we have added this summer helps anyone here. The contract is that when invoked from a resource (such as an explorer entry) its URI will be passed to the function

When a command is invoked from a (context) menu, VS Code tries to infer the currently selected resource and passes that as a parameter when invoking the command. For instance, a menu item inside the Explorer is passed the URI of the selected resource and a menu item inside an editor is passed the URI of the document.

@fbricon
Copy link
Contributor

fbricon commented Nov 29, 2017

@jrieken executing a command from the menu, I get the uri as expected, except it doesn't work when that command is invoked from a KB shortcut (I get an empty parameter instead).

@jrieken
Copy link
Member

jrieken commented Nov 30, 2017

except it doesn't work when that command is invoked from a KB shortcut

Yeah, thats the design of things as KB shortcuts are global (not contextual) by definition... It's like someone using you command-id with the executeCommand-api. The rule to always be prepared to be invoked without sufficient arguments. In those cases you need to figure them out by yourself. In this case by using an API that you don't have (yet)

@dalvikroid
Copy link

with some api or not?

@jrieken jrieken added the file-explorer Explorer widget issues label Sep 19, 2018
@alianZhen
Copy link

I find out a way to get the path of selected folder. You can contribute a menu of explorer,then run the vscode.commands.executeCommand('copyFilePath');. You can get the value into your clipboard

@akosyakov
Copy link
Contributor

akosyakov commented Mar 1, 2019

In this case by using an API that you don't have (yet)

@jrieken Do you have an idea for API? Something like window.activeResource, that returns a current selected resource of an active part?

@jrieken
Copy link
Member

jrieken commented Mar 1, 2019

no

@matheus-sales
Copy link

matheus-sales commented May 3, 2019

I too need this function for a plugin. Has forecast for implementation?

In Theia was implemented.

@HenrikHolmIT
Copy link

I need this feature now. Has it ever been implemented or is there another way to get the selected items in the explorer view?

I need to search for specific files but only in the currently selected folder.

@isidorn isidorn self-assigned this Oct 8, 2019
@tjx666
Copy link
Contributor

tjx666 commented Oct 10, 2022

Any progress on this api? I think this is a very basic api.

@R35007
Copy link

R35007 commented Dec 2, 2022

Yes I need that as well in part of my vscode. I need to get the list of files or folders selected by the user. Is there any update on this ticket ?

@eugeneniemand
Copy link

I found this solution. It wasn't very clear from docs that register command passes a URI which can then be used for getting the path of the selected file when activating your command from the context menu in the explorer

export function activate(context: vscode.ExtensionContext) {
	console.log('Congratulations, your extension "My-lite-switcher" is now active!');
	let disposable = vscode.commands.registerCommand('My-lite-switcher.activate', (uri:vscode.Uri) => {
		let f = uri.fsPath;
		vscode.window.showInformationMessage(`My Lite Switcher: ${f}`);
	});
	context.subscriptions.push(disposable);
}

@lramos15
Copy link
Member

Does the new tabs API help anyone here? That's not limited by the text document and can get you the open file which is often the same as the selected file. I'm trying to understand the use cases in this thread and it seems like at least most would be solved with the tabs API.

@r3m0t
Copy link
Contributor

r3m0t commented Feb 23, 2023

I think they mean the file or folder which was clicked in the explorer view, or selected with the keyboard, not the open editor.

@dian-yu-luo
Copy link

dian-yu-luo commented Aug 9, 2023

could you convert it to js

@Draugor
Copy link

Draugor commented Aug 17, 2023

A workaround that should work for most use cases (@ArturoDent version while preserving the clipboard state):

this solution from @JeremyFunk is the closest i found to work, but there is still some situations where it doesn't work

  • right click, select menu item that runs command
    (everything here after was not run via right-click menu)
  • select a file and open it, run command
  • select multiple files, run command
    -> returns all
  • select folder, run command
  • have (one or) multiple files selected, but the currently opened file is not from this workspace (e.g. user/settings.json), run command
    -> returns the file path of the opened file (e.g. settings.json)
  • select folder, click in editor window with a pre-opened file from the workspace (folder is still selected and not the file)
    -> returns the path of the file, not the selected folder
  • select multiple files including the one currently opened, click in editor window with the opened file, run command
    -> returns only the path of the file in the editor window, not the other selected ones

@Simon-He95
Copy link

Indeed, this API is very basic and should be exposed. I hope vscode can support it.

@msweeney1999
Copy link

I need this too! Any updates please?

@gravity-api
Copy link

This can be very easily achieved - please follow my post on StackOverflow
https://stackoverflow.com/a/77659797/13547712

@Simon-He95
Copy link

This can be very easily achieved - please follow my post on StackOverflow https://stackoverflow.com/a/77659797/13547712

Please take a closer look at our question, we are talking about the file or folder which was clicked in the explorer view, or selected with the keyboard, not the open editor.

@msweeney1999
Copy link

msweeney1999 commented Dec 19, 2023

I agree with Simon-He95. Getting the URI for an Explorer Context menu right mouse click is already present in vscode. What is needed is a URI notification for a left mouse click event on a folder.

Something like this proposed API for a Treeview would be fantastic:

https://github.com/microsoft/vscode/blob/main/src/vscode-dts/vscode.proposed.treeViewActiveItem.d.ts#L24

The onDidChangeActiveItem event would specify the URI of the active item in Explorer.

In fact, vscode's own git implementation could use this feature. If I have a multi-rooted workspace, with subfolders containing repos, vscode git will always use the topmost folder as the git repo. It should use the repo of the currently selected folder in Explorer.

@frudman
Copy link

frudman commented Feb 6, 2024

adding my 2 cents: such an event emitter would be great to implement the most basic "Show README" for selected folder, a very useful feature when browsing large repos. This is exactly what GITHUB does whenever a folder is selected, the relevant README file is displayed. Would Love Love to get this same feature for vscode. I have the code ready for this (currently implemented as right-click context menu, but that's really a pita).

@ChiriVulpes
Copy link

I would love to be able to add keybindings for these commands but can't because the extension can't currently support getting the current file list from the explorer. :<

@mirrortom
Copy link

Now, is there such an API? I need to use it too.
var selectedWorkspaceFolder=;
var fileOrDirs = selectedWorkspaceFolder.getSelectedItems();

@tgrue-openai
Copy link

tgrue-openai commented Sep 10, 2024

Sadly the workaround where you copy clipboard, load path to clipboard, retrieve it, and restore state has two unacceptable issues for me.

  1. Unsurprisingly, it's racey with the user. If they copy to the clipboard mid execution you can end up overwriting what they put in the clipboard. And, afaict, there is no solution to this (since there is no compare_and_swap like clipboard functionality).
  2. The readText and writeText calls to the clipboard, at least on mac, lose rich state. This breaks some people's workflows and is particularly noticeable.

Thus, add me to the bucket of people who are trying to show a README.md for a folder in my extension panel, with no way of being able to tell when a folder is selected by a left click.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api feature-request Request for new features or functionality file-explorer Explorer widget issues
Projects
None yet
Development

Successfully merging a pull request may close this issue.