Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jcartledge committed Oct 16, 2012
1 parent 2659d61 commit a8d93f2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 37 deletions.
68 changes: 32 additions & 36 deletions Surround.py
Expand Up @@ -6,37 +6,38 @@


class SurroundCommand(sublime_plugin.TextCommand):
def surround_pairs_for_addition(self, surround):
pairs = surround_settings.get('surround_pairs_for_addition')
return self.surround_pairs(surround, pairs)
""" Base class for surround commands
"""

def pairs_for_replacement(self, surround):
pairs = surround_settings.get('surround_pairs_for_replacement')
return self.pair(surround, pairs)

def surround_pairs(self, surround, pairs):
def pair(self, surround, pairs):
if surround[0] in pairs:
return pairs[surround[0]]
else:
return surround

def surround_tags_for_addition(self, surround):
def tags_for_replacement(self, surround):
matches = re.search(r"<([\S]+)[^>]*>", surround[0])
if matches:
return [surround[0], "</" + matches.group(1) + ">"]
else:
return surround

def surround_addition(self, surround):
def preprocess_replacement(self, surround):
surround = [surround, surround]
surround = self.surround_pairs_for_addition(surround)
surround = self.surround_tags_for_addition(surround)
surround = self.pairs_for_replacement(surround)
surround = self.tags_for_replacement(surround)
return surround

def run_surround(self, caption, callback):
window = self.view.window()
window.show_input_panel(caption, '', callback, None, None)
self.view.window().show_input_panel(caption, '', callback, None, None)


class SurroundSelectionCommand(SurroundCommand):
"""
Surround the current selection(s) with something
""" Surround the current selection(s) with something
"""

def run(self, edit):
Expand All @@ -47,7 +48,7 @@ def run(self, edit):

def surround_selection(self, surround):
view = self.view
surround = self.surround_addition(surround)
surround = self.preprocess_replacement(surround)
edit = view.begin_edit()
try:
for region in reversed(self.sel):
Expand All @@ -58,8 +59,7 @@ def surround_selection(self, surround):


class SurroundChangeCommand(SurroundCommand):
"""
Change something surrounding the current insertion point(s) to something else
""" Change something surrounding the current insertion point(s) to something else
"""

def run(self, edit):
Expand All @@ -72,8 +72,8 @@ def replace_with(self, surround):
'Replace with:', '', self.replace_surround, None, None)

def replace_surround(self, replacement):
search = self.surround_search_patterns(self.surround)
replacement = self.surround_addition(replacement)
search = self.search_patterns_for_surround(self.surround)
replacement = self.preprocess_replacement(replacement)
view = self.view
edit = view.begin_edit()
try:
Expand All @@ -96,8 +96,8 @@ def find_start(self, to_pos, search):
previous = matches.pop()
# balance pairs
close_search = [search[1], search[0], search[2]]
count_closing_pairs = len(self.find_between(previous.end(), to_pos, close_search))
if count_closing_pairs % 2 is 0:
count_pairs = len(self.find_between(previous.end(), to_pos, close_search))
if count_pairs % 2 is 0:
return previous
else:
return self.find_start(previous.begin(), search)
Expand All @@ -107,36 +107,33 @@ def find_end(self, from_pos, search):
if next is None:
raise RuntimeError('Ending pair not found: ' + search[1])
# balance pairs
count_opening_pairs = len(self.find_between(from_pos, next.begin(), search))
if count_opening_pairs % 2 is 0:
count_pairs = len(self.find_between(from_pos, next.begin(), search))
if count_pairs % 2 is 0:
return next
else:
return self.find_end(next.end(), search)

def find_between(self, from_pos, to_pos, search):
found = []
possible_finds = self.view.find_all(search[0], search[2])
for possible_find in possible_finds:
if possible_find.end() <= to_pos and possible_find.begin() >= from_pos:
found.append(possible_find)
return found

def surround_search_patterns(self, surround):
return [find for find in self.view.find_all(search[0], search[2])
if find.end() <= to_pos
and find.begin() >= from_pos]

def search_patterns_for_surround(self, surround):
surround = [surround, surround]
surround = self.surround_pairs_for_search(surround)
surround = self.surround_tags_for_search(surround)
surround = self.pairs_for_search(surround)
surround = self.tags_for_search(surround)
if len(surround[0]) <= 1:
flag = sublime.LITERAL
else:
flag = 0
surround.append(flag)
return surround

def surround_pairs_for_search(self, surround):
def pairs_for_search(self, surround):
pairs = surround_settings.get('surround_pairs_for_search')
return self.surround_pairs(surround, pairs)
return self.pair(surround, pairs)

def surround_tags_for_search(self, surround):
def tags_for_search(self, surround):
matches = re.search(r"<([\S]+)([^>]*)>", surround[0])
if matches:
attrs = matches.group(2)
Expand All @@ -150,8 +147,7 @@ def surround_tags_for_search(self, surround):


class SurroundDeleteCommand(SurroundChangeCommand):
"""
Delete something surrounding current insertion point(s)
""" Delete something surrounding current insertion point(s)
"""

def run(self, edit):
Expand Down
2 changes: 1 addition & 1 deletion surround.sublime-settings
@@ -1,5 +1,5 @@
{
"surround_pairs_for_addition": {
"surround_pairs_for_replacement": {
"{": ["{ ", " }"],
"}": ["{", "}"],
"[": ["[ ", " ]"],
Expand Down

0 comments on commit a8d93f2

Please sign in to comment.