3

How can I set "Goto Definition" work according to the language I'm working on.

For example:

In Python I want to use PythonIDE's go to definition:

{
        "keys": ["ctrl+d"],
        "command": "python_goto_definition"
    },

And, for any other language, for instance Go, I want to use GoSublime's go to definition:

{
        "keys": ["ctrl+d"],
        "command": "go_sublime_goto_definition"
    },

I'm wondering how can I set the context?

SAMO
  • 458
  • 1
  • 3
  • 20
Humble Learner
  • 791
  • 12
  • 26
  • Why aren't you using language-specific plugins that will handle that for you? GoSublime for example. – OneOfOne Aug 05 '16 at 15:00
  • That works but not according to my requirement. In that case I need to define a shortcut for every language's goto definition function. – Humble Learner Aug 05 '16 at 15:18
  • A shortcut definition for specific file type(json) example is https://stackoverflow.com/a/73857676/1020512 . This is working for me, and i hope a modified version of this will work for you. – Kambaa Sep 26 '22 at 17:26

1 Answers1

3

The context that you want is for selector:

{
    "keys": ["ctrl+d"],
    "command": "python_goto_definition",
    "context": [
        { "key": "selector", "operator": "equal", "operand": "source.python" }
    ]
},

You can add more or less specificity as needed. Use Ctrl+Shift+Alt+P or Shift+Ctrl+P (MacOS) to view the scope selector for the current position.

OdatNurd
  • 21,371
  • 3
  • 50
  • 68
  • And, what if we want to do the same for Ctrl+click shortcuts? – Humble Learner Aug 08 '16 at 13:32
  • I don't think there is an analog for setting such a context in a `sublime-mousemap` file, although I could be wrong. Documentation on it seems sparse and I wasn't able to find any examples that do it. It might be possible via a custom plugin command that would examine the filename/syntax of the clicked in view and then run the appropriate command, though. – OdatNurd Aug 08 '16 at 17:57