Repository: sublimelsp/LSP

Issue:
Capabilites with selector fails to register
**Describe the bug**

LSP-SourceKit fails to register `semanticTokensProvider` capability in LSP because of its server sending `documentSelector` property within the response.

**To Reproduce**

Steps to reproduce the behavior:
1. Install https://github.com/sublimelsp/LSP-SourceKit
2. open a swift file within this any swift package i.e. https://github.com/Alamofire/Alamofire
3. The error bellow would be thrown

```log
Package Control: Installing 2 libraries...
Error handling None
Traceback (most recent call last):
  File "/Users/user/Library/Application Support/Sublime Text/Packages/LSP/plugin/core/sessions.py", line 2339, in on_payload
    handler(result)
  File "/Users/user/Library/Application Support/Sublime Text/Packages/LSP/plugin/session_buffer.py", line 597, in _on_semantic_tokens_async
    self._draw_semantic_tokens_async()
  File "/Users/user/Library/Application Support/Sublime Text/Packages/LSP/plugin/session_buffer.py", line 646, in _draw_semantic_tokens_async
    token_type, token_modifiers, scope = self.session.decode_semantic_token(
  File "/Users/user/Library/Application Support/Sublime Text/Packages/LSP/plugin/core/sessions.py", line 1818, in decode_semantic_token
    types_legend = tuple(cast(List[str], self.get_capability('semanticTokensProvider.legend.tokenTypes')
```


**Expected behavior**

LSP should handle semantic response from a server.

**Screenshots**

If applicable, add screenshots to help explain your problem.

**Logs**

**Environment (please complete the following information):**

- OS: macOS 14.4.1
- Sublime Text version: 4173
- LSP version: 2.0.0
- Language servers used: sourcekit-lsp

**Additional context**

The cause of error is this lines within capabilities registration logic from `sessions.py`

```py
# for sb in self.session_buffers_async():
                #     debug("{}: session buffer:".format(self.config.name), sb, data.selector)
                #     data.check_applicable(sb)
```

or more precisely this from class `_RegistrationData`:

```py
    def check_applicable(self, sb: SessionBufferProtocol) -> None:
        for sv in sb.session_views:
            if self.selector.matches(sv.view):
                self.session_buffers.add(sb)
                sb.register_capability_async(
                    self.registration_id, self.capability_path, self.registration_path, self.options)
                return
```
It seems that `sb.register_capability_asyn` isn't implemented.

Addition of this line `self.capabilities.register(registration_id, capability_path, registration_path, options)` in `if data.selector` leaf of a control flow fixes the issue.


Discussion:

Resolving patch:
diff --git a/plugin/core/sessions.py b/plugin/core/sessions.py
--- a/plugin/core/sessions.py
+++ b/plugin/core/sessions.py
@@ -1814,9 +1814,12 @@ def _set_focused_sheet(self, sheet: Optional[sublime.Sheet]) -> None:
             self.window.focus_sheet(sheet)
 
     def decode_semantic_token(
-            self, token_type_encoded: int, token_modifiers_encoded: int) -> Tuple[str, List[str], Optional[str]]:
-        types_legend = tuple(cast(List[str], self.get_capability('semanticTokensProvider.legend.tokenTypes')))
-        modifiers_legend = tuple(cast(List[str], self.get_capability('semanticTokensProvider.legend.tokenModifiers')))
+        self,
+        types_legend: Tuple[str, ...],
+        modifiers_legend: Tuple[str, ...],
+        token_type_encoded: int,
+        token_modifiers_encoded: int
+    ) -> Tuple[str, List[str], Optional[str]]:
         return decode_semantic_token(
             types_legend, modifiers_legend, self._semantic_tokens_map, token_type_encoded, token_modifiers_encoded)
 
diff --git a/plugin/session_buffer.py b/plugin/session_buffer.py
--- a/plugin/session_buffer.py
+++ b/plugin/session_buffer.py
@@ -630,6 +630,8 @@ def _draw_semantic_tokens_async(self) -> None:
         scope_regions = dict()  # type: Dict[int, Tuple[str, List[sublime.Region]]]
         prev_line = 0
         prev_col_utf16 = 0
+        types_legend = tuple(cast(List[str], self.get_capability('semanticTokensProvider.legend.tokenTypes')))
+        modifiers_legend = tuple(cast(List[str], self.get_capability('semanticTokensProvider.legend.tokenModifiers')))
         for idx in range(0, len(self.semantic_tokens.data), 5):
             delta_line = self.semantic_tokens.data[idx]
             delta_start_utf16 = self.semantic_tokens.data[idx + 1]
@@ -644,7 +646,7 @@ def _draw_semantic_tokens_async(self) -> None:
             prev_line = line
             prev_col_utf16 = col_utf16
             token_type, token_modifiers, scope = self.session.decode_semantic_token(
-                token_type_encoded, token_modifiers_encoded)
+                types_legend, modifiers_legend, token_type_encoded, token_modifiers_encoded)
             if scope is None:
                 # We can still use the meta scope and draw highlighting regions for custom token types if there is a
                 # color scheme rule for this particular token type.


Test patch: