Class: Toml::Merge::FileAnalysis

Inherits:
Object
  • Object
show all
Includes:
Ast::Merge::FileAnalyzable
Defined in:
lib/toml/merge/file_analysis.rb

Overview

Analyzes TOML file structure, extracting statements for merging. This is the main analysis class that prepares TOML content for merging.

Examples:

Basic usage

analysis = FileAnalysis.new(toml_source)
analysis.valid? # => true
analysis.statements # => [NodeWrapper, ...]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, signature_generator: nil) ⇒ FileAnalysis

Initialize file analysis

Parameters:

  • source (String)

    TOML source code to analyze

  • signature_generator (Proc, nil) (defaults to: nil)

    Custom signature generator



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/toml/merge/file_analysis.rb', line 25

def initialize(source, signature_generator: nil)
  @source = source
  @lines = source.lines.map(&:chomp)
  @signature_generator = signature_generator
  @errors = []

  # Parse the TOML
  DebugLogger.time("FileAnalysis#parse_toml") { parse_toml }

  @statements = integrate_nodes

  DebugLogger.debug("FileAnalysis initialized", {
    signature_generator: signature_generator ? "custom" : "default",
    statements_count: @statements.size,
    valid: valid?,
  })
end

Instance Attribute Details

#astTreeHaver::Tree? (readonly)

Returns Parsed AST.

Returns:

  • (TreeHaver::Tree, nil)

    Parsed AST



16
17
18
# File 'lib/toml/merge/file_analysis.rb', line 16

def ast
  @ast
end

#errorsArray (readonly)

Returns Parse errors if any.

Returns:

  • (Array)

    Parse errors if any



19
20
21
# File 'lib/toml/merge/file_analysis.rb', line 19

def errors
  @errors
end

Instance Method Details

#fallthrough_node?(value) ⇒ Boolean

Override to detect tree-sitter nodes for signature generator fallthrough

Parameters:

  • value (Object)

    The value to check

Returns:

  • (Boolean)

    true if this is a fallthrough node



52
53
54
# File 'lib/toml/merge/file_analysis.rb', line 52

def fallthrough_node?(value)
  value.is_a?(NodeWrapper) || super
end

#root_nodeNodeWrapper?

Get the root node of the parse tree

Returns:



58
59
60
61
62
# File 'lib/toml/merge/file_analysis.rb', line 58

def root_node
  return unless valid?

  NodeWrapper.new(@ast.root_node, lines: @lines, source: @source)
end

#root_pairsArray<NodeWrapper>

Get all top-level key-value pairs (not in tables)

Returns:



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/toml/merge/file_analysis.rb', line 87

def root_pairs
  return [] unless valid?

  result = []
  @ast.root_node.each do |child|
    next unless child.type.to_s == "pair"

    result << NodeWrapper.new(child, lines: @lines, source: @source)
  end
  result
end

#signature_mapHash<Array, NodeWrapper>

Get a hash mapping signatures to nodes

Returns:



66
67
68
# File 'lib/toml/merge/file_analysis.rb', line 66

def signature_map
  @signature_map ||= build_signature_map
end

#tablesArray<NodeWrapper>

Get all top-level tables (sections) in the TOML document

Returns:



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/toml/merge/file_analysis.rb', line 72

def tables
  return [] unless valid?

  result = []
  @ast.root_node.each do |child|
    child_type = child.type.to_s
    next unless %w[table array_of_tables].include?(child_type)

    result << NodeWrapper.new(child, lines: @lines, source: @source)
  end
  result
end

#valid?Boolean

Check if parse was successful

Returns:

  • (Boolean)


45
46
47
# File 'lib/toml/merge/file_analysis.rb', line 45

def valid?
  @errors.empty? && !@ast.nil?
end