class 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 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 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