Class: Json::Merge::SourceLocator

Inherits:
Object
  • Object
show all
Defined in:
lib/json/merge/source_locator.rb,
sig/json/merge.rbs

Overview

Locates JSON object-pair owners that can be copied as whole source lines.

Defined Under Namespace

Classes: Range

Instance Method Summary collapse

Constructor Details

#initialize(source, dialect:, backend: nil) ⇒ SourceLocator

Returns a new instance of SourceLocator.

Parameters:

  • source (String)
  • dialect: (Symbol)
  • backend: (String, Symbol, nil) (defaults to: nil)


9
10
11
12
13
14
15
16
17
# File 'lib/json/merge/source_locator.rb', line 9

def initialize(source, dialect:, backend: nil)
  @source = source
  @lines = source.lines
  Json::Merge.register_backend!
  backend_id = backend.to_s.empty? ? Json::Merge::TREE_SITTER_BACKEND.id : backend.to_s
  @analysis = TreeHaver.with_backend(backend_id) do
    FileAnalysis.new(source, dialect: dialect)
  end
end

Instance Method Details

#pair_range(path) ⇒ Range?

Parameters:

  • path (String)

Returns:



19
20
21
22
23
24
25
# File 'lib/json/merge/source_locator.rb', line 19

def pair_range(path)
  pair = pair_for(path)
  return unless pair
  return unless whole_line_owner?(pair)

  Range.new(start_line: pair.start_line, end_line: pair.end_line)
end