Module: RuVim::KeywordChars

Defined in:
lib/ruvim/keyword_chars.rb

Constant Summary collapse

DEFAULT_CHAR_CLASS =
"[:alnum:]_".freeze
DEFAULT_REGEX =
/[[:alnum:]_]/.freeze

Class Method Summary collapse

Class Method Details

.char_class(raw) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ruvim/keyword_chars.rb', line 10

def char_class(raw)
  spec = raw.to_s
  return DEFAULT_CHAR_CLASS if spec.empty?

  @char_class_cache ||= {}
  return @char_class_cache[spec] if @char_class_cache.key?(spec)

  extra = []
  spec.split(",").each do |tok|
    t = tok.strip
    next if t.empty? || t == "@"

    if t.length == 1
      extra << Regexp.escape(t)
    elsif t.match?(/\A\d+-\d+\z/)
      a, b = t.split("-", 2).map(&:to_i)
      lo, hi = [a, b].minmax
      next if lo < 0 || hi > 255
      extra << "#{Regexp.escape(lo.chr)}-#{Regexp.escape(hi.chr)}"
    end
  end

  @char_class_cache[spec] = "#{DEFAULT_CHAR_CLASS}#{extra.join}".freeze
end

.regex(raw) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ruvim/keyword_chars.rb', line 35

def regex(raw)
  spec = raw.to_s
  return DEFAULT_REGEX if spec.empty?

  @regex_cache ||= {}
  return @regex_cache[spec] if @regex_cache.key?(spec)

  klass = char_class(spec)
  @regex_cache[spec] = /[#{klass}]/
rescue RegexpError
  DEFAULT_REGEX
end