diff options
author | schneems <[email protected]> | 2023-03-08 18:30:33 -0600 |
---|---|---|
committer | Hiroshi SHIBATA <[email protected]> | 2023-04-06 15:45:29 +0900 |
commit | 2acbcec056f54df9b4d98d4d15b1e9f612ac1432 () | |
tree | a68919e7725c1ca706c5a62c4e63e2354acd041f /lib/syntax_suggest | |
parent | 5487ee4fe8b1311d42367969860468e48667cc87 (diff) |
[ruby/syntax_suggest] Add comments and refactor AroundBlockScan methods
https://.com/ruby/syntax_suggest/commit/cecd12292c
-rw-r--r-- | lib/syntax_suggest/around_block_scan.rb | 136 | ||||
-rw-r--r-- | lib/syntax_suggest/block_expand.rb | 4 | ||||
-rw-r--r-- | lib/syntax_suggest/parse_blocks_from_indent_line.rb | 4 |
3 files changed, 125 insertions, 19 deletions
@@ -38,35 +38,64 @@ module SyntaxSuggest @before_array = [] @stop_after_kw = false - @skip_hidden = false - @skip_empty = false end - def skip(name) - case name - when :hidden? - @skip_hidden = true - when :empty? - @skip_empty = true - else - raise "Unsupported skip #{name}" - end self end def stop_after_kw @stop_after_kw = true self end def scan_while stop_next = false kw_count = 0 end_count = 0 index = before_lines.reverse_each.take_while do |line| next false if stop_next - next true if @skip_hidden && line.hidden? - next true if @skip_empty && line.empty? kw_count += 1 if line.is_kw? end_count += 1 if line.is_end? @@ -86,8 +115,8 @@ module SyntaxSuggest end_count = 0 index = after_lines.take_while do |line| next false if stop_next - next true if @skip_hidden && line.hidden? - next true if @skip_empty && line.empty? kw_count += 1 if line.is_kw? end_count += 1 if line.is_end? @@ -104,6 +133,33 @@ module SyntaxSuggest self end def capture_neigr_context lines = [] kw_count = 0 @@ -145,6 +201,20 @@ module SyntaxSuggest lines end def on_falling_indent last_indent = @orig_indent before_lines.reverse_each do |line| @@ -213,18 +283,31 @@ module SyntaxSuggest self end def scan_neigrs_not_empty scan_while { |line| line.not_empty? && line.indent >= @orig_indent } end def next_up @code_lines[before_index.pred] end def next_down @code_lines[after_index.next] end def scan_adjacent_indent before_after_indent = [] before_after_indent << (next_up&.indent || 0) @@ -236,6 +319,16 @@ module SyntaxSuggest self end def start_at_next_line before_index after_index @@ -244,26 +337,39 @@ module SyntaxSuggest self end def code_block CodeBlock.new(lines: lines) end def lines @code_lines[before_index..after_index] end def before_index @before_index ||= @orig_before_index end def after_index @after_index ||= @orig_after_index end private def before_lines @code_lines[0...before_index] || [] end private def after_lines @code_lines[after_index.next..-1] || [] end @@ -62,7 +62,7 @@ module SyntaxSuggest # as there's no undo (currently). def expand_indent(block) AroundBlockScan.new(code_lines: @code_lines, block: block) - .skip(:hidden?) .stop_after_kw .scan_adjacent_indent .code_block @@ -126,7 +126,7 @@ module SyntaxSuggest # We try to resolve this edge case with `lookahead_balance_one_line` below. def expand_neigrs(block) neigrs = AroundBlockScan.new(code_lines: @code_lines, block: block) - .skip(:hidden?) .stop_after_kw .scan_neigrs_not_empty @@ -36,8 +36,8 @@ module SyntaxSuggest # Builds blocks from bottom up def each_neigr_block(target_line) scan = AroundBlockScan.new(code_lines: code_lines, block: CodeBlock.new(lines: target_line)) - .skip(:empty?) - .skip(:hidden?) .scan_while { |line| line.indent >= target_line.indent } neigrs = scan.code_block.lines |