Last active
December 21, 2017 21:31
-
-
Save dantonnoriega/ee9f8d4d3e13f0e9750298b611f53306 to your computer and use it in GitHub Desktop.
sublime text script to create two new expand commands: to end of file and expand to section. a section is defined by a header using the Rstudio syntax '# SECTION TITLE ----------' or anything with 4 hashes '####'. flexible with comment sections as well.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sublime, sublime_plugin | |
class ExpandSelectionToEofCommand(sublime_plugin.TextCommand): | |
def run(self, edit): | |
v = self.view | |
s = v.sel() | |
eof = v.size() | |
first_point = v.line(s[0]).a | |
region_to_eof = sublime.Region(first_point, eof) | |
s.clear() | |
s.add(region_to_eof) | |
class ExpandSelectionToSectionCommand(sublime_plugin.TextCommand): | |
def run(self, edit): | |
v = self.view | |
s = v.sel() | |
def get_text(row, col): | |
return v.substr(v.line(v.text_point(row, col))) | |
def find_top(line, pattern): | |
# print('%d' % line) | |
if pattern.match(get_text(line, 0)): # match text | |
top = v.line(v.text_point(line + 1, 0)) | |
# print('section values: (%d, %d)' % (top.a, top.b)) | |
return top | |
else: | |
return find_top(line - 1, pattern) | |
def find_bottom(line, eof_line_num, pattern): | |
# print('%d' % line) | |
if pattern.match(get_text(line, 0)): # match text | |
bottom = v.line(v.text_point(line - 1, 0)) | |
# print('section values: (%d, %d)' % (bottom.a, bottom.b)) | |
return bottom | |
elif line == eof_line_num: | |
bottom = v.line(v.text_point(eof_line_num, 0)) | |
# print('eof values: (%d, %d)' % (bottom.a, bottom.b)) | |
return bottom | |
else: | |
return find_bottom(line + 1, eof_line_num, pattern) | |
# find pattern | |
import re | |
pattern_str = "^[ ]*# [#]{4,}|^#+[ ]+#+[ ][\w|\s]+[-]+$|^[ ]*[#]{4,}|^[ ]*#+ [\w|\s]+[-]+$|^`{3}" | |
pattern = re.compile(pattern_str) | |
# get current point and then look forward | |
first_point = v.line(s[0]).a | |
section_line = v.find(pattern_str, first_point) | |
# if at section_line header, jump forward 1 spot then expand and reassign `first_point` | |
# print('%d %d' % (first_point, section_line.a)) | |
if first_point == section_line.a: | |
line = v.rowcol(section_line.a)[0] # get current line number | |
first_point = v.line(v.text_point(line + 1, 0)).a | |
# print('%d' % (first_point)) | |
# search for closest section top and bottom | |
current_line = v.rowcol(first_point)[0] # get line number | |
eof = v.size() | |
eof_line_num = v.rowcol(eof)[0] | |
top_line = find_top(current_line, pattern) | |
bottom_line = find_bottom(current_line, eof_line_num, pattern) | |
section = sublime.Region(top_line.a, bottom_line.b) | |
s.clear() | |
s.add(section) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment