1
1
" Vim completion script
2
- " Version: 2.3
2
+ " Version: 2.3.1
3
3
" Language: Java
4
4
" Maintainer: artur shaik <ashaihullin@gmail.com>
5
- " Last Change: 2015-07-29
5
+ " Last Change: 2015-09-08
6
6
" Copyright: Copyright (C) 2006-2015 cheng fang, artur shaik. All rights reserved.
7
7
" License: Vim License (see vim's :help license)
8
8
@@ -251,13 +251,13 @@ endfunction
251
251
252
252
function ! javacomplete#ShowPort ()
253
253
if s: PollServer ()
254
- JavacompletePy vim .command (' echom "Javavi port: %d"' % bridgeState.port ())
254
+ JavacompletePy vim .command (' echo "Javavi port: %d"' % bridgeState.port ())
255
255
endif
256
256
endfunction
257
257
258
258
function ! javacomplete#ShowPID ()
259
259
if s: PollServer ()
260
- JavacompletePy vim .command (' echom "Javavi pid: %d"' % bridgeState.pid ())
260
+ JavacompletePy vim .command (' echo "Javavi pid: %d"' % bridgeState.pid ())
261
261
endif
262
262
endfunction
263
263
@@ -266,19 +266,40 @@ function! s:GetClassNameWithScope(...)
266
266
let curline = getline (' .' )
267
267
let word_l = offset - 1
268
268
let word_r = offset - 2
269
- while curline[word_l - 1 ] = ~ ' [. @A-Za-z0-9_]'
269
+ while curline[word_l - 1 ] = ~ ' [@A-Za-z0-9_]'
270
270
if curline[word_l - 1 ] == ' @'
271
271
break
272
272
endif
273
273
let word_l -= 1
274
274
endwhile
275
- while curline[word_r + 1 ] = ~ ' [. A-Za-z0-9_]'
275
+ while curline[word_r + 1 ] = ~ ' [A-Za-z0-9_]'
276
276
let word_r += 1
277
277
endwhile
278
278
279
279
return curline[word_l : word_r]
280
280
endfunction
281
281
282
+ function ! s: SortImports ()
283
+ let imports = s: GetImports (' imports' )
284
+ if (len (imports) > 0 )
285
+ let beginLine = imports[0 ][1 ]
286
+ let lastLine = imports[len (imports) - 1 ][1 ]
287
+ let importsList = []
288
+ for import in imports
289
+ call add (importsList, import [0 ])
290
+ endfor
291
+
292
+ call sort (importsList)
293
+ let saveCursor = getcurpos ()
294
+ silent execute beginLine.' ,' .lastLine. ' delete _'
295
+ for imp in importsList
296
+ call append (beginLine - 1 , ' import ' . imp . ' ;' )
297
+ let beginLine += 1
298
+ endfor
299
+ call setpos (' .' , saveCursor)
300
+ endif
301
+ endfunction
302
+
282
303
function ! s: AddImport (import )
283
304
let imports_fqn = s: GetImports (' imports_fqn' )
284
305
for import in imports_fqn
@@ -311,22 +332,13 @@ function! s:AddImport(import)
311
332
call append (insertline, ' import ' . a: import . ' ;' )
312
333
call append (insertline, ' ' )
313
334
else
314
- let idx = 0
315
- while idx < len (imports)
316
- let i = imports[idx][0 ]
317
- let list = [i , a: import ]
318
- call sort (list )
319
- if list [0 ] == a: import || idx == len (imports) - 1
320
- call append (imports[idx][1 ] - 1 , ' import ' . a: import . ' ;' )
321
- return
322
- endif
323
- let idx += 1
324
- endwhile
335
+ let lastLine = imports[len (imports) - 1 ][1 ]
336
+ call append (lastLine, ' import ' . a: import . ' ;' )
325
337
endif
326
338
327
339
endfunction
328
340
329
- function ! javacomplete#AddImport ()
341
+ function ! javacomplete#AddImport (... )
330
342
call javacomplete#StartServer ()
331
343
332
344
let i = 0
@@ -340,24 +352,93 @@ function! javacomplete#AddImport()
340
352
let i += 1
341
353
endwhile
342
354
343
- let response = s: RunReflection (" -class-packages" , classname, ' Filter packages to add import' )
355
+ let response = s: CommunicateToServer (" -class-packages" , classname, ' Filter packages to add import' )
344
356
if response = ~ ' ^['
345
357
let result = eval (response)
358
+ let import = ' '
346
359
if len (result) == 0
347
360
echo " JavaComplete: classname '" . classname. " ' not found in any scope."
348
- return
349
- endif
350
361
351
- let import = 0
352
- if len (result) > 1
362
+ elseif len (result) == 1
363
+ let import = result[0 ]
364
+
365
+ else
366
+ if exists (' g:ClassnameCompleted' ) && g: ClassnameCompleted
367
+ return
368
+ endif
369
+
353
370
let index = 0
354
371
for cn in result
355
372
echo " candidate [" . index . " ]: " . cn
356
373
let index += 1
357
374
endfor
358
- if exists (' g:ClassnameCompleted' ) && g: ClassnameCompleted
375
+ let userinput = input (' select one candidate [0]: ' , ' ' )
376
+ if empty (userinput)
359
377
let userinput = 0
378
+ elseif userinput = ~ ' ^[0-9]*$'
379
+ let userinput = str2nr (userinput)
360
380
else
381
+ let userinput = -1
382
+ endif
383
+ redraw !
384
+
385
+ if userinput < 0 || userinput >= len (result)
386
+ echo " JavaComplete: wrong input"
387
+ else
388
+ let import = result[userinput]
389
+ endif
390
+ endif
391
+
392
+ if ! empty (import )
393
+ call s: AddImport (import )
394
+ call s: SortImports ()
395
+ endif
396
+
397
+ endif
398
+
399
+ if a: 0 > 0 && a: 1
400
+ let cur = getcurpos ()
401
+ let cur[2 ] = cur[2 ] + 1
402
+ execute ' startinsert'
403
+ call setpos (' .' , cur)
404
+ endif
405
+ endfunction
406
+
407
+ function ! javacomplete#RemoveUnusedImports ()
408
+ let currentBuf = getline (1 ,' $' )
409
+ let current = join (currentBuf, ' <_javacomplete-linebreak>' )
410
+
411
+ let response = s: CommunicateToServer (' -unused-imports -content' , current, ' RemoveUnusedImports' )
412
+ if response = ~ ' ^['
413
+ let saveCursor = getcurpos ()
414
+ let unused = eval (response)
415
+ for unusedImport in unused
416
+ let imports = s: GetImports (' imports' )
417
+ for import in imports
418
+ if import [0 ] == unusedImport
419
+ silent execute import [1 ]. ' delete _'
420
+ endif
421
+ endfor
422
+ endfor
423
+ let saveCursor[1 ] = saveCursor[1 ] - len (unused)
424
+ call setpos (' .' , saveCursor)
425
+ endif
426
+ endfunction
427
+
428
+ function ! javacomplete#AddMissingImports ()
429
+ let currentBuf = getline (1 ,' $' )
430
+ let current = join (currentBuf, ' <_javacomplete-linebreak>' )
431
+
432
+ let response = s: CommunicateToServer (' -missing-imports -content' , current, ' RemoveUnusedImports' )
433
+ if response = ~ ' ^['
434
+ let missing = eval (response)
435
+ for import in missing
436
+ if len (import ) > 1
437
+ let index = 0
438
+ for cn in import
439
+ echo " candidate [" . index . " ]: " . cn
440
+ let index += 1
441
+ endfor
361
442
let userinput = input (' select one candidate [0]: ' , ' ' )
362
443
if empty (userinput)
363
444
let userinput = 0
@@ -367,20 +448,18 @@ function! javacomplete#AddImport()
367
448
let userinput = -1
368
449
endif
369
450
redraw !
370
- endif
371
-
372
- if userinput < 0 || userinput >= len (result)
373
- echo " JavaComplete: wrong input"
374
- return
375
- endif
376
451
377
- let import = result[userinput]
378
- else
379
- let import = result[0 ]
380
- endif
381
-
382
- call s: AddImport (import )
452
+ if userinput < 0 || userinput >= len (import )
453
+ echo " JavaComplete: wrong input"
454
+ continue
455
+ endif
383
456
457
+ call s: AddImport (import [userinput])
458
+ else
459
+ call s: AddImport (import [0 ])
460
+ endif
461
+ endfor
462
+ call s: SortImports ()
384
463
endif
385
464
endfunction
386
465
@@ -528,9 +607,9 @@ function! javacomplete#Complete(findstart, base)
528
607
" Try to complete incomplete class name
529
608
if b: context_type == s: CONTEXT_COMPLETE_CLASS && a: base = ~ ' ^[@A-Z][A-Za-z0-9_]*$'
530
609
if a: base = ~ s: RE_ANNOTATION
531
- let response = s: RunReflection (" -similar-annotations" , a: base [1 :], ' Filter packages by incomplete class name' )
610
+ let response = s: CommunicateToServer (" -similar-annotations" , a: base [1 :], ' Filter packages by incomplete class name' )
532
611
else
533
- let response = s: RunReflection (" -similar-classes" , a: base , ' Filter packages by incomplete class name' )
612
+ let response = s: CommunicateToServer (" -similar-classes" , a: base , ' Filter packages by incomplete class name' )
534
613
endif
535
614
if response = ~ ' ^['
536
615
let result = eval (response)
@@ -1387,7 +1466,7 @@ fu! s:SearchStaticImports(name, fullmatch)
1387
1466
endif
1388
1467
endfor
1389
1468
if commalist != ' '
1390
- let res = s: RunReflection (' -E' , commalist, ' s:SearchStaticImports in Batch' )
1469
+ let res = s: CommunicateToServer (' -E' , commalist, ' s:SearchStaticImports in Batch' )
1391
1470
if res = ~ " ^{'"
1392
1471
let dict = eval (res )
1393
1472
for key in keys (dict )
@@ -2621,15 +2700,16 @@ fu! s:Sort(ci)
2621
2700
return ci
2622
2701
endfu
2623
2702
2624
- " Function to run Reflection {{{2
2625
- fu ! s: RunReflection (option , args , log )
2703
+ " Function for server communication {{{2
2704
+ fu ! s: CommunicateToServer (option , args , log )
2626
2705
if ! s: PollServer ()
2627
2706
call javacomplete#StartServer ()
2628
2707
endif
2629
2708
2630
2709
if s: PollServer ()
2631
- let cmd = a: option . ' "' . a: args . ' "'
2632
- call s: Info (" RunReflection: " . cmd. " [" . a: log . " ]" )
2710
+ let args = substitute (a: args , ' "' , ' \\"' , ' g' )
2711
+ let cmd = a: option . ' "' . args . ' "'
2712
+ call s: Info (" CommunicateToServer: " . cmd. " [" . a: log . " ]" )
2633
2713
let a: result = " "
2634
2714
JavacompletePy << EOPC
2635
2715
vim .command (' let a:result = "%s"' % bridgeState.send (vim .eval (" cmd" )))
@@ -2713,7 +2793,7 @@ fu! s:FetchClassInfo(fqn)
2713
2793
return s: cache [a: fqn ]
2714
2794
endif
2715
2795
2716
- let res = s: RunReflection (' -E' , a: fqn , ' FetchClassInfo in Batch' )
2796
+ let res = s: CommunicateToServer (' -E' , a: fqn , ' FetchClassInfo in Batch' )
2717
2797
if res = ~ " ^{'"
2718
2798
let dict = eval (res )
2719
2799
for key in keys (dict )
@@ -2930,7 +3010,7 @@ endfunction
2930
3010
" See ClassInfoFactory.getClassInfo() in insenvim.
2931
3011
function ! s: DoGetReflectionClassInfo (fqn)
2932
3012
if ! has_key (s: cache , a: fqn )
2933
- let res = s: RunReflection (' -C' , a: fqn , ' s:DoGetReflectionClassInfo' )
3013
+ let res = s: CommunicateToServer (' -C' , a: fqn , ' s:DoGetReflectionClassInfo' )
2934
3014
if res = ~ ' ^{'
2935
3015
let s: cache [a: fqn ] = s: Sort (eval (res ))
2936
3016
elseif res = ~ ' ^['
@@ -3092,7 +3172,7 @@ fu! s:DoGetInfoByReflection(class, option)
3092
3172
return s: cache [a: class ]
3093
3173
endif
3094
3174
3095
- let res = s: RunReflection (a: option , a: class , ' s:DoGetInfoByReflection' )
3175
+ let res = s: CommunicateToServer (a: option , a: class , ' s:DoGetInfoByReflection' )
3096
3176
if res = ~ ' ^[{\[]'
3097
3177
let v = eval (res )
3098
3178
if type (v ) == type ([])
0 commit comments