14

So obviously there are arguments for commands and registering a command like

vscode.commands.registerCommand("bla", (arg1: any, arg2: any) => {});

brings arg1 with a strange object containing only one key and that's context; an object holding some information about - you guessed it - the context.

There is also no way for the user to specify arguments. Not through the command palette and not for keybindings.

So are those arguments only for internal stuff or are they supposed to be used by an extension developer?

5
  • 1
    Besides context, what are you trying to bring in? Remember these commands will be triggered by a string from the user through a command pallet actions or keyboard shortcut. From there, you can gather the context or ask for input. Commented Feb 4, 2016 at 16:29
  • I wrote an extension where you can copy selected text to registers. When triggering the copy to register command the user is asked to which register the text should be copied. But when using the keyboard it would be handy to use key cords and then directly specify a registers by key (a, b, c, etc.). To achieve this I currently have to create multiple commands alá copyToReg1 and then the number of registers a user can have is technically bound to the number of commands, that I create. Wouldn't it be nice to just pass the register as a string parameter specified in the keyboard.json? Commented Feb 4, 2016 at 23:19
  • Why not create a drop down list for the user in one command? You are just as limited by number of free keys as possible commands, but your idea seems to make more sense as one command with a list. Then user can type first litter and search/select the one they want with a couple of key strokes. Even better, they won't have to memorize a chord Commented Feb 5, 2016 at 3:16
  • Hi @Databyte did you manage to solve this? So far seems that the functionality to achieve this is simply not there :( Commented Aug 19, 2016 at 16:38
  • 1
    Hi @LeoGallucci unfortunately it looks like you're right :( you can not pass arguments from kedboard.json Commented Aug 21, 2016 at 20:56

3 Answers 3

7

In keybindings.json you can specify arguments as such:

{
    "command": "workbench.action.tasks.runTask",
    "key": "alt+d",
    "args": "docker"
}

To access keybindings.json open View > Command Palette and type/choose Preferences: Open Keyboard Shortcuts (JSON). You may also want to assign a keyboard shortcut to this command.

Sign up to request clarification or add additional context in comments.

Comments

1

Dirty hack: If you don't mind how the command shows up in the editor, you can pass arguments, like ${file}, as the "name" in your launch.json. Then you just grab it from the context like so:

vscode.commands.registerCommand('extensiontest.helloWorld', (context) => { 
   
    type ObjectKey = keyof typeof context;
    const argsKey = 'name' as ObjectKey;
    console.log(context[argsKey]);
    ...

As a side effect, the name of the launch configuration in the drop down list will be {file}, or whatever your arguments are.

I want to stress that this is awful, but I believe it does answer the question.

Comments

-1

A little late to this problem, but I've solved this using the fact that function is first-class citizen in TypeScript

You can write a function that return a function, it's that returned function will be called by registerCommand:

// extension.ts
import { test } from "./test"

...

context.subscriptions.push(
    commands.registerCommand(
      `<your extension id>.test`,
      test(arg1, arg2, ...)
    )
  )
// test.ts

// double arrow expression
const test = (arg1, arg2, ...) => () => {
   // do anything you want with extensionPath
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.