Class: NodeMutation::IndentAction

Inherits:
Action
  • Object
show all
Defined in:
lib/node_mutation/action/indent_action.rb

Overview

IndentAction to indent code.

Instance Attribute Summary

Attributes inherited from Action

#actions, #end, #start, #type

Instance Method Summary collapse

Methods inherited from Action

#process, #to_struct

Constructor Details

#initialize(node, tab_size = 1, adapter:) ⇒ IndentAction

Initialize a IndentAction.

Parameters:



10
11
12
13
14
# File 'lib/node_mutation/action/indent_action.rb', line 10

def initialize(node, tab_size = 1, adapter:)
  super(node, nil, adapter: adapter)
  @tab_size = tab_size
  @type = :replace
end

Instance Method Details

#new_codeString

The rewritten source code with proper indent.

Returns:

  • (String)

    rewritten code.



19
20
21
22
23
# File 'lib/node_mutation/action/indent_action.rb', line 19

def new_code
  source = @adapter.get_source(@node)
  source.each_line.map { |line| (' ' * NodeMutation.tab_width * @tab_size) + line }
        .join
end