Created
April 3, 2013 15:15
-
-
Save robballou/5302105 to your computer and use it in GitHub Desktop.
Work-in-progress of a sublime plugin to fold just functions.
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 | |
import re | |
class FoldFunctionsCommand(sublime_plugin.TextCommand): | |
def run(self, edit): | |
# find things marked as a meta.function | |
functions = self.view.find_by_selector('meta.function.php') | |
print "FoldFunctions: functions found: %d" % len(functions) | |
# loop through those functions and figure out what region that they | |
# take up. | |
function_contents = [] | |
for function in functions: | |
# each "function" is a region (a,b) that is compressed of the line | |
# defining the function. So we fill our function_contents with | |
# region coordinates f | |
# | |
# Figure out the index in function_contents for the last item | |
last_function = len(function_contents) - 1 | |
# start a new function (either we have no function contents or the previous | |
# entry has two coordinates meaning that it's complete) | |
if len(function_contents) == 0 or len(function_contents[last_function]) == 2: | |
# store the end of the current function in a new entry in our function | |
# contents list | |
function_contents.append([function.b]) | |
elif len(function_contents[last_function]) == 1: | |
# we found a new function after an existing one that only has the first | |
# part of it's coordinates | |
function_contents[last_function].append(function.a) | |
# we now have a rough region of one function to another function, but | |
# we need to account for a few things: | |
# | |
# 1. The fold should end on the line where the previous function ends | |
# 2. Comments/stuff in between the functions should not be folded | |
# create regions out of each of the function_contents | |
for index in range(0, len(function_contents)): | |
this_function = function_contents[index] | |
previous_function = None | |
if index > 0: | |
previous_function = function_contents[(index - 1)] | |
if len(this_function) == 2: | |
x, y = this_function[0], this_function[1] | |
else: | |
x, y = this_function[0], self.view.size() | |
starting_line = self.view.line(x) | |
print self.view.substr(starting_line) | |
x = starting_line.b + 1 | |
# find the end of the function by going backwards line-by-line | |
while y > x: | |
this_line = self.view.line(y) | |
print this_line | |
contents = self.view.substr(this_line) | |
print contents | |
if this_line.a == this_line.b: | |
y -= 1 | |
elif re.match(r'\s*}', contents): | |
y -= 1 | |
break | |
else: | |
break | |
region = sublime.Region(x, y) | |
# print self.view.substr(region) | |
print region | |
self.view.fold(region) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment