-
Notifications
You must be signed in to change notification settings - Fork 31
/
__init__.py
1058 lines (835 loc) · 36 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import asyncio
from collections import Counter
import concurrent.futures
import difflib
import fnmatch
import functools
import glob
import heapq
import importlib
from importlib.util import find_spec
import io
import itertools
import os
from pathlib import Path
import re
import shutil
import stat
import subprocess
import sys
from tempfile import TemporaryFile
import threading
import time
if find_spec('pynvim'):
import pynvim as neovim
else:
import neovim
@neovim.plugin
class Wilder(object):
def __init__(self, nvim):
self.nvim = nvim
self.has_init = False
self.events = []
self.events_lock = threading.Lock()
self.executor = None
self.cached_buffer = {'bufnr': -1, 'undotree_seq_cur': -1, 'buffer': []}
self.run_id = -1
self.find_files_lock = threading.Lock()
self.find_files_timestamp = -1
self.find_files_cache = dict()
self.help_tags_lock = threading.Lock()
self.help_tags_session_id = -1
self.help_tags_result = None
self.added_sys_path = set()
def resolve(self, ctx, x):
self.nvim.async_call(self._resolve, ctx, x)
def _resolve(self, ctx, x):
self.nvim.call('wilder#resolve', ctx, x)
def reject(self, ctx, x):
self.nvim.async_call(self._reject, ctx, x)
def _reject(self, ctx, x):
self.nvim.call('wilder#reject', ctx, x)
def echomsg(self, x):
self.nvim.session.threadsafe_call(lambda: self.nvim.command('echomsg "' + x + '"'))
def run_in_background(self, fn, args):
event = threading.Event()
ctx = args[0]
old_events = []
with self.events_lock:
run_id = ctx['run_id']
if run_id < self.run_id:
return
if run_id > self.run_id:
self.run_id = run_id
old_events = self.events
self.events = []
self.events.append(event)
for ev in old_events:
ev.set()
return self.executor.submit(functools.partial( fn, *([event] + args), ))
@neovim.function('_wilder_init', sync=True)
def _init(self, args):
if self.has_init:
return
self.has_init = True
opts = args[0]
self.executor = concurrent.futures.ThreadPoolExecutor(max_workers=opts['num_workers'])
def add_sys_path(self, path):
path = os.path.expanduser(path)
if not path in self.added_sys_path:
self.added_sys_path.add(path)
sys.path.insert(0, path)
@neovim.function('_wilder_python_file_finder', sync=False, allow_nested=True)
def _file_finder(self, args):
self.run_in_background(self.file_finder_handler, args)
def file_finder_handler(self, event, ctx, opts, command, filters, cwd, path, query, find_dir, timestamp):
try:
if not path:
path = cwd
path = Path(os.path.expanduser(path)).resolve()
path_str = str(path)
key = str(path) + ':' + str(command)
result = None
with self.find_files_lock:
if timestamp > self.find_files_timestamp:
self.find_files_timestamp = timestamp
for result in self.find_files_cache.values():
result['kill'].set()
self.find_files_cache = dict()
if key in self.find_files_cache:
result = self.find_files_cache[key]
else:
timeout_ms = opts['timeout'] if 'timeout' in opts else 5000
kill_event = threading.Event()
done_event = threading.Event()
result = {'kill': kill_event, 'done': done_event}
self.find_files_cache[key] = result
self.executor.submit(functools.partial( self.find_files_subprocess, *([command, path, timeout_ms, result]), ))
while True:
if result['done'].is_set():
break
if event.wait(timeout=0.01):
return
if 'timeout' in result:
self.resolve(ctx, False)
return
if 'error' in result:
self.reject(ctx, 'python_file_finder: %s %s' % (command, result['error']))
return
candidates = result['files']
if not candidates:
return self.resolve(ctx, [])
if query:
for filter in filters:
filter_name = filter['name']
filter_opts = filter['opts']
if filter_name == 'cpsm_filter':
filter_opts['ispath'] = True
candidates = self.cpsm_filt(event, filter_opts, candidates, query)
elif filter_name == 'clap_filter':
candidates = self.clap_filt(event, filter_opts, candidates, query)
elif filter_name == 'fruzzy_filter':
candidates = self.fruzzy_filt(event, filter_opts, candidates, query)
elif filter_name == 'fuzzy_filter':
case_sensitive = filter_opts['case_sensitive'] if 'case_sensitive' in filter_opts else 2
pattern = self.make_fuzzy_pattern(query, case_sensitive)
candidates = self.fuzzy_filt(event, filter_opts, candidates, pattern)
elif filter_name == 'difflib_sorter':
candidates = self.difflib_sort(event, filter_opts, candidates, query)
elif filter_name == 'fuzzywuzzy_sorter':
candidates = self.fuzzywuzzy_sort(event, filter_opts, candidates, query)
else:
raise Exception('Unsupported filter: ' + filter_name)
if candidates is None or event.is_set():
return
candidates = list(candidates)
if event.is_set():
return
relative_to_cwd = opts['relative_to_cwd'] if 'relative_to_cwd' in opts else True
if relative_to_cwd:
relpath = os.path.relpath(path, cwd)
if relpath != '.':
candidates = [os.path.join(relpath, c) for c in candidates]
if find_dir:
candidates = [c + os.sep if c and c[-1] != os.sep else c for c in candidates]
self.resolve(ctx, candidates)
except Exception as e:
self.reject(ctx, 'python_file_finder: ' + str(e))
def find_files_subprocess(self, command, path, timeout_ms, result):
try:
with TemporaryFile() as output:
with subprocess.Popen(
command, bufsize=0, stdin=subprocess.DEVNULL,
stdout=output, stderr=subprocess.PIPE, cwd=path) as p:
start_time = time.time()
while p.poll() is None:
if time.time() - start_time >= timeout_ms / 1000:
result['timeout'] = 'timeout after %dms' % (timeout_ms)
p.kill()
break
if result['kill'].wait(timeout=0.01):
p.kill()
break
if p.returncode is not 0:
has_error = False
# rg sets error code 2 for partial matches (e.g. some files can't be read), which we are fine with
if command[0] != 'rg' or p.returncode is not 2:
has_error = True
else:
output.seek(0)
# check if return code 2 is due to partial match by checking if stdout is populated
# if stdout is empty, treat it as an error
if not output.read(1):
has_error = True
if has_error:
pattern = re.compile(r'(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]')
error = p.stderr.read().decode('utf-8')
error = pattern.sub('', error)
result['error'] = 'return code %d:\n%s' % (p.returncode, error)
return
output.seek(0)
buff = output.read()
result['files'] = [line.decode('utf-8') for line in buff.splitlines()]
except Exception as e:
result['error'] = repr(e)
finally:
result['done'].set()
@neovim.function('_wilder_python_sleep', sync=False, allow_nested=True)
def _sleep(self, args):
self.run_in_background(self.sleep_handler, args)
def sleep_handler(self, event, ctx, t, x):
if event.is_set():
return
time.sleep(t)
self.resolve(ctx, x)
@neovim.function('_wilder_python_search', sync=False, allow_nested=True)
def _search(self, args):
bufnr = self.nvim.current.buffer.number
undotree_seq_cur = self.nvim.eval('undotree().seq_cur')
if (bufnr != self.cached_buffer['bufnr'] or
undotree_seq_cur != self.cached_buffer['undotree_seq_cur']):
self.cached_buffer = {
'bufnr': bufnr,
'undotree_seq_cur': undotree_seq_cur,
'buffer': list(self.nvim.current.buffer),
}
self.run_in_background(self.search_handler, args + [self.cached_buffer['buffer']])
def search_handler(self, event, ctx, *args):
try:
candidates = list(self.search(event, *args))
if event.is_set():
return
self.resolve(ctx, candidates)
except Exception as e:
self.reject(ctx, 'python_search: ' + str(e))
def search(self, event, opts, x, buf):
module_name = opts['engine'] if 'engine' in opts else 're'
max_candidates = opts['max_candidates'] if 'max_candidates' in opts else 300
re = importlib.import_module(module_name)
# re2 does not use re.UNICODE by default
pattern = re.compile(x, re.UNICODE)
seen = set()
checker = EventChecker(event)
for line in buf:
for match in pattern.finditer(line):
if checker.check():
return
candidate = match.group()
if not candidate in seen:
seen.add(candidate)
yield candidate
if max_candidates > 0 and len(seen) >= max_candidates:
return
@neovim.function('_wilder_python_uniq_filt', sync=False, allow_nested=True)
def _uniq_filt(self, args):
self.run_in_background(self.uniq_filt_handler, args)
def uniq_filt_handler(self, event, ctx, candidates):
if event.is_set():
return
seen = set()
try:
res = [x for x in candidates if not (x in seen or seen.add(x))]
self.resolve(ctx, res)
except Exception as e:
self.reject(ctx, 'python_uniq_filt: ' + str(e))
@neovim.function('_wilder_python_lexical_sort', sync=False, allow_nested=True)
def _sort(self, args):
self.run_in_background(self.lexical_sort_handler, args)
def lexical_sort_handler(self, event, ctx, candidates):
if event.is_set():
return
try:
res = sorted(candidates)
self.resolve(ctx, res)
except Exception as e:
self.reject(ctx, 'python_lexical_sort: ' + str(e))
# sync=True as it needs to query nvim for some data
@neovim.function('_wilder_python_get_file_completion', sync=True, allow_nested=True)
def _get_file_completion(self, args):
expand_arg = args[1]
expand_type = args[2]
cwd = self.nvim.eval('getcwd()')
wildignore_opt = self.nvim.eval('&wildignore')
current_file_dir = self.nvim.eval('expand("%:p:h")')
add_dot = False
if expand_type == 'file_in_path':
path_opt = self.nvim.eval('&path')
directories = path_opt.split(',')
elif expand_type == 'shellcmd':
directories = []
if expand_arg:
if expand_arg[0:2] == './':
directories = [cwd]
else:
relpath = os.path.relpath(expand_arg, cwd)
if relpath[0:2] == '..':
add_dot = True
directories = [cwd]
if not directories:
path = os.environ['PATH']
directories = path.split(':')
directories += [cwd]
else:
directories = [cwd]
self.run_in_background(self.get_file_completion_handler, args + [wildignore_opt, directories, add_dot, cwd, current_file_dir])
def get_file_completion_handler(self,
event,
ctx,
expand_arg,
expand_type,
has_wildcard,
path_prefix,
wildignore_opt,
directories,
add_dot,
cwd,
current_file_dir):
if event.is_set():
return
try:
original_has_wildcard = has_wildcard
res = list()
seen_files = set()
wildignore_list = wildignore_opt.split(',')
is_file_in_path = expand_type == 'file_in_path'
visited_directories = set()
checker = EventChecker(event)
for directory in directories:
if checker.check():
return
if is_file_in_path:
if not directory:
directory = cwd
elif directory == '.':
directory = current_file_dir
if not directory:
continue
if directory in visited_directories:
continue
visited_directories.add(directory)
has_wildcard = original_has_wildcard or '*' in directory
if has_wildcard:
tail = os.path.basename(expand_arg)
show_hidden = tail.startswith('.')
pattern = ''
if is_file_in_path:
if directory == '**':
wildcard = os.path.join(cwd, '**')
else:
wildcard = directory
elif expand_arg:
wildcard = os.path.join(directory, expand_arg)
else:
wildcard = directory
wildcard = os.path.expandvars(wildcard)
it = glob.iglob(wildcard, recursive=True)
else:
if add_dot:
path = os.path.join('.', directory, expand_arg)
else:
path = os.path.join(directory, expand_arg)
head, tail = os.path.split(path)
show_hidden = tail.startswith('.')
if is_file_in_path:
pattern = ''
else:
pattern = tail + '*'
try:
it = os.scandir(head)
except FileNotFoundError:
continue
except NotADirectoryError:
continue
for entry in it:
if checker.check():
return
try:
if has_wildcard:
entry = Path(entry)
try:
entry = entry.relative_to(directory)
old_entry = Path(entry)
except ValueError:
pass
if entry.name.startswith('.') and not show_hidden:
continue
if expand_type == 'dir' and not entry.is_dir():
continue
ignore = False
for wildignore in wildignore_list:
if fnmatch.fnmatch(entry.name, wildignore):
ignore = True
break
if ignore:
continue
if not has_wildcard and pattern and not fnmatch.fnmatch(entry.name, pattern):
continue
if expand_type == 'shellcmd' and entry.is_file():
if has_wildcard and not entry.stat().st_mode & stat.S_IXUSR:
continue
elif not has_wildcard and not os.access(entry, os.X_OK):
continue
if has_wildcard and Path(entry) == Path(path_prefix):
continue
if is_file_in_path and directory == '**' and Path(entry) == Path(cwd):
continue
if is_file_in_path:
# iglob and scandir return different types
entry_name = str(entry) if has_wildcard else entry.path
entry_name = self.get_path_relative_to_cwd(entry_name, cwd)
else:
entry_name = str(entry) if has_wildcard else entry.name
if entry.is_dir():
entry_name += os.sep
if entry_name in seen_files:
continue
seen_files.add(entry_name)
res.append(entry_name)
except OSError:
pass
res = sorted(res)
if not original_has_wildcard and not is_file_in_path:
head = os.path.dirname(expand_arg)
res = list(map(lambda f: os.path.join(head, f) if head else f, res))
if expand_arg == '.':
res.insert(0, '../')
res.insert(0, './')
elif expand_arg == '..':
res.insert(0, '../')
self.resolve(ctx, res)
except Exception as e:
self.reject(ctx, 'python_get_file_completion: ' + str(e))
# Returns True if p2 is a descendant of p1
def is_descendant_path(self, p1, p2):
return os.path.relpath(p2, p1)[0:2] != '..'
def get_basename(self, f, is_dir):
if is_dir:
return os.path.basename(f[:-1]) + os.sep
return os.path.basename(f)
def get_path_relative_to_cwd(self, p, cwd):
if not self.is_descendant_path(cwd, p):
return p
p = os.path.relpath(p, cwd)
if not p.startswith('/'):
p = os.path.join('.', p)
return p
@neovim.function('_wilder_python_get_help_tags', sync=False, allow_nested=True)
def _get_help_tags(self, args):
self.run_in_background(self.get_help_tags_handler, args)
def get_help_tags_handler(self, event, ctx, rtp, helplang):
try:
result = None
with self.help_tags_lock:
if ctx['session_id'] > self.help_tags_session_id:
self.help_tags_session_id = ctx['session_id']
if self.help_tags_result is not None:
self.help_tags_result['kill'].set()
self.help_tags_result = None
if self.help_tags_result is not None:
result = self.help_tags_result
else:
kill_event = threading.Event()
done_event = threading.Event()
result = {'kill': kill_event, 'done': done_event}
self.help_tags_result = result
self.executor.submit(functools.partial(self.get_help_tags_thread, result, rtp, helplang))
while True:
if result['done'].is_set():
break
if event.wait(timeout=0.01):
return
if 'error' in result:
self.reject(ctx, 'python_get_help_tags: ' + result['error'])
return
self.resolve(ctx, result['tags'])
except Exception as e:
self.reject(ctx, 'python_get_help_tags: ' + str(e))
def get_help_tags_thread(self, result, rtp, helplang):
try:
directories = rtp.split(',')
if not helplang:
langs = ['en']
else:
langs = helplang.split(',')
if not 'en' in langs:
langs.append('en')
default_lang = langs[0]
lang_tags_dict = dict()
tag_counter = Counter()
for lang in langs:
lang_tags_dict[lang] = set()
checker = EventChecker(result['kill'])
for directory in directories:
tags_path = os.path.join(directory, 'doc', 'tags*')
it = glob.iglob(tags_path)
for name in it:
if checker.check():
return
tail = os.path.basename(name)
if tail == 'tags':
lang = 'en'
# tags-zz
elif len(tail) == 7 and tail.startswith('tags-'):
lang = tail[5:7]
else:
continue
if lang not in langs:
langs.append(lang)
if lang not in lang_tags_dict:
lang_tags_dict[lang] = set()
if not os.path.isfile(name) or not os.access(name, os.R_OK):
continue
try:
with open(name, 'r') as f:
for line in f.readlines():
if line.startswith('!_TAG_'):
continue
columns = line.split("\t")
if not len(columns):
continue
tag = columns[0]
tag_counter.update([tag])
lang_tags_dict[lang].add(tag)
except UnicodeDecodeError:
continue
except IOError:
continue
tags = list(lang_tags_dict[default_lang])
for lang in lang_tags_dict:
if lang == default_lang:
continue
lang_tags = lang_tags_dict[lang]
for tag in lang_tags:
if tag in lang_tags_dict[default_lang]:
continue
elif tag_counter.get(tag) == 1:
tags.append(tag)
else:
tags.append(tag + '@' + lang)
result['tags'] = sorted(tags)
except Exception as e:
result['error'] = str(e)
finally:
result['done'].set()
@neovim.function('_wilder_python_get_users', sync=False, allow_nested=True)
def _get_users(self, args):
self.run_in_background(self.get_users_handler, args)
def get_users_handler(self, event, ctx, expand_arg, expand_type):
if event.is_set():
return
if os.name == 'nt':
self.resolve(ctx, [])
return
try:
res = []
pwd = importlib.import_module('pwd')
for user in pwd.getpwall():
if user.pw_name.startswith(expand_arg):
res.append(user.pw_name)
res = sorted(res)
self.resolve(ctx, res)
except Exception as e:
self.reject(ctx, 'python_get_users: ' + str(e))
@neovim.function('_wilder_python_fuzzy_filt', sync=False, allow_nested=True)
def _fuzzy_filt(self, args):
self.run_in_background(self.fuzzy_filt_handler, args)
def fuzzy_filt_handler(self, event, ctx, *args):
try:
candidates = list(self.fuzzy_filt(event, *args))
if event.is_set():
return
self.resolve(ctx, candidates)
except Exception as e:
self.reject(ctx, 'python_fuzzy_filt: ' + str(e))
# case_sensitive: 0 - case insensitive
# 1 - case sensitive
# 2 - smartcase - x matches x|X, X matches X
def make_fuzzy_pattern(self, query, case_sensitive=2):
chars = list(query)
pattern = '(?i)' if not case_sensitive else ''
first = True
for char in chars:
if not first:
pattern += '.*?'
first = False
if char == '\\':
pattern += '\\\\'
elif (char == '.' or
char == '^' or
char == '$' or
char == '*' or
char == '+' or
char == '?' or
char == '|' or
char == '(' or
char == ')' or
char == '{' or
char == '}' or
char == '[' or
char == ']'):
pattern += '\\' + char + ''
else:
if case_sensitive == 2:
if char.isupper():
pattern += char
else:
pattern += '(' + char + '|' + char.upper() + ')'
else:
pattern += char
return pattern
def fuzzy_filt(self, event, opts, candidates, pattern):
engine = opts['engine'] if 'engine' in opts else 're'
re = importlib.import_module(engine)
# re2 does not use re.UNICODE by default
pattern = re.compile(pattern, re.UNICODE)
checker = EventChecker(event)
for candidate in candidates:
if checker.check():
return
if pattern.search(candidate):
yield candidate
@neovim.function('_wilder_python_fruzzy_filt', sync=False, allow_nested=True)
def _fruzzy_filt(self, args):
self.run_in_background(self.fruzzy_filt_handler, args)
def fruzzy_filt_handler(self, event, ctx, *args):
try:
candidates = list(self.fruzzy_filt(event, *args))
if event.is_set():
return
self.resolve(ctx, candidates)
except Exception as e:
self.reject(ctx, 'python_fruzzy_filt: ' + str(e))
def fruzzy_filt(self, *args):
opts = args[1]
if 'fruzzy_path' in opts:
self.add_sys_path(opts['fruzzy_path'])
use_native = opts['use_native'] if 'use_native' in opts else False
if use_native:
return self.fruzzy_filt_native(*args)
return self.fruzzy_filt_py(*args)
def fruzzy_filt_native(self, event, opts, candidates, query):
fruzzy_mod = importlib.import_module('fruzzy_mod')
limit = opts['limit'] if 'limit' in opts else 1000
indexes = fruzzy_mod.scoreMatchesStr(query, candidates, '', limit)
sorted_matches = []
for index, score in indexes:
sorted_matches.append(candidates[index])
return sorted_matches
def fruzzy_filt_py(self, event, opts, candidates, query):
fruzzy = importlib.import_module('fruzzy')
limit = opts['limit'] if 'limit' in opts else 1000
matches = fruzzy.fuzzyMatches(query, candidates, '', limit)
checker = EventChecker(event)
arr = []
for match in matches:
if checker.check():
return
arr.append(match)
sorted_matches = heapq.nlargest(limit, arr, key=lambda i: i[5])
return [match[0] for match in sorted_matches]
@neovim.function('_wilder_python_cpsm_filt', sync=False, allow_nested=True)
def _cpsm_filt(self, args):
self.run_in_background(self.cpsm_filt_handler, args)
def cpsm_filt_handler(self, event, ctx, *args):
try:
candidates = list(self.cpsm_filt(event, *args))
if event.is_set():
return
self.resolve(ctx, candidates)
except Exception as e:
self.reject(ctx, 'python_cpsm_filt: ' + str(e))
def cpsm_filt(self, event, opts, candidates, query):
if not candidates:
return candidates
if 'cpsm_path' in opts:
self.add_sys_path(opts['cpsm_path'])
ispath = opts['ispath'] if 'ispath' in opts else False
cpsm = importlib.import_module('cpsm_py')
return cpsm.ctrlp_match(candidates, query, ispath=ispath)[0]
@neovim.function('_wilder_python_clap_filt', sync=False, allow_nested=True)
def _clap_filt(self, args):
self.run_in_background(self.clap_filt_handler, args)
def clap_filt_handler(self, event, ctx, *args):
try:
candidates = list(self.clap_filt(event, *args))
if event.is_set():
return
self.resolve(ctx, candidates)
except Exception as e:
self.reject(ctx, 'python_clap_filt: ' + str(e))
def clap_filt(self, event, opts, candidates, query):
if not candidates:
return candidates
if 'clap_path' in opts:
clap_path_head = os.path.split(opts['clap_path'])[0]
self.add_sys_path(clap_path_head)
use_rust = opts['use_rust'] if 'use_rust' in opts else False
if use_rust:
fuzzymatch_rs = importlib.import_module('clap.fuzzymatch_rs')
result = fuzzymatch_rs.fuzzy_match(query, candidates, [], {'winwidth': '0'})
return result[1]
else:
scorer = importlib.import_module('clap.scorer')
result = []
NO_MATCH = float("-inf")
checker = EventChecker(event)
for candidate in candidates:
if checker.check():
return
score, indices = scorer.fzy_scorer(query, candidate)
if score != NO_MATCH:
result.append((candidate, score))
result.sort(key=lambda x: x[1], reverse=True)
return map(lambda x: x[0], result)
@neovim.function('_wilder_python_difflib_sort', sync=False, allow_nested=True)
def _difflib_sort(self, args):
self.run_in_background(self.difflib_sort_handler, args)
def difflib_sort_handler(self, event, ctx, *args):
if event.is_set():
return
try:
candidates = list(self.difflib_sort(event, *args))
if event.is_set():
return
self.resolve(ctx, candidates)
except Exception as e:
self.reject(ctx, 'python_difflib_sort: ' + str(e))
def difflib_sort(self, event, opts, candidates, query):
quick = opts['quick'] if 'quick' in opts else True
case_sensitive = opts['case_sensitive'] if 'case_sensitive' in opts else True
xs = [None] * len(candidates)
checker = EventChecker(event)
for index, candidate in enumerate(candidates):
if checker.check():
return
if case_sensitive:
matcher = difflib.SequenceMatcher(None, candidate, query)
else:
matcher = difflib.SequenceMatcher(None, candidate.lower(), query.lower())
score = -matcher.quick_ratio() if quick else -matcher.ratio()
xs[index] = (candidate, score,)
return [x[0] for x in sorted(xs, key=lambda x: x[1])]
@neovim.function('_wilder_python_fuzzywuzzy_sort', sync=False, allow_nested=True)
def _fuzzywuzzy_sort(self, args):
self.run_in_background(self.fuzzywuzzy_sort_handler, args)
def fuzzywuzzy_sort_handler(self, event, ctx, *args):
try:
candidates = list(self.fuzzywuzzy_sort(event, *args))
if event.is_set():
return
self.resolve(ctx, candidates)
except Exception as e:
self.reject(ctx, 'python_fuzzywuzzy_sort: ' + str(e))
def fuzzywuzzy_sort(self, event, opts, candidates, query):
fuzzy = importlib.import_module('fuzzywuzzy.fuzz')
partial = opts['partial'] if 'partial' in opts else True
ratio = fuzzy.partial_ratio if partial else fuzzy.ratio
xs = [None] * len(candidates)
checker = EventChecker(event)
for index, candidate in enumerate(candidates):
if checker.check():
return []
xs[index] = (candidate, -ratio(candidate, query),)
return [x[0] for x in sorted(xs, key=lambda x: x[1])]
@neovim.function('_wilder_python_basic_highlight', sync=True)
def _basic_highlight(self, args):
string = args[0]
query = args[1]
case_sensitive = args[2]
if not case_sensitive:
string = string.upper()
query = query.upper()
spans = []
span = [-1, 0]
byte_pos = 0
i = 0
j = 0
while i < len(string) and j < len(query):
match = string[i] == query[j]
if match:
j += 1
if span[0] == -1:
span[0] = byte_pos
span[1] += len(string[i].encode('utf-8'))
if not match and span[0] != -1:
spans.append(span)
span = [-1, 0]
byte_pos += len(string[i].encode('utf-8'))
i += 1
if span[0] != -1:
spans.append(span)
return spans
@neovim.function('_wilder_python_pcre2_highlight', sync=True)
def _pcre2_highlight(self, args):
pattern = args[0]
string = args[1]
module_name = args[2]
re = importlib.import_module(module_name)
match = re.search(pattern, string)
if not match or not match.lastindex:
return 0