Skip to content

Commit

Permalink
Added context and suggestions to output
Browse files Browse the repository at this point in the history
  • Loading branch information
drbrain committed Apr 5, 2012
1 parent 403c948 commit e92ba75
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 9 deletions.
39 changes: 36 additions & 3 deletions lib/rdoc/generator/spellcheck.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,49 @@ def initialize options # :not-new:
@spell = Aspell.new @options.spell_language
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
Expand Down
41 changes: 35 additions & 6 deletions test/test_rdoc_generator_spellcheck.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e92ba75

Please sign in to comment.