Permalink
Browse files

Added context and suggestions to output

  • Loading branch information...
1 parent 403c948 commit e92ba75d37e2d7c4bc2b6d9b0c944a636658d2d9 @drbrain drbrain committed Apr 5, 2012
Showing with 71 additions and 9 deletions.
  1. +36 −3 lib/rdoc/generator/spellcheck.rb
  2. +35 −6 test/test_rdoc_generator_spellcheck.rb
@@ -62,15 +62,48 @@ def initialize options # :not-new:
end
##
+ # Returns a report of misspelled words in +comment+. The report contains
+ # each misspelled word and its offset in the comment's text.
+
+ def find_misspelled comment
+ report = []
+
+ comment.text.scan(/[a-z_]+/i) do |word|
+ next if @spell.check word
+
+ report << [word, $`.length + 1]
+ end
+
+ report
+ end
+
+ ##
# Creates the spelling report
def generate files
RDoc::TopLevel.all_classes_and_modules.each do |mod|
mod.comment_location.each do |comment, location|
- comment.text.scan(/[a-z_]+/i) do |word|
- next if @spell.check word
+ misspelled = find_misspelled comment
+
+ next if misspelled.empty?
+
+ puts "#{mod.definition} in #{location.full_name}:"
+ puts
+ misspelled.each do |word, offset|
+ comment.text =~ /.{#{offset - 10}}(.{0,10})#{Regexp.escape word}(.{0,10})/
+
+ before = $1
+ after = $2
+ underline = word.chars.map { |char| "_\b#{char}" }.join
+
+ puts "\"...#{$1}#{underline}#{$2}...\""
+ puts
+
+ suggestions = @spell.suggest(word).first 5
- puts word
+ puts "\"#{word}\" suggestions:"
+ puts "\t#{suggestions.join ', '}"
+ puts
end
end
end
@@ -9,6 +9,8 @@ class TestRDocGeneratorSpellcheck < RDoc::TestCase
def setup
super
+ @top_level = RDoc::TopLevel.new 'file.rb'
+
@SC = RDoc::Generator::Spellcheck
@options = RDoc::Options.new
@options.spell_language = 'en_US'
@@ -39,19 +41,46 @@ def test_class_setup_options_spell_language
assert_equal 'en_GB', options.spell_language
end
+ def test_find_misspelled
+ c = comment 'Hello, this class has real gud spelling!'
+
+ report = @sc.find_misspelled c
+
+ word, offset = report.shift
+
+ assert_equal 'gud', word
+ assert_equal 28, offset
+ end
+
def test_generate
- tl = RDoc::TopLevel.new 'file.rb'
- klass = tl.add_class RDoc::NormalClass, 'Object'
+ klass = @top_level.add_class RDoc::NormalClass, 'Object'
- comment = RDoc::Comment.new 'Hello, this class has real gud spelling!', tl
- klass.add_comment comment, tl
+ c = comment 'Hello, this class has real gud spelling!'
+ klass.add_comment c, @top_level
out, err = capture_io do
- @sc.generate [tl]
+ @sc.generate [@top_level]
end
assert_empty err
- assert_equal "gud\n", out
+
+ suggestions = suggest('gud').join ', '
+
+ expected = <<-EXPECTED
+class Object in file.rb:
+
+"...has real _\bg_\bu_\bd spelling!..."
+
+"gud" suggestions:
+\t#{suggestions}
+
+ EXPECTED
+
+ assert_equal expected, out
+ end
+
+ def suggest word
+ Aspell.new('en_US').suggest(word).first 5
end
end

0 comments on commit e92ba75

Please sign in to comment.