Module: VER::Methods::Snippet

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

Class Method Summary collapse

Class Method Details

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



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

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

.complete(text) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# File 'lib/ver/methods/snippet.rb', line 95

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

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

.dwim(text) ⇒ Object



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

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

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



106
107
108
109
110
111
# File 'lib/ver/methods/snippet.rb', line 106

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

.insert_string(text, string) ⇒ Object



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

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

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

.jump(text) ⇒ Object



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

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

.jump_home(text) ⇒ Object



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

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

.jump_mark(text, name) ⇒ Object



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

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

  true
end

.jump_marks_and_tags(text) ⇒ Object



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

def jump_marks_and_tags(text)
  (marks(text) + tags(text)).
    sort_by{|_, name, _|
      name =~ /_0$/ ? 'zero' : name
    }.each do |idx, name, type|
    case type
    when :tag
      return jump_tag(text, name)
    when :mark
      return jump_mark(text, name)
    end
  end

  false
end

.jump_tag(text, name) ⇒ Object



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

def jump_tag(text, name)
  text.minor_mode(:control, :snippet)
  from, to = text.tag_ranges(name).first
  return unless from

  text.mark_set(:insert, from)
  true
end

.marks(text) ⇒ Object



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

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

.tags(text) ⇒ Object



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

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