Permalink
Browse files

-More comments and clean up

  • Loading branch information...
1 parent 3dc4c4b commit e7004b9e48ceaea6d96e085297d7125d9533e424 @facelessuser committed Nov 17, 2012
Showing with 127 additions and 6 deletions.
  1. +43 −5 bh_plugin.py
  2. +12 −0 bh_remove.py
  3. +72 −1 bh_wrapping.py
View
@@ -8,13 +8,29 @@
class BracketRegion (namedtuple('BracketRegion', ['begin', 'end'], verbose=False)):
+ """
+ Bracket Regions for plugins
+ """
+
def move(self, begin, end):
+ """
+ Move bracket region to different points
+ """
+
return self._replace(begin=begin, end=end)
def size(self):
+ """
+ Get the size of the region
+ """
+
return abs(self.begin - self.end)
def toregion(self):
+ """
+ Convert to sublime region
+ """
+
return sublime.Region(self.begin, self.end)
@@ -26,11 +42,23 @@ def toregion(self):
def is_bracket_region(obj):
+ """
+ Check if object is a BracketRegion
+ """
+
return isinstance(obj, BracketRegion)
class BracketPlugin(object):
+ """
+ Class for preparing and running plugins
+ """
+
def __init__(self, plugin, loaded):
+ """
+ Load plugin module
+ """
+
self.enabled = False
self.args = plugin['args'] if ("args" in plugin) else {}
self.plugin = None
@@ -58,9 +86,17 @@ def __init__(self, plugin, loaded):
print 'BracketHighlighter: Load Plugin Error: %s\n%s' % (plugin['command'], traceback.format_exc())
def is_enabled(self):
+ """
+ Check if plugin is enabled
+ """
+
return self.enabled
def run_command(self, view, name, left, right, selection):
+ """
+ Load arguments into plugin and run
+ """
+
plugin = self.plugin()
setattr(plugin, "left", left)
setattr(plugin, "right", right)
@@ -79,11 +115,13 @@ def run_command(self, view, name, left, right, selection):
class BracketPluginCommand(object):
- def __setattr__(self, name, value):
- # if name in ["left", "right"] and not is_bracket_region(value):
- # print type(value)
- # raise TypeError
- super(BracketPluginCommand, self).__setattr__(name, value)
+ """
+ Bracket Plugin base class
+ """
def run(self, bracket, content, selection):
+ """
+ Runs the plugin class
+ """
+
pass
View
@@ -10,7 +10,15 @@
class BhRemoveBracketsCommand(sublime_plugin.WindowCommand):
+ """
+ Command to remove current highlighted brackets and optionally content
+ """
+
def remove_brackets(self, value):
+ """
+ Perform removal of brackets
+ """
+
if value != -1:
menu_item = MENU[value]
indent = menu_item == MENU.block_indent
@@ -33,6 +41,10 @@ def remove_brackets(self, value):
)
def run(self):
+ """
+ Show menu of removal options
+ """
+
self.window.show_quick_panel(
list(MENU),
self.remove_brackets
View
@@ -11,6 +11,10 @@
def exclude_entry(enabled, filter_type, language_list, language):
+ """
+ Exclude bracket wrapping entry by filter
+ """
+
exclude = True
if enabled:
# Black list languages
@@ -32,25 +36,48 @@ def exclude_entry(enabled, filter_type, language_list, language):
class TextInsertion(object):
+ """
+ Wrapper class for inserting text
+ """
+
def __init__(self, view, edit):
+ """
+ Store view and edit objects
+ """
+
self.view = view
self.edit = edit
def insert(self, pt, text):
+ """
+ Peform insertion
+ """
+
return self.view.insert(self.edit, pt, text)
class WrapBracketsCommand(sublime_plugin.TextCommand):
+ """
+ Wrap the current selection(s) with the defined wrapping options
+ """
+
def inline(self, edit, sel):
+ """
+ Inline wrap
+ """
+
ti = TextInsertion(self.view, edit)
- # self.insert_regions.append(sublime.Region(sel.begin() + len(self.brackets[0])))
offset1 = ti.insert(sel.begin(), self.brackets[0])
self.insert_regions.append(sublime.Region(sel.begin(), sel.begin() + offset1))
offset2 = ti.insert(sel.end() + offset1, self.brackets[1])
self.insert_regions.append(sublime.Region(sel.end() + offset1, sel.end() + offset1 + offset2))
def block(self, edit, sel, indent=False):
+ """
+ Wrap brackets around selection and block off the content
+ """
+
# Calculate number of lines between brackets
self.calculate_lines(sel)
# Calculate the current indentation of first bracket
@@ -84,6 +111,10 @@ def block(self, edit, sel, indent=False):
self.insert_regions.append(sublime.Region(first_end + second_start, first_end + second_start + second_end))
def indent_content(self, ti, line_offset):
+ """
+ Indent the block content
+ """
+
first = True
offset = 0
for l in range(line_offset, self.total_lines + line_offset):
@@ -96,17 +127,29 @@ def indent_content(self, ti, line_offset):
return offset
def calculate_lines(self, sel):
+ """
+ Calculate lines between brackets
+ """
+
self.first_line, self.col_position = self.view.rowcol(sel.begin())
last_line = self.view.rowcol(sel.end())[0]
self.total_lines = last_line - self.first_line + 1
def calculate_indentation(self, sel):
+ """
+ Calculate how much lines should be indented
+ """
+
tab_size = self.view.settings().get("tab_size", 4)
tab_count = self.view.substr(sublime.Region(sel.begin() - self.col_position, sel.begin())).count('\t')
spaces = self.col_position - tab_count
self.indent_to_col = "\t" * tab_count + "\t" * (spaces / tab_size) + " " * (spaces % tab_size if spaces >= tab_size else spaces)
def select(self, edit):
+ """
+ Select defined regions after wrapping
+ """
+
self.view.sel().clear()
map(lambda x: self.view.sel().add(x), self.insert_regions)
@@ -143,6 +186,10 @@ def select(self, edit):
self.view.sel().add(final_sel[0])
def wrap_brackets(self, value):
+ """
+ Wrap selection(s) with defined brackets
+ """
+
if value < 0:
return
@@ -169,6 +216,10 @@ def wrap_brackets(self, value):
self.view.end_edit(edit)
def read_wrap_entries(self):
+ """
+ Read wrap entries from the settings file
+ """
+
settings = sublime.load_settings("bh_wrapping.sublime-settings")
syntax = self.view.settings().get('syntax')
language = basename(syntax).replace('.tmLanguage', '').lower() if syntax != None else "plain text"
@@ -187,6 +238,10 @@ def read_wrap_entries(self):
pass
def run(self, edit):
+ """
+ Display the wrapping menu
+ """
+
self._menu = []
self._brackets = []
self._insert = []
@@ -200,7 +255,15 @@ def run(self, edit):
class BhNextWrapSelCommand(sublime_plugin.TextCommand):
+ """
+ Navigate wrapping tab stop regions
+ """
+
def run(self, edit):
+ """
+ Look for the next wrapping tab stop region
+ """
+
regions = self.view.get_regions(SEL_REGION) + self.view.get_regions(OUT_REGION)
if len(regions):
self.view.sel().clear()
@@ -212,7 +275,15 @@ def run(self, edit):
class BhWrapListener(sublime_plugin.EventListener):
+ """
+ Listen for wrapping tab stop tabbing
+ """
+
def on_query_context(self, view, key, operator, operand, match_all):
+ """
+ Mark the next regions to navigate to.
+ """
+
accept_query = False
if key == "bh_wrapping":
select = []

0 comments on commit e7004b9

Please sign in to comment.