class Pronto::Spell

Constants

CONFIG_FILE

Public Instance Methods

ignored_words() click to toggle source
# File lib/pronto/spell.rb, line 10
def ignored_words
  @ignored_words ||= begin
    Set.new(spelling_config['ignored_words'].to_a.flatten.map(&:downcase))
  end
end
keywords() click to toggle source
# File lib/pronto/spell.rb, line 16
def keywords
  @keywords ||= begin
    Set.new(spelling_config['only_lines_matching'].to_a.flatten.map(&:downcase))
  end
end
run() click to toggle source
# File lib/pronto/spell.rb, line 22
def run
  return [] if !@patches || @patches.count.zero?

  @patches
    .select { |patch| patch.additions.positive? }
    .map { |patch| inspect(patch) }
    .flatten.compact
end

Private Instance Methods

inspect(patch) click to toggle source
# File lib/pronto/spell.rb, line 33
def inspect(patch)
  patch.added_lines.map do |line|
    if keywords.any? && !keywords_regexp.match(line.content)
      next
    end

    words = line.content.scan(/([A-Z]{2,})|([A-Z]{0,1}[a-z]+)/)
      .flatten.compact.uniq

    words
      .select { |word| misspelled?(word) }
      .map { |word| new_message(word, line) }
  end
end
keywords_regexp() click to toggle source
# File lib/pronto/spell.rb, line 76
def keywords_regexp
  @keywords_regexp ||= %r{#{keywords.to_a.join('|')}}
end
language() click to toggle source
# File lib/pronto/spell.rb, line 80
def language
  spelling_config['language'] || 'en_US'
end
lintable_word?(word) click to toggle source
# File lib/pronto/spell.rb, line 104
def lintable_word?(word)
  (min_word_length..max_word_length).cover?(word.length) &&
    !ignored_words.include?(word.downcase)
end
max_suggestions_number() click to toggle source
# File lib/pronto/spell.rb, line 96
def max_suggestions_number
  spelling_config['max_suggestions_number'] || 3
end
max_word_length() click to toggle source
# File lib/pronto/spell.rb, line 92
def max_word_length
  spelling_config['max_word_length'] || Float::INFINITY
end
min_word_length() click to toggle source
# File lib/pronto/spell.rb, line 88
def min_word_length
  spelling_config['min_word_length'] || 5
end
misspelled?(word) click to toggle source
# File lib/pronto/spell.rb, line 100
def misspelled?(word)
  lintable_word?(word) && !speller.correct?(word)
end
new_message(word, line) click to toggle source
# File lib/pronto/spell.rb, line 48
def new_message(word, line)
  path = line.patch.delta.new_file[:path]
  level = :warning

  suggestions = speller.suggestions(word)

  msg = %("#{word}" might not be spelled correctly.)
  if suggestions.any?
    suggestions_text = suggestions[0..max_suggestions_number - 1].join(', ')
    msg += " Spelling suggestions: #{suggestions_text}"
  end

  Message.new(path, line, level, msg, nil, self.class)
end
speller() click to toggle source
# File lib/pronto/spell.rb, line 63
def speller
  @speller ||= FFI::Aspell::Speller.new(
    language, 'sug-mode': suggestion_mode
  )
end
spelling_config() click to toggle source
# File lib/pronto/spell.rb, line 69
def spelling_config
  @spelling_config ||= begin
    config_path = File.join(repo_path, CONFIG_FILE)
    File.exist?(config_path) ? YAML.load_file(config_path) : {}
  end
end
suggestion_mode() click to toggle source
# File lib/pronto/spell.rb, line 84
def suggestion_mode
  spelling_config['suggestion_mode'] || 'fast'
end