class SublimeDSL::TextMate::Grammar::BeginEndRule

A 'begin/end' rule.

Attributes

captures[R]
content_scope[RW]
from[RW]
patterns[RW]
to[RW]
to_last[RW]

Public Class Methods

new() click to toggle source
# File lib/sublime_dsl/textmate/grammar.rb, line 152
def initialize
  @patterns = []
  @captures = {}
end

Public Instance Methods

add_capture(scope, index, captures) click to toggle source
# File lib/sublime_dsl/textmate/grammar.rb, line 208
def add_capture(scope, index, captures)
  captures[index] = scope
  h = {}
  captures.keys.sort.each do |i|
    h[i] = captures[i]
  end
  captures.clear
  captures.merge! h
end
complete!(grammar) click to toggle source
# File lib/sublime_dsl/textmate/grammar.rb, line 157
def complete!(grammar)
  captures.each_pair do |index, scope|
    fscope = from.captures[index]
    tscope = to.captures[index]
    if fscope
      if tscope == fscope
        if fscope == scope
          # from scope == to scope == common scope => just keep common
          from.captures.delete(index)
          to.captures.delete(index)
        else
          # from scope == to scope != common scope => set common = from/to
          warn "grammar #{grammar}: 'both' capture #{index} => #{scope.inspect} replaced by 'from/to' capture #{index} => #{fscope.inspect}"
          captures[index] = fscope
          from.captures.delete index
          to.captures.delete index
        end
      elsif tscope.nil?
        if fscope == scope
          # from scope == common scope, no 'to' scope => just keep common
          from.captures.delete(index)
        else
          # from scope != common scope, no 'to' scope => common become 'to'
          warn "grammar #{grammar}: 'both' capture #{index} => #{scope.inspect} moved to 'to' ('from' has #{index} => #{fscope.inspect})"
          add_capture captures.delete(index), index, to.captures
        end
      else
        # from scope != to scope => ignore common
        warn "grammar #{grammar}: 'both' capture #{index} => #{scope.inspect} ignored: 'from' and 'to' already given"
        captures.delete(index)
      end
    elsif tscope.nil?
      # both fscope & tscope nil: ok
    else
      if tscope == scope
        # to scope == common scope, no 'from' scope => just keep common
        to.captures.delete(index)
      else
        # to scope != common scope, no 'from' scope => common become 'from'
        warn "grammar #{grammar}: 'both' capture #{index} => #{scope.inspect} moved to 'from' ('to' has #{index} => #{tscope.inspect})"
        add_capture captures.delete(index), index, from.captures
      end
    end
  end
  if !from.captures.empty? && from.captures == to.captures
    captures.merge! from.captures
    from.captures.clear
    to.captures.clear
  end
end