diff --git a/.rubocop.yml b/.rubocop.yml index 85570542..0f5ecf44 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -18,7 +18,7 @@ Layout/LineLength: Max: 120 Metrics/BlockLength: - ExcludedMethods: + AllowedMethods: - describe - it - context diff --git a/lib/appmap/trace.rb b/lib/appmap/trace.rb index d5046777..43c7f6ab 100644 --- a/lib/appmap/trace.rb +++ b/lib/appmap/trace.rb @@ -21,8 +21,11 @@ def source_location end def comment - @method.comment - rescue MethodSource::SourceNotFoundError, Errno::EINVAL + return nil if source_location.nil? || source_location.first.start_with?('<') + + # Do not use method_source's comment method because it's slow + @comment ||= RubyMethod.last_comment *source_location + rescue Errno::EINVAL, Errno::ENOENT nil end @@ -37,6 +40,21 @@ def name def labels @package.labels end + + private + + # Read file and get last comment before line. + def self.last_comment(file, line_number) + File.open(file) do |f| + buffer = [] + f.each_line.lazy.take(line_number - 1).reverse_each do |line| + break unless (line =~ /^\s*#/) || (line =~ /^\s*$/) + + buffer << line.lstrip + end + buffer.reverse.join + end + end end class Tracing