zed-industries / zed

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
https://zed.dev
Other
46.97k stars 2.7k forks source link

GoBack/GoForward for cursor position history #11429

Closed artemijan closed 3 months ago

artemijan commented 4 months ago

Check for existing issues

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

CharlesChen0823 commented 4 months ago

try ctrl-o and ctrl-i?

artemijan commented 4 months ago

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

0x2CA commented 4 months ago

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"
artemijan commented 4 months ago

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

0x2CA commented 4 months ago

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

artemijan commented 4 months ago

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

LoganDark commented 4 months ago

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.

0x2CA commented 4 months ago

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

LoganDark commented 4 months ago

@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

0x2CA commented 4 months ago

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

LoganDark commented 4 months ago

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.

alexander-irbis commented 4 months ago

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.

artemijan commented 4 months ago

@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

alexander-irbis commented 4 months ago

@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 🤔

artemijan commented 4 months ago

@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

alexander-irbis commented 4 months ago

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.

artemijan commented 4 months ago

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

alexander-irbis commented 4 months ago

@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.

artemijan commented 3 months ago

@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.