class SublimeDSL::SublimeText::KeyMap::Context::Condition

A condition. There are 3 types of conditions:

Attributes

left[R]
match_all[R]
operator[R]
right[R]

Public Class Methods

from_dsl(args) click to toggle source

Returns a new condition from an array of captures.

# File lib/sublime_dsl/sublime_text/keymap.rb, line 241
def self.from_dsl(args)
  passed = args.dup
  match_all = args.first == :all
  if match_all
    args.shift
    args.empty? and raise Error, "'all' is not a valid condition"
  else
    args.empty? and raise Error, "condition missing"
  end
  left = args.shift.to_s
  if left == 'setting' && !args.empty?
    left = 'setting.' << args.shift.to_s
  end
  case args.length
  when 0
    op = nil
    right = nil
  when 2
    case args.first
    when :'=='
      if args.last.is_a?(Symbol)
        op = nil
        args[-1] = args.last.to_s
      else
        op = 'equal'
      end
    when :'!='
      op = 'not_equal'
    when :is
      op = nil
    when :'=~'
      op = 'regex_contains'
    when :'!~'
      op = 'not_regex_contains'
    when :regex_match, :not_regex_match
      op = args.first
    else
      raise Error, "invalid operator: #{args.first.inspect}"
    end
    right = args.last
    right = right.source if right.is_a?(Regexp)
  else
    raise Error, "expected [all.]<setting> [operator] [value]: #{passed.map(&:to_s).join(' ')}"
  end
  new(left, op, right, match_all)
end
new(left, operator, right, match_all) click to toggle source
# File lib/sublime_dsl/sublime_text/keymap.rb, line 290
def initialize(left, operator, right, match_all)
  @left = left
  @operator = operator && operator.to_sym
  if operator && operator =~ /regex/
    @right = Tools::RegexpWannabe.new(right)
  else
    @right = right
  end
  @match_all = match_all ? true : nil   # normalize match_all
end

Public Instance Methods

fixmes() click to toggle source
# File lib/sublime_dsl/sublime_text/keymap.rb, line 301
def fixmes
  right && right.is_a?(Tools::RegexpWannabe) ? right.fixmes : []
end
to_dsl() click to toggle source

DSL for the condition. The 3 types are rendered as:

left operator right

Same condition, with operator mapped to its ruby equivalent:

equal

==

not_equal

!=

regex_contains

=~

not_regex_contains

!~

regex_match

.regex_match + comment

not_regex_match

.not_regex_match + comment

The comment is 'could use =~ with A and z'

left right
  • if right is a String: left == :right

  • otherwise: left is right

left

left

If match_all is true, left is prefixed by “all.”.

# File lib/sublime_dsl/sublime_text/keymap.rb, line 326
def to_dsl
  ( match_all ? 'all.' : '' ) <<
  left << (
    case operator
    when nil
      if right.nil?
        ''
      elsif right.is_a?(String)
        ' == ' + right.to_sym.inspect
      else
        ' is ' << right.inspect
      end
    when :equal
      ' == ' << right.inspect
    when :not_equal
      ' != ' << right.inspect
    when :regex_contains
      ' =~ ' << right.inspect
    when :not_regex_contains
      ' !~ ' << right.inspect
    when :regex_match
      ' .regex_match ' << right.inspect(true) << ' # could use =~ with \A and \z'
    when :not_regex_match
      ' .not_regex_match ' << right.inspect(true) << ' # could use !~ with \A and \z'
    else
      raise Error, "unknown operator: #{operator.inspect} #{right.to_s}"
    end
  )
end
Also aliased as: to_s
to_h() click to toggle source
# File lib/sublime_dsl/sublime_text/keymap.rb, line 356
def to_h
  h = { 'key' => left }
  h['operator'] = operator if operator
  unless right.nil?
    h['operand'] = right.is_a?(Tools::RegexpWannabe) ? right.to_s(true) : right
  end
  h['match_all'] = match_all if match_all
  h
end
to_json() click to toggle source
# File lib/sublime_dsl/sublime_text/keymap.rb, line 366
def to_json
  JSON.generate(to_h)
end
to_s()
Alias for: to_dsl
value_id() click to toggle source
# File lib/sublime_dsl/sublime_text/keymap.rb, line 374
def value_id
  to_h
end