Lsp: Provide a context key to check if LSP has a specific capability

Created on 7 Nov 2019  路  10Comments  路  Source: sublimelsp/LSP

Possible use case of the context key is to define custom keybind for go to definitions. With the context key, it allows me to bind `super+alt+down` to both `goto_definition` and `lsp_symbol_definition`.
{ 
   // this is the default key for `goto_definition`
  "keys": ["super+alt+down"], "command": "goto_definition" 
},
{ 
  "keys": ["super+alt+down"], "command": "lsp_symbol_definition" ,
  "context": [
            { "key": "lsp.is.active", "operator": "equal", "operand": true }
        ]
}

Update: @rwols pointed out that there is lsp_active to check, so this issue is now requesting a key for checking more specific capability.

enhancement

Most helpful comment

Though lsp_active is good enough for my purpose, the context key I posted above also checks if the client has such capability.

{ "keys": ["f12"], "command": "lsp_symbol_definition", "context": [{"key": "setting.lsp_active"}]},

Maybe this is worth written in the docs :)

Perhaps, we should put the context in all these keyblindings

All 10 comments

I got my hand dirty and made this

import sublime_plugin
from LSP.plugin.core.registry import session_for_view


class LspCapability(sublime_plugin.EventListener):
    def on_query_context(self, view, key, operator, operand, match_all):
        if key == "lsp.client_with_capability":
            return session_for_view(view, operand)

and now I could do

    {
      "keys": ["super+alt+down"], "command": "lsp_symbol_definition" ,
      "context": [
                { "key": "lsp.client_with_capability", "operator": "equal", "operand": "definitionProvider" }
            ]
    }

Any interest in getting this merged?

Pretty cool :-)

https://lsp.readthedocs.io/en/latest/features/

image

Isn't this already the default behavior?

In a non lsp document, lsp_symbol_definition is disabled, hence f12 is simply malfunction. It only fallbacks to goto_definition when lsp is at work.

You are right... No wonder sometimes I get my F12 not working :open_mouth:
lsp_symbol_definition is not enabled hence not even be run in that case.

@rwols I can confirm that lsp_active works. Thanks.

{ "keys": ["f12"], "command": "lsp_symbol_definition", "context": [{"key": "setting.lsp_active"}]},

Maybe it is worth writing it in the docs :)

Though lsp_active is good enough for my purpose, the context key I posted above also checks if the client has such capability.

{ "keys": ["f12"], "command": "lsp_symbol_definition", "context": [{"key": "setting.lsp_active"}]},

Maybe this is worth written in the docs :)

Perhaps, we should put the context in all these keyblindings

I changed the original post and the title for a key checking a more specific capability. (Though I am already happy wtih lsp_active) :)

Fixed by #1142 for ST4. Check out the new keybinding contexts.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

arsham picture arsham  路  3Comments

arsham picture arsham  路  4Comments

frou picture frou  路  3Comments

gerardroche picture gerardroche  路  3Comments

predragnikolic picture predragnikolic  路  7Comments