From b404e32c46e16893b6d0c46030182c3ce867b4b4 Mon Sep 17 00:00:00 2001 From: Natalie Weizenbaum Date: Fri, 23 Feb 2018 17:02:22 -0800 Subject: [PATCH] Fix /* formatting to match Ruby Sass --- CHANGELOG.md | 3 +++ lib/src/parse/parser.dart | 8 ++++++++ lib/src/parse/sass.dart | 12 +++++++++++- lib/src/util/character.dart | 5 ++++- 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ff1b9ccd..be044a26 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ * Don't add an extra `*/` to comments in the indented syntax that already have it. +* Improve the formatting of comments that don't start on the same line as the + opening `/*`. + ## 1.0.0-beta.5.2 * Fix a bug where some colors would crash `compressed` mode. diff --git a/lib/src/parse/parser.dart b/lib/src/parse/parser.dart index 0a8c4e3a..c61842e5 100644 --- a/lib/src/parse/parser.dart +++ b/lib/src/parse/parser.dart @@ -48,6 +48,14 @@ abstract class Parser { } } + /// Consumes spaces and tabs. + @protected + void spaces() { + while (!scanner.isDone && isSpaceOrTab(scanner.peekChar())) { + scanner.readChar(); + } + } + /// Consumes and ignores a comment if possible. /// /// Returns whether the comment was consumed. diff --git a/lib/src/parse/sass.dart b/lib/src/parse/sass.dart index caf59da6..cf2ad529 100644 --- a/lib/src/parse/sass.dart +++ b/lib/src/parse/sass.dart @@ -208,7 +208,17 @@ class SassParser extends StylesheetParser { var buffer = new InterpolationBuffer()..write("/*"); var parentIndentation = currentIndentation; while (true) { - if (!first) { + if (first) { + // If the first line is empty, ignore it. + var beginningOfComment = scanner.position; + spaces(); + if (isNewline(scanner.peekChar())) { + _readIndentation(); + buffer.writeCharCode($space); + } else { + buffer.write(scanner.substring(beginningOfComment)); + } + } else { buffer.writeln(); buffer.write(" * "); } diff --git a/lib/src/util/character.dart b/lib/src/util/character.dart index a9307d1a..7ec51c6d 100644 --- a/lib/src/util/character.dart +++ b/lib/src/util/character.dart @@ -12,12 +12,15 @@ const _asciiCaseBit = 0x20; /// Returns whether [character] is an ASCII whitespace character. bool isWhitespace(int character) => - character == $space || character == $tab || isNewline(character); + isSpaceOrTab(character) || isNewline(character); /// Returns whether [character] is an ASCII newline. bool isNewline(int character) => character == $lf || character == $cr || character == $ff; +/// Returns whether [character] is a space or a tab character. +bool isSpaceOrTab(int character) => character == $space || character == $tab; + /// Returns whether [character] is a letter or number. bool isAlphanumeric(int character) => isAlphabetic(character) || isDigit(character);