Skip to content

Commit

Permalink
Merge pull request #376 from Cnly/improve-big-evals
Browse files Browse the repository at this point in the history
Improve evaluation experience for large values
  • Loading branch information
liquidz authored Dec 21, 2021
2 parents 3005526 + dd9c323 commit 383705f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 61 deletions.
2 changes: 1 addition & 1 deletion autoload/iced/nrepl.vim
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ function! s:dispatcher(ch, resp) abort
"" To avoid freezing vim/nvim when too large values are returned.
"" c.f. https://github.com/liquidz/vim-iced/issues/219
if text_len > g:iced#nrepl#buffer_size
call iced#message#error('too_large_values')
call iced#message#warning('too_large_values')
if g:iced#nrepl#skip_evaluation_when_buffer_size_is_exceeded
let s:response_buffer = ''
let s:messages = {}
Expand Down
4 changes: 2 additions & 2 deletions message/iced/en.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
'timeout': 'Timed out.',
'toggle_warn_on_reflection': 'Toggled *warn-on-reflection* to %s.',
'too_deep_to_slurp': 'Too deep to slurp.',
'too_large_values': 'Timed out because evaluated values are too large.',
'too_large_values': 'Evaluating values too large may freeze vim. See: g:iced#nrepl#skip_evaluation_when_buffer_size_is_exceeded',
'try_connect': 'Not connected. Try `:IcedConnect <port>`',
'unaliased': 'Unaliased "%s".',
'undefined': 'Undefined %s.',
Expand All @@ -117,4 +117,4 @@

'___EOF___': ''
}
#vim:ft=ruby
# vim:ft=ruby
119 changes: 61 additions & 58 deletions python/bencode.py
Original file line number Diff line number Diff line change
@@ -1,76 +1,80 @@
#!/usr/bin/python3

def __decode_string(s):
def __decode_string(b, start=0):
"""
>>> __decode_string('3:foo3:bar')
{'value': 'foo', 'rest': '3:bar'}
>>> __decode_string('0:3:bar')
{'value': '', 'rest': '3:bar'}
>>> __decode_string(b'3:foo3:bar')
{'value': 'foo', 'start': 5}
>>> __decode_string(b'3:foo3:bar', start=5)
{'value': 'bar', 'start': 10}
>>> __decode_string(b'0:3:bar')
{'value': '', 'start': 2}
"""
i = s.find(':')
if i == -1:
raise Exception('failed to decode string token', s)
else:
l = int(s[:i])
b = s[i+1:].encode()
return {'value': b[:l].decode(encoding='utf-8'), 'rest': b[l:].decode(encoding='utf-8')}
cpos = b.find(b':', start)
if cpos == -1:
raise Exception('failed to decode string token', b[start:])
l = int(b[start:cpos])
return {'value': b[cpos+1:cpos+1+l].decode(encoding='utf-8'), 'start': cpos+1+l}

def __decode_integer(s):
def __decode_integer(b, start=0):
"""
>>> __decode_integer('i123e3:foo')
{'value': 123, 'rest': '3:foo'}
>>> __decode_integer(b'i123e3:foo')
{'value': 123, 'start': 5}
>>> __decode_integer(b'i123ei4567e', start=5)
{'value': 4567, 'start': 11}
"""
i = s.find('e')
if i == -1:
epos = b.find(b'e', start)
if epos == -1:
raise Exception('failed to decode integer token')
else:
return {'value': int(s[1:i]), 'rest': s[i+1:]}
return {'value': int(b[start+1:epos]), 'start': epos+1}

def __decode_list(s):
def __decode_list(b, start=0):
"""
>>> __decode_list('li123e3:foo0:e3:bar')
{'value': [123, 'foo', ''], 'rest': '3:bar'}
>>> __decode_list('le3:bar')
{'value': [], 'rest': '3:bar'}
>>> __decode_list('llee3:bar')
{'value': [[]], 'rest': '3:bar'}
>>> __decode_list(b'li123e3:foo0:e3:bar')
{'value': [123, 'foo', ''], 'start': 14}
>>> __decode_list(b'lel3:bare')
{'value': [], 'start': 2}
>>> __decode_list(b'lel3:bare', start=2)
{'value': ['bar'], 'start': 9}
>>> __decode_list(b'llee3:bar')
{'value': [[]], 'start': 4}
"""
rest = s[1:]
start += 1
result = []
while rest[0] != 'e':
decoded = __decode(rest)
while chr(b[start]) != 'e':
decoded = __decode(b, start)
result.append(decoded['value'])
rest = decoded['rest']
return {'value': result, 'rest': rest[1:]}
start = decoded['start']
return {'value': result, 'start': start+1}

def __decode_dict(s):
def __decode_dict(b, start=0):
"""
>>> __decode_dict('d3:fooi123ee3:bar')
{'value': {'foo': 123}, 'rest': '3:bar'}
>>> __decode_dict('de3:bar')
{'value': {}, 'rest': '3:bar'}
>>> __decode_dict(b'd3:fooi123ee3:bar')
{'value': {'foo': 123}, 'start': 12}
>>> __decode_dict(b'de3:bar')
{'value': {}, 'start': 2}
>>> __decode_dict(b'ded3:bari1ee', start=2)
{'value': {'bar': 1}, 'start': 12}
"""
rest = s[1:]
start += 1
result = {}
while rest[0] != 'e':
k = __decode(rest)
v = __decode(k['rest'])
while chr(b[start]) != 'e':
k = __decode(b, start)
v = __decode(b, k['start'])
result[k['value']] = v['value']
rest = v['rest']
return {'value': result, 'rest': rest[1:]}
start = v['start']
return {'value': result, 'start': start+1}

def __decode(s):
c = s[0]
def __decode(b, start=0):
c = chr(b[start])
if c in {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}:
return __decode_string(s)
return __decode_string(b, start)
elif c == 'i':
return __decode_integer(s)
return __decode_integer(b, start)
elif c == 'l':
return __decode_list(s)
return __decode_list(b, start)
elif c == 'd':
return __decode_dict(s)
else:
raise Exception('invalid token')
return __decode_dict(b, start)
raise Exception('invalid token ' + c)

def iced_bencode_decode(s):
"""
Expand All @@ -92,16 +96,16 @@ def iced_bencode_decode(s):
'__FAILED__'
"""
result = []
rest = s
b = s.encode('utf-8')
start = 0
try:
while rest != '':
ret = __decode(rest)
while start < len(b):
ret = __decode(b, start)
result.append(ret['value'])
rest = ret['rest']
start = ret['start']
if len(result) == 1:
return result[0]
else:
return result
return result
except Exception:
return '__FAILED__'

Expand Down Expand Up @@ -135,5 +139,4 @@ def iced_vim_repr(x):
elif t is dict:
ret = [iced_vim_repr(k) + ':' + iced_vim_repr(v) for k,v in x.items()]
return '{' + ','.join(ret) + '}'
else:
raise Exception
raise Exception

0 comments on commit 383705f

Please sign in to comment.