Permalink
Browse files

Correctly placed elipsis when the correction occurs near the beginnin…

…g and end of the comment
  • Loading branch information...
1 parent ffa36c7 commit 53636c77a5afa0b01f24a100abd5399e0b1ced89 @drbrain drbrain committed Apr 5, 2012
Showing with 39 additions and 4 deletions.
  1. +7 −4 lib/rdoc/generator/spellcheck.rb
  2. +32 −0 test/test_rdoc_generator_spellcheck.rb
@@ -103,15 +103,18 @@ def generate files
end
def suggestion_text text, word, offset
- text =~ /.{#{offset - 10}}(.{0,10})#{Regexp.escape word}(.{0,10})/
+ prefix = offset - 10
+ prefix = 0 if prefix < 0
- before = $1
- after = $2
+ text =~ /\A.{#{prefix}}(.{0,10})#{Regexp.escape word}(.{0,10})/
+
+ before = "#{prefix.zero? ? nil : '...'}#{$1}"
+ after = "#{$2}#{$2.empty? ? nil : '...'}"
underline = word.chars.map { |char| "_\b#{char}" }.join
suggestions = @spell.suggest(word).first 5
<<-TEXT
-"...#{before}#{underline}#{after}..."
+"#{before}#{underline}#{after}"
"#{word}" suggestions:
\t#{suggestions.join ', '}
@@ -100,6 +100,38 @@ def test_suggestion_text
assert_equal expected, out
end
+ def test_suggestion_text_start
+ out = @sc.suggestion_text 'gud night world, see you tomorrow', 'gud', 0
+
+ suggestions = suggest('gud').join ', '
+
+ expected = <<-EXPECTED
+"_\bg_\bu_\bd night wor..."
+
+"gud" suggestions:
+\t#{suggestions}
+
+ EXPECTED
+
+ assert_equal expected, out
+ end
+
+ def test_suggestion_text_end
+ out = @sc.suggestion_text 'you did real gud', 'gud', 14
+
+ suggestions = suggest('gud').join ', '
+
+ expected = <<-EXPECTED
+"...did real _\bg_\bu_\bd"
+
+"gud" suggestions:
+\t#{suggestions}
+
+ EXPECTED
+
+ assert_equal expected, out
+ end
+
def suggest word
Aspell.new('en_US').suggest(word).first 5
end

0 comments on commit 53636c7

Please sign in to comment.