Class: CodeRay::Encoders::LinesOfCode

Inherits:
Encoder
  • Object
show all
Defined in:
lib/coderay/encoders/lines_of_code.rb

Overview

Counts the LoC (Lines of Code). Returns an Integer >= 0.

Alias: :loc

Everything that is not comment, markup, doctype/shebang, or an empty line, is considered to be code.

For example,

  • HTML files not containing JavaScript have 0 LoC

  • in a Java class without comments, LoC is the number of non-empty lines

A Scanner class should define the token kinds that are not code in the KINDS_NOT_LOC constant, which defaults to [:comment, :doctype].

Constant Summary collapse

NON_EMPTY_LINE =
/^\s*\S.*$/

Constants inherited from Encoder

Encoder::DEFAULT_OPTIONS

Instance Attribute Summary

Attributes inherited from Encoder

#options, #token_stream

Instance Method Summary collapse

Methods inherited from Encoder

const_missing, #encode, #encode_stream, #encode_tokens, #file_extension, #initialize, streamable?, #to_proc

Methods included from Plugin

#helper, #included, #plugin_host, #plugin_id, #register_for, #title

Constructor Details

This class inherits a constructor from CodeRay::Encoders::Encoder

Instance Method Details

#compile(tokens, options) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/coderay/encoders/lines_of_code.rb', line 24

def compile tokens, options
  if scanner = tokens.scanner
    kinds_not_loc = scanner.class::KINDS_NOT_LOC
  else
    warn ArgumentError, 'Tokens have no scanner.' if $VERBOSE
    kinds_not_loc = CodeRay::Scanners::Scanner::KINDS_NOT_LOC
  end
  code = tokens.token_class_filter :exclude => kinds_not_loc
  @loc = code.text.scan(NON_EMPTY_LINE).size
end

#finish(options) ⇒ Object



35
36
37
# File 'lib/coderay/encoders/lines_of_code.rb', line 35

def finish options
  @loc
end