Class: Tryouts::CLI::LineSpecParser

Inherits:
Object
  • Object
show all
Defined in:
lib/tryouts/cli/line_spec_parser.rb

Class Method Summary collapse

Class Method Details

.parse(path_with_spec) ⇒ Object

Parse a file path with optional line specification Supports formats:

- file.rb:19        (single line)
- file.rb:19-45     (range)
- file.rb:L19       (GitHub-style single line)
- file.rb:L19-45    (GitHub-style range)
- file.rb:L19-L45   (GitHub-style range with L on both)

Returns [filepath, line_spec] where line_spec is nil or a Range/Integer



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/tryouts/cli/line_spec_parser.rb', line 15

def self.parse(path_with_spec)
  return [path_with_spec, nil] unless path_with_spec.include?(':')

  # Split on the last colon to handle paths with colons
  parts = path_with_spec.rpartition(':')
  filepath = parts[0]
  line_spec_str = parts[2]

  # If the filepath is empty, it means there was no colon or the input started with colon
  return [path_with_spec, nil] if filepath.empty?

  # If the "line spec" part looks like a Windows drive letter, this isn't a line spec
  return [path_with_spec, nil] if line_spec_str =~ /\A[a-zA-Z]\z/

  # Parse the line specification
  line_spec = parse_line_spec(line_spec_str)

  # If we couldn't parse it, treat the whole thing as a filepath
  return [path_with_spec, nil] if line_spec.nil?

  [filepath, line_spec]
end