Skip to content

Commit fa7f7fa

Browse files
committed
Merge pull request #7 from corvisa/fix-completions
Fix completions when multiple options are possible
2 parents 1538f0d + 68c625d commit fa7f7fa

File tree

1 file changed

+53
-13
lines changed

1 file changed

+53
-13
lines changed

completions.py

+53-13
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,17 @@
55
import json
66

77
import_regex = r"""^(local\s+)?([\w\d_]+)\s*=\s*require\s*\(?['"]?(.+?)['"]?\)?$"""
8-
assign_regex = r"""^[ \t]*(?:local\s+)?([\w\d_.,]+)\s*=\s*([\w\d_.:]+)\s*(?:\(.*\)\s*)?$"""
8+
9+
# assign_regex matches multiple types of lua assignments
10+
# examples:
11+
#
12+
# local setrow = chain:insert(value) -- with an ending comment
13+
# setrow = chain:insert(value) -- with an ending comment
14+
# local db = datastore.get_table(name, type)
15+
# db = datastore.get_table(name, type)
16+
# somevar = tbl.some_field
17+
# somevar = tbl.some_field -- with a comment
18+
assign_regex = r"""^[ \t]*(?:local\s+)?([\w\d_.,]+)\s*=\s*([\w\d_.:]+)\s*(?:\(.*\)\s*)?(?:--.*)?$"""
919

1020

1121
def is_summit_file(view):
@@ -33,6 +43,8 @@ class SummitCompletions:
3343
obj_types = {}
3444

3545
def __init__(self):
46+
self.completions = []
47+
self.obj_types = {}
3648
self.load_completions()
3749

3850
def load_completions(self):
@@ -98,18 +110,46 @@ def find_return_types(self, symbol, imports, objects):
98110
# in the data as an empty function)
99111
target_name = ''
100112

113+
rhs_types = []
101114
for found_type in found_types:
102-
type_opts = self.obj_types[found_type]
103-
104-
type_funcs = type_opts['functions']
105-
type_fields = type_opts['fields']
106-
107-
if target_name in type_funcs:
108-
if 'returns' in type_funcs[target_name]:
109-
return type_funcs[target_name]['returns']
110-
if target_name in type_fields:
111-
if 'returns' in type_fields[target_name]:
112-
return type_fields[target_name]['returns']
115+
# found_types are the potential types that obj_name
116+
# can be, so iterate over them and try to get the
117+
# function and field data for each of those types
118+
type_opts = self.obj_types.get(found_type)
119+
if type_opts:
120+
# found the type we were looking for, so see if we find
121+
# a match for the target (such as a function call) and
122+
# then add the return values for that function/field
123+
# to the correct set of returns
124+
type_funcs = type_opts['functions']
125+
if target_name in type_funcs:
126+
returns = type_funcs[target_name].get('returns')
127+
if returns:
128+
ret_types_diff = len(returns) - len(rhs_types)
129+
for _ in range(0, ret_types_diff):
130+
# Add any missing return sets to the list
131+
rhs_types.append(set())
132+
133+
for type_idx, ret_types in enumerate(returns):
134+
ret_idx_types = rhs_types[type_idx]
135+
for ret_type in ret_types:
136+
ret_idx_types.add(ret_type)
137+
138+
type_fields = type_opts['fields']
139+
if target_name in type_fields:
140+
returns = type_fields[target_name].get('returns')
141+
if returns:
142+
ret_types_diff = len(returns) - len(rhs_types)
143+
for _ in range(0, ret_types_diff):
144+
# Add any missing return sets to the list
145+
rhs_types.append(set())
146+
147+
for type_idx, ret_types in enumerate(returns):
148+
ret_idx_types = rhs_types[type_idx]
149+
for ret_type in ret_types:
150+
ret_idx_types.add(ret_type)
151+
152+
return rhs_types
113153

114154
return None
115155

@@ -202,7 +242,7 @@ def find_completions(self, view, prefix, imports, objects):
202242
# it looks weird and ends up with duplicates like:
203243
# mymenu
204244
# mymenu.
205-
if not c.endswith('.') and c not in used_names:
245+
if re.search(r'[\.:]$', c) is None and c not in used_names:
206246
comps.append((c, c))
207247

208248
comps.sort()

0 commit comments

Comments
 (0)