Class: Refract::Formatter

Inherits:
BasicVisitor show all
Defined in:
lib/refract/formatter.rb

Constant Summary collapse

Result =
Data.define(:source, :source_map)

Instance Method Summary collapse

Methods inherited from BasicVisitor

visit, #visit, #visit_each

Constructor Details

#initialize(starting_line: 1) ⇒ Formatter

Returns a new instance of Formatter.



7
8
9
10
11
12
13
# File 'lib/refract/formatter.rb', line 7

def initialize(starting_line: 1)
	super()
	@buffer = []
	@source_map = []
	@current_line = starting_line
	@indent = 0
end

Instance Method Details

#around_visit(node) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/refract/formatter.rb', line 15

def around_visit(node)
	if (start_line = node.start_line)
		@source_map[@current_line] = start_line
	end

	super
end

#format_node(node) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/refract/formatter.rb', line 23

def format_node(node)
	visit(node)

	Result.new(
		source: @buffer.join,
		source_map: @source_map,
	)
end