zed icon indicating copy to clipboard operation
zed copied to clipboard

GoBack/GoForward for cursor position history

Open artemijan opened this issue 9 months ago • 17 comments

Check for existing issues

  • [X] Completed

Describe the feature

There is a feature in all modern IDEs and text editors, to navigate in the text based on history. For example in my PyCharm set up it's CMD-[ and CMD-]. It's really handy when you debugging something and jumping from implementation to avstraction and want to go back or forward depending on cursor position history. Also this is really helpful when you checking large file with more than 5k lines (swagger files, external libs etc.)

Behaviour: Editor will remember the cursor position including: pane + line number + column number there will be editor actions "editor::GoBack" and "editor::GoForward" which will navigate through cursor history (don't mix with pane::GoBack and pane::GoForward they work differently and don't remember whole cursor position history) there will be a possibility to map those actions in keymap.json config file

PS: Maybe this is a bug in keymap of Zed (see my comments + video below). for some reason Zed ignores custom keymap and doesn't respect cmd-[ / cmd-] and treats it as line tabulation instead of using custom settings in a keymap pane::GoBack/pane::GoForward

If applicable, add mockups / screenshots to help present your vision of the feature

No response

artemijan avatar May 06 '24 11:05 artemijan

try ctrl-o and ctrl-i?

CharlesChen0823 avatar May 06 '24 11:05 CharlesChen0823

try ctrl-o and ctrl-i?

Not sure where did you get it but it doesn't work. I checked default keymap/documentation and there is no such a thing

artemijan avatar May 06 '24 12:05 artemijan

try ctrl-o and ctrl-i?

Not sure where did you get it but it doesn't work. I checked default keymap/documentation and there is no such a thing

This is a vim shortcut, and the effect in vim seems to be what you need?

try ?

 "ctrl--": "pane::GoBack",
 "ctrl-_": "pane::GoForward"

0x2CA avatar May 06 '24 12:05 0x2CA

These are pane actions, you can go back and forward in panes, what I want is to go back and forward where cursor was. Let me explain. Imagine a large file, open api swagger yaml file for instance which describes OPenBanking XS2a api, and it has more than 10k lines. I want to be able to go back/forward in a single pane/file, there is no next or previous pane, there is always one and I want to navigate where the cursor was previously, instead of remembering each time line number

artemijan avatar May 06 '24 12:05 artemijan

These are pane actions, you can go back and forward in panes, what I want is to go back and forward where cursor was. Let me explain. Imagine a large file, open api swagger yaml file for instance which describes OPenBanking XS2a api, and it has more than 10k lines. I want to be able to go back/forward in a single pane/file, there is no next or previous pane, there is always one and I want to navigate where the cursor was previously, instead of remembering each time line number

I think you mean that the history of the cursor is bound only to the current file, not globally In vim it is usually possible to mark the current position and then quickly jump to the marked function, similarly the vscode plugin has bookmarks

0x2CA avatar May 06 '24 13:05 0x2CA

No, the history of cursor is not bound to anything it's global, if I edit 2 or 3 large files I want text editor to remember where the cursor was e.g. pane + line number + column number. PS: regarding vim, I am not using it. And to navigate you actually have to explicitly mark the place where you want to remember, in my case I really often jump between pieces of documentation and don't want to "mark" it each time, and once I forget to mark I will need to find this piece of documentation again by searching it which takes time

artemijan avatar May 06 '24 13:05 artemijan

This is about using Mouse4/Mouse5 (back/forward) to undo changes to caret position / selected tab. For example if I command+click something to go to definition, I should be able to press Mouse4 (back) to return to the usage where I had just been, even if it's in a different tab (select that tab then). Or go back multiple times if I'm 4 or 5 nested go-to-definitions deep. IntelliJ has this feature, I discovered it by accident a day or two ago, it's very useful. Would also be nice for Zed to have it too.

LoganDark avatar May 07 '24 20:05 LoganDark

https://github.com/zed-industries/zed/assets/19868292/3a9b59df-bf72-453e-a174-61a955739993https://github.com/zed-industries/zed/assets/19868292/3a9b59df-bf72-453e-a174-61a955739993

0x2CA avatar May 10 '24 05:05 0x2CA

@0x2CA what are you showing in your video? I see you opening something in a pane, but that's not what's being requested (if I liked panes, I'd be using both of my monitors - I actually have two monitors but prefer to switch fullscreen views instead of having multiple arranged side by side). If you're trying to show something, please elaborate

LoganDark avatar May 10 '24 05:05 LoganDark

My video just reflects the current situation, backward and forward only apply to windows, you can't go backward when opening from one window or jumping to another window. As far as I know, vscode and vim have the same behavior, so I don't see any problem. The only difference is that when you open a window, you should copy the history of the current window, e.g., the history of the current window is a/b/c, and then you open a new file, d, and the history of the new window is a/b/c/d, so that in the new window, d, you can backtrack c

0x2CA avatar May 10 '24 05:05 0x2CA

As far as I know, vscode and vim have the same behavior, so I don't see any problem.

TBH I think supporting this is better than not supporting it even if some of the editors out there don't go through the effort. Zed's all about efficiency so I absolutely think this feature's going to be amazing for the 1 in 50,000 Mac users that have a five-button mouse.

Jokes aside I have forward/back mapped to a trackpad gesture, I use it in browsers and IntelliJ and it'd be nice to have it in Zed too.

LoganDark avatar May 10 '24 05:05 LoganDark

These are pane actions, you can go back and forward in panes, what I want is to go back and forward where cursor was.

Hi! In the Jetbrains compatibility keymap layout, the bindings for cmd-[ and cmd-] are set as follows:

  {
    "context": "Editor",
    "bindings": {
      // ....
      "cmd-[": "pane::GoBack",
      "cmd-]": "pane::GoForward",

These behave exactly as they do in JetBrains IDEs, allowing navigation through the history of cursor movements and even reopening closed tabs.

I also have the following custom keybindings:

      // MacOS-style keybindings.
      "cmd-alt-left": "pane::ActivatePrevItem",
      "cmd-alt-right": "pane::ActivateNextItem",

These bindings behave as mentioned, switching the active pane.

alexander-irbis avatar May 10 '24 09:05 alexander-irbis

@alexander-irbis , have you tried to use it? As I said, these are pane actions and they do not respect cursor position history, they only remember cursor position in each tab, so when you press cmd-[ or cmd-] it will just switch open panes and blink the cursor at position where you left of in this pane, but if I am navigating in a single file, this doesn't work as it goes to a previous pane instead of moving to the previous cursor position in the same tab

artemijan avatar May 10 '24 10:05 artemijan

@artemijan , yes, I use them daily, and they work exactly as described, considering the entire history of movements in the code, including when I simply place the cursor by clicking on a position in the file. The history preserves this, and navigation occurs through all the points where I moved.

I also bound these actions to cmd-9 / cmd-0 to double-check that these are indeed the correct actions for binding, which is a bit counterintuitive since the navigation commands through the editing history have a prefix "pane:".

I primarily work with Rust code, and I just tried navigating in the JSON of the keymap bindings - it works here. Perhaps when working with the types of files that you handle, something else occurs that disrupts the history, or there is some other local factor involved 🤔

alexander-irbis avatar May 10 '24 10:05 alexander-irbis

@artemijan , yes, I use them daily, and they work exactly as described, considering the entire history of movements in the code, including when I simply place the cursor by clicking on a position in the file. The history preserves this, and navigation occurs through all the points where I moved.

I also bound these actions to cmd-9 / cmd-0 to double-check that these are indeed the correct actions for binding, which is a bit counterintuitive since the navigation commands through the editing history have a prefix "pane:".

I primarily work with Rust code, and I just tried navigating in the JSON of the keymap bindings - it works here. Perhaps when working with the types of files that you handle, something else occurs that disrupts the history, or there is some other local factor involved 🤔

Okay, once I get to my laptop,I record a video, maybe this will help to understand the issue

artemijan avatar May 10 '24 10:05 artemijan

I looked into what might be special about my configuration, and I think it's worth mentioning that my settings include the following options:

  "format_on_save": "on",
  "formatter": "language_server",

I don’t know what the default value of the "formatter" option is, but perhaps this is what could have had a positive impact on the outcome on my side.

alexander-irbis avatar May 10 '24 10:05 alexander-irbis

Okay, I have just tried to reproduce it again, but for some reason my keymap was changed so pane::GoBack/Forward was ctrl-[ and ctrl-]. So when I use it with ctrl then everything is working and it seems like history of the cursor is respected. But if I change it from ctrl-[ to cmd-[ then it stops working and instead of moving through history of cursor it just changing tabulation of the line. Maybe someone can help me understand why it's happenning?

video

artemijan avatar May 14 '24 08:05 artemijan

@artemijan I noticed you edit the section with a "context": "Pane". When I experimented with cmd-9 / cmd-0, I added them to the "Editor" context.

alexander-irbis avatar May 18 '24 14:05 alexander-irbis

@alexander-irbis you are the hero, it helped. So in order to achieve this behaviourr I should move those keymaps from pane context to editor. Thanks you. I believe we can close the issue now.

artemijan avatar May 19 '24 08:05 artemijan