Sublime Forum

Fold blocks of certain scope when in view

#1

I’ve extended the default JavaScript syntax definitions to include a special comment block to have them highlighted in a bright colour. I’d like to write a small plugin to automagically gold these blocks when they come into view, and unfold them when they are scrolled out of view.

How I envision this happening is by getting the visible region with sublime.View’s visibleRegion method, somehow getting all the comment blocks that have the special scope, and getting Regions for them, and using sublime.Region’s intersects method to see which ones are in view and folding them.

The problem is I have no idea on how to

  1. get all the comment blocks’ regions by scope
  2. fold/unfold regions programmatically.

I also couldn’t find any way to detect when a view has been scrolled, but I’d be content with triggering the folding and unfolding when the user has been inactive for 2 seconds or something similar.

Any hints on where to start reading up on things or any other tips on how to tackle this would be greatly appreciated!

The documentation sources I’ve been able to find are the official API reference and plugin examples and the unofficial documentation’s Python API reference, if there are other good docs out there I’d love to hear about them!

0 Likes

#2
  1. View.find_by_selector
  2. View.fold and View.unfold
3 Likes

#3

Thank you! I got it working rather well triggered with a slight delay from on_modified, I’ll continue to research on options for detecting scroll events.

edit: it’s very hacky, and I’m sure it’s terribly inefficient, but starting a recursive set_timeout loop in on_activate and comparing view.viewport_position() between timeout callback executions seems to work fairly well. Any tips on how to improve on this solutions will be gladly accepted, but for now I’m very happy to have my little system working :slight_smile:

0 Likes

#4

That would have been my suggestion as well, except I’d use set_timeout_async to not lock up the UI. You could also listen to on_deactivated to terminate the call chain with a flag, or alternatively just spawn a custom thread and utilize time.sleep. There are certainly no scroll event hooks.

0 Likes