Created
April 28, 2016 08:15
-
-
Save leonid-ed/de14ca89536be478031b6f35da36874d to your computer and use it in GitHub Desktop.
This is a Sublime Text 3 plugin that makes an adjusted multiline C-macro
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
# The MIT License (MIT) | |
# Copyright (c) 2016 Leonid Edrenkin | |
# | |
# This is a Sublime Text 3 plugin that makes an adjusted multiline C-macro. | |
# You should select some code and apply one of commands: | |
# view.run_command('make_multi_line_macro') | |
# view.run_command('unmake_multi_line_macro') | |
import sublime, sublime_plugin | |
class MakeMultiLineMacroCommand(sublime_plugin.TextCommand): | |
def run(self, edit): | |
v = self.view | |
ruler_size = 80 | |
regions = [ s for s in self.view.sel() if not s.empty() ] | |
if len(regions) == 0: | |
return | |
for region in regions: | |
region = v.full_line(region) | |
new_lines = [] | |
lines = v.split_by_newlines(region) | |
for i, line in enumerate(lines): | |
s = v.substr(line) | |
if (i < len(lines)-1): | |
s = s.rstrip() | |
if len(s) > 0 and s[-1] == '\\': s = s[:-1].rstrip() | |
space_size = ruler_size - len(s) - 1 | |
if space_size < 0 : space_size = 1 | |
s += " " * space_size + "\\\n" | |
else: | |
s += "\n" | |
new_lines.append(s) | |
p = region.begin() | |
v.erase(edit, region) | |
v.insert(edit, p, ''.join(new_lines)) | |
class UnmakeMultiLineMacroCommand(sublime_plugin.TextCommand): | |
def run(self, edit): | |
v = self.view | |
ruler_size = 80 | |
regions = [ s for s in self.view.sel() if not s.empty() ] | |
if len(regions) == 0: | |
return | |
for region in regions: | |
region = v.full_line(region) | |
new_lines = [] | |
lines = v.split_by_newlines(region) | |
for i, line in enumerate(lines): | |
s = v.substr(line) | |
if (i < len(lines)-1): | |
s = s.rstrip() | |
if len(s) > 0 and s[-1] == '\\': | |
s = s[:-1].rstrip() + '\n' | |
else: | |
s += '\n' | |
else: | |
s += '\n' | |
new_lines.append(s) | |
p = region.begin() | |
v.erase(edit, region) | |
v.insert(edit, p, ''.join(new_lines)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment