prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  goto commands don't restore selection when location picking is canceled
  **Describe the bug**
  when there's more than one location available for a `goto*` command, a quick panel is shown to pick.
  highlighting entries modifies the selection, canceling the operation doesn't restore the initial selection.
  
  **Expected behavior**
  it should restore the selection, like ST's built-in
  
  **Screenshots**
  ![lsp_goto_cancel_bug](https://user-images.githubusercontent.com/33235928/174485692-f6eb2c91-e309-464d-b44e-9dad258a58e3.gif)
  
  **Environment (please complete the following information):**
  - OS: Windows 10
  - Sublime Text version: 4126
  - LSP version: 1.16.3
  - Language servers used: LSP-rust-analyzer
  
  **Additional context**
  Add any other context about the problem here. For example, whether you're using a helper
  package or your manual server configuration in LSP.sublime-settings. When using
  a manual server configuration please include it here if you believe it's applicable.
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `plugin/locationpicker.py`
  Content:
  ```
  1 from .core.logging import debug
  2 from .core.protocol import DocumentUri, Location, Position
  3 from .core.protocol import LocationLink
  4 from .core.sessions import Session
  5 from .core.typing import Union, List, Optional, Tuple
  6 from .core.views import get_uri_and_position_from_location
  7 from .core.views import location_to_human_readable
  8 from .core.views import to_encoded_filename
  9 import functools
  10 import sublime
  11 import weakref
  12 
  13 
  14 def open_location_async(
  15     session: Session,
  16     location: Union[Location, LocationLink],
  17     side_by_side: bool,
  18     force_group: bool
  19 ) -> None:
  20     flags = sublime.ENCODED_POSITION
  21     if force_group:
  22         flags |= sublime.FORCE_GROUP
  23     if side_by_side:
  24         flags |= sublime.ADD_TO_SELECTION | sublime.SEMI_TRANSIENT
  25 
  26     def check_success_async(view: Optional[sublime.View]) -> None:
  27         if not view:
  28             sublime.error_message("Unable to open URI")
  29 
  30     session.open_location_async(location, flags).then(check_success_async)
  31 
  32 
  33 def open_basic_file(
  34     session: Session,
  35     uri: str,
  36     position: Position,
  37     flags: int = 0,
  38     group: Optional[int] = None
  39 ) -> sublime.View:
  40     filename = session.config.map_server_uri_to_client_path(uri)
  41     if group is None:
  42         group = session.window.active_group()
  43     return session.window.open_file(to_encoded_filename(filename, position), flags=flags, group=group)
  44 
  45 
  46 class LocationPicker:
  47 
  48     def __init__(
  49         self,
  50         view: sublime.View,
  51         session: Session,
  52         locations: Union[List[Location], List[LocationLink]],
  53         side_by_side: bool
  54     ) -> None:
  55         self._view = view
  56         window = view.window()
  57         if not window:
  58             raise ValueError("missing window")
  59         self._window = window
  60         self._weaksession = weakref.ref(session)
  61         self._side_by_side = side_by_side
  62         self._items = locations
  63         self._highlighted_view = None  # type: Optional[sublime.View]
  64         manager = session.manager()
  65         base_dir = manager.get_project_path(view.file_name() or "") if manager else None
  66         self._window.show_quick_panel(
  67             items=[location_to_human_readable(session.config, base_dir, location) for location in locations],
  68             on_select=self._select_entry,
  69             on_highlight=self._highlight_entry,
  70             flags=sublime.KEEP_OPEN_ON_FOCUS_LOST
  71         )
  72 
  73     def _unpack(self, index: int) -> Tuple[Optional[Session], Union[Location, LocationLink], DocumentUri, Position]:
  74         location = self._items[index]
  75         uri, position = get_uri_and_position_from_location(location)
  76         return self._weaksession(), location, uri, position
  77 
  78     def _select_entry(self, index: int) -> None:
  79         if index >= 0 and self._view.is_valid():
  80             session, location, uri, position = self._unpack(index)
  81             if not session:
  82                 return
  83             # Note: this has to run on the main thread (and not via open_location_async)
  84             # otherwise the bevior feels weird. It's the only reason why open_basic_file exists.
  85             if uri.startswith("file:"):
  86                 flags = sublime.ENCODED_POSITION
  87                 if not self._side_by_side:
  88                     open_basic_file(session, uri, position, flags)
  89             else:
  90                 sublime.set_timeout_async(
  91                     functools.partial(open_location_async, session, location, self._side_by_side, True))
  92         else:
  93             self._window.focus_view(self._view)
  94             # When in side-by-side mode close the current highlighted
  95             # sheet upon canceling if the sheet is semi-transient
  96             if self._side_by_side and self._highlighted_view:
  97                 sheet = self._highlighted_view.sheet()
  98                 if sheet and sheet.is_semi_transient():
  99                     self._highlighted_view.close()
  100 
  101     def _highlight_entry(self, index: int) -> None:
  102         session, _, uri, position = self._unpack(index)
  103         if not session:
  104             return
  105         if uri.startswith("file:"):
  106             flags = sublime.ENCODED_POSITION | sublime.FORCE_GROUP
  107             if self._side_by_side:
  108                 if self._highlighted_view and self._highlighted_view.is_valid():
  109                     # Replacing the MRU is done relative to the current highlighted sheet
  110                     self._window.focus_view(self._highlighted_view)
  111                     flags |= sublime.REPLACE_MRU | sublime.SEMI_TRANSIENT
  112                 else:
  113                     flags |= sublime.ADD_TO_SELECTION | sublime.SEMI_TRANSIENT
  114             else:
  115                 flags |= sublime.TRANSIENT
  116             self._highlighted_view = open_basic_file(session, uri, position, flags, self._window.active_group())
  117         else:
  118             # TODO: Preview non-file uris?
  119             debug("no preview for", uri)
  120
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/plugin/locationpicker.py b/plugin/locationpicker.py
  --- a/plugin/locationpicker.py
  +++ b/plugin/locationpicker.py
  @@ -53,6 +53,7 @@
           side_by_side: bool
       ) -> None:
           self._view = view
  +        self._view_states = ([r.to_tuple() for r in view.sel()], view.viewport_position())
           window = view.window()
           if not window:
               raise ValueError("missing window")
  @@ -76,6 +77,9 @@
           return self._weaksession(), location, uri, position
   
       def _select_entry(self, index: int) -> None:
  +        if self._view.is_valid() and not self._side_by_side:
  +            self._view.set_viewport_position(self._view_states[1])
  +            self._view.run_command('lsp_selection_set', {'regions': self._view_states[0]})
           if index >= 0 and self._view.is_valid():
               session, location, uri, position = self._unpack(index)
               if not session:
  
verification_info: {"golden_diff": "diff --git a/plugin/locationpicker.py b/plugin/locationpicker.py\n--- a/plugin/locationpicker.py\n+++ b/plugin/locationpicker.py\n@@ -53,6 +53,7 @@\n         side_by_side: bool\n     ) -> None:\n         self._view = view\n+        self._view_states = ([r.to_tuple() for r in view.sel()], view.viewport_position())\n         window = view.window()\n         if not window:\n             raise ValueError(\"missing window\")\n@@ -76,6 +77,9 @@\n         return self._weaksession(), location, uri, position\n \n     def _select_entry(self, index: int) -> None:\n+        if self._view.is_valid() and not self._side_by_side:\n+            self._view.set_viewport_position(self._view_states[1])\n+            self._view.run_command('lsp_selection_set', {'regions': self._view_states[0]})\n         if index >= 0 and self._view.is_valid():\n             session, location, uri, position = self._unpack(index)\n             if not session:\n", "issue": "goto commands don't restore selection when location picking is canceled\n**Describe the bug**\r\nwhen there's more than one location available for a `goto*` command, a quick panel is shown to pick.\r\nhighlighting entries modifies the selection, canceling the operation doesn't restore the initial selection.\r\n\r\n**Expected behavior**\r\nit should restore the selection, like ST's built-in\r\n\r\n**Screenshots**\r\n![lsp_goto_cancel_bug](https://user-images.githubusercontent.com/33235928/174485692-f6eb2c91-e309-464d-b44e-9dad258a58e3.gif)\r\n\r\n**Environment (please complete the following information):**\r\n- OS: Windows 10\r\n- Sublime Text version: 4126\r\n- LSP version: 1.16.3\r\n- Language servers used: LSP-rust-analyzer\r\n\r\n**Additional context**\r\nAdd any other context about the problem here. For example, whether you're using a helper\r\npackage or your manual server configuration in LSP.sublime-settings. When using\r\na manual server configuration please include it here if you believe it's applicable.\r\n\n", "before_files": [{"content": "from .core.logging import debug\nfrom .core.protocol import DocumentUri, Location, Position\nfrom .core.protocol import LocationLink\nfrom .core.sessions import Session\nfrom .core.typing import Union, List, Optional, Tuple\nfrom .core.views import get_uri_and_position_from_location\nfrom .core.views import location_to_human_readable\nfrom .core.views import to_encoded_filename\nimport functools\nimport sublime\nimport weakref\n\n\ndef open_location_async(\n    session: Session,\n    location: Union[Location, LocationLink],\n    side_by_side: bool,\n    force_group: bool\n) -> None:\n    flags = sublime.ENCODED_POSITION\n    if force_group:\n        flags |= sublime.FORCE_GROUP\n    if side_by_side:\n        flags |= sublime.ADD_TO_SELECTION | sublime.SEMI_TRANSIENT\n\n    def check_success_async(view: Optional[sublime.View]) -> None:\n        if not view:\n            sublime.error_message(\"Unable to open URI\")\n\n    session.open_location_async(location, flags).then(check_success_async)\n\n\ndef open_basic_file(\n    session: Session,\n    uri: str,\n    position: Position,\n    flags: int = 0,\n    group: Optional[int] = None\n) -> sublime.View:\n    filename = session.config.map_server_uri_to_client_path(uri)\n    if group is None:\n        group = session.window.active_group()\n    return session.window.open_file(to_encoded_filename(filename, position), flags=flags, group=group)\n\n\nclass LocationPicker:\n\n    def __init__(\n        self,\n        view: sublime.View,\n        session: Session,\n        locations: Union[List[Location], List[LocationLink]],\n        side_by_side: bool\n    ) -> None:\n        self._view = view\n        window = view.window()\n        if not window:\n            raise ValueError(\"missing window\")\n        self._window = window\n        self._weaksession = weakref.ref(session)\n        self._side_by_side = side_by_side\n        self._items = locations\n        self._highlighted_view = None  # type: Optional[sublime.View]\n        manager = session.manager()\n        base_dir = manager.get_project_path(view.file_name() or \"\") if manager else None\n        self._window.show_quick_panel(\n            items=[location_to_human_readable(session.config, base_dir, location) for location in locations],\n            on_select=self._select_entry,\n            on_highlight=self._highlight_entry,\n            flags=sublime.KEEP_OPEN_ON_FOCUS_LOST\n        )\n\n    def _unpack(self, index: int) -> Tuple[Optional[Session], Union[Location, LocationLink], DocumentUri, Position]:\n        location = self._items[index]\n        uri, position = get_uri_and_position_from_location(location)\n        return self._weaksession(), location, uri, position\n\n    def _select_entry(self, index: int) -> None:\n        if index >= 0 and self._view.is_valid():\n            session, location, uri, position = self._unpack(index)\n            if not session:\n                return\n            # Note: this has to run on the main thread (and not via open_location_async)\n            # otherwise the bevior feels weird. It's the only reason why open_basic_file exists.\n            if uri.startswith(\"file:\"):\n                flags = sublime.ENCODED_POSITION\n                if not self._side_by_side:\n                    open_basic_file(session, uri, position, flags)\n            else:\n                sublime.set_timeout_async(\n                    functools.partial(open_location_async, session, location, self._side_by_side, True))\n        else:\n            self._window.focus_view(self._view)\n            # When in side-by-side mode close the current highlighted\n            # sheet upon canceling if the sheet is semi-transient\n            if self._side_by_side and self._highlighted_view:\n                sheet = self._highlighted_view.sheet()\n                if sheet and sheet.is_semi_transient():\n                    self._highlighted_view.close()\n\n    def _highlight_entry(self, index: int) -> None:\n        session, _, uri, position = self._unpack(index)\n        if not session:\n            return\n        if uri.startswith(\"file:\"):\n            flags = sublime.ENCODED_POSITION | sublime.FORCE_GROUP\n            if self._side_by_side:\n                if self._highlighted_view and self._highlighted_view.is_valid():\n                    # Replacing the MRU is done relative to the current highlighted sheet\n                    self._window.focus_view(self._highlighted_view)\n                    flags |= sublime.REPLACE_MRU | sublime.SEMI_TRANSIENT\n                else:\n                    flags |= sublime.ADD_TO_SELECTION | sublime.SEMI_TRANSIENT\n            else:\n                flags |= sublime.TRANSIENT\n            self._highlighted_view = open_basic_file(session, uri, position, flags, self._window.active_group())\n        else:\n            # TODO: Preview non-file uris?\n            debug(\"no preview for\", uri)\n", "path": "plugin/locationpicker.py"}], "after_files": [{"content": "from .core.logging import debug\nfrom .core.protocol import DocumentUri, Location, Position\nfrom .core.protocol import LocationLink\nfrom .core.sessions import Session\nfrom .core.typing import Union, List, Optional, Tuple\nfrom .core.views import get_uri_and_position_from_location\nfrom .core.views import location_to_human_readable\nfrom .core.views import to_encoded_filename\nimport functools\nimport sublime\nimport weakref\n\n\ndef open_location_async(\n    session: Session,\n    location: Union[Location, LocationLink],\n    side_by_side: bool,\n    force_group: bool\n) -> None:\n    flags = sublime.ENCODED_POSITION\n    if force_group:\n        flags |= sublime.FORCE_GROUP\n    if side_by_side:\n        flags |= sublime.ADD_TO_SELECTION | sublime.SEMI_TRANSIENT\n\n    def check_success_async(view: Optional[sublime.View]) -> None:\n        if not view:\n            sublime.error_message(\"Unable to open URI\")\n\n    session.open_location_async(location, flags).then(check_success_async)\n\n\ndef open_basic_file(\n    session: Session,\n    uri: str,\n    position: Position,\n    flags: int = 0,\n    group: Optional[int] = None\n) -> sublime.View:\n    filename = session.config.map_server_uri_to_client_path(uri)\n    if group is None:\n        group = session.window.active_group()\n    return session.window.open_file(to_encoded_filename(filename, position), flags=flags, group=group)\n\n\nclass LocationPicker:\n\n    def __init__(\n        self,\n        view: sublime.View,\n        session: Session,\n        locations: Union[List[Location], List[LocationLink]],\n        side_by_side: bool\n    ) -> None:\n        self._view = view\n        self._view_states = ([r.to_tuple() for r in view.sel()], view.viewport_position())\n        window = view.window()\n        if not window:\n            raise ValueError(\"missing window\")\n        self._window = window\n        self._weaksession = weakref.ref(session)\n        self._side_by_side = side_by_side\n        self._items = locations\n        self._highlighted_view = None  # type: Optional[sublime.View]\n        manager = session.manager()\n        base_dir = manager.get_project_path(view.file_name() or \"\") if manager else None\n        self._window.show_quick_panel(\n            items=[location_to_human_readable(session.config, base_dir, location) for location in locations],\n            on_select=self._select_entry,\n            on_highlight=self._highlight_entry,\n            flags=sublime.KEEP_OPEN_ON_FOCUS_LOST\n        )\n\n    def _unpack(self, index: int) -> Tuple[Optional[Session], Union[Location, LocationLink], DocumentUri, Position]:\n        location = self._items[index]\n        uri, position = get_uri_and_position_from_location(location)\n        return self._weaksession(), location, uri, position\n\n    def _select_entry(self, index: int) -> None:\n        if self._view.is_valid() and not self._side_by_side:\n            self._view.set_viewport_position(self._view_states[1])\n            self._view.run_command('lsp_selection_set', {'regions': self._view_states[0]})\n        if index >= 0 and self._view.is_valid():\n            session, location, uri, position = self._unpack(index)\n            if not session:\n                return\n            # Note: this has to run on the main thread (and not via open_location_async)\n            # otherwise the bevior feels weird. It's the only reason why open_basic_file exists.\n            if uri.startswith(\"file:\"):\n                flags = sublime.ENCODED_POSITION\n                if not self._side_by_side:\n                    open_basic_file(session, uri, position, flags)\n            else:\n                sublime.set_timeout_async(\n                    functools.partial(open_location_async, session, location, self._side_by_side, True))\n        else:\n            self._window.focus_view(self._view)\n            # When in side-by-side mode close the current highlighted\n            # sheet upon canceling if the sheet is semi-transient\n            if self._side_by_side and self._highlighted_view:\n                sheet = self._highlighted_view.sheet()\n                if sheet and sheet.is_semi_transient():\n                    self._highlighted_view.close()\n\n    def _highlight_entry(self, index: int) -> None:\n        session, _, uri, position = self._unpack(index)\n        if not session:\n            return\n        if uri.startswith(\"file:\"):\n            flags = sublime.ENCODED_POSITION | sublime.FORCE_GROUP\n            if self._side_by_side:\n                if self._highlighted_view and self._highlighted_view.is_valid():\n                    # Replacing the MRU is done relative to the current highlighted sheet\n                    self._window.focus_view(self._highlighted_view)\n                    flags |= sublime.REPLACE_MRU | sublime.SEMI_TRANSIENT\n                else:\n                    flags |= sublime.ADD_TO_SELECTION | sublime.SEMI_TRANSIENT\n            else:\n                flags |= sublime.TRANSIENT\n            self._highlighted_view = open_basic_file(session, uri, position, flags, self._window.active_group())\n        else:\n            # TODO: Preview non-file uris?\n            debug(\"no preview for\", uri)\n", "path": "plugin/locationpicker.py"}]}