Repository: sublimelsp/LSP

Issue:
Provide a context key to check if LSP has a specific capability
<del>
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`.


```js
{ 
   // 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 }
		]
}
```
</del>


Update: @rwols pointed out that there is `lsp_active` to check, so this issue is now requesting a key for checking more specific capability.


Discussion:
I got my hand dirty and made this

```py
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
```js
    {
      "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](https://user-images.githubusercontent.com/6594915/68409208-a6625700-01c1-11ea-973d-9ea0a2329c58.png)

Isn't this already the default behavior?
In a non lsp document, `lsp_symbol_definition` is [disabled](https://github.com/tomv564/LSP/blob/master/plugin/goto.py#L26), 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.
Related: https://github.com/tomv564/LSP/issues/651, https://github.com/tomv564/LSP/commit/88fc8f9786569aab2a09a62f873af7e46aaef025

I think this already works with `lsp_active`, no?
@rwols I can confirm that `lsp_active` works. Thanks.

```js
{ "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.


> ```js
> { "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](https://github.com/tomv564/LSP/blob/master/Keymaps/Default%20(OSX).sublime-keymap#L28-L56)

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

Resolving patch:
diff --git a/plugin/core/documents.py b/plugin/core/documents.py
--- a/plugin/core/documents.py
+++ b/plugin/core/documents.py
@@ -126,10 +126,14 @@ def on_post_save(self) -> None:
     def on_close(self) -> None:
         self._clear_async()
 
-    def on_query_context(self, key: str, operator: str, operand: Any, match_all: bool) -> bool:
-        capability_prefix = "lsp.capabilities."
-        if key.startswith(capability_prefix):
-            return isinstance(self.view.settings().get(key[len(capability_prefix):]), dict)
+    def on_query_context(self, key: str, operator: int, operand: Any, match_all: bool) -> bool:
+        if key == "lsp.session_with_capability" and operator == sublime.OP_EQUAL and isinstance(operand, str):
+            capabilities = [s.strip() for s in operand.split("|")]
+            get = self.view.settings().get
+            for capability in capabilities:
+                if isinstance(get(capability), dict):
+                    return True
+            return False
         elif key in ("lsp.sessions", "setting.lsp_active"):
             return bool(self._session_views)
         else:


Test patch: