Sublime Forum

Add to jump_back history stack

#1

How do we add the current location of the cursor to the history stack before moving the cursor, so that the user can use jump_back to get back?

For example, if the user is currently on line 1, and invoking the plugin takes them to line 15, and then invoking the plugin again takes them to line 55. The behavior I want is that jump_back should take them to line 15. Currently, it goes back to line 1, because I’m assuming that’s on the top of the history stack, and my plugin didn’t add line 15 to the history stack.

0 Likes

#2

Related: https://packagecontrol.io/packages/Back There

0 Likes

#3

Thanks for the pointer @FHTheron. Unfortunately, it looks like Back There keeps it’s own jump list, which doesn’t help with my goal.

I did find a workaround that forces the current position onto the “jump stack.” It appears that opening an overlay puts the current cursor position onto the stack, so if I add that to my plugin, then I can replicate the intended behavior of going back to the previous position.

However, showing (and hiding) an overlay just to add the current cursor position onto the stack looks like an ugly hack, and I’d imagine that there’s a command to push the current position to the jump stack/list.

0 Likes

#4

You can take a look at the stack management in Default/history_list.py using View Package File command.

For example, this plugin add a point in the stack when a command with a name that end with _switch_interface_implementation is executed.

import sublime_plugin
import Default.history_list as history_list

class MyJumpHistoryUpdater(sublime_plugin.EventListener):
    def on_text_command(self, view, name, args):
        if view.settings().get('is_widget'):
            return
        if name.endswith('_switch_interface_implementation'):
            history_list.get_jump_history_for_view(view).push_selection(view)

Now you have either to create the save point using a timer or a specific custom command (Add To Stack).

2 Likes

#5

Thanks you @bizoo! That is exactly what I was looking for!

In my case, I was looking at updating the move_by_paragraph command so that it tracks the history. I pretty much followed your example, and it works!

from sublime_plugin import EventListener
import Default.history_list as history_list

class MoveByPragraphJumpHistoryUpdater(EventListener):
    """Updates the jump history with the current location before jumping."""
    def on_text_command(self, view, name, args):
        if view.settings().get('is_widget'):
            return
        if name == 'move_by_paragraph':
            history_list.get_jump_history_for_view(view).push_selection(view)
0 Likes