Top Level Namespace

Defined Under Namespace

Modules: Enolib

Constant Summary collapse

RANGE_BEGIN =
0
RANGE_END =
1
RESET =
"\x1b[0m"
BOLD =
"\x1b[1m"
DIM =
"\x1b[2m"
BLACK =
"\x1b[30m"
BRIGHT_BLACK =
"\x1b[90m"
WHITE =
"\x1b[37m"
BRIGHT_WHITE =
"\x1b[97m"
BRIGHT_BLACK_BACKGROUND =
"\x1b[40m"
BRIGHT_RED_BACKGROUND =
"\x1b[101m"
WHITE_BACKGROUND =
"\x1b[47m"
INDICATORS =
{
  display: ' ',
  emphasize: '>',
  indicate: '*',
  question: '?'
}.freeze
GUTTER_STYLE =
{
  display: BRIGHT_BLACK_BACKGROUND,
  emphasize: BLACK + BRIGHT_RED_BACKGROUND,
  indicate: BLACK + WHITE_BACKGROUND,
  question: BLACK + WHITE_BACKGROUND
}.freeze
RANGE_STYLE =
{
  'element_operator': WHITE,
  'escape_begin_operator': WHITE,
  'escape_end_operator': WHITE,
  'item_operator': WHITE,
  'entry_operator': WHITE,
  'section_operator': WHITE,
  'copy_operator': WHITE,
  'deepCopy_operator': WHITE,
  'multiline_field_operator': WHITE,
  'direct_line_continuation_operator': WHITE,
  'spaced_line_continuation_operator': WHITE,
  'key': BOLD + BRIGHT_WHITE,
  'template': BOLD + BRIGHT_WHITE,
  'value': DIM + WHITE
}.freeze

Instance Method Summary collapse

Instance Method Details

#check_field_by_index(field, index) ⇒ Object



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

def check_field_by_index(field, index)
  return false if index < field[:ranges][:line][RANGE_BEGIN]
  return { element: field, instruction: field } if index <= field[:ranges][:line][RANGE_END]
  return false unless field.has_key?(:continuations) &&
                      index <= field[:continuations].last[:ranges][:line][RANGE_END]

  field[:continuations].each do |continuation|
    return { element: field, instruction: nil } if index < continuation[:ranges][:line][RANGE_BEGIN]
    return { element: field, instruction: continuation } if index <= continuation[:ranges][:line][RANGE_END]
  end
end

#check_field_by_line(field, line) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/enolib/lookup.rb', line 38

def (field, line)
  return false if line < field[:line]
  return { element: field, instruction: field } if line == field[:line]
  return false unless field.has_key?(:continuations) &&
                      line <= field[:continuations].last[:line]

  field[:continuations].each do |continuation|
    return { element: field, instruction: continuation } if line == continuation[:line]
    return { element: field, instruction: nil } if line < continuation[:line]
  end
end

#check_fieldset_by_index(fieldset, index) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/enolib/lookup.rb', line 88

def check_fieldset_by_index(fieldset, index)
  return false if index < fieldset[:ranges][:line][RANGE_BEGIN]
  return { element: fieldset, instruction: fieldset } if index <= fieldset[:ranges][:line][RANGE_END]
  return false unless fieldset.has_key?(:entries) &&
                      index <= fieldset[:entries].last[:ranges][:line][RANGE_END]

  fieldset[:entries].each do |entry|
    if index < entry[:ranges][:line][RANGE_BEGIN]
      if entry.has_key?(:comments) && index >= entry[:comments][0][:ranges][:line][RANGE_BEGIN]
        return {
          element: entry,
          instruction: entry[:comments].find { |comment| index <= comment[:ranges][:line][RANGE_END] }
        }
      end

      return { element: fieldset, instruction: nil }
    end

    return { element: entry, instruction: entry } if index <= entry[:ranges][:line][RANGE_END]

    match_in_entry = check_field_by_index(entry, index)

    return match_in_entry if match_in_entry
  end
end

#check_fieldset_by_line(fieldset, line) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/enolib/lookup.rb', line 62

def (fieldset, line)
  return false if line < fieldset[:line]
  return { element: fieldset, instruction: fieldset } if line == fieldset[:line]
  return false unless fieldset.has_key?(:entries) &&
                      line <= fieldset[:entries].last[:line]

  fieldset[:entries].each do |entry|
    return { element: entry, instruction: entry } if line == entry[:line]

    if line < entry[:line]
      if entry.has_key?(:comments) && line >= entry[:comments][0][:line]
        return {
          element: entry,
          instruction: entry[:comments].find { |comment| line == comment[:line] }
        }
      end

      return { element: fieldset, instruction: nil }
    end

    match_in_entry = (entry, line)

    return match_in_entry if match_in_entry
  end
end

#check_in_section_by_index(section, index) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/enolib/lookup.rb', line 207

def check_in_section_by_index(section, index)
  section[:elements].reverse_each do |element|
    if element.has_key?(:comments)
      next if index < element[:comments][0][:ranges][:line][RANGE_BEGIN]

      if index <= element[:comments][-1][:ranges][:line][RANGE_END]
        return {
          element: element,
          instruction: element[:comments].find { |comment| index <= comment[:ranges][:line][RANGE_END] }
        }
      end
    end

    next if index < element[:ranges][:line][RANGE_BEGIN]

    return { element: element, instruction: element } if index <= element[:ranges][:line][RANGE_END]

    case element[:type]
    when :field
      match_in_field = check_field_by_index(element, index)
      return match_in_field if match_in_field
    when :fieldset
      match_in_fieldset = check_fieldset_by_index(element, index)
      return match_in_fieldset if match_in_fieldset
    when :list
      match_in_list = check_list_by_index(element, index)
      return match_in_list if match_in_list
    when :multiline_field_begin
      unless element.has_key?(:template)
        match_in_multiline_field = check_multiline_field_by_index(element, index)
        return match_in_multiline_field if match_in_multiline_field
      end
    when :section
      return check_in_section_by_index(element, index)
    end

    break
  end

  { element: section, instruction: nil }
end

#check_in_section_by_line(section, line) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/enolib/lookup.rb', line 165

def (section, line)
  section[:elements].reverse_each do |element|
    if element.has_key?(:comments)
      next if line < element[:comments][0][:line]

      if line <= element[:comments][-1][:line]
        return {
          element: element,
          instruction: element[:comments].find { |comment| line == comment[:line] }
        }
      end
    end

    next if element[:line] > line

    return { element: element, instruction: element } if element[:line] == line

    case element[:type]
    when :field
      match_in_field = (element, line)
      return match_in_field if match_in_field
    when :fieldset
      match_in_fieldset = (element, line)
      return match_in_fieldset if match_in_fieldset
    when :list
      match_in_list = (element, line)
      return match_in_list if match_in_list
    when :multiline_field_begin
      unless element.has_key?(:template)
        match_in_multiline_field = (element, line)
        return match_in_multiline_field if match_in_multiline_field
      end
    when :section
      return (element, line)
    end

    break
  end

  { element: section, instruction: nil }
end

#check_list_by_index(list, index) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/enolib/lookup.rb', line 139

def check_list_by_index(list, index)
  return false if index < list[:ranges][:line][RANGE_BEGIN]
  return { element: list, instruction: list } if index <= list[:ranges][:line][RANGE_END]
  return false unless list.has_key?(:items) &&
                      index > list[:items].last[:ranges][:line][RANGE_END]

  list[:items].each do |item|
    if index < item[:ranges][:line][RANGE_BEGIN]
      if item.has_key?(:comments) && index >= item[:comments][0][:ranges][:line][RANGE_BEGIN]
        return {
          element: item,
          instruction: item[:comments].find { |comment| index <= comment[:ranges][:line][RANGE_END] }
        }
      end

      return { element: list, instruction: nil }
    end

    return { element: item, instruction: item } if index <= item[:ranges][:line][RANGE_END]

    match_in_item = check_field_by_index(item, index)

    return match_in_item if match_in_item
  end
end

#check_list_by_line(list, line) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/enolib/lookup.rb', line 114

def (list, line)
  return false if line < list[:line]
  return { element: list, instruction: list } if line == list[:line]
  return false unless list.has_key?(:items) && line > list[:items].last[:line]

  list[:items].each do |item|
    return { element: item, instruction: item } if line == item[:line]

    if line < item[:line]
      if item.has_key?(:comments) && line >= item[:comments][0][:line]
        return {
          element: item,
          instruction: item[:comments].find { |comment| line == comment[:line] }
        }
      end

      return { element: list, instruction: nil }
    end

    match_in_item = (item, line)

    return match_in_item if match_in_item
  end
end

#check_multiline_field_by_index(field, index) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/enolib/lookup.rb', line 22

def check_multiline_field_by_index(field, index)
  return false if index < field[:ranges][:line][RANGE_BEGIN] ||
                  index > field[:end][:ranges][:line][RANGE_END]

  if index <= field[:ranges][:line][RANGE_END]
    { element: field, instruction: field }
  elsif index >= field[:end][:ranges][:line][RANGE_BEGIN]
    { element: field, instruction: field[:end] }
  else
    {
      element: field,
      instruction: field[:lines].find { |candidate| index <= candidate[:ranges][:line][RANGE_END] }
    }
  end
end

#check_multiline_field_by_line(field, line) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/enolib/lookup.rb', line 6

def (field, line)
  return false if line < field[:line] ||
                  line > field[:end][:line]

  if line == field[:line]
    { element: field, instruction: field }
  elsif line == field[:end][:line]
    { element: field, instruction: field[:end] }
  else
    {
      element: field,
      instruction: field[:lines].find { |candidate| candidate[:line] == line }
    }
  end
end