def find_block_starts(view, block_tokens):
regex = re.compile(block_tokens if block_tokens else BLOCKERS)
return [r for r in view.find_by_selector('keyword,storage') if
view.find(r'\S', view.line(r).begin()).begin() == r.begin() and
regex.match(view.substr(r)) ]
Basically by finding block starting tokens, bisecting the closest to each selection and then using some indentation comparing routines, to find extents of a block. Used a bit of scope awareness etc.
Things like that worked better in ST 1 & 2 when the api was faster and you could do a lot of pt
by pt
stuff.