Module: SnippetCli::YamlLineResolver

Defined in:
lib/snippet_cli/yaml_line_resolver.rb

Overview

Resolves a JSON Pointer (e.g. “/matches/354”) to a 1-based line number in a YAML file by navigating the Psych AST node tree.

Class Method Summary collapse

Class Method Details

.resolve(file, pointer) ⇒ Object

Returns the 1-based line number for the given JSON pointer, or nil if the pointer is empty, invalid, or cannot be resolved in the file.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/snippet_cli/yaml_line_resolver.rb', line 11

def self.resolve(file, pointer)
  return nil if pointer.to_s.empty?

  segments = pointer.to_s.sub(%r{\A/}, '').split('/')
  return nil if segments.empty?

  tree = Psych.parse_file(file)
  node = navigate(tree.root, segments)
  node&.start_line&.+(1)
rescue StandardError
  nil
end