Module: VER::Methods::Completion
- Defined in:
- lib/ver/methods/completion.rb
Constant Summary collapse
- ASPELL_PARSE =
/^& (.*?) (\d+) (\d+): (.*)$/
Class Method Summary collapse
- .aspell(text) ⇒ Object
- .aspell_completions(text, word) ⇒ Object
- .aspell_execute(word) ⇒ Object
- .complete(text, options = {}, &block) ⇒ Object
- .complete_again(text) ⇒ Object
- .complete_fallback(text) ⇒ Object
- .contextual ⇒ Object
- .contextual_completions(text, from, to) ⇒ Object
- .file(text) ⇒ Object
-
.file_completions(text, from, to) ⇒ Object
TODO: use filename_under_cursor, that should be much more accurate.
- .line(text) ⇒ Object
- .line_completions(text, from, to) ⇒ Object
- .scope_compare(tags, scopes) ⇒ Object
-
.smart_tab(text) ⇒ Object
TODO: use the tag names at the location to customize completion choices the textmate bundles have quite some stuff for that.
- .snippet(text) ⇒ Object
- .word(text) ⇒ Object
- .word_completions(text, from, to) ⇒ Object
Class Method Details
.aspell(text) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/ver/methods/completion.rb', line 38 def aspell(text) text.store(self, :last_used, :aspell) from = text.index('insert - 1 chars wordstart') to = text.index('insert - 1 chars wordend') word = text.get(from, to) complete text do [from, to, aspell_completions(text, word)] end end |
.aspell_completions(text, word) ⇒ Object
175 176 177 178 179 180 181 |
# File 'lib/ver/methods/completion.rb', line 175 def aspell_completions(text, word) if result = aspell_execute(word)[word] result[:suggestions] else [word] end end |
.aspell_execute(word) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/ver/methods/completion.rb', line 185 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(text, options = {}, &block) ⇒ Object
22 23 24 25 |
# File 'lib/ver/methods/completion.rb', line 22 def complete(text, = {}, &block) Undo.separator(text) VER::HoverCompletion.new(text, , &block) end |
.complete_again(text) ⇒ Object
32 33 34 35 36 |
# File 'lib/ver/methods/completion.rb', line 32 def complete_again(text) if last_used = text.store(self, :last_used) send(last_used, text) end end |
.complete_fallback(text) ⇒ Object
27 28 29 30 |
# File 'lib/ver/methods/completion.rb', line 27 def complete_fallback(text) indent = ' ' * text..shiftwidth text.insert(:insert, indent) end |
.contextual ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/ver/methods/completion.rb', line 50 def contextual return unless text.load_preferences text.store(self, :last_used, :contextual) from, to = 'insert - 1 chars wordstart', 'insert - 1 chars wordend' complete(text) do [from, to, contextual_completions(text, from, to)] end end |
.contextual_completions(text, from, to) ⇒ Object
97 98 99 100 101 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 |
# File 'lib/ver/methods/completion.rb', line 97 def contextual_completions(text, from, to) = Set.new(text.tag_names(to)) completions = [] text.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(, scopes) current_word = text.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 |
.file(text) ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/ver/methods/completion.rb', line 61 def file(text) text.store(self, :last_used, :file) complete text, continue: true do file_completions(text, 'insert linestart', 'insert') end end |
.file_completions(text, from, to) ⇒ Object
TODO: use filename_under_cursor, that should be much more accurate.
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/ver/methods/completion.rb', line 142 def file_completions(text, from, to) lineno = text.index(from).line line = text.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 "#{lineno}.#{to - 1}", "#{lineno}.#{to}", list end |
.line(text) ⇒ Object
69 70 71 72 73 74 75 76 |
# File 'lib/ver/methods/completion.rb', line 69 def line(text) text.store(self, :last_used, :line) from, to = 'insert linestart', 'insert lineend' lines = line_completions(text, from, to) complete(text){ [from, to, lines] } end |
.line_completions(text, from, to) ⇒ Object
133 134 135 136 137 138 139 |
# File 'lib/ver/methods/completion.rb', line 133 def line_completions(text, from, to) line = text.get(from, to).to_s.strip return [] if line.empty? needle = Regexp.escape(line) text.search_all(/^.*#{needle}.*$/).map{|match, *_| match }.uniq end |
.scope_compare(tags, scopes) ⇒ Object
129 130 131 |
# File 'lib/ver/methods/completion.rb', line 129 def scope_compare(, scopes) scopes.all?{|scope| .any?{|tag| scope.start_with?(tag) }} end |
.smart_tab(text) ⇒ Object
TODO: use the tag names at the location to customize completion choices the textmate bundles have quite some stuff for that.
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/ver/methods/completion.rb', line 8 def smart_tab(text) context = text.get('insert - 1 chars', 'insert + 1 chars') if context =~ /^\S\W/ if text.store(self, :last_used) complete_again(text) else snippet(text) end else Snippet.jump(text) || complete_fallback(text) end end |
.snippet(text) ⇒ Object
78 79 80 81 82 83 |
# File 'lib/ver/methods/completion.rb', line 78 def snippet(text) return unless text.load_snippets text.store(self, :last_used, :snippet) Snippet.dwim(text) end |
.word(text) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/ver/methods/completion.rb', line 85 def word(text) text.store(self, :last_used, :word) y, x = *text.index('insert') x = (x - 1).abs from, to = text.index("#{y}.#{x} wordstart"), text.index("#{y}.#{x} wordend") words = word_completions(text, from, to) complete(text){ [from, to, words] } end |
.word_completions(text, from, to) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/ver/methods/completion.rb', line 162 def word_completions(text, from, to) prefix = text.get(from, to).strip return [] if prefix.empty? prefix = Regexp.escape(prefix) found = text.search_all(/(^|\W)(#{prefix}[\w-]*)/). sort_by{|match, mf, mt| [VER::Text::Index.new(text, mf).delta(from), match] }.map{|match, *_| match.strip[/[\w-]+/] }.uniq found.delete prefix found end |