1
1
"""ropevim, a vim mode for using rope refactoring library"""
2
+ import glob
2
3
import os
3
4
import tempfile
4
5
import re
9
10
10
11
import vim
11
12
13
+ # Gobal var to be able to shutup output
14
+ _rope_quiet = False
15
+
12
16
13
17
class VimUtils (environment .Environment ):
14
18
@@ -21,8 +25,8 @@ def ask(self, prompt, default=None, starting=None):
21
25
if starting is None :
22
26
starting = ''
23
27
if default is not None :
24
- prompt = prompt + ( '[%s ] '% default )
25
- result = call ('input("%s ", "%s ")' % (prompt ,starting ))
28
+ prompt = prompt + '[{0} ] '. format ( default )
29
+ result = call ('input("{0} ", "{1} ")' . format (prompt ,starting ))
26
30
if default is not None and result == '' :
27
31
return default
28
32
return result
@@ -32,11 +36,14 @@ def ask_values(self, prompt, values, default=None,
32
36
if show_values or (show_values is None and len (values )< 14 ):
33
37
self ._print_values (values )
34
38
if default is not None :
35
- prompt = prompt + ( '[%s ] '% default )
39
+ prompt = prompt + '[{0} ] '. format ( default )
36
40
starting = starting or ''
37
41
_completer .values = values
38
- answer = call ('input("%s", "%s", "customlist,RopeValueCompleter")' %
39
- (prompt ,starting ))
42
+ answer = call (
43
+ 'input("{0}", "{1}", "customlist,RopeValueCompleter")' .format (
44
+ prompt ,starting
45
+ )
46
+ )
40
47
if answer is None :
41
48
if 'cancel' in values :
42
49
return 'cancel'
@@ -54,14 +61,14 @@ def _print_values(self, values):
54
61
echo ('\n ' .join (numbered )+ '\n ' )
55
62
56
63
def ask_directory (self ,prompt ,default = None ,starting = None ):
57
- return call ('input("%s ", ".", "dir")' % prompt )
64
+ return call ('input("{0} ", ".", "dir")' . format ( prompt ) )
58
65
59
66
def _update_proposals (self ,values ):
60
67
self .completeopt = vim .eval ('&completeopt' )
61
68
self .preview = 'preview' in self .completeopt
62
69
63
70
if not self .get ('extended_complete' ):
64
- return u',' .join (u"'%s'" % self ._completion_text (proposal )
71
+ return u',' .join (u"'{0}'" . format ( self ._completion_text (proposal ) )
65
72
for proposal in values )
66
73
67
74
return u',' .join (self ._extended_completion (proposal )
@@ -78,7 +85,7 @@ def ask_completion(self, prompt, values, starting=None):
78
85
col = int (call ('col(".")' ))
79
86
if starting :
80
87
col -= len (starting )
81
- self ._command (u'call complete(%s , [%s ])' % (col ,proposals ),
88
+ self ._command (u'call complete({0} , [{1} ])' . format (col ,proposals ),
82
89
encode = True )
83
90
return None
84
91
@@ -95,8 +102,8 @@ def y_or_n(self, prompt):
95
102
return self .yes_or_no (prompt )
96
103
97
104
def get (self ,name ,default = None ):
98
- vimname = 'g:pymode_rope_%s' % name
99
- if str (vim .eval ('exists("%s ")' % vimname ))== '0' :
105
+ vimname = 'g:pymode_rope_{0}' . format ( name )
106
+ if str (vim .eval ('exists("{0} ")' . format ( vimname ) ))== '0' :
100
107
return default
101
108
result = vim .eval (vimname )
102
109
if isinstance (result ,str )and result .isdigit ():
@@ -261,8 +268,9 @@ def _writedefs(self, locations, filename):
261
268
262
269
def show_doc (self ,docs ,altview = False ):
263
270
if docs :
264
- cmd = 'call pymode#ShowStr("%s")' % str (docs .replace ('"' ,'\\ "' ))
265
- vim .command (cmd )
271
+ vim .command (
272
+ 'call pymode#ShowStr("{0}")' .format (docs .replace ('"' ,'\\ "' ))
273
+ )
266
274
267
275
def preview_changes (self ,diffs ):
268
276
echo (diffs )
@@ -281,23 +289,33 @@ def add_hook(self, name, callback, hook):
281
289
'after_save' :'FileWritePost,BufWritePost' ,
282
290
'exit' :'VimLeave' }
283
291
self ._add_function (name ,callback )
284
- vim .command ('autocmd %s *.py call %s()' %
285
- (mapping [hook ],_vim_name (name )))
292
+ vim .command (
293
+ 'autocmd {0} *.py call {1}()' .format (
294
+ mapping [hook ],_vim_name (name )
295
+ )
296
+ )
286
297
287
298
def _add_command (self ,name ,callback ,key ,prefix ,prekey ):
288
299
self ._add_function (name ,callback ,prefix )
289
- vim .command ('command! -range %s call %s()' %
290
- (_vim_name (name ),_vim_name (name )))
300
+ vim .command (
301
+ 'command! -range {0} call {1}()' .format (
302
+ _vim_name (name ),_vim_name (name )
303
+ )
304
+ )
291
305
if key is not None :
292
306
key = prekey + key .replace (' ' ,'' )
293
- vim .command ('noremap %s :call %s()<cr>' % (key ,_vim_name (name )))
307
+ vim .command (
308
+ 'noremap {0} :call {1}()<cr>' .format (key ,_vim_name (name ))
309
+ )
294
310
295
311
def _add_function (self ,name ,callback ,prefix = False ):
296
312
globals ()[name ]= callback
297
313
arg = 'None' if prefix else ''
298
- vim .command ('function! %s()\n ' % _vim_name (name )+
299
- 'python ropevim.%s(%s)\n ' % (name ,arg )+
300
- 'endfunction\n ' )
314
+ vim .command (
315
+ 'function! {0}()\n '
316
+ 'python ropevim.{1}({2})\n '
317
+ 'endfunction\n ' .format (_vim_name (name ),name ,arg )
318
+ )
301
319
302
320
def _completion_data (self ,proposal ):
303
321
return proposal
@@ -317,7 +335,7 @@ def _extended_completion(self, proposal):
317
335
318
336
if proposal .scope == 'parameter_keyword' :
319
337
default = proposal .get_default ()
320
- ci ["menu" ]+= '*' if default is None else '=%s' % default
338
+ ci ["menu" ]+= '*' if default is None else '={0}' . format ( default )
321
339
322
340
if self .preview and not ci ['menu' ]:
323
341
doc = proposal .get_doc ()
@@ -328,9 +346,9 @@ def _extended_completion(self, proposal):
328
346
def _conv (self ,obj ):
329
347
if isinstance (obj ,dict ):
330
348
return u'{' + u',' .join ([
331
- u"%s:%s" % (self ._conv (key ),self ._conv (value ))
349
+ u"{0}:{1}" . format (self ._conv (key ),self ._conv (value ))
332
350
for key ,value in obj .iteritems ()])+ u'}'
333
- return u'"%s"' % str (obj ).replace (u'"' ,u'\\ "' )
351
+ return u'"{0}"' . format ( str (obj ).replace (u'"' ,u'\\ "' ) )
334
352
335
353
336
354
def _vim_name (name ):
@@ -344,31 +362,38 @@ class VimProgress(object):
344
362
def __init__ (self ,name ):
345
363
self .name = name
346
364
self .last = 0
347
- status ('%s ... ' % self .name )
365
+ status ('{0} ... ' . format ( self .name ) )
348
366
349
367
def update (self ,percent ):
350
368
try :
351
369
vim .eval ('getchar(0)' )
352
370
except vim .error :
353
- raise KeyboardInterrupt ('Task %s was interrupted!' % self .name )
371
+ raise KeyboardInterrupt (
372
+ 'Task {0} was interrupted!' .format (self .name )
373
+ )
354
374
if percent > self .last + 4 :
355
- status ('%s ...%s%%%%' % (self .name ,percent ))
375
+ status ('{0} ...{1}%' . format (self .name ,percent ))
356
376
self .last = percent
357
377
358
378
def done (self ):
359
- status ('%s ... done' % self .name )
379
+ status ('{0} ... done' . format ( self .name ) )
360
380
361
381
362
382
def echo (message ):
383
+ if _rope_quiet :
384
+ return
363
385
if isinstance (message ,unicode ):
364
386
message = message .encode (vim .eval ('&encoding' ))
365
387
print message
366
388
367
389
368
390
def status (message ):
391
+ if _rope_quiet :
392
+ return
393
+
369
394
if isinstance (message ,unicode ):
370
395
message = message .encode (vim .eval ('&encoding' ))
371
- vim .command ('redraw | echon "%s"' % message )
396
+ vim .command ('redraw | echon "{0}"' . format ( message ) )
372
397
373
398
374
399
def call (command ):
@@ -395,13 +420,37 @@ def __call__(self, arg_lead, cmd_line, cursor_pos):
395
420
else :
396
421
result = [proposal for proposal in self .values
397
422
if proposal .startswith (arg_lead )]
398
- vim .command ('let s:completions = %s' % result )
423
+ vim .command ('let s:completions = {0}' .format (result ))
424
+
425
+
426
+ class RopeMode (interface .RopeMode ):
427
+ @decorators .global_command ('o' )
428
+ def open_project (self ,root = None ,quiet = False ):
429
+ global _rope_quiet
430
+ _rope_quiet = quiet
431
+
432
+ super (RopeMode ,self ).open_project (root = root )
433
+ rope_project_dir = os .path .join (self .project .address ,'.ropeproject' )
434
+ vimfiles = glob .glob (os .path .join (rope_project_dir ,'*.vim' ))
435
+
436
+ if not vimfiles :
437
+ return
438
+
439
+ txt = 'Sourcing vim files under\' .ropeproject/\' '
440
+ progress = self .env .create_progress (txt )
441
+ for idx ,vimfile in enumerate (sorted (vimfiles )):
442
+ progress .name = txt + ' ({0})' .format (os .path .basename (vimfile ))
443
+ vim .command (':silent source {0}' .format (vimfile ))
444
+ progress .update (idx * 100 / len (vimfiles ))
399
445
446
+ progress .name = txt
447
+ progress .done ()
448
+ echo ('Project opened!' )
400
449
401
450
decorators .logger .message = echo
402
451
decorators .logger .only_short = True
403
452
404
453
_completer = _ValueCompleter ()
405
454
406
455
_env = VimUtils ()
407
- _interface = interface . RopeMode (env = _env )
456
+ _interface = RopeMode (env = _env )