- Tools > New Plugin
- Paste
code_folding.py
- Add key binding at Sublime Text 2 > Preferences > Key Bindings - User
- Restart sublime
Created
November 30, 2012 18:49
-
-
Save jakebellacera/4177691 to your computer and use it in GitHub Desktop.
CSS code folding for sublime text 2
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, sublime_plugin, re | |
DEBUG_ENABLED = True | |
PRINT_CONTEXT = True | |
# toggle a single-line or multi-line formatted css statement | |
# { "keys": ["ctrl+shift+j"], "command": "toggle_single_line_css" } | |
class ToggleSingleLineCssCommand(sublime_plugin.TextCommand): | |
def run(self,edit): | |
debug('Invoked "toggle_single_line_css" with %d region(s)' % (len(self.view.sel()))) | |
for region in reversed(self.view.sel()): | |
text = self.view.substr(region) | |
# check if the css statement needs to be expanded or collapsed | |
if re.match('^.*\{.*}\s*$', text): | |
# expand the css statement | |
debug('The css statement needs to be expanded', text) | |
m = re.search('^(?P<key>.*)\{(?P<params>.*)\;\s*}$', text) | |
multiline = '%s{\n\t%s;\n}' % (m.group('key'), m.group('params').strip().replace('; ', ';\n\t')) | |
self.view.replace(edit, region, multiline) | |
else: | |
debug('The css statement needs to be collapsed', text) | |
# collapse the css statement | |
singleline = ' '.join([x.strip() for x in text.split('\n')]) | |
self.view.replace(edit, region, singleline) | |
def debug(text, context=""): | |
if DEBUG_ENABLED: | |
print '[toggle_single_line_css]: ' + text | |
if PRINT_CONTEXT and context != "": | |
print '>>> ' + context |
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
{ "keys": ["super+shift+j"], "command": "toggle_single_line_css" } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment