Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: kneath/kss
base: 82c85ab796
...
head fork: kneath/kss
compare: 441568790b
Checking mergeability… Don't worry, you can still create the pull request.
  • 5 commits
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Showing with 27 additions and 14 deletions.
  1. +26 −13 lib/kss/comment_parser.rb
  2. +1 −1  test/comment_parser_test.rb
View
39 lib/kss/comment_parser.rb
@@ -8,30 +8,31 @@ class CommentParser
# Returns a boolean.
def self.single_line_comment?(line)
- !!(line =~ /[\s+]?\/\//)
+ !!(line =~ /^\s*\/\//)
end
# Returns a boolean.
def self.start_multi_line_comment?(line)
- !!(line =~ /[\s+]?\/\*/)
+ !!(line =~ /^\s*\/\*/)
end
# Returns a boolean.
def self.end_multi_line_comment?(line)
- !!(line =~ /[.+]?\*\//)
+ return false if self.single_line_comment?(line)
+ !!(line =~ /.*\*\//)
end
# Returns a String.
def self.parse_single_line(line)
- matches = line.match /([.+]?\/\/)(.+)?/
- matches[2].to_s
+ cleaned = line.to_s.sub(/\s*\/\//, '')
+ cleaned.rstrip
end
# Returns a String.
def self.parse_multi_line(line)
- cleaned = line.to_s.sub(/\/\*/, '')
+ cleaned = line.to_s.sub(/\s*\/\*/, '')
cleaned = cleaned.sub(/\*\//, '')
- cleaned
+ cleaned.rstrip
end
# Public: Initializes a new comment parser object. Does not parse on
@@ -92,9 +93,9 @@ def parse_blocks
# End a multi-line block if detected
inside_multi_line_block = false if self.class.end_multi_line_comment?(line)
- if !self.class.single_line_comment?(line) && !inside_multi_line_block
- # Store the current comment block
- @blocks << normalize(current_block) if inside_single_line_block || inside_multi_line_block
+ # Store the current block if we're done
+ unless self.class.single_line_comment?(line) || inside_multi_line_block
+ @blocks << normalize(current_block) unless current_block.nil?
inside_single_line_block = false
current_block = nil
@@ -108,17 +109,29 @@ def parse_blocks
# Normalizes the comment block to ignore any consistent preceding
# whitespace. Consistent means the same amount of whitespace on every line
- # of the comment block.
+ # of the comment block. Also strips any whitespace at the start and end of
+ # the whole block.
#
# Returns a String of normalized text.
def normalize(text_block)
return text_block if @options[:preserve_whitespace]
+ # Strip out any preceding [whitespace]* that occur on every line. Not
+ # the smartest, but I wonder if I care.
+ text_block = text_block.gsub(/^(\s*\*+)/, '')
+
# Strip consistent indenting by measuring first line's whitespace
indent_size = nil
unindented = text_block.split("\n").collect do |line|
- indent_size = line.scan(/^\s*/)[0].to_s.size if indent_size.nil?
- line == "" ? "" : line.slice(indent_size, line.length - 1)
+ preceding_whitespace = line.scan(/^\s*/)[0].to_s.size
+ indent_size = preceding_whitespace if indent_size.nil?
+ if line == ""
+ ""
+ elsif indent_size <= preceding_whitespace
+ line.slice(indent_size, line.length - 1)
+ else
+ line
+ end
end.join("\n")
unindented.strip
View
2  test/comment_parser_test.rb
@@ -27,7 +27,7 @@ def setup
end
test "parses the multi-line comment syntax" do
- assert_equal " yuuuup ", Kss::CommentParser.parse_multi_line("/* yuuuup */")
+ assert_equal " yuuuup", Kss::CommentParser.parse_multi_line("/* yuuuup */")
end
test "finds single-line comment styles" do

No commit comments for this range

Something went wrong with that request. Please try again.