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

How do I make a command that only appears in the command pallet when my extension is loaded? #10401

Closed
DanTup opened this issue Aug 10, 2016 · 18 comments
Assignees
Labels
api feature-request Request for new features or functionality

Comments

@DanTup
Copy link
Contributor

DanTup commented Aug 10, 2016

I'd like to some commands that only appear when a Dart project is open (eg. my extension is activated). If I add a command to packages.json it seems to always be available in the command pallet. (These commands are equiv of nuget restore etc., you wouldn't want them visible when you open a Perl project, for example).

I'm sure I'm missing something; but I just can't figure out how to control this :(

@jrieken jrieken added feature-request Request for new features or functionality api labels Aug 11, 2016
@jrieken
Copy link
Member

jrieken commented Aug 11, 2016

You can actually not... Today all commands are known upfront to decouple UI for a command from the actual command implementation. We do that to prevent eager loading of the world. I do understand tho that you want some additions after the fact...

@DanTup
Copy link
Contributor Author

DanTup commented Aug 11, 2016

PoSh "Expand Alias" is listed in there and just fails with "command not found" when the extension isn't loaded. Does this seem like a reasonable behaviour to copy for now (eg. don't add it to activateOn)?

We do that to prevent eager loading of the world

Registering a command in activate and disposing in deactivate (or pushing to subscriptions) would allow what we want without introducing any issues like that? (Actually, we already register in activate... so maybe just hiding a command that has no handlers registered is a clean fix?)

@jrieken
Copy link
Member

jrieken commented Aug 11, 2016

Well, registerCommand is about assigning an identifier to a function. The commands and also the menuitems-part is to assign UI to that identifier. So, when not having a command activation event nothing bad will happen but it's also not necessarily the recommended way - that why we have the onCommand:xyz activation event.

This is a fair feature request, at least the F1 commands list should be dynamic in some way

@eamodio
Copy link
Contributor

eamodio commented Feb 14, 2017

Without any changes here -- is there anyway today to have an extension get rid of command defined in package.json? I've gotten a request in GitLens to "disable" when there is no git repository. I currently detect that case and don't initialize, but all the commands still show up in the command palette as well as in all the context menus. The only way I know that I could get the commands removed from the context menus, is to have some hidden config setting that gets enabled/disabled in this case, and have all the menus conditional on it. But that doesn't help with the command palette.

Is there another (better) way to get this to work for the menus and/or the command palette?

@jrieken
Copy link
Member

jrieken commented Feb 15, 2017

With #19345 (comment) we added the ability to show a command only in some cases

@eamodio
Copy link
Contributor

eamodio commented Feb 15, 2017

@jrieken that sounds great -- but is there another way to use when that isn't something built-in or configuration setting? For example just a flag or something on the extension itself? I'd rather not have to set a "transient" configuration setting to signal commands on and off

@jrieken
Copy link
Member

jrieken commented Feb 15, 2017

Yeah, we have an half-official command that lets you set custom context keys but it needs some doc. I'll make we have proper docs when this happens

@eamodio
Copy link
Contributor

eamodio commented Feb 15, 2017

Great! FYI -- I'd be happy with only half-official docs too ;)

@jrieken
Copy link
Member

jrieken commented Feb 15, 2017

:-) there is the setContext-command. So from an extension you should be able to do this: vscode.command.executeCommand('setContex', 'myExtKey', true)

@eamodio
Copy link
Contributor

eamodio commented Feb 16, 2017

@jrieken that changes everything! ;) Thank you!

@jrieken
Copy link
Member

jrieken commented Feb 16, 2017

Glad you like it. We have a request to APIify the context keys et al but this will work for now

@DanTup
Copy link
Contributor Author

DanTup commented Feb 17, 2017

@jrieken Is there any example of this? I'm not sure how to make it work? I can set a context at startup but can I add when to the commands in packages.json? Should this work in insiders? Thanks!

@eamodio
Copy link
Contributor

eamodio commented Feb 17, 2017

@DanTup You can see an example of this here and here. Seems to work in both stable and insiders. I have also added the new commandPalette support in insiders here.

Hope that helps -- if not let me know.

Also make sure you are using setContext there is a typo in @jrieken example. It should be
vscode.commands.executeCommand('setContext', 'myExtKey', true)

@DanTup
Copy link
Contributor Author

DanTup commented Feb 17, 2017

Yeah I spotted the typo (also s missing from commands!) but it didn't work - looking at your code it seems I was putting it in the wrong place (in command definitions). I'll give it another go when next working on this - thanks! 👍

ryanluker added a commit to ryanluker/vscode-coverage-gutters that referenced this issue Mar 14, 2017
* limit to 5 keywords

* cleanup gutters config

* fix icon

* update colours, activate on ide open and menu adds

* use setContext undocumented command for custom UI
microsoft/vscode#19345 (comment)
microsoft/vscode#10401

* set default true for removeCoverage context menu

* fix extension.test

* add settings grid

* add proper test for extension activate on open

* increment version
@DanTup
Copy link
Contributor Author

DanTup commented May 13, 2017

@eamodio @jrieken Sorry to be a pain, but I still can't make this work :( Here's the changes I've made - I've added when's to my packages.json and some setContext calls, but the commands always still appear. Eg. I hit F5 to load the extension host and do not have a Dart project open but the commands are still listed in the command palette.

Changes

Testing

@DanTup
Copy link
Contributor Author

DanTup commented May 13, 2017

ok, so I should've re-read my previous comment - I did the same thing (put in command definitions). However, even if I add them to the menus parts they won't work.

Unfortunately the samples linked above don't match up now because they weren't linked to specific versions and the code has all moved around. Any pointers appreciated!

Still stuck!

@DanTup
Copy link
Contributor Author

DanTup commented May 13, 2017

Ok, sorted this out - I was missing the commandPalette section! :-)

@jrieken
Copy link
Member

jrieken commented May 15, 2017

Yeah, closing this issue because we have the commandPalette section and context keys. A sample is here: https://code.visualstudio.com/updates/v1_10#_context-specific-visibility-of-command-palette-menu-items

@jrieken jrieken closed this as completed May 15, 2017
@jrieken jrieken self-assigned this May 15, 2017
@vscodebot vscodebot bot locked and limited conversation to collaborators Nov 18, 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
Projects
None yet
Development

No branches or pull requests

3 participants