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
|
# ============================================================================
# FILE: util.py
# AUTHOR: Shougo Matsushita <Shougo.Matsu at gmail.com>
# License: MIT license
# ============================================================================
import json
import re
import os
import sys
import unicodedata
import glob
def get_buffer_config(context, filetype, buffer_var, user_var, default_var):
if buffer_var in context['bufvars']:
return context['bufvars'][buffer_var]
ft = filetype if (filetype in context['vars'][user_var] or
filetype in context['vars'][default_var]) else '_'
default = context['vars'][default_var].get(ft, '')
return context['vars'][user_var].get(ft, default)
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(runtimepath, path):
ret = []
for rtp in re.split(',', runtimepath):
ret += glob.glob(rtp + '/' + path)
return ret
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(encoding, input, pos):
return len(bytes(input[: pos], encoding))
def bytepos2charpos(encoding, input, pos):
return len(bytes(input, encoding)[: pos].decode(encoding))
def get_custom(custom, source_name, key, default):
if source_name not in custom:
return get_custom(custom, '_', key, default)
elif key in custom[source_name]:
return custom[source_name][key]
elif key in custom['_']:
return custom['_'][key]
else:
return default
def get_syn_name(vim):
return vim.call('deoplete#util#get_syn_name')
def parse_file_pattern(f, pattern):
p = re.compile(pattern)
ret = []
for l in f:
ret += p.findall(l)
return list(set(ret))
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
|