Skip to content

Commit ff5bc9f

Browse files
rchlrwols
authored andcommitted
Don't expose disabled code actions
1 parent a5df681 commit ff5bc9f

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

plugin/code_actions.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,12 @@ def create_collector(self, config_name: str) -> Callable[[CodeActionsResponse],
5656

5757
def _collect_response(self, config_name: str, actions: CodeActionsResponse) -> None:
5858
self._response_count += 1
59-
self._commands_by_config[config_name] = actions or []
59+
self._commands_by_config[config_name] = self._get_enabled_actions(actions or [])
6060
self._notify_if_all_finished()
6161

62+
def _get_enabled_actions(self, actions: List[CodeActionOrCommand]) -> List[CodeActionOrCommand]:
63+
return [action for action in actions if not action.get('disabled')]
64+
6265
def _notify_if_all_finished(self) -> None:
6366
if self._all_requested and self._request_count == self._response_count:
6467
# Call back on Sublime's async thread

plugin/core/protocol.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,17 @@ class SemanticTokenModifiers:
164164
}, total=True)
165165

166166

167+
CodeActionDisabledInformation = TypedDict('CodeActionDisabledInformation', {
168+
'reason': str
169+
}, total=True)
170+
171+
167172
CodeAction = TypedDict('CodeAction', {
168173
'title': str,
169174
'kind': Optional[str],
170175
'diagnostics': Optional[List[Any]],
171176
'isPreferred': Optional[bool],
177+
'disabled': Optional[CodeActionDisabledInformation],
172178
'edit': Optional[dict],
173179
'command': Optional[Command],
174180
}, total=True)

plugin/core/sessions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ def get_initialize_params(variables: Dict[str, str], workspace_folders: List[Wor
317317
}
318318
},
319319
"dataSupport": True,
320+
"disabledSupport": True,
320321
"resolveSupport": {
321322
"properties": [
322323
"edit"

tests/test_code_actions.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ def create_test_code_action2(command_name: str, command_args: Optional[List[Any]
5959
return action
6060

6161

62+
def create_disabled_code_action(view: sublime.View, version: int, edits: List[Tuple[str, Range]]) -> Dict[str, Any]:
63+
action = {
64+
"title": "Fix errors",
65+
"edit": create_code_action_edit(view, version, edits),
66+
"disabled": {
67+
"reason": "Do not use"
68+
},
69+
}
70+
return action
71+
72+
6273
def create_test_diagnostics(diagnostics: List[Tuple[str, Range]]) -> Dict:
6374
def diagnostic_to_lsp(diagnostic: Tuple[str, Range]) -> Dict:
6475
message, range = diagnostic
@@ -299,6 +310,22 @@ def test_requests_with_no_diagnostics(self) -> Generator:
299310
self.assertEquals(annotations_range[0].a, 3)
300311
self.assertEquals(annotations_range[0].b, 0)
301312

313+
def test_excludes_disabled_code_actions(self) -> Generator:
314+
initial_content = 'a\n'
315+
self.insert_characters(initial_content)
316+
yield from self.await_message("textDocument/didChange")
317+
code_action = create_disabled_code_action(
318+
self.view,
319+
self.view.change_count(),
320+
[(';', Range(Point(0, 0), Point(0, 1)))]
321+
)
322+
self.set_response('textDocument/codeAction', [code_action])
323+
self.view.run_command('lsp_selection_set', {"regions": [(0, 1)]}) # Select a
324+
yield 100
325+
yield from self.await_message('textDocument/codeAction')
326+
code_action_ranges = self.view.get_regions(SessionView.CODE_ACTIONS_KEY)
327+
self.assertEquals(len(code_action_ranges), 0)
328+
302329
def test_extends_range_to_include_diagnostics(self) -> Generator:
303330
self.insert_characters('x diagnostic')
304331
yield from self.await_message("textDocument/didChange")

0 commit comments

Comments
 (0)