Created
June 5, 2012 17:02
-
-
Save vitaLee/2876253 to your computer and use it in GitHub Desktop.
SublimeText command for snapping displaced text to indentation
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
# sample shortcuts | |
{ "keys": ["ctrl+super+]"], "command": "snap_lines_to_indent_level", "args": { "snap_direction": 1 } }, | |
{ "keys": ["ctrl+super+["], "command": "snap_lines_to_indent_level", "args": { "snap_direction": -1 } } |
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 | |
import sublime_plugin | |
import re | |
import math | |
class SnapLinesToIndentLevel(sublime_plugin.TextCommand): | |
def run(self, edit, snap_direction=1): | |
view = self.view | |
tab_size = view.settings().get('tab_size') | |
use_spaces = view.settings().get('translate_tabs_to_spaces') | |
lines = [] | |
for sel in view.sel(): | |
lines += view.lines(sel) | |
lines.reverse() | |
for line_region in lines: | |
content = view.substr(line_region) | |
match = re.match("^\s*", content) | |
if not match: | |
continue | |
whitespace = match.group(0) | |
spaces = map(lambda c: 1 if c == ' ' else tab_size, | |
list(whitespace)) | |
curr_indent = sum(spaces) | |
indent_offset = float(curr_indent) / tab_size | |
if indent_offset == 0: | |
continue | |
#backward | |
if snap_direction == -1: | |
indent_level = math.floor(indent_offset) * tab_size | |
else: | |
indent_level = math.ceil(indent_offset) * tab_size | |
indent_content = '' | |
if use_spaces: | |
indent_content = ' ' * int(indent_level) | |
else: | |
indent_content = '\t' * int(indent_level / tab_size) | |
replace_region = sublime.Region(line_region.a, | |
line_region.a + len(whitespace)) | |
view.replace(edit, replace_region, indent_content) |
Very useful. Thanks!
not working now
putting py inside user folder
in key binding entered shortcuts
help!!!!!)))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you!