Skip to content

Commit

Permalink
Added setting for beautifying on save. Cleanup. Refs #33
Browse files Browse the repository at this point in the history
  • Loading branch information
badsyntax committed Feb 7, 2014
1 parent c382d57 commit 4315c91
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 30 deletions.
75 changes: 45 additions & 30 deletions SassBeautify.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def __init__(self, cmd, env, stdin):

def run(self):
'''
Execute the command in a sub-process.
Executes the command in a sub-process.
'''
try:
process = subprocess.Popen(
Expand All @@ -55,25 +55,38 @@ def run(self):
self.returncode = 1


class ReplaceTextCommand(sublime_plugin.TextCommand):
class SassBeautifyReplaceTextCommand(sublime_plugin.TextCommand):

'''
A custom ST text command to replace the entire view with new text.
A Text Command to replace the entire view with new text.
'''

def run(self, edit, text=None):
self.view.replace(edit, sublime.Region(0, self.view.size()), text)

class SassBeautifyEvents(sublime_plugin.EventListener):

def on_post_save(self, view):
if SassBeautifyCommand.saving == False:
view.run_command('sass_beautify', { 'show_errors': False })
'''
An EventListener class to utilize Sublime's events.
'''

def on_post_save(self, view):
'''
After saving the file, optionally beautify it. This is done on_post_save
because the beautification is asynchronous.
'''

beautify_on_save = sublime.load_settings('SassBeautify.sublime-settings').get('beautifyOnSave', False)

if not SassBeautifyCommand.saving and beautify_on_save:
view.run_command('sass_beautify', {
'show_errors': False
})

class SassBeautifyCommand(sublime_plugin.TextCommand):

'''
Our main SassBeautify ST text command.
Our main SassBeautify Text Command.
'''

saving = False
Expand All @@ -85,12 +98,12 @@ def run(self, edit, action='beautify', convert_from_type=None, show_errors=True)
self.settings = sublime.load_settings('SassBeautify.sublime-settings')
self.show_errors = show_errors

if self.check_file() != False:
if self.check_file():
self.beautify()

def check_file(self):
'''
Perform some validation checks on the file to ensure we're working with
Performs some validation checks on the file to ensure we're working with
something that we can beautify.
'''
# A file has to be saved so we can get the conversion type from the
Expand All @@ -104,22 +117,18 @@ def check_file(self):
self.error_message('Not a valid Sass file.')
return False

def error_message(self, message):
if self.show_errors == True:
sublime.error_message(message)
return True

def beautify(self):
def error_message(self, message):
'''
Run the sass beautify command.
Shows a Sublime error message.
'''
# The conversion operation might take a little while on slower
# machines so we should let the user know something is happening.
sublime.status_message('Beautifying your sass...')
self.exec_cmd()
if self.show_errors:
sublime.error_message(message)

def exec_cmd(self):
def beautify(self):
'''
Execute the threaded sass command.
Runs the sass beautify command.
'''
thread = ExecSassCommand(
self.get_cmd(),
Expand All @@ -130,6 +139,9 @@ def exec_cmd(self):
self.check_thread(thread)

def check_thread(self, thread, i=0, dir=1):
'''
Checks if the thread is still running.
'''

# This animates a little activity indicator in the status area
# Taken from https://github.com/wbond/sublime_prefixr
Expand All @@ -153,6 +165,9 @@ def check_thread(self, thread, i=0, dir=1):
self.handle_process(thread.returncode, thread.stdout, thread.stderr)

def handle_process(self, returncode, output, error):
'''
Handles the streams from the threaded process.
'''

if type(output) is bytes:
output = output.decode('utf-8')
Expand All @@ -174,52 +189,52 @@ def handle_process(self, returncode, output, error):
output = re.sub(r'\n\n\n', '\n\n', re.sub(r'(.*)\{', r'\n\1{', output).strip())

# Update the text in the editor
self.view.run_command('replace_text', {'text': output})
self.view.run_command('sass_beautify_replace_text', {'text': output})

# Save the file
sublime.set_timeout(self.save, 1)

def get_cmd(self):
'''
Generate the sass command we'll use to beauitfy the sass.
Generates the sass command.
'''
filetype = self.get_type()

cmd = [
'sass-convert',
'--unix-newlines',
'--stdin',
'--indent', str(self.settings.get('indent')),
'--indent', str(self.settings.get('indent', 't')),
'--from', filetype if self.action == 'beautify' else self.convert_from_type,
'--to', filetype
]

# Convert underscores to dashes.
if self.settings.get('dasherize') == True:
if self.settings.get('dasherize', False):
cmd.append('--dasherize')

# Output the old-style ':prop val' property syntax.
# Only meaningful when generating Sass.
if self.settings.get('old') == True and filetype == 'sass':
if self.settings.get('old', False) and filetype == 'sass':
cmd.append('--old')

return cmd

def get_env(self):
'''
Generate the process environment.
Generates the process environment.
'''
env = os.environ.copy()

# If path is set, modify environment. (Issue #1)
if self.settings.get('path'):
if self.settings.get('path', False):
env['PATH'] = self.settings.get('path')

return env

def get_ext(self):
'''
Extract the extension from the filename.
Extracts the extension from the filename.
'''
(basename, ext) = os.path.splitext(self.view.file_name())
return ext.strip('.')
Expand All @@ -237,13 +252,13 @@ def get_type(self):

def get_text(self):
'''
Get the sass text from the Sublime view.
Gets the sass text from the Sublime view.
'''
return self.view.substr(sublime.Region(0, self.view.size())).encode('utf-8')

def save(self):
'''
Save the file and show a success message.
Saves the file and show a success message.
'''

SassBeautifyCommand.saving = True;
Expand Down
1 change: 1 addition & 0 deletions SassBeautify.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"dasherize": false,
"old": false,
"path": false,
"beautifyOnSave": false,
"blanklineBetweenSelectors": false
}

0 comments on commit 4315c91

Please sign in to comment.