Module: Enolib::Selections

Defined in:
lib/enolib/errors/selections.rb

Constant Summary collapse

RANGE_BEGIN =
0
RANGE_END =
1
DOCUMENT_BEGIN =
{
  from: { column: 0, index: 0, line: 0 },
  to: { column: 0, index: 0, line: 0 }
}.freeze

Class Method Summary collapse

Class Method Details

.cursor(instruction, range, position) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/enolib/errors/selections.rb', line 30

def self.cursor(instruction, range, position)
  index = instruction[:ranges][range][position]

  {
    column: index - instruction[:ranges][:line][RANGE_BEGIN],
    index: index,
    line: instruction[:line]
  }
end

.last_in(element) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/enolib/errors/selections.rb', line 12

def self.last_in(element)
  if (element[:type] == :field ||
      element[:type] == :list_item ||
      element[:type] == :fieldset_entry) && element.has_key?(:continuations)
    element[:continuations].last
  elsif element[:type] == :list && element.has_key?(:items)
    last_in(element[:items].last)
  elsif element[:type] == :fieldset && element.has_key?(:entries)
    last_in(element[:entries].last)
  elsif element[:type] == :multiline_field_begin
    element[:end]
  elsif element[:type] == :section && !element[:elements].empty?
    last_in(element[:elements].last)
  else
    element
  end
end

.select_comments(element) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/enolib/errors/selections.rb', line 51

def self.select_comments(element)
  comments = element[:comments]

  if comments.length == 1
    if comments.first.has_key?(:comment)
      selection(comments.first, :comment, RANGE_BEGIN, RANGE_END)
    else
      selection(comments.first, :line, RANGE_BEGIN, RANGE_END)
    end
  elsif comments.length > 1
    selection(comments.first, :line, RANGE_BEGIN, comments.last, :line, RANGE_END)
  else
    selection(element, :line, RANGE_BEGIN)
  end
end

.select_element(element) ⇒ Object



67
68
69
# File 'lib/enolib/errors/selections.rb', line 67

def self.select_element(element)
  selection(element, :line, RANGE_BEGIN, last_in(element), :line, RANGE_END)
end

.select_key(element) ⇒ Object



71
72
73
# File 'lib/enolib/errors/selections.rb', line 71

def self.select_key(element)
  selection(element, :key, RANGE_BEGIN, RANGE_END)
end

.select_line(element) ⇒ Object



75
76
77
# File 'lib/enolib/errors/selections.rb', line 75

def self.select_line(element)
  selection(element, :line, RANGE_BEGIN, RANGE_END)
end

.select_template(element) ⇒ Object



79
80
81
# File 'lib/enolib/errors/selections.rb', line 79

def self.select_template(element)
  selection(element, :template, RANGE_BEGIN, RANGE_END)
end

.selection(instruction, range, position, *to) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/enolib/errors/selections.rb', line 40

def self.selection(instruction, range, position, *to)
  to_instruction = to.find { |argument| argument.is_a?(Hash) } || instruction
  to_range = to.find { |argument| argument.is_a?(Symbol) } || range
  to_position = to.find { |argument| argument.is_a?(Numeric) } || position

  {
    from: cursor(instruction, range, position),
    to: cursor(to_instruction, to_range, to_position)
  }
end