Module: VER::Methods::Snippet

Defined in:
lib/ver/methods/snippet.rb

Defined Under Namespace

Classes: Scope

Class Method Summary collapse

Class Method Details

.cancel(buffer, into_mode = :control) ⇒ Object



78
79
80
81
82
# File 'lib/ver/methods/snippet.rb', line 78

def cancel(buffer, into_mode = :control)
  marks(buffer).each{|_, mark, _| buffer.mark_unset(mark) }
  tags(buffer).each{|_, tag, _| buffer.tag_delete(tag) }
  buffer.minor_mode(:snippet, into_mode) if buffer.minor_mode?(:snippet)
end

.complete(buffer) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/ver/methods/snippet.rb', line 102

def complete(buffer)
  sofar = buffer.get('insert linestart', 'insert')
  l sofar: sofar

  scope = Scope.new(buffer.tag_names(:insert))

  buffer.snippets.each do |snippet|
    next unless tab_trigger = snippet[:tabTrigger]
    next unless sofar.end_with?(tab_trigger)
    next unless scope.include?(snippet[:scope])
    from = buffer.index("insert - #{tab_trigger.size} displaychars")
    to = buffer.at_insert
    return insert(buffer, from, to, snippet)
  end

  return

  head = buffer.get('insert linestart', 'insert')
  name = head[/\S+$/]
  from = buffer.index("insert - #{name.size} chars")
  to = buffer.index("insert")

  l buffer.snippets

  return unless snippet = buffer.snippets[name]
  insert(buffer, from, to, snippet)
  true
end

.dwim(buffer) ⇒ Object



8
9
10
# File 'lib/ver/methods/snippet.rb', line 8

def dwim(buffer)
  jump(buffer) or complete(buffer) && jump(buffer)
end

.insert(buffer, from, to, snippet_source) ⇒ Object



131
132
133
134
135
136
# File 'lib/ver/methods/snippet.rb', line 131

def insert(buffer, from, to, snippet_source)
  buffer.delete(from, to)
  buffer.mark_set(:insert, from)
  snippet = VER::Snippet.new(snippet_source[:content])
  snippet.apply_on(buffer)
end

.jump(buffer) ⇒ Object



12
13
14
# File 'lib/ver/methods/snippet.rb', line 12

def jump(buffer)
  jump_marks_and_tags(buffer) || jump_home(buffer)
end

.jump_home(buffer) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/ver/methods/snippet.rb', line 16

def jump_home(buffer)
  if buffer.mark_names.include?(:ver_snippet_0)
    buffer.mark_set(:insert, :ver_snippet_0)
    buffer.mark_unset(:ver_snippet_0)
    true
  else
    cancel(buffer)
    false
  end
end

.jump_mark(buffer, name) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/ver/methods/snippet.rb', line 67

def jump_mark(buffer, name)
  buffer.mark_set('insert', name)
  if name =~ /_0$/
    cancel(buffer, :insert)
  else
    buffer.mark_unset(name)
  end

  true
end

.jump_marks_and_tags(buffer) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ver/methods/snippet.rb', line 27

def jump_marks_and_tags(buffer)
  (marks(buffer) + tags(buffer)).
    sort_by{|_, name, _|
      name =~ /_0$/ ? 'zero' : name
    }.each do |idx, name, type|

    case type
    when :tag
      return jump_tag(buffer, name)
    when :mark
      return jump_mark(buffer, name)
    end
  end

  false
end

.jump_tag(buffer, name) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/ver/methods/snippet.rb', line 58

def jump_tag(buffer, name)
  buffer.minor_mode(:insert, :snippet)
  from, to = *buffer.tag_ranges(name).first
  return unless from

  buffer.mark_set(:insert, from)
  true
end

.marks(buffer) ⇒ Object



44
45
46
47
48
49
# File 'lib/ver/methods/snippet.rb', line 44

def marks(buffer)
  buffer.mark_names.map{|mark|
    next unless mark =~ /^ver_snippet_(\d+)$/
    [$1.to_i, mark, :mark]
  }.compact
end

.on_insert(buffer, string = buffer.event.unicode) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ver/methods/snippet.rb', line 84

def on_insert(buffer, string = buffer.event.unicode)
  tag = buffer.tag_names(:insert).find{|name| name =~ /^ver\.snippet\.(\d+)$/ }

  if tag
    from, to = *buffer.tag_ranges(tag).first
    buffer.tag_delete(tag, from, to)
    buffer.replace(from, to, string)
  else
    buffer.insert(:insert, string)
  end
end

.tags(buffer) ⇒ Object



51
52
53
54
55
56
# File 'lib/ver/methods/snippet.rb', line 51

def tags(buffer)
  buffer.tag_names.map{|tag|
    next unless tag =~ /^ver\.snippet\.(\d+)$/
    [$1.to_i, tag, :tag]
  }.compact
end