Module: VER::Methods::Completion

Included in:
VER::Methods
Defined in:
lib/ver/methods/completion.rb

Constant Summary collapse

ASPELL_PARSE =
/^& (.*?) (\d+) (\d+): (.*)$/

Instance Method Summary collapse

Instance Method Details

#aspell_completions(from, to) ⇒ Object



152
153
154
155
156
157
158
159
160
# File 'lib/ver/methods/completion.rb', line 152

def aspell_completions(from, to)
  word = get(from, to)

  if result = aspell_execute(word)[word]
    result[:suggestions]
  else
    [word]
  end
end

#aspell_execute(word) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/ver/methods/completion.rb', line 164

def aspell_execute(word)
  require 'open3'
  results = {}

  Open3.popen3("aspell pipe") do |stdin, stdout, stderr|
    stdin.print("^#{word}")
    stdin.close
    result = stdout.read

    result.scan(ASPELL_PARSE) do |original, count, offset, suggestions|
      results[original] = {
        count: count.to_i,
        offset: offset.to_i,
        suggestions: suggestions.split(/\s*,\s*/),
      }
    end
  end

  return results
end

#complete(options = {}, &block) ⇒ Object



185
186
187
188
# File 'lib/ver/methods/completion.rb', line 185

def complete(options = {}, &block)
  edit_separator
  HoverCompletion.new(self, options, &block)
end

#complete_againObject



21
22
23
# File 'lib/ver/methods/completion.rb', line 21

def complete_again
  send(@complete_last_used) if @complete_last_used
end

#complete_aspellObject



142
143
144
145
146
147
148
149
150
# File 'lib/ver/methods/completion.rb', line 142

def complete_aspell
  @complete_last_used = :complete_aspell

  complete do
    pos = index('insert - 1 chars')
    from, to = pos.wordstart, pos.wordend
    [from, to, aspell_completions(from, to)]
  end
end

#complete_fileObject



90
91
92
93
94
95
96
# File 'lib/ver/methods/completion.rb', line 90

def complete_file
  @complete_last_used = :complete_file

  complete continue: true do
    file_completions('insert linestart', 'insert')
  end
end

#complete_lineObject



73
74
75
76
77
78
79
80
# File 'lib/ver/methods/completion.rb', line 73

def complete_line
  @complete_last_used = :complete_line

  from, to = 'insert linestart', 'insert lineend'
  lines = line_completions(from, to)

  complete{ [from, to, lines] }
end

#complete_tmObject



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ver/methods/completion.rb', line 25

def complete_tm
  return unless load_preferences
  require 'set'
  require 'tempfile'

  @complete_last_used = :complete_tm

  from, to = 'insert - 1 chars wordstart', 'insert - 1 chars wordend'

  complete{ [from, to, tm_completions(from, to)] }
end

#complete_wordObject



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/ver/methods/completion.rb', line 118

def complete_word
  @complete_last_used = :complete_word

  y, x = index('insert').split
  x = (x - 1).abs
  from, to = index("#{y}.#{x} wordstart"), index("#{y}.#{x} wordend")

  words = word_completions(from, to)

  complete{ [from, to, words] }
end

#file_completions(from, to) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/ver/methods/completion.rb', line 98

def file_completions(from, to)
  y = index(from).y
  line = get(from, to)

  return [] unless match = line.match(/(?<pre>.*?)(?<path>\/[^\s"'{}()\[\]]*)(?<post>.*?)/)
  from, to = match.offset(:path)
  path = match[:path]

  path.sub!(/\/*$/, '/') if File.directory?(path)

  list = Dir["#{path}*"].uniq - [path]

  list.map! do |item|
    item << '/' if File.directory?(item)
    item.sub(path, '/')
  end

  return "#{y}.#{to - 1}", "#{y}.#{to}", list
end

#line_completions(from, to) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/ver/methods/completion.rb', line 82

def line_completions(from, to)
  line = get(from, to).to_s.strip

  return [] if line.empty?
  needle = Regexp.escape(line)
  search_all(/^.*#{needle}.*$/).map{|match, *_| match }.uniq
end

#scope_compare(tags, scopes) ⇒ Object



69
70
71
# File 'lib/ver/methods/completion.rb', line 69

def scope_compare(tags, scopes)
  scopes.all?{|scope| tags.any?{|tag| scope.start_with?(tag) }}
end

#smart_tabObject

TODO: use the tag names at the location to customize completion choices the textmate bundles have quite some stuff for that.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/ver/methods/completion.rb', line 6

def smart_tab
  context = get('insert - 1 chars', 'insert + 1 chars')

  if context =~ /^\S\W/
    if @complete_last_used
      complete_again
    else
      complete_word
    end
  else
    indent = ' ' * VER.options.shiftwidth
    insert :insert, indent
  end
end

#tm_completions(from, to) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ver/methods/completion.rb', line 37

def tm_completions(from, to)
  tags  = Set.new(tag_names(to))
  completions = []

  @preferences.each do |key, value|
    name, scope, settings, uuid =
      value.values_at('name', 'scope', 'settings', 'uuid')
    scopes = Set.new(scope.split(/\s*,\s*|\s+/))

    next unless completion_command = settings['completionCommand']
    next unless scope_compare(tags, scopes)

    current_word = get(from, to).strip
    ENV['TM_CURRENT_WORD'] = current_word

    tmp = Tempfile.new('ver/complete_tm')
    tmp.print(completion_command)
    tmp.close
    begin
      FileUtils.chmod(0700, tmp.path)
      result = `exec '#{tmp.path}'`
      completions = result.scan(/[^\r\n]+/)
    ensure
      tmp.close!
    end
  end

  completions
rescue => ex
  VER.error(ex)
end

#word_completions(from, to) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
# File 'lib/ver/methods/completion.rb', line 130

def word_completions(from, to)
  prefix = get(from, to).strip
  return [] if prefix.empty?
  prefix = Regexp.escape(prefix)

  found = search_all(/(^|\W)(#{prefix}[\w-]*)/).
    sort_by{|match, mf, mt| [Text::Index.new(self, mf).delta(from), match] }.
    map{|match, *_| match.strip[/[\w-]+/] }.uniq
  found.delete prefix
  found
end