Sha256: 212577d90276ad82c537c2bcc9335cd1ec6156de2ed310cb3ed8e0cde0f446b5

Contents?: true

Size: 972 Bytes

Versions: 10

Compression:

Stored size: 972 Bytes

Contents

# frozen_string_literal: true

# PrependAction to prepend code to the top of node body.
class NodeMutation::PrependAction < NodeMutation::Action
  # Initialize an PrependAction.
  #
  # @param node [Node]
  # @param code [String] new code to prepend.
  # @param adapter [NodeMutation::Adapter]
  def initialize(node, code, adapter:)
    super(node, code, adapter: adapter)
    @type = :insert
  end

  private

  # Calculate the begin and end positions.
  def calculate_position
    node_start = @adapter.get_start(@node)
    node_source = @adapter.get_source(@node)
    first_line = node_source.split("\n").first
    @start = first_line.end_with?("do") ? node_start + first_line.rindex("do") + "do".length : node_start + first_line.length
    @end = @start
  end

  # Indent of the node.
  #
  # @param node [Parser::AST::Node]
  # @return [String] n times whitesphace
  def indent(node)
    ' ' * (@adapter.get_start_loc(node).column + NodeMutation.tab_width)
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
node_mutation-1.24.0 lib/node_mutation/action/prepend_action.rb
node_mutation-1.23.3 lib/node_mutation/action/prepend_action.rb
node_mutation-1.23.2 lib/node_mutation/action/prepend_action.rb
node_mutation-1.23.1 lib/node_mutation/action/prepend_action.rb
node_mutation-1.23.0 lib/node_mutation/action/prepend_action.rb
node_mutation-1.22.4 lib/node_mutation/action/prepend_action.rb
node_mutation-1.22.3 lib/node_mutation/action/prepend_action.rb
node_mutation-1.22.2 lib/node_mutation/action/prepend_action.rb
node_mutation-1.22.1 lib/node_mutation/action/prepend_action.rb
node_mutation-1.22.0 lib/node_mutation/action/prepend_action.rb