utils 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. data/Rakefile +68 -0
  2. data/VERSION +1 -0
  3. data/bin/chroot-exec +12 -0
  4. data/bin/chroot-libs +18 -0
  5. data/bin/classify +37 -0
  6. data/bin/discover +137 -0
  7. data/bin/edit +74 -0
  8. data/bin/errf +32 -0
  9. data/bin/git-empty +8 -0
  10. data/bin/myex +90 -0
  11. data/bin/number_files +26 -0
  12. data/bin/same_files +37 -0
  13. data/bin/search +205 -0
  14. data/bin/sedit +3 -0
  15. data/bin/sshscreen +68 -0
  16. data/bin/term +21 -0
  17. data/bin/unquarantine_apps +8 -0
  18. data/bin/untest +17 -0
  19. data/bin/utils-install-config +10 -0
  20. data/bin/vacuum_firefox_sqlite +22 -0
  21. data/bin/xmp +74 -0
  22. data/lib/utils.rb +8 -0
  23. data/lib/utils/config.rb +23 -0
  24. data/lib/utils/config/gdb/asm +179 -0
  25. data/lib/utils/config/gdb/ruby +528 -0
  26. data/lib/utils/config/gdbinit +8 -0
  27. data/lib/utils/config/irbrc +455 -0
  28. data/lib/utils/config/rdebugrc +2 -0
  29. data/lib/utils/config/screenrc +143 -0
  30. data/lib/utils/config/vim/autoload/Align.vim +1029 -0
  31. data/lib/utils/config/vim/autoload/AlignMaps.vim +330 -0
  32. data/lib/utils/config/vim/autoload/rails.vim +4744 -0
  33. data/lib/utils/config/vim/autoload/rubycomplete.vim +801 -0
  34. data/lib/utils/config/vim/autoload/sqlcomplete.vim +741 -0
  35. data/lib/utils/config/vim/autoload/vimball.vim +750 -0
  36. data/lib/utils/config/vim/colors/flori.vim +113 -0
  37. data/lib/utils/config/vim/compiler/eruby.vim +40 -0
  38. data/lib/utils/config/vim/compiler/ruby.vim +67 -0
  39. data/lib/utils/config/vim/compiler/rubyunit.vim +34 -0
  40. data/lib/utils/config/vim/ftdetect/ragel.vim +2 -0
  41. data/lib/utils/config/vim/ftdetect/ruby.vim +17 -0
  42. data/lib/utils/config/vim/ftplugin/eruby.vim +100 -0
  43. data/lib/utils/config/vim/ftplugin/ruby.vim +260 -0
  44. data/lib/utils/config/vim/ftplugin/xml.vim +941 -0
  45. data/lib/utils/config/vim/indent/IndentAnything_html.vim +35 -0
  46. data/lib/utils/config/vim/indent/eruby.vim +77 -0
  47. data/lib/utils/config/vim/indent/javascript.vim +116 -0
  48. data/lib/utils/config/vim/indent/ruby.vim +377 -0
  49. data/lib/utils/config/vim/plugin/AlignMapsPlugin.vim +242 -0
  50. data/lib/utils/config/vim/plugin/AlignPlugin.vim +41 -0
  51. data/lib/utils/config/vim/plugin/Decho.vim +592 -0
  52. data/lib/utils/config/vim/plugin/IndentAnything.vim +675 -0
  53. data/lib/utils/config/vim/plugin/bufexplorer.vim +1144 -0
  54. data/lib/utils/config/vim/plugin/cecutil.vim +482 -0
  55. data/lib/utils/config/vim/plugin/fugitive.vim +1703 -0
  56. data/lib/utils/config/vim/plugin/lusty-explorer.vim +1509 -0
  57. data/lib/utils/config/vim/plugin/rails.vim +340 -0
  58. data/lib/utils/config/vim/plugin/rubyextra.vim +193 -0
  59. data/lib/utils/config/vim/plugin/surround.vim +628 -0
  60. data/lib/utils/config/vim/plugin/taglist.vim +4546 -0
  61. data/lib/utils/config/vim/plugin/test/IndentAnything/test.js +131 -0
  62. data/lib/utils/config/vim/plugin/vimballPlugin.vim +40 -0
  63. data/lib/utils/config/vim/syntax/Decho.vim +101 -0
  64. data/lib/utils/config/vim/syntax/eruby.vim +73 -0
  65. data/lib/utils/config/vim/syntax/javascript.vim +246 -0
  66. data/lib/utils/config/vim/syntax/ragel.vim +165 -0
  67. data/lib/utils/config/vim/syntax/ruby.vim +367 -0
  68. data/lib/utils/config/vimrc +461 -0
  69. data/lib/utils/file.rb +49 -0
  70. data/lib/utils/find.rb +54 -0
  71. data/lib/utils/md5.rb +23 -0
  72. data/lib/utils/patterns.rb +34 -0
  73. data/lib/utils/version.rb +8 -0
  74. data/utils.gemspec +33 -0
  75. metadata +183 -0
@@ -0,0 +1,801 @@
1
+ " Vim completion script
2
+ " Language: Ruby
3
+ " Maintainer: Mark Guzman <segfault@hasno.info>
4
+ " URL: http://vim-ruby.rubyforge.org
5
+ " Anon CVS: See above site
6
+ " Release Coordinator: Doug Kearns <dougkearns@gmail.com>
7
+ " Maintainer Version: 0.8
8
+ " ----------------------------------------------------------------------------
9
+ "
10
+ " Ruby IRB/Complete author: Keiju ISHITSUKA(keiju@ishitsuka.com)
11
+ " ----------------------------------------------------------------------------
12
+
13
+ " {{{ requirement checks
14
+ if !has('ruby')
15
+ s:ErrMsg( "Error: Rubycomplete requires vim compiled with +ruby" )
16
+ s:ErrMsg( "Error: falling back to syntax completion" )
17
+ " lets fall back to syntax completion
18
+ setlocal omnifunc=syntaxcomplete#Complete
19
+ finish
20
+ endif
21
+
22
+ if version < 700
23
+ s:ErrMsg( "Error: Required vim >= 7.0" )
24
+ finish
25
+ endif
26
+ " }}} requirement checks
27
+
28
+ " {{{ configuration failsafe initialization
29
+ if !exists("g:rubycomplete_rails")
30
+ let g:rubycomplete_rails = 0
31
+ endif
32
+
33
+ if !exists("g:rubycomplete_classes_in_global")
34
+ let g:rubycomplete_classes_in_global = 0
35
+ endif
36
+
37
+ if !exists("g:rubycomplete_buffer_loading")
38
+ let g:rubycomplete_buffer_loading = 0
39
+ endif
40
+
41
+ if !exists("g:rubycomplete_include_object")
42
+ let g:rubycomplete_include_object = 0
43
+ endif
44
+
45
+ if !exists("g:rubycomplete_include_objectspace")
46
+ let g:rubycomplete_include_objectspace = 0
47
+ endif
48
+ " }}} configuration failsafe initialization
49
+
50
+ " {{{ vim-side support functions
51
+ let s:rubycomplete_debug = 0
52
+
53
+ function! s:ErrMsg(msg)
54
+ echohl ErrorMsg
55
+ echo a:msg
56
+ echohl None
57
+ endfunction
58
+
59
+ function! s:dprint(msg)
60
+ if s:rubycomplete_debug == 1
61
+ echom a:msg
62
+ endif
63
+ endfunction
64
+
65
+ function! s:GetBufferRubyModule(name, ...)
66
+ if a:0 == 1
67
+ let [snum,enum] = s:GetBufferRubyEntity(a:name, "module", a:1)
68
+ else
69
+ let [snum,enum] = s:GetBufferRubyEntity(a:name, "module")
70
+ endif
71
+ return snum . '..' . enum
72
+ endfunction
73
+
74
+ function! s:GetBufferRubyClass(name, ...)
75
+ if a:0 >= 1
76
+ let [snum,enum] = s:GetBufferRubyEntity(a:name, "class", a:1)
77
+ else
78
+ let [snum,enum] = s:GetBufferRubyEntity(a:name, "class")
79
+ endif
80
+ return snum . '..' . enum
81
+ endfunction
82
+
83
+ function! s:GetBufferRubySingletonMethods(name)
84
+ endfunction
85
+
86
+ function! s:GetBufferRubyEntity( name, type, ... )
87
+ let lastpos = getpos(".")
88
+ let lastline = lastpos
89
+ if (a:0 >= 1)
90
+ let lastline = [ 0, a:1, 0, 0 ]
91
+ call cursor( a:1, 0 )
92
+ endif
93
+
94
+ let stopline = 1
95
+
96
+ let crex = '^\s*\<' . a:type . '\>\s*\<' . a:name . '\>\s*\(<\s*.*\s*\)\?'
97
+ let [lnum,lcol] = searchpos( crex, 'w' )
98
+ "let [lnum,lcol] = searchpairpos( crex . '\zs', '', '\(end\|}\)', 'w' )
99
+
100
+ if lnum == 0 && lcol == 0
101
+ call cursor(lastpos[1], lastpos[2])
102
+ return [0,0]
103
+ endif
104
+
105
+ let curpos = getpos(".")
106
+ let [enum,ecol] = searchpairpos( crex, '', '\(end\|}\)', 'wr' )
107
+ call cursor(lastpos[1], lastpos[2])
108
+
109
+ if lnum > enum
110
+ return [0,0]
111
+ endif
112
+ " we found a the class def
113
+ return [lnum,enum]
114
+ endfunction
115
+
116
+ function! s:IsInClassDef()
117
+ return s:IsPosInClassDef( line('.') )
118
+ endfunction
119
+
120
+ function! s:IsPosInClassDef(pos)
121
+ let [snum,enum] = s:GetBufferRubyEntity( '.*', "class" )
122
+ let ret = 'nil'
123
+
124
+ if snum < a:pos && a:pos < enum
125
+ let ret = snum . '..' . enum
126
+ endif
127
+
128
+ return ret
129
+ endfunction
130
+
131
+ function! s:GetRubyVarType(v)
132
+ let stopline = 1
133
+ let vtp = ''
134
+ let pos = getpos('.')
135
+ let sstr = '^\s*#\s*@var\s*'.a:v.'\>\s\+[^ \t]\+\s*$'
136
+ let [lnum,lcol] = searchpos(sstr,'nb',stopline)
137
+ if lnum != 0 && lcol != 0
138
+ call setpos('.',pos)
139
+ let str = getline(lnum)
140
+ let vtp = substitute(str,sstr,'\1','')
141
+ return vtp
142
+ endif
143
+ call setpos('.',pos)
144
+ let ctors = '\(now\|new\|open\|get_instance'
145
+ if exists('g:rubycomplete_rails') && g:rubycomplete_rails == 1 && s:rubycomplete_rails_loaded == 1
146
+ let ctors = ctors.'\|find\|create'
147
+ else
148
+ endif
149
+ let ctors = ctors.'\)'
150
+
151
+ let fstr = '=\s*\([^ \t]\+.' . ctors .'\>\|[\[{"''/]\|%[xwQqr][(\[{@]\|[A-Za-z0-9@:\-()\.]\+...\?\|lambda\|&\)'
152
+ let sstr = ''.a:v.'\>\s*[+\-*/]*'.fstr
153
+ let [lnum,lcol] = searchpos(sstr,'nb',stopline)
154
+ if lnum != 0 && lcol != 0
155
+ let str = matchstr(getline(lnum),fstr,lcol)
156
+ let str = substitute(str,'^=\s*','','')
157
+
158
+ call setpos('.',pos)
159
+ if str == '"' || str == '''' || stridx(tolower(str), '%q[') != -1
160
+ return 'String'
161
+ elseif str == '[' || stridx(str, '%w[') != -1
162
+ return 'Array'
163
+ elseif str == '{'
164
+ return 'Hash'
165
+ elseif str == '/' || str == '%r{'
166
+ return 'Regexp'
167
+ elseif strlen(str) >= 4 && stridx(str,'..') != -1
168
+ return 'Range'
169
+ elseif stridx(str, 'lambda') != -1 || str == '&'
170
+ return 'Proc'
171
+ elseif strlen(str) > 4
172
+ let l = stridx(str,'.')
173
+ return str[0:l-1]
174
+ end
175
+ return ''
176
+ endif
177
+ call setpos('.',pos)
178
+ return ''
179
+ endfunction
180
+
181
+ "}}} vim-side support functions
182
+
183
+ "{{{ vim-side completion function
184
+ function! rubycomplete#Init()
185
+ execute "ruby VimRubyCompletion.preload_rails"
186
+ endfunction
187
+
188
+ function! rubycomplete#Complete(findstart, base)
189
+ "findstart = 1 when we need to get the text length
190
+ if a:findstart
191
+ let line = getline('.')
192
+ let idx = col('.')
193
+ while idx > 0
194
+ let idx -= 1
195
+ let c = line[idx-1]
196
+ if c =~ '\w'
197
+ continue
198
+ elseif ! c =~ '\.'
199
+ idx = -1
200
+ break
201
+ else
202
+ break
203
+ endif
204
+ endwhile
205
+
206
+ return idx
207
+ "findstart = 0 when we need to return the list of completions
208
+ else
209
+ let g:rubycomplete_completions = []
210
+ execute "ruby VimRubyCompletion.get_completions('" . a:base . "')"
211
+ return g:rubycomplete_completions
212
+ endif
213
+ endfunction
214
+ "}}} vim-side completion function
215
+
216
+ "{{{ ruby-side code
217
+ function! s:DefRuby()
218
+ ruby << RUBYEOF
219
+ # {{{ ruby completion
220
+
221
+ begin
222
+ require 'rubygems' # let's assume this is safe...?
223
+ rescue Exception
224
+ #ignore?
225
+ end
226
+ class VimRubyCompletion
227
+ # {{{ constants
228
+ @@debug = false
229
+ @@ReservedWords = [
230
+ "BEGIN", "END",
231
+ "alias", "and",
232
+ "begin", "break",
233
+ "case", "class",
234
+ "def", "defined", "do",
235
+ "else", "elsif", "end", "ensure",
236
+ "false", "for",
237
+ "if", "in",
238
+ "module",
239
+ "next", "nil", "not",
240
+ "or",
241
+ "redo", "rescue", "retry", "return",
242
+ "self", "super",
243
+ "then", "true",
244
+ "undef", "unless", "until",
245
+ "when", "while",
246
+ "yield",
247
+ ]
248
+
249
+ @@Operators = [ "%", "&", "*", "**", "+", "-", "/",
250
+ "<", "<<", "<=", "<=>", "==", "===", "=~", ">", ">=", ">>",
251
+ "[]", "[]=", "^", ]
252
+ # }}} constants
253
+
254
+ # {{{ buffer analysis magic
255
+ def load_requires
256
+ buf = VIM::Buffer.current
257
+ enum = buf.line_number
258
+ nums = Range.new( 1, enum )
259
+ nums.each do |x|
260
+ ln = buf[x]
261
+ begin
262
+ eval( "require %s" % $1 ) if /.*require\s*(.*)$/.match( ln )
263
+ rescue Exception
264
+ #ignore?
265
+ end
266
+ end
267
+ end
268
+
269
+ def load_buffer_class(name)
270
+ dprint "load_buffer_class(%s) START" % name
271
+ classdef = get_buffer_entity(name, 's:GetBufferRubyClass("%s")')
272
+ return if classdef == nil
273
+
274
+ pare = /^\s*class\s*(.*)\s*<\s*(.*)\s*\n/.match( classdef )
275
+ load_buffer_class( $2 ) if pare != nil && $2 != name # load parent class if needed
276
+
277
+ mixre = /.*\n\s*include\s*(.*)\s*\n/.match( classdef )
278
+ load_buffer_module( $2 ) if mixre != nil && $2 != name # load mixins if needed
279
+
280
+ begin
281
+ eval classdef
282
+ rescue Exception
283
+ VIM::evaluate( "s:ErrMsg( 'Problem loading class \"%s\", was it already completed?' )" % name )
284
+ end
285
+ dprint "load_buffer_class(%s) END" % name
286
+ end
287
+
288
+ def load_buffer_module(name)
289
+ dprint "load_buffer_module(%s) START" % name
290
+ classdef = get_buffer_entity(name, 's:GetBufferRubyModule("%s")')
291
+ return if classdef == nil
292
+
293
+ begin
294
+ eval classdef
295
+ rescue Exception
296
+ VIM::evaluate( "s:ErrMsg( 'Problem loading module \"%s\", was it already completed?' )" % name )
297
+ end
298
+ dprint "load_buffer_module(%s) END" % name
299
+ end
300
+
301
+ def get_buffer_entity(name, vimfun)
302
+ loading_allowed = VIM::evaluate("exists('g:rubycomplete_buffer_loading') && g:rubycomplete_buffer_loading")
303
+ return nil if loading_allowed.to_i.zero?
304
+ return nil if /(\"|\')+/.match( name )
305
+ buf = VIM::Buffer.current
306
+ nums = eval( VIM::evaluate( vimfun % name ) )
307
+ return nil if nums == nil
308
+ return nil if nums.min == nums.max && nums.min == 0
309
+
310
+ dprint "get_buffer_entity START"
311
+ visited = []
312
+ clscnt = 0
313
+ bufname = VIM::Buffer.current.name
314
+ classdef = ""
315
+ cur_line = VIM::Buffer.current.line_number
316
+ while (nums != nil && !(nums.min == 0 && nums.max == 0) )
317
+ dprint "visited: %s" % visited.to_s
318
+ break if visited.index( nums )
319
+ visited << nums
320
+
321
+ nums.each do |x|
322
+ if x != cur_line
323
+ next if x == 0
324
+ ln = buf[x]
325
+ if /^\s*(module|class|def|include)\s+/.match(ln)
326
+ clscnt += 1 if $1 == "class"
327
+ #dprint "\$1$1
328
+ classdef += "%s\n" % ln
329
+ classdef += "end\n" if /def\s+/.match(ln)
330
+ dprint ln
331
+ end
332
+ end
333
+ end
334
+
335
+ nm = "%s(::.*)*\", %s, \"" % [ name, nums.last ]
336
+ nums = eval( VIM::evaluate( vimfun % nm ) )
337
+ dprint "nm: \"%s\"" % nm
338
+ dprint "vimfun: %s" % (vimfun % nm)
339
+ dprint "got nums: %s" % nums.to_s
340
+ end
341
+ if classdef.length > 1
342
+ classdef += "end\n"*clscnt
343
+ # classdef = "class %s\n%s\nend\n" % [ bufname.gsub( /\/|\\/, "_" ), classdef ]
344
+ end
345
+
346
+ dprint "get_buffer_entity END"
347
+ dprint "classdef====start"
348
+ lns = classdef.split( "\n" )
349
+ lns.each { |x| dprint x }
350
+ dprint "classdef====end"
351
+ return classdef
352
+ end
353
+
354
+ def get_var_type( receiver )
355
+ if /(\"|\')+/.match( receiver )
356
+ "String"
357
+ else
358
+ VIM::evaluate("s:GetRubyVarType('%s')" % receiver)
359
+ end
360
+ end
361
+
362
+ def dprint( txt )
363
+ print txt if @@debug
364
+ end
365
+
366
+ def get_buffer_entity_list( type )
367
+ # this will be a little expensive.
368
+ loading_allowed = VIM::evaluate("exists('g:rubycomplete_buffer_loading') && g:rubycomplete_buffer_loading")
369
+ allow_aggressive_load = VIM::evaluate("exists('g:rubycomplete_classes_in_global') && g:rubycomplete_classes_in_global")
370
+ return [] if allow_aggressive_load.to_i.zero? || loading_allowed.to_i.zero?
371
+
372
+ buf = VIM::Buffer.current
373
+ eob = buf.length
374
+ ret = []
375
+ rg = 1..eob
376
+ re = eval( "/^\s*%s\s*([A-Za-z0-9_:-]*)(\s*<\s*([A-Za-z0-9_:-]*))?\s*/" % type )
377
+
378
+ rg.each do |x|
379
+ if re.match( buf[x] )
380
+ next if type == "def" && eval( VIM::evaluate("s:IsPosInClassDef(%s)" % x) ) != nil
381
+ ret.push $1
382
+ end
383
+ end
384
+
385
+ return ret
386
+ end
387
+
388
+ def get_buffer_modules
389
+ return get_buffer_entity_list( "modules" )
390
+ end
391
+
392
+ def get_buffer_methods
393
+ return get_buffer_entity_list( "def" )
394
+ end
395
+
396
+ def get_buffer_classes
397
+ return get_buffer_entity_list( "class" )
398
+ end
399
+
400
+
401
+ def load_rails
402
+ allow_rails = VIM::evaluate("exists('g:rubycomplete_rails') && g:rubycomplete_rails")
403
+ return if allow_rails.to_i.zero?
404
+
405
+ buf_path = VIM::evaluate('expand("%:p")')
406
+ file_name = VIM::evaluate('expand("%:t")')
407
+ vim_dir = VIM::evaluate('getcwd()')
408
+ file_dir = buf_path.gsub( file_name, '' )
409
+ file_dir.gsub!( /\\/, "/" )
410
+ vim_dir.gsub!( /\\/, "/" )
411
+ vim_dir << "/"
412
+ dirs = [ vim_dir, file_dir ]
413
+ sdirs = [ "", "./", "../", "../../", "../../../", "../../../../" ]
414
+ rails_base = nil
415
+
416
+ dirs.each do |dir|
417
+ sdirs.each do |sub|
418
+ trail = "%s%s" % [ dir, sub ]
419
+ tcfg = "%sconfig" % trail
420
+
421
+ if File.exists?( tcfg )
422
+ rails_base = trail
423
+ break
424
+ end
425
+ end
426
+ break if rails_base
427
+ end
428
+
429
+ return if rails_base == nil
430
+ $:.push rails_base unless $:.index( rails_base )
431
+
432
+ rails_config = rails_base + "config/"
433
+ rails_lib = rails_base + "lib/"
434
+ $:.push rails_config unless $:.index( rails_config )
435
+ $:.push rails_lib unless $:.index( rails_lib )
436
+
437
+ bootfile = rails_config + "boot.rb"
438
+ envfile = rails_config + "environment.rb"
439
+ if File.exists?( bootfile ) && File.exists?( envfile )
440
+ begin
441
+ require bootfile
442
+ require envfile
443
+ begin
444
+ require 'console_app'
445
+ require 'console_with_helpers'
446
+ rescue Exception
447
+ dprint "Rails 1.1+ Error %s" % $!
448
+ # assume 1.0
449
+ end
450
+ #eval( "Rails::Initializer.run" ) #not necessary?
451
+ VIM::command('let s:rubycomplete_rails_loaded = 1')
452
+ dprint "rails loaded"
453
+ rescue Exception
454
+ dprint "Rails Error %s" % $!
455
+ VIM::evaluate( "s:ErrMsg('Error loading rails environment')" )
456
+ end
457
+ end
458
+ end
459
+
460
+ def get_rails_helpers
461
+ allow_rails = VIM::evaluate("exists('g:rubycomplete_rails') && g:rubycomplete_rails")
462
+ rails_loaded = VIM::evaluate('s:rubycomplete_rails_loaded')
463
+ return [] if allow_rails.to_i.zero? || rails_loaded.to_i.zero?
464
+
465
+ buf_path = VIM::evaluate('expand("%:p")')
466
+ buf_path.gsub!( /\\/, "/" )
467
+ path_elm = buf_path.split( "/" )
468
+ dprint "buf_path: %s" % buf_path
469
+ types = [ "app", "db", "lib", "test", "components", "script" ]
470
+
471
+ i = nil
472
+ ret = []
473
+ type = nil
474
+ types.each do |t|
475
+ i = path_elm.index( t )
476
+ break if i
477
+ end
478
+ type = path_elm[i]
479
+ type.downcase!
480
+
481
+ dprint "type: %s" % type
482
+ case type
483
+ when "app"
484
+ i += 1
485
+ subtype = path_elm[i]
486
+ subtype.downcase!
487
+
488
+ dprint "subtype: %s" % subtype
489
+ case subtype
490
+ when "views"
491
+ ret += ActionView::Base.instance_methods
492
+ ret += ActionView::Base.methods
493
+ when "controllers"
494
+ ret += ActionController::Base.instance_methods
495
+ ret += ActionController::Base.methods
496
+ when "models"
497
+ ret += ActiveRecord::Base.instance_methods
498
+ ret += ActiveRecord::Base.methods
499
+ end
500
+
501
+ when "db"
502
+ ret += ActiveRecord::ConnectionAdapters::SchemaStatements.instance_methods
503
+ ret += ActiveRecord::ConnectionAdapters::SchemaStatements.methods
504
+ end
505
+
506
+
507
+ return ret
508
+ end
509
+
510
+ def add_rails_columns( cls )
511
+ allow_rails = VIM::evaluate("exists('g:rubycomplete_rails') && g:rubycomplete_rails")
512
+ rails_loaded = VIM::evaluate('s:rubycomplete_rails_loaded')
513
+ return [] if allow_rails.to_i.zero? || rails_loaded.to_i.zero?
514
+
515
+ begin
516
+ eval( "#{cls}.establish_connection" )
517
+ return [] unless eval( "#{cls}.ancestors.include?(ActiveRecord::Base).to_s" )
518
+ col = eval( "#{cls}.column_names" )
519
+ return col if col
520
+ rescue
521
+ dprint "add_rails_columns err: (cls: %s) %s" % [ cls, $! ]
522
+ return []
523
+ end
524
+ return []
525
+ end
526
+
527
+ def clean_sel(sel, msg)
528
+ sel.delete_if { |x| x == nil }
529
+ sel.uniq!
530
+ sel.grep(/^#{Regexp.quote(msg)}/) if msg != nil
531
+ end
532
+
533
+ def get_rails_view_methods
534
+ allow_rails = VIM::evaluate("exists('g:rubycomplete_rails') && g:rubycomplete_rails")
535
+ rails_loaded = VIM::evaluate('s:rubycomplete_rails_loaded')
536
+ return [] if allow_rails.to_i.zero? || rails_loaded.to_i.zero?
537
+
538
+ buf_path = VIM::evaluate('expand("%:p")')
539
+ buf_path.gsub!( /\\/, "/" )
540
+ pelm = buf_path.split( "/" )
541
+ idx = pelm.index( "views" )
542
+
543
+ return [] unless idx
544
+ idx += 1
545
+
546
+ clspl = pelm[idx].camelize.pluralize
547
+ cls = clspl.singularize
548
+
549
+ ret = []
550
+ begin
551
+ ret += eval( "#{cls}.instance_methods" )
552
+ ret += eval( "#{clspl}Helper.instance_methods" )
553
+ rescue Exception
554
+ dprint "Error: Unable to load rails view helpers for %s: %s" % [ cls, $! ]
555
+ end
556
+
557
+ return ret
558
+ end
559
+ # }}} buffer analysis magic
560
+
561
+ # {{{ main completion code
562
+ def self.preload_rails
563
+ a = VimRubyCompletion.new
564
+ require 'Thread'
565
+ Thread.new(a) do |b|
566
+ begin
567
+ b.load_rails
568
+ rescue
569
+ end
570
+ end
571
+ a.load_rails
572
+ rescue
573
+ end
574
+
575
+ def self.get_completions(base)
576
+ b = VimRubyCompletion.new
577
+ b.get_completions base
578
+ end
579
+
580
+ def get_completions(base)
581
+ loading_allowed = VIM::evaluate("exists('g:rubycomplete_buffer_loading') && g:rubycomplete_buffer_loading")
582
+ if loading_allowed.to_i == 1
583
+ load_requires
584
+ load_rails
585
+ end
586
+
587
+ input = VIM::Buffer.current.line
588
+ cpos = VIM::Window.current.cursor[1] - 1
589
+ input = input[0..cpos]
590
+ input += base
591
+ input.sub!(/.*[ \t\n\"\\'`><=;|&{(]/, '') # Readline.basic_word_break_characters
592
+ input.sub!(/self\./, '')
593
+ input.sub!(/.*((\.\.[\[(]?)|([\[(]))/, '')
594
+
595
+ dprint 'input %s' % input
596
+ message = nil
597
+ receiver = nil
598
+ methods = []
599
+ variables = []
600
+ classes = []
601
+ constants = []
602
+
603
+ case input
604
+ when /^(\/[^\/]*\/)\.([^.]*)$/ # Regexp
605
+ receiver = $1
606
+ message = Regexp.quote($2)
607
+ methods = Regexp.instance_methods(true)
608
+
609
+ when /^([^\]]*\])\.([^.]*)$/ # Array
610
+ receiver = $1
611
+ message = Regexp.quote($2)
612
+ methods = Array.instance_methods(true)
613
+
614
+ when /^([^\}]*\})\.([^.]*)$/ # Proc or Hash
615
+ receiver = $1
616
+ message = Regexp.quote($2)
617
+ methods = Proc.instance_methods(true) | Hash.instance_methods(true)
618
+
619
+ when /^(:[^:.]*)$/ # Symbol
620
+ dprint "symbol"
621
+ if Symbol.respond_to?(:all_symbols)
622
+ receiver = $1
623
+ message = $1.sub( /:/, '' )
624
+ methods = Symbol.all_symbols.collect{|s| s.id2name}
625
+ methods.delete_if { |c| c.match( /'/ ) }
626
+ end
627
+
628
+ when /^::([A-Z][^:\.\(]*)$/ # Absolute Constant or class methods
629
+ dprint "const or cls"
630
+ receiver = $1
631
+ methods = Object.constants
632
+ methods.grep(/^#{receiver}/).collect{|e| "::" + e}
633
+
634
+ when /^(((::)?[A-Z][^:.\(]*)+)::?([^:.]*)$/ # Constant or class methods
635
+ receiver = $1
636
+ message = Regexp.quote($4)
637
+ dprint "const or cls 2 [recv: \'%s\', msg: \'%s\']" % [ receiver, message ]
638
+ load_buffer_class( receiver )
639
+ begin
640
+ classes = eval("#{receiver}.constants")
641
+ #methods = eval("#{receiver}.methods")
642
+ rescue Exception
643
+ dprint "exception: %s" % $!
644
+ methods = []
645
+ end
646
+ methods.grep(/^#{message}/).collect{|e| receiver + "::" + e}
647
+
648
+ when /^(:[^:.]+)\.([^.]*)$/ # Symbol
649
+ dprint "symbol"
650
+ receiver = $1
651
+ message = Regexp.quote($2)
652
+ methods = Symbol.instance_methods(true)
653
+
654
+ when /^([0-9_]+(\.[0-9_]+)?(e[0-9]+)?)\.([^.]*)$/ # Numeric
655
+ dprint "numeric"
656
+ receiver = $1
657
+ message = Regexp.quote($4)
658
+ begin
659
+ methods = eval(receiver).methods
660
+ rescue Exception
661
+ methods = []
662
+ end
663
+
664
+ when /^(\$[^.]*)$/ #global
665
+ dprint "global"
666
+ methods = global_variables.grep(Regexp.new(Regexp.quote($1)))
667
+
668
+ when /^((\.?[^.]+)+)\.([^.]*)$/ # variable
669
+ dprint "variable"
670
+ receiver = $1
671
+ message = Regexp.quote($3)
672
+ load_buffer_class( receiver )
673
+
674
+ cv = eval("self.class.constants")
675
+ vartype = get_var_type( receiver )
676
+ dprint "vartype: %s" % vartype
677
+ if vartype != ''
678
+ load_buffer_class( vartype )
679
+
680
+ begin
681
+ methods = eval("#{vartype}.instance_methods")
682
+ variables = eval("#{vartype}.instance_variables")
683
+ rescue Exception
684
+ dprint "load_buffer_class err: %s" % $!
685
+ end
686
+ elsif (cv).include?(receiver)
687
+ # foo.func and foo is local var.
688
+ methods = eval("#{receiver}.methods")
689
+ vartype = receiver
690
+ elsif /^[A-Z]/ =~ receiver and /\./ !~ receiver
691
+ vartype = receiver
692
+ # Foo::Bar.func
693
+ begin
694
+ methods = eval("#{receiver}.methods")
695
+ rescue Exception
696
+ end
697
+ else
698
+ # func1.func2
699
+ ObjectSpace.each_object(Module){|m|
700
+ next if m.name != "IRB::Context" and
701
+ /^(IRB|SLex|RubyLex|RubyToken)/ =~ m.name
702
+ methods.concat m.instance_methods(false)
703
+ }
704
+ end
705
+ variables += add_rails_columns( "#{vartype}" ) if vartype && vartype.length > 0
706
+
707
+ when /^\(?\s*[A-Za-z0-9:^@.%\/+*\(\)]+\.\.\.?[A-Za-z0-9:^@.%\/+*\(\)]+\s*\)?\.([^.]*)/
708
+ message = $1
709
+ methods = Range.instance_methods(true)
710
+
711
+ when /^\.([^.]*)$/ # unknown(maybe String)
712
+ message = Regexp.quote($1)
713
+ methods = String.instance_methods(true)
714
+
715
+ else
716
+ dprint "default/other"
717
+ inclass = eval( VIM::evaluate("s:IsInClassDef()") )
718
+
719
+ if inclass != nil
720
+ dprint "inclass"
721
+ classdef = "%s\n" % VIM::Buffer.current[ inclass.min ]
722
+ found = /^\s*class\s*([A-Za-z0-9_-]*)(\s*<\s*([A-Za-z0-9_:-]*))?\s*\n$/.match( classdef )
723
+
724
+ if found != nil
725
+ receiver = $1
726
+ message = input
727
+ load_buffer_class( receiver )
728
+ begin
729
+ methods = eval( "#{receiver}.instance_methods" )
730
+ variables += add_rails_columns( "#{receiver}" )
731
+ rescue Exception
732
+ found = nil
733
+ end
734
+ end
735
+ end
736
+
737
+ if inclass == nil || found == nil
738
+ dprint "inclass == nil"
739
+ methods = get_buffer_methods
740
+ methods += get_rails_view_methods
741
+
742
+ cls_const = Class.constants
743
+ constants = cls_const.select { |c| /^[A-Z_-]+$/.match( c ) }
744
+ classes = eval("self.class.constants") - constants
745
+ classes += get_buffer_classes
746
+ classes += get_buffer_modules
747
+
748
+ include_objectspace = VIM::evaluate("exists('g:rubycomplete_include_objectspace') && g:rubycomplete_include_objectspace")
749
+ ObjectSpace.each_object(Class) { |cls| classes << cls.to_s } if include_objectspace == "1"
750
+ message = receiver = input
751
+ end
752
+
753
+ methods += get_rails_helpers
754
+ methods += Kernel.public_methods
755
+ end
756
+
757
+
758
+ include_object = VIM::evaluate("exists('g:rubycomplete_include_object') && g:rubycomplete_include_object")
759
+ methods = clean_sel( methods, message )
760
+ methods = (methods-Object.instance_methods) if include_object == "0"
761
+ rbcmeth = (VimRubyCompletion.instance_methods-Object.instance_methods) # lets remove those rubycomplete methods
762
+ methods = (methods-rbcmeth)
763
+
764
+ variables = clean_sel( variables, message )
765
+ classes = clean_sel( classes, message ) - ["VimRubyCompletion"]
766
+ constants = clean_sel( constants, message )
767
+
768
+ valid = []
769
+ valid += methods.collect { |m| { :name => m, :type => 'm' } }
770
+ valid += variables.collect { |v| { :name => v, :type => 'v' } }
771
+ valid += classes.collect { |c| { :name => c, :type => 't' } }
772
+ valid += constants.collect { |d| { :name => d, :type => 'd' } }
773
+ valid.sort! { |x,y| x[:name] <=> y[:name] }
774
+
775
+ outp = ""
776
+
777
+ rg = 0..valid.length
778
+ rg.step(150) do |x|
779
+ stpos = 0+x
780
+ enpos = 150+x
781
+ valid[stpos..enpos].each { |c| outp += "{'word':'%s','item':'%s','kind':'%s'}," % [ c[:name], c[:name], c[:type] ] }
782
+ outp.sub!(/,$/, '')
783
+
784
+ VIM::command("call extend(g:rubycomplete_completions, [%s])" % outp)
785
+ outp = ""
786
+ end
787
+ end
788
+ # }}} main completion code
789
+
790
+ end # VimRubyCompletion
791
+ # }}} ruby completion
792
+ RUBYEOF
793
+ endfunction
794
+
795
+ let s:rubycomplete_rails_loaded = 0
796
+
797
+ call s:DefRuby()
798
+ "}}} ruby-side code
799
+
800
+
801
+ " vim:tw=78:sw=4:ts=8:et:fdm=marker:ft=vim:norl: