Last active
September 21, 2016 13:40
-
-
Save kemayo/5131191 to your computer and use it in GitHub Desktop.
Tiny sublime plugin to wrap a selection in something.
This file contains 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
Show hidden characters
[ | |
{ "keys": ["alt+p"], "command": "wrap_text", "args": {"start":"<? ", "end":" ?>" }}, | |
] |
This file contains 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 WrapTextCommand(sublime_plugin.TextCommand): | |
def run(self, edit, start="", end="", select_mode='contents'): | |
regions = [] | |
for region in self.view.sel(): | |
text = self.view.substr(region) | |
self.view.replace(edit, region, "%s%s%s" % (start, text, end)) | |
if select_mode == 'contents': | |
regions.append(sublime.Region(region.begin() + len(start), region.begin()+len(start)+len(text))) | |
elif select_mode == 'wrappers': | |
regions.append(sublime.Region(region.begin(), region.begin() + len(start))) | |
regions.append(sublime.Region(region.begin() + len(start) + len(text), region.begin() + len(start) + len(text) + len(end))) | |
elif select_mode == 'tags': | |
# Assumes the start/end are html tags, and you want to select the "tag part" | |
regions.append(sublime.Region(region.begin() + 1, region.begin() + len(start) - 1)) | |
regions.append(sublime.Region(region.begin() + len(start) + len(text) + 2, region.begin() + len(start) + len(text) + len(end) - 1)) | |
if not select_mode or select_mode == 'all': | |
return | |
self.view.sel().clear() | |
for region in regions: | |
self.view.sel().add(region) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment