Class: Rvim::Abbreviations

Inherits:
Object
  • Object
show all
Defined in:
lib/rvim/abbreviations.rb

Defined Under Namespace

Classes: Entry

Constant Summary collapse

MODES =
%i[insert cmdline].freeze

Instance Method Summary collapse

Constructor Details

#initializeAbbreviations

Returns a new instance of Abbreviations.



9
10
11
12
# File 'lib/rvim/abbreviations.rb', line 9

def initialize
  @table = {} # mode => { lhs => Entry }
  MODES.each { |m| @table[m] = {} }
end

Instance Method Details

#add(modes, lhs, rhs, recursive: true) ⇒ Object



14
15
16
# File 'lib/rvim/abbreviations.rb', line 14

def add(modes, lhs, rhs, recursive: true)
  Array(modes).each { |m| @table[m][lhs] = Entry.new(rhs: rhs, recursive: recursive) }
end

#clear(modes) ⇒ Object



22
23
24
# File 'lib/rvim/abbreviations.rb', line 22

def clear(modes)
  Array(modes).each { |m| @table[m] = {} }
end

#detect(line, byte_pointer, mode) ⇒ Object

Given a buffer line and a byte position right after a word-terminator was typed, find the abbreviation lhs to expand (if any). Returns

start_byte, end_byte, entry

for the lhs run, or nil.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rvim/abbreviations.rb', line 41

def detect(line, byte_pointer, mode)
  return nil if byte_pointer <= 0

  # The terminator was just inserted at byte_pointer-1; the word ends at
  # byte_pointer-1 (exclusive of the terminator). Walk back to find word
  # boundary.
  word_end = byte_pointer - 1
  return nil if word_end <= 0

  i = word_end - 1
  while i >= 0 && line.byteslice(i, 1) =~ /[A-Za-z0-9_]/
    i -= 1
  end
  word_start = i + 1
  return nil if word_start >= word_end

  lhs = line.byteslice(word_start, word_end - word_start)
  entry = lookup(mode, lhs)
  return nil unless entry

  [word_start, word_end, entry]
end

#each(mode) ⇒ Object



30
31
32
# File 'lib/rvim/abbreviations.rb', line 30

def each(mode)
  @table[mode].each { |lhs, entry| yield(lhs, entry) }
end

#empty?(mode) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/rvim/abbreviations.rb', line 34

def empty?(mode)
  @table[mode].empty?
end

#lookup(mode, lhs) ⇒ Object



26
27
28
# File 'lib/rvim/abbreviations.rb', line 26

def lookup(mode, lhs)
  @table[mode][lhs]
end

#remove(modes, lhs) ⇒ Object



18
19
20
# File 'lib/rvim/abbreviations.rb', line 18

def remove(modes, lhs)
  Array(modes).each { |m| @table[m].delete(lhs) }
end