Lift `setContext` from a command to proper API
This would be very useful to activate extension build feature per project type. would help for this issue
Like so ..., command: "xyz", when: "workspaceConatins:foo.txt"?
@farfromrefug What exactly do you want to achieve with that?
Yes exactly like that.
That's actually the only way to trigger a special build defined in an extension (see the linked issue).
An extension cannot define a new task builder. So i you want to trigger, let's say, a cordova build on cmd+b but the default task in non cordova projects, that would be the only way
@farfromrefug Your extension could activate on workspaceConatins:foo.txt and then invoke the command setContext:
vscode.commands.executeCommand('setContext', 'inCordovaProject', true);
and then you can use "when": "inCordovaProject".
@jrieken Now that there are more use-cases than vim, should we consider lifting setContext to a proper API method?
I like. Maybe something similar to how we have it internally, like
interface ContextKey<V> {
key: string;
value: V;
}
function createContextKey<V>('name'):ContextKey<V>;
I'll let it cook for a little
@alexandrudima i love that. Does it already works (i don't see this in the cordova plugin)? Do i have to "unset it"?
@farfromrefug You can create an issue in the cordova extension repo and introduce them to the problem and provide them with the recommendation.
@alexandrudima it s not an issue. I was just wondering if that API was already existing. And i was talking of cordova as an example. Thahnks will try it
vscode.commands.executeCommand('setContext', 'inCordovaProject', true); works today. It is a "sort-of-hidden" API that we introduced for vim extensions in particular. We did not want to make it a top level API concept until we found more use-cases, so having found more use-cases, we are now considering making it a top level API concept, as @jrieken suggests, perhaps via a createContextKey method. We will keep this issue around to track this work.
@alexandrudima i can confirm that it works! BTW I have a lot of questions concerning extension dev. My extension is doing some wrong things in VSCode, where can i ask questions? Thanks
Usually we direct questions to Stack Overflow. You can also open issues if you feel you're running into something that looks like a bug, but each issue should be self-contained. We also have some issues that come in which are in fact questions.
TL;DR Stack Overflow preferably, otherwise issues
@alexandrudima i wont create issues though it might be bug related. I am not sure what's the cause yet. Thanks
@alexandrudima from my experience in handling keybindings in Vim, the need for lifting it to API is because we only support limited context key type https://github.com/Microsoft/vscode/blob/c67ef57cda90b5f28499646f7cc94e8dcc5b0586/src/vs/platform/contextkey/common/contextkey.ts which makes keybindings contribution doesn't always meet our need.
For example, we have quite a few Vim commands implemented in our ext and ppl always come and say, hey we want to use these Vim commands and for others, we prefer Code's. In this case, a single context key to enable/disable all Vim keybinds is not enough.
Is there any possibility that we support objects instead of just booleans for context keys then we can write below code to implement whatever we want
vscode.commands.executeCommand('setContext', 'vim.keys', {esc: true, ctrld:false})
{
"key": "Escape",
"command": "extension.vim_escape",
"when": "editorTextFocus && !inDebugRepl && vim.keys.esc"
}
@rebornix You raise a good point, but IMHO this should not be tackled with options that then pollute the keybindings. All these users can today, without any code change in vscode or in the vim extension, customize their keybindings 100%. They just need to go to their keybindings.json and write rules and IMHO that should be our de-facto answer. Introducing an option that disables a keybinding is IMHO a clutch offered to end-users that don't want to open their keybindings.json file. Also, the same users will come back in one month time and ask for a new option and so on, when we could try to educate them and let them run wild :).
For your case, the end user would need to write the following in their keybindings.json file:
{ "command": "-extension.vim_escape" }
Maybe not as easy as an option, but then that is a separate bug, that our keybindings UI is non existant.
But I agree, there is a real problem here. I install an extension and all of a sudden I need to add all these rules with { "command": "-extension.vim_escape" } to remove those keybindings. Not nice. IMHO we should tackle that by adding the possibility to disable a certain contribution point from a certain extension (think of a more fine-grained "disable extension", where instead of disabling the entire extension, you'd just disable a contribution point of an extension).
@alexandrudima I love your proposal here. A contribution point level enable/disable makes it possible to choose a specific command for a particular key combination. A concrete example is when you install Resharper on Visual Studio, you get a chance to decide which command you want to use for any keycap you press. We can make it happen in Code by enabling a contribution for one extension and disabling all others for a single key combination :)
Is this documented anywhere yet?
only in this issue
Moving this issue to the next milestone. It's not clear to me now we bring this to the API given that there are actually different contexts active and that extensions might want to utilise that
Promoting this to be part of the extension API would make it easier to encurage developers to make more well behaved extensions, that only adds entries to the menues (especially the command palette), key bindings and maybe status bar in the right context.
given that there are actually different contexts active and that extensions might want to utilise that
How about something like this?
interface ContextChangeEvent {
name: string;
active: boolean;
// Is it also possible to provide a source?
}
setContextState(name: string, active: boolean): void;
getContextState(name: string): boolean;
getActiveContexts(): string[];
onDidChangeContext: Event<ContextChangeEvent>
Is there already a generic way to subscribe to context changes?
I'm also lending my support to this issue as I have a number of use cases in mind.
Sent from my Motorola XT1635-02 using FastHub
It would probably also be useful to allow string in addition to boolean (like the context on TreeItems).