Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions SassBeautify.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,6 @@ def check_file(self):
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
# file extension.
if self.view.file_name() is None:
self.error_message('Please save this file before trying to beautify.')
return False

# Check the file has the correct extension before beautifying.
if self.get_type() not in ['sass', 'scss']:
Expand Down Expand Up @@ -143,7 +138,7 @@ def beautify(self):

def restore_end_of_line_comments(self, content):
def restore(m):
return ' ' + m.group(2) + m.group(4);
return ' ' + m.group(2) + m.group(4)

# Restore line and block comments at the end of lines that have been pushed to the next line by sass-convert
content = re.sub('(\s+)(//|/\\*)(---end-of-line-comment---)(.*)', restore, content)
Expand Down Expand Up @@ -277,8 +272,11 @@ def get_env(self):

def get_ext(self):
'''
Extracts the extension from the filename.
Extracts the extension from the filename. Defaults to scss
'''
if not self.is_saved():
return "scss"

(basename, ext) = os.path.splitext(self.view.file_name())
return ext.strip('.')

Expand All @@ -293,6 +291,12 @@ def get_type(self):
filetype = 'scss'
return filetype

def is_saved(self):
'''
Returns true if file is saved
'''
return bool(self.view.file_name())

def get_text(self):

def mark_end_of_line_comment(m):
Expand All @@ -318,12 +322,14 @@ def save(self):

# We have to store the state to prevent us getting in an infinite loop
# when beautifying on_post_save.
SassBeautifyCommand.saving = True
self.view.run_command('save')
SassBeautifyCommand.saving = False
if self.is_saved():
SassBeautifyCommand.saving = True
self.view.run_command('save')
SassBeautifyCommand.saving = False

self.view.set_viewport_position(self.viewport_pos, False)
self.view.sel().clear()
self.view.sel().add(self.selection)

sublime.status_message('Successfully beautified ' + self.view.file_name())
filename = self.view.file_name() if self.view.file_name() else 'file'
sublime.status_message('Successfully beautified ' + filename)