Skip to content

Commit

Permalink
gocode: fix completion for funcs with no arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
yields committed Aug 20, 2019
1 parent 7639117 commit c0e70f3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -3,3 +3,5 @@
__pycache__/
*.pyc
*.sublime-package
*.mod
*.sum
18 changes: 14 additions & 4 deletions go/gocode.py
Expand Up @@ -61,6 +61,7 @@ def parse_func(item):
args, buf = parse_args(item['type'])
preview_args = []
named_args = []
snip = ''

for arg in args:
prev = '{name} {type}'.format(**arg)
Expand All @@ -71,19 +72,25 @@ def parse_func(item):
tmpl = '${{{}:{}}}'.format(i + 1, placeholder.strip())
named_args.append(tmpl)

if len(named_args) > 0:
print("named_args", args)
snip = '{}({})'.format(name, ', '.join(named_args))
else:
snip = '{}()$0'.format(name)

return {
'name': name,
'args': ', '.join(preview_args),
'rets': '\t-> ()',
'snip': '{}({})'.format(name, ', '.join(named_args)),
'snip': snip,
}

def parse_args(buf):
"""
Parse args parses all function arguments.
"""
buf = buf[len("func("):]
arg = {'name': '', 'type': ''}
arg = None
all = []

while len(buf) != 0:
Expand All @@ -100,11 +107,14 @@ def parse_args(buf):
continue

if buf[0] == ')':
all.append(arg)
if arg != None:
all.append(arg)
buf = buf[1:]
arg = arg.copy()
break

if arg == None:
arg = {'name': '', 'type': ''}

(v,) = re.findall(r'^ *([!\.\w{}\[\] ]+)(?:[,)]|func\()', buf)
buf = buf[len(v):]

Expand Down
11 changes: 11 additions & 0 deletions tests/test_gocode.py
Expand Up @@ -72,3 +72,14 @@ def test_parse_func_args():
'args': [{ 'type': 'int', 'name': '' }]
}]

def test_parse_func_no_args():
completion = gocode.parse({
'package': '',
'class': 'func',
'name': 'print',
'type': 'func()'
})
assert completion == [
"ƒ・print() \t\t-> ()",
'print()$0',
]

0 comments on commit c0e70f3

Please sign in to comment.