Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP attempting to track output locations #256

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/unparser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,15 @@ def initialize(message, node)
# if the node passed is invalid
#
# @api public
def self.unparse(node, comment_array = [])
def self.unparse(node, comment_array = [], &callback)
return '' if node.nil?

Buffer.new.tap do |buffer|
Emitter::Root.new(
buffer,
node,
Comments.new(comment_array)
Comments.new(comment_array),
callback
).write_to_buffer
end.content
end
Expand Down
9 changes: 6 additions & 3 deletions lib/unparser/buffer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ def append(string)
prefix
end
write(string)
self
end

# Append a string without an indentation prefix
Expand All @@ -44,7 +43,6 @@ def append(string)
#
def append_without_prefix(string)
write(string)
self
end

# Increase indent
Expand Down Expand Up @@ -127,8 +125,13 @@ def capture_content
#
# @return [self]
def write(fragment)
start_pos = @content.length
@content << fragment
self
start_pos...@content.length
end

def length
@content.length
end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/unparser/concord.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Concord < Module
include Adamantium, Equalizer.new(:names)

# The maximum number of objects the hosting class is composed of
MAX_NR_OF_OBJECTS = 3
MAX_NR_OF_OBJECTS = 4

# Return names
#
Expand Down
7 changes: 4 additions & 3 deletions lib/unparser/emitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Unparser
# Emitter base class
class Emitter
include Adamantium, AbstractType, Constants, Generation, NodeHelpers
include Anima.new(:buffer, :comments, :node, :local_variable_scope)
include Anima.new(:buffer, :comments, :node, :local_variable_scope, :callback)

public :node

Expand Down Expand Up @@ -67,7 +67,7 @@ def emit_mlhs
# @api private
#
# rubocop:disable Metrics/ParameterLists
def self.emitter(buffer:, comments:, node:, local_variable_scope:)
def self.emitter(buffer:, comments:, node:, local_variable_scope:, callback:)
type = node.type

klass = REGISTRY.fetch(type) do
Expand All @@ -78,7 +78,8 @@ def self.emitter(buffer:, comments:, node:, local_variable_scope:)
buffer: buffer,
comments: comments,
local_variable_scope: local_variable_scope,
node: node
node: node,
callback: callback
)
end
# rubocop:enable Metrics/ParameterLists
Expand Down
2 changes: 1 addition & 1 deletion lib/unparser/emitter/argument.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class Argument < self
private

def dispatch
write(name.to_s)
write_loc(name.to_s, node.location.name.to_range)
end

end # Argument
Expand Down
3 changes: 2 additions & 1 deletion lib/unparser/emitter/class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class Class < self
private

def dispatch
write('class ')
write_loc('class', node.location.keyword.to_range)
write(' ')
visit(name)
emit_superclass
emit_optional_body(body)
Expand Down
8 changes: 5 additions & 3 deletions lib/unparser/emitter/def.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class Def < self
private :body

def dispatch
write('def ')
write_loc('def', node.location.keyword.to_range)
write(' ')
emit_name
emit_arguments
emit_optional_body_ensure_rescue(body)
Expand All @@ -39,7 +40,7 @@ class Instance < self
private

def emit_name
write(name.to_s)
write_loc(name.to_s, node.location.name.to_range)
end

end # Instance
Expand All @@ -57,7 +58,8 @@ def emit_name
conditional_parentheses(!subject_without_parens?) do
visit(subject)
end
write('.', name.to_s)
write_loc('.', node.location.operator.to_range)
write_loc(name.to_s, node.location.name.to_range)
end

def subject_without_parens?
Expand Down
3 changes: 2 additions & 1 deletion lib/unparser/emitter/module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class Module < self
private

def dispatch
write('module ')
write_loc('module', node.location.keyword.to_range)
write(' ')
visit(name)
emit_optional_body(body)
k_end
Expand Down
25 changes: 20 additions & 5 deletions lib/unparser/emitter/primitive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,33 @@ class Primitive < self

children :value

# Emitter for primitives based on Object#inspect
class Inspect < self
class Str < self

handle :sym, :str
handle :str

private

def dispatch
write(value.inspect)
new_loc = buffer.append(value.inspect)
return unless callback

new_loc_adjusted = (new_loc.first + 1)...(new_loc.last - 1)
callback.call(buffer, node.location.expression.to_range, new_loc_adjusted)
end

end # Str

class Sym < self

handle :sym

private

def dispatch
write_loc(value.inspect, node.location.expression.to_range)
end

end # Inspect
end # Sym

# Emitter for complex literals
class Complex < self
Expand Down
2 changes: 1 addition & 1 deletion lib/unparser/emitter/root.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Unparser
class Emitter
# Root emitter a special case
class Root < self
include Concord::Public.new(:buffer, :node, :comments)
include Concord::Public.new(:buffer, :node, :comments, :callback)
include LocalVariableRoot

END_NL = %i[class sclass module begin].freeze
Expand Down
9 changes: 4 additions & 5 deletions lib/unparser/emitter/variable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Variable < self
private

def dispatch
write(name.to_s)
write_loc(name.to_s, node.location.name.to_range)
end

end # Access
Expand All @@ -27,14 +27,14 @@ class Const < self

def dispatch
emit_scope
write(name.to_s)
write_loc(name.to_s, node.location.name.to_range)
end

def emit_scope
return unless scope

visit(scope)
write('::') unless n_cbase?(scope)
write_loc('::', node.location.double_colon.to_range) unless n_cbase?(scope)
end
end

Expand All @@ -48,8 +48,7 @@ class NthRef < self
private

def dispatch
write(PREFIX)
write(name.to_s)
write_loc([PREFIX, name.to_s], node.location.expression.to_range)
end

end # NthRef
Expand Down
16 changes: 13 additions & 3 deletions lib/unparser/generation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,27 @@ def write(*strings)
strings.each(&buffer.method(:append))
end

def write_loc(strings, old_location, new_location = nil)
locs = Array(strings).map(&buffer.method(:append))
return unless callback

if old_location
new_location ||= locs.first.first...locs.last.last
callback.call(buffer, old_location, new_location)
end
end

def k_end
buffer.indent
emit_comments_before(:end)
buffer.unindent
write('end')
write_loc('end', node.location.end.to_range)
end

def parentheses(open = '(', close = ')')
write(open)
write_loc(open, node.location.begin.to_range) if node.location.respond_to?(:begin)
yield
write(close)
write_loc(close, node.location.end.to_range) if node.location.respond_to?(:begin)
end

def indented
Expand Down
2 changes: 1 addition & 1 deletion lib/unparser/writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Writer

def self.included(descendant)
descendant.class_eval do
include Anima.new(:buffer, :comments, :node, :local_variable_scope)
include Anima.new(:buffer, :comments, :node, :local_variable_scope, :callback)

extend DSL
end
Expand Down
4 changes: 2 additions & 2 deletions lib/unparser/writer/send.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def emit_mlhs
end

def emit_selector
write(details.string_selector)
write_loc(details.string_selector, node.location.selector.to_range)
end

def emit_heredoc_reminders
Expand Down Expand Up @@ -59,7 +59,7 @@ def write_as_attribute_assignment?
end

def emit_operator
write(OPERATORS.fetch(node.type))
write_loc(OPERATORS.fetch(node.type), node.location.dot.to_range)
end

def emit_arguments
Expand Down