@@ -20,11 +20,89 @@ def source_location
2020 @method . source_location
2121 end
2222
23+ # =======================================================================
24+ # The following appmap_ functions were copied from
25+ # https://github.com/banister/method_source under the license:
26+ #
27+ # MIT License
28+
29+ # Copyright (c) 2011 John Mair (banisterfiend)
30+
31+ # Permission is hereby granted, free of charge, to any person obtaining
32+ # a copy of this software and associated documentation files (the
33+ # 'Software'), to deal in the Software without restriction, including
34+ # without limitation the rights to use, copy, modify, merge, publish,
35+ # distribute, sublicense, and/or sell copies of the Software, and to
36+ # permit persons to whom the Software is furnished to do so, subject to
37+ # the following conditions:
38+
39+ # The above copyright notice and this permission notice shall be
40+ # included in all copies or substantial portions of the Software.
41+
42+ # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
43+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
44+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
45+ # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
46+ # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
47+ # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
48+ # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
49+
50+ class SourceNotFoundError < StandardError ; end
51+
52+ def appmap_extract_last_comment ( lines )
53+ buffer = [ ]
54+
55+ lines . reverse_each do |line |
56+ # Add any line that is a valid ruby comment, and stop as
57+ # soon as we hit a non comment line.
58+ if ( line =~ /^\s *#/ ) || ( line =~ /^\s *$/ )
59+ buffer . append ( line . lstrip )
60+ else
61+ break
62+ end
63+ end
64+
65+ buffer . reverse . join ( )
66+ end
67+
68+ def appmap_comment_describing ( file , line_number )
69+ lines = file . is_a? ( Array ) ? file : file . each_line . to_a
70+
71+ appmap_extract_last_comment ( lines [ 0 ..( line_number - 2 ) ] )
72+ end
73+
74+ def appmap_lines_for ( file_name , name = nil )
75+ @lines_for_file ||= { }
76+ @lines_for_file [ file_name ] ||= File . readlines ( file_name )
77+ rescue Errno ::ENOENT => e
78+ raise AppMap ::Trace ::RubyMethod ::SourceNotFoundError , "Could not load source for #{ name } : #{ e . message } "
79+ end
80+
81+ def appmap_comment_helper ( source_location , name = nil )
82+ raise AppMap ::Trace ::RubyMethod ::SourceNotFoundError , "Could not locate source for #{ name } !" unless source_location
83+ file , line = *source_location
84+
85+ appmap_comment_describing ( appmap_lines_for ( file ) , line )
86+ end
87+
2388 def comment
24- @method . comment
25- rescue MethodSource ::SourceNotFoundError , Errno ::EINVAL
89+ # use AppMap's optimization in appmap_extract_last_comment to
90+ # extract comments...
91+ appmap_comment_helper ( source_location , defined? ( name ) ? name : inspect )
92+ # ... instead of use MethodSource which hasn't merged
93+ # https://github.com/banister/method_source/pull/78. When/if
94+ # this pr gets merged, AppMap's optimization can be removed
95+ # and the previous implementation of "def comment" can be uncommented:
96+ rescue AppMap ::Trace ::RubyMethod ::SourceNotFoundError , Errno ::EINVAL
2697 nil
2798 end
99+ # =======================================================================
100+
101+ # def comment
102+ # @method.comment
103+ # rescue MethodSource::SourceNotFoundError, Errno::EINVAL
104+ # nil
105+ # end
28106
29107 def package
30108 @package . name
0 commit comments