Skip to content

Commit

Permalink
Keep tab selection and focus when applying WorkspaceEdit (#2431)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwortmann authored Mar 12, 2024
1 parent 1a22893 commit ca03baf
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
21 changes: 16 additions & 5 deletions plugin/core/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1771,8 +1771,7 @@ def _apply_code_action_async(
arguments = command.get("arguments")
if arguments is not None:
execute_command['arguments'] = arguments
return promise.then(
lambda _: self.execute_command(execute_command, progress=False, view=view))
return promise.then(lambda _: self.execute_command(execute_command, progress=False, view=view))
return promise

def apply_workspace_edit_async(self, edit: WorkspaceEdit) -> Promise[None]:
Expand All @@ -1783,12 +1782,16 @@ def apply_workspace_edit_async(self, edit: WorkspaceEdit) -> Promise[None]:
return self.apply_parsed_workspace_edits(parse_workspace_edit(edit))

def apply_parsed_workspace_edits(self, changes: WorkspaceChanges) -> Promise[None]:
active_sheet = self.window.active_sheet()
selected_sheets = self.window.selected_sheets()
promises = [] # type: List[Promise[None]]
for uri, (edits, view_version) in changes.items():
promises.append(
self.open_uri_async(uri).then(functools.partial(self._apply_text_edits, edits, view_version, uri))
)
return Promise.all(promises).then(lambda _: None)
return Promise.all(promises) \
.then(lambda _: self._set_selected_sheets(selected_sheets)) \
.then(lambda _: self._set_focused_sheet(active_sheet))

def _apply_text_edits(
self, edits: List[TextEdit], view_version: Optional[int], uri: str, view: Optional[sublime.View]
Expand All @@ -1798,6 +1801,14 @@ def _apply_text_edits(
return
apply_text_edits(view, edits, required_view_version=view_version)

def _set_selected_sheets(self, sheets: List[sublime.Sheet]) -> None:
if len(sheets) > 1 and len(self.window.selected_sheets()) != len(sheets):
self.window.select_sheets(sheets)

def _set_focused_sheet(self, sheet: Optional[sublime.Sheet]) -> None:
if sheet and sheet != self.window.active_sheet():
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')))
Expand Down Expand Up @@ -1926,8 +1937,8 @@ def m_workspace_configuration(self, params: Dict[str, Any], request_id: Any) ->

def m_workspace_applyEdit(self, params: Any, request_id: Any) -> None:
"""handles the workspace/applyEdit request"""
self.apply_workspace_edit_async(params.get('edit', {})).then(
lambda _: self.send_response(Response(request_id, {"applied": True})))
self.apply_workspace_edit_async(params.get('edit', {})) \
.then(lambda _: self.send_response(Response(request_id, {"applied": True})))

def m_workspace_codeLens_refresh(self, _: Any, request_id: Any) -> None:
"""handles the workspace/codeLens/refresh request"""
Expand Down
6 changes: 4 additions & 2 deletions plugin/hover.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,10 @@ def request_document_link_async(self, listener: AbstractViewListener, point: int
if target:
link_promises.append(Promise.resolve(link))
elif sv.has_capability_async("documentLinkProvider.resolveProvider"):
link_promises.append(sv.session.send_request_task(Request.resolveDocumentLink(link, sv.view)).then(
lambda link: self._on_resolved_link(sv.session_buffer, link)))
link_promises.append(
sv.session.send_request_task(Request.resolveDocumentLink(link, sv.view))
.then(lambda link: self._on_resolved_link(sv.session_buffer, link))
)
if link_promises:
Promise.all(link_promises).then(partial(self._on_all_document_links_resolved, listener, point))

Expand Down
4 changes: 2 additions & 2 deletions tests/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ def await_promise(cls, promise: Union[YieldPromise, Promise]) -> Generator:
def await_run_code_action(self, code_action: Dict[str, Any]) -> Generator:
promise = YieldPromise()
sublime.set_timeout_async(
lambda: self.session.run_code_action_async(code_action, progress=False, view=self.view).then(
promise.fulfill))
lambda: self.session.run_code_action_async(code_action, progress=False, view=self.view)
.then(promise.fulfill))
yield from self.await_promise(promise)

def set_response(self, method: str, response: Any) -> None:
Expand Down

0 comments on commit ca03baf

Please sign in to comment.