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
|
# ============================================================================
# FILE: util.py
# AUTHOR: Shougo Matsushita <Shougo.Matsu at gmail.com>
# License: MIT license
# ============================================================================
import json
import re
import os
import sys
import unicodedata
def get_buffer_config(vim, filetype, buffer_var, user_var, default_var):
return vim.call('deoplete#util#get_buffer_config',
filetype, buffer_var, user_var, default_var)
def get_simple_buffer_config(context, buffer_var, user_var):
return (context['bufvars'][buffer_var]
if buffer_var in context['bufvars']
else context['vars'][user_var])
def set_pattern(vim, variable, keys, pattern):
return vim.call('deoplete#util#set_pattern', variable, keys, pattern)
def set_list(vim, variable, keys, list):
return vim.call('deoplete#util#set_pattern', variable, keys, list)
def set_default(vim, var, val):
return vim.call('deoplete#util#set_default', var, val)
def convert2list(expr):
return (expr if isinstance(expr, list) else [expr])
def globruntime(vim, path):
return vim.funcs.globpath(vim.options['runtimepath'], path, 1, 1)
def debug(vim, expr):
if vim.vars['deoplete#enable_debug']:
try:
json_data = json.dumps(str(expr).strip())
except Exception:
vim.command('echomsg string(\'' + str(expr).strip() + '\')')
else:
vim.command('echomsg string(\'' + escape(json_data) + '\')')
else:
error(vim, "not in debug mode, but debug called")
def error(vim, msg):
vim.call('deoplete#util#print_error', msg)
def escape(expr):
return expr.replace("'", "''")
def charpos2bytepos(vim, encoding, input, pos):
return len(bytes(input[: pos], encoding))
def bytepos2charpos(vim, input, pos):
return len(vim.funcs.substitute(
vim.funcs.strpart(input, 0, pos), '.', 'x', 'g'))
def get_custom(vim, source_name):
return vim.call('deoplete#custom#get', source_name)
def get_syn_name(vim):
return vim.call('deoplete#util#get_syn_name')
def parse_file_pattern(f, pattern):
p = re.compile(pattern)
return list(set(p.findall('\n'.join(f.read()))))
def parse_buffer_pattern(b, pattern, complete_str):
p = re.compile(pattern)
return [x for x in p.findall('\n'.join(b)) if x != complete_str]
def fuzzy_escape(string, camelcase):
# Escape string for python regexp.
p = re.sub(r'([a-zA-Z0-9_])', r'\1.*', re.escape(string))
if camelcase and re.search(r'[A-Z]', string):
p = re.sub(r'([a-z])', (lambda pat:
'['+pat.group(1)+pat.group(1).upper()+']'), p)
p = re.sub(r'([a-zA-Z0-9_])\.\*', r'\1[^\1]*', p)
return p
def load_external_module(file, module):
current = os.path.dirname(os.path.abspath(file))
module_dir = os.path.join(os.path.dirname(current), module)
sys.path.insert(0, module_dir)
def truncate_skipping(string, max, footer, footer_len):
if len(string) <= max/2:
return string
if strwidth(string) <= max:
return string
footer += string[
-len(truncate(string[::-1], footer_len)):]
return truncate(string, max - strwidth(footer)) + footer
def truncate(string, max):
if len(string) <= max/2:
return string
if strwidth(string) <= max:
return string
width = 0
ret = ''
for c in string:
wc = charwidth(c)
if width + wc > max:
break
ret += c
width += wc
return ret
def strwidth(string):
width = 0
for c in string:
width += charwidth(c)
return width
def charwidth(c):
wc = unicodedata.east_asian_width(c)
return 2 if wc == 'F' or wc == 'W' else 1
|