Skip to content

Instantly share code, notes, and snippets.

@brynary
Created July 19, 2012 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brynary/3145552 to your computer and use it in GitHub Desktop.
Save brynary/3145552 to your computer and use it in GitHub Desktop.
This code was used by Code Climate to figure out where classes, modules and methods live
def in_module(name, file, line, last_line)
if Sexp === name then
name = case name.first
when :colon2 then
name = name.flatten
name.delete :const
name.delete :colon2
name.join("::")
when :colon3 then
name.last.to_s
else
raise "unknown type #{name.inspect}"
end
end
@constant_stack.unshift name
line_location = LineLocation.new(file, line..last_line)
unless @module_line_counts_stack.empty?
old_line_count = @module_line_counts_stack.first.last
@module_line_counts_stack.first[1] = old_line_count - line_location.line_count
end
@module_line_counts_stack.unshift([klass_name, line_location.line_count])
yield
module_name, exclusive_line_count = @module_line_counts_stack.shift
@constant_stack.shift
if !@class_locations[module_name] || (@class_locations[module_name].line_count < exclusive_line_count)
@class_locations[module_name] = line_location
end
end
def in_klass name, file, line, last_line
if Sexp === name then
name = case name.first
when :colon2 then
name = name.flatten
name.delete :const
name.delete :colon2
name.join("::")
when :colon3 then
name.last.to_s
else
raise "unknown type #{name.inspect}"
end
end
@constant_stack.unshift name
line_location = LineLocation.new(file, line..last_line)
if !@class_locations[klass_name] || (@class_locations[klass_name].line_count < line_location.line_count)
@class_locations[klass_name] = line_location
end
unless @module_line_counts_stack.empty?
old_line_count = @module_line_counts_stack.first.last
@module_line_counts_stack.first[1] = old_line_count - line_location.line_count
end
yield
@constant_stack.shift
end
def in_method(name, file, line, last_line)
@method_stack.unshift name.to_s
line_location = LineLocation.new(file, line..last_line)
if !@method_locations[signature] || (@method_locations[signature].line_count < line_location.line_count)
@method_locations[signature] = line_location
end
yield
@method_stack.shift
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment