Repository: sublimelsp/LSP

Issue:
The Go To on a cloned view is enabled on the first view, instead of the current one
Copied from: https://github.com/SublimeTextIssues/Core/issues/1482 (The Go To on a cloned view is enabled on the first view, instead of the current one)

```patch
diff --git a/plugin/definition.py b/plugin/definition.py
index 7e77681..a84899c 100644
--- a/plugin/definition.py
+++ b/plugin/definition.py
@@ -42,12 +42,17 @@ class LspSymbolDefinitionCommand(LspTextCommand):
             # save to jump back history
             get_jump_history_for_view(self.view).push_selection(self.view)
 
+            # https://github.com/SublimeTextIssues/Core/issues/1482
+            group, view_index = window.get_view_index(self.view)
+            window.set_view_index(self.view, group, 0)
+
             location = response if isinstance(response, dict) else response[0]
             file_path = uri_to_filename(location.get("uri"))
             start = Point.from_lsp(location['range']['start'])
             file_location = "{}:{}:{}".format(file_path, start.row + 1, start.col + 1)
             log(2, "opening location %s <%s>", location, file_location)
             window.open_file(file_location, sublime.ENCODED_POSITION)
+            window.set_view_index(self.view, group, view_index)
             # TODO: can add region here.
         else:
             window.run_command("goto_definition")
```


Discussion:
How do people who don't use LSP deal with it?
Also, how often do you work with cloned views deliberately? I usually close accidental clones immediately when I find them.
> How do people who don't use LSP deal with it?

They use the same patch I suggested here and there (https://github.com/SublimeTextIssues/Core/issues/1482). On Sublime Text, you need to patch the `Default/symbol.py` file. While on this plugin, you need to patch the `plugin/definition.py` file.

> Also, how often do you work with cloned views deliberately?

Every day, every hour. Sometimes I clone the same view up to 10 clones.
I cannot actually reproduce this anymore with the latest st4000-exploration branch. The caret jumps to the def in the same view if I have multiple views open of the same underlying buffer.
I'd like to reopen this because I can reproduce this issue on the current main branch. So it seems like this was regressed at some point.

I'm working on a fix.


Resolving patch:
diff --git a/plugin/core/open.py b/plugin/core/open.py
--- a/plugin/core/open.py
+++ b/plugin/core/open.py
@@ -73,6 +73,15 @@ def _return_existing_view(flags: int, existing_view_group: int, active_group: in
     return not bool(flags & sublime.FORCE_GROUP)
 
 
+def _find_open_file(window: sublime.Window, fname: str, group: int = -1) -> Optional[sublime.View]:
+    """A replacement for Window.find_open_file that prefers the active view instead of the leftmost one."""
+    _group = window.active_group() if group == -1 else group
+    view = window.active_view_in_group(_group)
+    if fname == view.file_name():
+        return view
+    return window.find_open_file(fname, group)
+
+
 def open_file(
     window: sublime.Window, uri: DocumentUri, flags: int = 0, group: int = -1
 ) -> Promise[Optional[sublime.View]]:
@@ -84,7 +93,7 @@ def open_file(
     file = parse_uri(uri)[1]
     # window.open_file brings the file to focus if it's already opened, which we don't want (unless it's supposed
     # to open as a separate view).
-    view = window.find_open_file(file)
+    view = _find_open_file(window, file)
     if view and _return_existing_view(flags, window.get_view_index(view)[0], window.active_group(), group):
         return Promise.resolve(view)
 


Test patch: