Skip to content

Instantly share code, notes, and snippets.

@rchl
Created March 11, 2023 21:21
Show Gist options
  • Save rchl/31538cbe39f28871c6bc41cecbd43199 to your computer and use it in GitHub Desktop.
Save rchl/31538cbe39f28871c6bc41cecbd43199 to your computer and use it in GitHub Desktop.
A Sublime Text plugin for keeping viewport position stable on (un)folding
from typing import Optional, Tuple
import sublime_plugin
class ViewEventListener(sublime_plugin.ViewEventListener):
def on_text_command(self, command_name: str, args: Optional[dict]) -> Optional[Tuple[str, dict]]:
if 'fold' in command_name:
view = self.view
self._old_layout = view.text_to_layout(view.sel()[0].begin())
self._old_viewport = view.viewport_position()
return None
def on_post_text_command(self, command_name: str, args: Optional[dict]) -> None:
if 'fold' in command_name:
view = self.view
_, y_after = view.text_to_layout(view.sel()[0].begin())
y_shift = y_after - self._old_layout[1]
if y_shift != 0:
old_viewport_x, old_viewport_y = self._old_viewport
new_y = old_viewport_y + y_shift
view.set_viewport_position((old_viewport_x, new_y), animate=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment