Class: Goodcheck::Buffer

Inherits:
Object
  • Object
show all
Defined in:
lib/goodcheck/buffer.rb

Constant Summary collapse

DISABLE_LINE_PATTERNS =
[
  /\/\/ goodcheck-disable-line$/, #JS, Java, C, ...
  /# goodcheck-disable-line$/, # Ruby, Python, PHP, ...
  /-- goodcheck-disable-line$/, # Haskel, SQL, ...
  /<!-- goodcheck-disable-line -->$/, # HTML, Markdown, ...
  /\/\* goodcheck-disable-line \*\/$/, # CSS, SCSS,
  /\{\s*\/\* goodcheck-disable-line \*\/\s*\}$/, # JSX, ...
  /<%# goodcheck-disable-line %>$/, # ERB, ...
  /' goodcheck-disable-line$/, # VB
].freeze
DISABLE_NEXT_LINE_PATTERNS =
[
  /\/\/ goodcheck-disable-next-line$/, #JS, Java, C, ...
  /# goodcheck-disable-next-line$/, # Ruby, Python, PHP, ...
  /-- goodcheck-disable-next-line$/, # Haskel, SQL, ...
  /<!-- goodcheck-disable-next-line -->$/, # HTML, Markdown, ...
  /\/\* goodcheck-disable-next-line \*\/$/, # CSS, SCSS,
  /\{\s*\/\* goodcheck-disable-next-line \*\/\s*\}$/, # JSX, ...
  /<%# goodcheck-disable-next-line %>$/, # ERB, ...
  /' goodcheck-disable-next-line$/, # VB
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, content:) ⇒ Buffer

Returns a new instance of Buffer.



28
29
30
31
32
# File 'lib/goodcheck/buffer.rb', line 28

def initialize(path:, content:)
  @path = path
  @content = content
  @line_ranges = nil
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



26
27
28
# File 'lib/goodcheck/buffer.rb', line 26

def content
  @content
end

#pathObject (readonly)

Returns the value of attribute path.



25
26
27
# File 'lib/goodcheck/buffer.rb', line 25

def path
  @path
end

Instance Method Details

#line(line_number) ⇒ Object



78
79
80
# File 'lib/goodcheck/buffer.rb', line 78

def line(line_number)
  lines[line_number - 1]
end

#line_disabled?(line_number) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
58
59
60
# File 'lib/goodcheck/buffer.rb', line 50

def line_disabled?(line_number)
  if line_number > 1
    return true if DISABLE_NEXT_LINE_PATTERNS.any? { |pattern| line(line_number - 1).match?(pattern) }
  end

  if line_number <= lines.length
    return DISABLE_LINE_PATTERNS.any? { |pattern| line(line_number).match?(pattern) }
  end

  return false
end

#line_rangesObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/goodcheck/buffer.rb', line 34

def line_ranges
  unless @line_ranges
    @line_ranges = []

    start_position = 0

    content.split(/\n/, -1).each do |line|
      range = start_position..(start_position + line.bytesize)
      @line_ranges << range
      start_position = range.end + 1
    end
  end

  @line_ranges
end

#linesObject



74
75
76
# File 'lib/goodcheck/buffer.rb', line 74

def lines
  @lines ||= content.lines
end

#location_for_position(position) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/goodcheck/buffer.rb', line 62

def location_for_position(position)
  line_index = line_ranges.bsearch_index do |range|
    position <= range.end
  end

  if line_index
    line_number = line_index + 1
    column_number = position - line_ranges[line_index].begin + 1
    [line_number, column_number]
  end
end