类 SyntaxSuggest::Capture::BeforeAfterKeywordEnds

显示周围的 kw/end 对

显示这些额外对的目的是为了解决仅匹配一行时出现的歧义情况。

例如

1  class Dog
2    def bark
4    def eat
5    end
6  end

在这种情况下,要么第 2 行可能缺少一个 'end',要么第 4 行可能是一个错误添加的额外行(这种情况很常见)。

当我们检测到上述问题时,它只显示第 2 行存在问题。

2    def bark

显示“邻居”关键字对提供了额外的上下文。

2    def bark
4    def eat
5    end

示例

lines = BeforeAfterKeywordEnds.new(
  block: block,
  code_lines: code_lines
).call()

公共类方法

new(code_lines:, block:) 点击切换源代码
# File lib/syntax_suggest/capture/before_after_keyword_ends.rb, line 41
def initialize(code_lines:, block:)
  @scanner = ScanHistory.new(code_lines: code_lines, block: block)
  @original_indent = block.current_indent
end

公共实例方法

call() 点击切换源代码
# File lib/syntax_suggest/capture/before_after_keyword_ends.rb, line 46
def call
  lines = []

  @scanner.scan(
    up: ->(line, kw_count, end_count) {
      next true if line.empty?
      break if line.indent < @original_indent
      next true if line.indent != @original_indent

      # If we're going up and have one complete kw/end pair, stop
      if kw_count != 0 && kw_count == end_count
        lines << line
        break
      end

      lines << line if line.is_kw? || line.is_end?
      true
    },
    down: ->(line, kw_count, end_count) {
      next true if line.empty?
      break if line.indent < @original_indent
      next true if line.indent != @original_indent

      # if we're going down and have one complete kw/end pair,stop
      if kw_count != 0 && kw_count == end_count
        lines << line
        break
      end

      lines << line if line.is_kw? || line.is_end?
      true
    }
  )
  @scanner.stash_changes

  lines
end