Sha256: 5ba72497a14c416d79788d04ba5d1e679af3e4123d2b5c545ca51bece76ff818

Contents?: true

Size: 1.36 KB

Versions: 8

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

module Phlex
	class Compiler
		class Formatter < SyntaxTree::Formatter
			def genspace
				-> (n) { "\t" * (n / 2) }
			end

			def format(node, stackable: true)
				stack << node if stackable
				doc = node.format(self)
				stack.pop if stackable
				doc
			end

			def flush
				text "" if @open_string_append

				super
			end

			def breakable(*args, **kwargs)
				if !@texting && @open_string_append
					@broken = kwargs
				else
					super
				end
			end

			def chain_append(&block)
				@appending = true

				if @open_string_append
					text '" << '
				elsif @open_chain_append
					text " << "
				else
					text "@_target << "
				end

				@open_string_append = false
				@open_chain_append = true

				yield(self)

				@appending = false
			end

			def append(&block)
				@appending = true

				unless @open_string_append
					if @open_chain_append
						text ' << "'
					else
						text '@_target << "'
					end

					@open_chain_append = false
					@open_string_append = true
				end

				yield(self)

				@appending = false
			end

			def text(value, ...)
				@texting = true

				unless @appending
					if @open_string_append
						super('"')
						@open_string_append = false
					end

					@open_chain_append = false

					breakable(**@broken) if @broken
				end

				@broken = false

				super

				@texting = false
			end
		end
	end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
phlex-0.5.3 lib/phlex/compiler/formatter.rb
phlex-0.5.2 lib/phlex/compiler/formatter.rb
phlex-0.5.1 lib/phlex/compiler/formatter.rb
phlex-0.5.0 lib/phlex/compiler/formatter.rb
phlex-0.4.0 lib/phlex/compiler/formatter.rb
phlex-0.3.2 lib/phlex/compiler/formatter.rb
phlex-0.3.1 lib/phlex/compiler/formatter.rb
phlex-0.3.0 lib/phlex/compiler/formatter.rb