Module: Act::ArgumentParser

Defined in:
lib/act/argument_parser.rb

Defined Under Namespace

Classes: FileInformation

Class Method Summary collapse

Class Method Details

.parse_file_information(file, context_lines = nil) ⇒ FileInformation

Returns:



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/act/argument_parser.rb', line 7

def self.parse_file_information(file, context_lines = nil)
  if file && file != ''
    if file =~ /:/
      components = file.split(':')
      path = components[0]
      line = components[1]

      if line
        if line =~ /^\d+$/
          highlight_line = line.to_i
          from_line = highlight_line - context_lines
          to_line = highlight_line + context_lines
        elsif line =~ /^\d+-\d+$/
          line_components = line.split('-')
          from_line = line_components[0].to_i
          to_line = line_components[1].to_i
        elsif line =~ /^\d+\+\d+$/
          line_components = line.split('+')
          highlight_line = line_components[0].to_i
          context = line_components[1].to_i
          from_line = highlight_line - context
          to_line = highlight_line + context
        else
          UI.warn 'Unable to parse line data'
        end
      end
    elsif file =~ /#/
      components = file.split('#')
      path = components[0]
      line = components[1]

      if line
        if line =~ /^L\d+$/
          highlight_line = line[1..-1].to_i
          from_line = highlight_line - context_lines
          to_line = highlight_line + context_lines
        elsif line =~ /^L\d+-L\d+$/
          line_components = line.split('-')
          from_line = line_components[0][1..-1].to_i
          to_line = line_components[1][1..-1].to_i
        else
          UI.warn 'Unable to parse line data'
        end
      end
    else
      path = file
    end

    result = FileInformation.new
    result.path = path
    result.from_line = from_line
    result.to_line = to_line
    result.highlight_line = highlight_line
    result
  end
end