Skip to content
Open
Show file tree
Hide file tree
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
19 changes: 17 additions & 2 deletions SassBeautify.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,24 @@ def insert_newline_between_capturing_parentheses(m):
content = re.sub(re.compile('(;.*|}.*)(\n +//.*\n.+[{,])$', re.MULTILINE), insert_newline_between_capturing_parentheses, content)

return content

def use_single_quotes(self, content):
content = content.replace('"', '\'')
return content

def beautify_semicolons(self, content):
'''
Appends a semicolon to the lines missing one
'''
def remove_semicolon_from_end_of_comment(m):
return m.group(1) + m.group(2)

content = re.sub(re.compile('(?<!,)$', re.MULTILINE), ';', content) # add ; to all lines not ending in ,
content = re.sub(re.compile(';(?=$\s*\{)', re.MULTILINE), '', content) # remove ; if next line starts with {
content = re.sub(re.compile('(//|/\\*)(.*);$', re.MULTILINE), remove_semicolon_from_end_of_comment, content) # remove ; if at end of comment
# sass-convert removes all other cases of extra ;s, so don't worry about them here
return content

def check_thread(self, thread, i=0, dir=1):
'''
Checks if the thread is still running.
Expand Down Expand Up @@ -229,7 +242,7 @@ def handle_process(self, returncode, output, error):

if self.settings.get('newlineBetweenSelectors', False):
output = self.beautify_newlines(output)

if self.settings.get('useSingleQuotes', False):
output = self.use_single_quotes(output)

Expand Down Expand Up @@ -310,6 +323,8 @@ def mark_end_of_line_comment(m):
'''
content = self.view.substr(sublime.Region(0, self.view.size()))

content = self.beautify_semicolons(content)

if self.settings.get('inlineComments', False):
'''
Mark comments at the end of lines so we can move them back to the end of the line after sass-convert has pushed them to a new line
Expand Down
42 changes: 42 additions & 0 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,45 @@ def test_skip_insert_newline_2(self):
}

"""))

class test_internal_function_beautify_semicolons(TestCase):

# Check that we add ;s to the end of lines, except for comments, lines ending in commas, and lines before an open {
# all other cases where we might introduce a double semicolon or where a semicolon isn't needed are cleaned up by
# sass-convert so we don't need to worry about them here
def test_semicolons(self):
beautified = SassBeautifyCommandInstance.beautify_semicolons(textwrap.dedent("""\

// @import '_common';
@import "_colors"

//TODO: eat pizza
// and other things

.ClassA,
.ClassB
{
height: 14px
font-size: 2em;
margin: -3px 0 !important
}

"""))

self.assertEqual(beautified, textwrap.dedent("""\
;
// @import '_common';
@import "_colors";
;
//TODO: eat pizza
// and other things
;
.ClassA,
.ClassB
{;
height: 14px;
font-size: 2em;;
margin: -3px 0 !important;
};
;
;"""))