From 878d4f0daeee8ca31b10915296404412071f7f46 Mon Sep 17 00:00:00 2001 From: symwell <111290954+symwell@users.noreply.github.com> Date: Wed, 31 Aug 2022 13:52:52 -0400 Subject: [PATCH 1/7] optimize extract_last_comment to try and get the comment based on the last few lines of the file instead of go over all of them --- lib/method_source/code_helpers.rb | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/method_source/code_helpers.rb b/lib/method_source/code_helpers.rb index ccf054a..d4142f5 100644 --- a/lib/method_source/code_helpers.rb +++ b/lib/method_source/code_helpers.rb @@ -103,12 +103,17 @@ def extract_first_expression(lines, consume=0, &block) # # @param [Array] lines # @return [String] - def extract_last_comment(lines) + def extract_last_comment_core(lines) buffer = "" + found_at_least_one_comment = false lines.each do |line| # Add any line that is a valid ruby comment, # but clear as soon as we hit a non comment line. + if (line =~ /^\s*#/) + found_at_least_one_comment = true + end + if (line =~ /^\s*#/) || (line =~ /^\s*$/) buffer << line.lstrip else @@ -116,6 +121,26 @@ def extract_last_comment(lines) end end + return [ buffer, found_at_least_one_comment ] + end + + def extract_last_comment(lines) + found_at_least_one_comment = false + lines_threshold = 100 + + if lines.size > lines_threshold + # if the last comment is found over fewer lines then don't + # need to go over all lines + index_start = (lines.size-lines_threshold) + index_start = 0 if index_start < 0 + index_stop = (lines.size-1) + buffer, found_at_least_one_comment = extract_last_comment_core(lines[index_start..index_stop]) + end + + if not found_at_least_one_comment + buffer, found_at_least_one_comment = extract_last_comment_core(lines) + end + buffer end From 0c357c086818d67d8c46e48a6aaddf73b760f27d Mon Sep 17 00:00:00 2001 From: symwell <111290954+symwell@users.noreply.github.com> Date: Wed, 31 Aug 2022 14:01:14 -0400 Subject: [PATCH 2/7] don't run twice regex that matches comment --- lib/method_source/code_helpers.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/method_source/code_helpers.rb b/lib/method_source/code_helpers.rb index d4142f5..1134d57 100644 --- a/lib/method_source/code_helpers.rb +++ b/lib/method_source/code_helpers.rb @@ -110,11 +110,12 @@ def extract_last_comment_core(lines) lines.each do |line| # Add any line that is a valid ruby comment, # but clear as soon as we hit a non comment line. - if (line =~ /^\s*#/) + starts_with_comment = (line =~ /^\s*#/) + if starts_with_comment found_at_least_one_comment = true end - if (line =~ /^\s*#/) || (line =~ /^\s*$/) + if starts_with_comment || (line =~ /^\s*$/) buffer << line.lstrip else buffer.replace("") From b58c1ab93a2c3983a29a8c99a5a8516eec86f45a Mon Sep 17 00:00:00 2001 From: symwell <111290954+symwell@users.noreply.github.com> Date: Wed, 31 Aug 2022 14:31:06 -0400 Subject: [PATCH 3/7] fix a couple of things that broke with Ruby 2.7.2p137 --- lib/method_source/code_helpers.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/method_source/code_helpers.rb b/lib/method_source/code_helpers.rb index 1134d57..79eb947 100644 --- a/lib/method_source/code_helpers.rb +++ b/lib/method_source/code_helpers.rb @@ -116,9 +116,16 @@ def extract_last_comment_core(lines) end if starts_with_comment || (line =~ /^\s*$/) - buffer << line.lstrip + # this works + buffer += line.lstrip + # this breaks with Ruby 2.7.2p137: execution doesn't + # continue past it and no exception is raised + # buffer << line.lstrip else - buffer.replace("") + # this often breaks + # buffer.replace("") + # this works + buffer = "" end end From 1fbfb992d30c984d08d9ba78ee471820ea73f628 Mon Sep 17 00:00:00 2001 From: symwell <111290954+symwell@users.noreply.github.com> Date: Wed, 31 Aug 2022 17:35:30 -0400 Subject: [PATCH 4/7] Add global variable @@EXTRACT_LAST_COMMENT_THRESHOLD --- lib/method_source/code_helpers.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/method_source/code_helpers.rb b/lib/method_source/code_helpers.rb index 79eb947..a2e3338 100644 --- a/lib/method_source/code_helpers.rb +++ b/lib/method_source/code_helpers.rb @@ -1,6 +1,9 @@ module MethodSource module CodeHelpers + + @@EXTRACT_LAST_COMMENT_THRESHOLD = 100 + # Retrieve the first expression starting on the given line of the given file. # # This is useful to get module or method source code. @@ -134,12 +137,11 @@ def extract_last_comment_core(lines) def extract_last_comment(lines) found_at_least_one_comment = false - lines_threshold = 100 - if lines.size > lines_threshold + if lines.size > @@EXTRACT_LAST_COMMENT_THRESHOLD # if the last comment is found over fewer lines then don't # need to go over all lines - index_start = (lines.size-lines_threshold) + index_start = (lines.size - @@EXTRACT_LAST_COMMENT_THRESHOLD) index_start = 0 if index_start < 0 index_stop = (lines.size-1) buffer, found_at_least_one_comment = extract_last_comment_core(lines[index_start..index_stop]) From dd0d6d19b2c5d6f8153fdcf0e679175f0ea3ec3c Mon Sep 17 00:00:00 2001 From: symwell <111290954+symwell@users.noreply.github.com> Date: Thu, 1 Sep 2022 11:53:55 -0400 Subject: [PATCH 5/7] extract_last_comment parses comments backwards --- lib/method_source/code_helpers.rb | 50 ++++++------------------------- 1 file changed, 9 insertions(+), 41 deletions(-) diff --git a/lib/method_source/code_helpers.rb b/lib/method_source/code_helpers.rb index a2e3338..7361976 100644 --- a/lib/method_source/code_helpers.rb +++ b/lib/method_source/code_helpers.rb @@ -106,52 +106,20 @@ def extract_first_expression(lines, consume=0, &block) # # @param [Array] lines # @return [String] - def extract_last_comment_core(lines) - buffer = "" - found_at_least_one_comment = false - - lines.each do |line| - # Add any line that is a valid ruby comment, - # but clear as soon as we hit a non comment line. - starts_with_comment = (line =~ /^\s*#/) - if starts_with_comment - found_at_least_one_comment = true - end + def extract_last_comment(lines) + buffer = [] - if starts_with_comment || (line =~ /^\s*$/) - # this works - buffer += line.lstrip - # this breaks with Ruby 2.7.2p137: execution doesn't - # continue past it and no exception is raised - # buffer << line.lstrip + lines.reverse.each do |line| + # Add any line that is a valid ruby comment, and stop as + # soon as we hit a non comment line. + if (line =~ /^\s*#/) || (line =~ /^\s*$/) + buffer.append(line.lstrip) else - # this often breaks - # buffer.replace("") - # this works - buffer = "" + break end end - return [ buffer, found_at_least_one_comment ] - end - - def extract_last_comment(lines) - found_at_least_one_comment = false - - if lines.size > @@EXTRACT_LAST_COMMENT_THRESHOLD - # if the last comment is found over fewer lines then don't - # need to go over all lines - index_start = (lines.size - @@EXTRACT_LAST_COMMENT_THRESHOLD) - index_start = 0 if index_start < 0 - index_stop = (lines.size-1) - buffer, found_at_least_one_comment = extract_last_comment_core(lines[index_start..index_stop]) - end - - if not found_at_least_one_comment - buffer, found_at_least_one_comment = extract_last_comment_core(lines) - end - - buffer + buffer.reverse.join() end # An exception matcher that matches only subsets of SyntaxErrors that can be From ae4f19409e783ee33443bf57edbbc7110cd2d891 Mon Sep 17 00:00:00 2001 From: symwell <111290954+symwell@users.noreply.github.com> Date: Thu, 1 Sep 2022 13:14:48 -0400 Subject: [PATCH 6/7] rm @@EXTRACT_LAST_COMMENT_THRESHOLD --- lib/method_source/code_helpers.rb | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/method_source/code_helpers.rb b/lib/method_source/code_helpers.rb index 7361976..be74a37 100644 --- a/lib/method_source/code_helpers.rb +++ b/lib/method_source/code_helpers.rb @@ -1,9 +1,6 @@ module MethodSource module CodeHelpers - - @@EXTRACT_LAST_COMMENT_THRESHOLD = 100 - # Retrieve the first expression starting on the given line of the given file. # # This is useful to get module or method source code. From b3bd0bccdacbd3246507875c2ff1364ab144a298 Mon Sep 17 00:00:00 2001 From: symwell <111290954+symwell@users.noreply.github.com> Date: Thu, 1 Sep 2022 14:12:59 -0400 Subject: [PATCH 7/7] reverse_each --- lib/method_source/code_helpers.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/method_source/code_helpers.rb b/lib/method_source/code_helpers.rb index be74a37..9ab2c73 100644 --- a/lib/method_source/code_helpers.rb +++ b/lib/method_source/code_helpers.rb @@ -106,7 +106,7 @@ def extract_first_expression(lines, consume=0, &block) def extract_last_comment(lines) buffer = [] - lines.reverse.each do |line| + lines.reverse_each do |line| # Add any line that is a valid ruby comment, and stop as # soon as we hit a non comment line. if (line =~ /^\s*#/) || (line =~ /^\s*$/)