How do I hide a command in the palette menu from my extension in VS Code - visual-studio-code

I am building a VS Code extension starting from this page. Now I want to hide in the palette menu the command extension.timerStart after I run it. I have read this page, didn't helped. I have the code bellow for package.json. How do I make the varFromMyExtension===false part work?
"contributes": {
"commands": [
{
"command": "extension.timerStart",
"title": "Timer Start"
}
],
"menus": {
"commandPalette": [
{
"command": "extension.timerStart",
"when": "varFromMyExtension===false"
}
]
}

I think it is not possible to access variables from your extension directly in a when clause. However you can access any configuration of the settings.json.
From the docs (at the bottom of the chapter):
Note: You can use any user or workspace setting that evaluates to a boolean here with the prefix "config.".
So when your extension contributes a boolean configuration called varFromMyExtension you should be able to use it in the when clause. This configuration then can be manipulated programmatically, too.
So your package.json would probably contain something like this (not tested):
"contributes": {
"commands": [
{
"command": "extension.timerStart",
"title": "Timer Start"
}
],
"menus": {
"commandPalette": [
{
"command": "extension.timerStart",
"when": "!config.myextension.varFromMyExtension"
}
]
},
"configuration": {
"type": "object",
"title": "Indicates whether ...",
"properties": {
"myextension.varFromMyExtension": {
"title": "My title.",
"description": "My description",
"type": "boolean",
"default": false,
"pattern": "(true|false)"
}
}
}
}
But bare in mind that the user can see and edit this setting, too.

Related

How to order inline view toolbar icons?

In my authoring of a VS Code extension, I'd like to have "+" icon shown to the right of refresh icon. However, the ordering in the package.json does not seem to represent the order rendered. I always get the add icon to the left of refresh icon.
Here is a snippet of the view definition:
{
"commands": [
{
"command": "refresh-jobs",
"title": "Refresh",
"icon": {
"light": "resources/light/refresh.svg",
"dark": "resources/dark/refresh.svg"
}
},
{
"command": "add-job",
"title": "Add",
"icon": {
"light": "resources/light/add.svg",
"dark": "resources/dark/add.svg"
}
},
],
...
"view/item/context": [
{
"command": "refresh-jobs",
"group": "inline",
"when": "viewItem == jobgroup"
},
{
"command": "add-job",
"group": "inline",
"when": "viewItem == jobgroup"
},
]
}
Any help? Thanks!
I found this old issue: Add custom ordering to title menu items. which suggest doing something like:
There is way to define the order and I am surprised we haven't
properly documented that... What you can do is adding an order to the
group-attribute like so group: name#number. In your case
{
"command": "md-shortcut.toggleBold",
"when": "editorLangId == 'markdown'",
"group": "2_markdown_1#1"
}
I see it here: menu example The #n syntax is used there to order menu entries but the issue I cited above seems to imply that it will also order icons in a title bar. Try this:
"view/item/context": [
{
"command": "refresh-jobs",
"group": "inline#1",
"when": "viewItem == jobgroup"
},
{
"command": "add-job",
"group": "inline#2",
"when": "viewItem == jobgroup"
},
]
"group": "inline#1", note the #1
Let me know if it works for you.

VS Code ESLint Checking for quick fixes... not working

I'm not sure if it was a recent update, but for some reason I'm not getting the quick fixes for ESLint any longer. It will say "Checking for quick fixes...", but I will not display any. In the ESLint server I can see the fix available, but the command is missing in VS Code.
[Trace - 5:30:49 PM] Received response 'textDocument/codeAction - (13)' in 0ms.
Result: [
{
"title": "Disable no-unused-vars for this line",
"command": {
"title": "Disable no-unused-vars for this line",
"command": "eslint.applyDisableLine",
"arguments": [
{
"uri": "file:///d%3A/Development/sfdx/insider/force-app/main/default/lwc/apexImperativeMethod/apexImperativeMethod.js",
"version": 14,
"ruleId": "no-unused-vars"
}
]
},
"kind": "quickfix"
},
{
"title": "Disable no-unused-vars for the entire file",
"command": {
"title": "Disable no-unused-vars for the entire file",
"command": "eslint.applyDisableFile",
"arguments": [
{
"uri": "file:///",
"version": 14,
"ruleId": "no-unused-vars"
}
]
},
"kind": "quickfix"
},
{
"title": "Show documentation for no-unused-vars",
"command": {
"title": "Show documentation for no-unused-vars",
"command": "eslint.openRuleDoc",
"arguments": [
{
"uri": "file:///",
"version": 14,
"ruleId": "no-unused-vars"
}
]
},
"kind": "quickfix"
}
]
Same was happening to me as well and after trying multiple things what worked for me was a clean reinstall of Code which meant uninstalling Code, deleting the extension folder and and installing the code followed by extensions. And quick fix is back.

Use built-in Octicons in editor/title menu for vscode extension command contribution point

Is it possible to use Octitons that ship with vscode without embedding it into my extension?
"contributes": {
"commands": [
{
"command": "my.extraordinary.command",
"title": "My command",
"icon":{
"dark": "<what should go here?>",
"light": "<what should go here?>"
}
},
],
"menus": {
"editor/title": [
{
"command": "my.extraordinary.command",
"group": "navigation"
}
]
},

Unable to create menu with contributes.menu

I am unable to create a menu using the documentation at the link below for contributes.menu. Actually, I was able to create one menu item, but could not create a second one. The code excerpt is from my package.json on Windows 10 using VS Code 1.37.0.
So my question is, can someone show me an example of adding two menu items?
-TIA
"contributes": {
"menus": {
"editor/title": [{
"title": "Underline Text",
"when": "editorHasSelection",
"command": "macros.underline",
"alt": "markdown.showPreviewToSide",
"group": "MyGroup#1"
}]
},
"languages": [
https://code.visualstudio.com/api/references/contribution-points#contributes.menus
Already tried the above code.
Here is an example package.json, based on the one in the tutorial, that creates two menu items:
{
...
"activationEvents": [
"onCommand:extension.helloWorld",
"onCommand:extension.helloAnotherWorld"
],
"contributes": {
"commands": [
{
"command": "extension.helloWorld",
"title": "Hello World"
},
{
"command": "extension.helloAnotherWorld",
"title": "Hello Another World"
}
],
"menus": {
"editor/title/context": [
{
"command": "extension.helloWorld"
},
{
"command": "extension.helloAnotherWorld"
}
]
}
},
...
}

Can i pass arguments to command in contributes block?

I would like to reusing command with some arguments.
I found key binding can do this with "args" property:
{
"key": "cmd+k 8",
"command": "editor.action.insertSnippet",
"when": "resourceLangId == 'markdown'",
"args": {
"name": "Insert bold text"
}
}
so i try to write my package.json/contributes/menus in a similar way and it didnt work:
"commands": [
{
"command": "extension.sayHello",
"title": "Say Hello"
}
],
"menus": {
"editor/context": [
{
//menu one
"command": "extension.sayHello",
"group": "navigation#1",
"args": {
"text": "Say Hello 1!"
}
},
{
//menu two
"command": "extension.sayHello",
"group": "navigation#2",
"args": {
"text": "Say Hello 2!"
}
}
]
}
I would like to pass "text" to command handler function.
Are there any feature to archive similar result?
This is an old question, founded it on Github firstly, but i wanted to share some info that i actually founded within many resources and code-analysis:
So, even now, in a date when i writing it down here, there is still no normal way for making it in package.json... Like, at all...
But, by looking at some other extensions, i see that some of them are actually founded another way for making it working:
To do this, we need to look at the vscode.commands.registerCommand command.
function commands.registerCommand(command: string, callback: (...args: any[]) => any, thisArg?: any): vscode.Disposable
That means, we can use third parameter to actually send some data.
As an example:
package.json:
"contributes": {
...
"menus": {
"view/title": [
{
"command": "extension.addEntryOne",
"when": "view == extension",
"group": "navigation"
},
{
"command": "extension.addEntryTwo",
"when": "view == extension2",
"group": "navigation"
}
]
},
"commands": [
{
"command": "extension.addEntryOne",
"title": "Add entry One",
"icon":"$(diff-insert)"
},
{
"command": "remospace.addEntryTwo",
"title": "Add entry Two",
"icon":"$(diff-insert)"
}
]
...
}
extension.ts:
//
//Activation part before
//
let Execution = function(this: any){
console.log(this)
}
let entryOne = vscode.commands.registerCommand('extension.addEntryOne', Execution,{from:'one'});
let entryTwo = vscode.commands.registerCommand('remospace.addEntryTwo', Execution,{from:'one'});
context.subscriptions.push(entryOne,entryTwo);
In this case, we can bypass issue when we need to do same thing with different args. Yes, this may not be the best solution to do it, but at least this is one of it.