|
5 | 5 | import json
|
6 | 6 |
|
7 | 7 | 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*)?(?:--.*)?$""" |
9 | 19 |
|
10 | 20 |
|
11 | 21 | def is_summit_file(view):
|
@@ -33,6 +43,8 @@ class SummitCompletions:
|
33 | 43 | obj_types = {}
|
34 | 44 |
|
35 | 45 | def __init__(self):
|
| 46 | + self.completions = [] |
| 47 | + self.obj_types = {} |
36 | 48 | self.load_completions()
|
37 | 49 |
|
38 | 50 | def load_completions(self):
|
@@ -98,18 +110,46 @@ def find_return_types(self, symbol, imports, objects):
|
98 | 110 | # in the data as an empty function)
|
99 | 111 | target_name = ''
|
100 | 112 |
|
| 113 | + rhs_types = [] |
101 | 114 | 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 |
113 | 153 |
|
114 | 154 | return None
|
115 | 155 |
|
@@ -202,7 +242,7 @@ def find_completions(self, view, prefix, imports, objects):
|
202 | 242 | # it looks weird and ends up with duplicates like:
|
203 | 243 | # mymenu
|
204 | 244 | # 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: |
206 | 246 | comps.append((c, c))
|
207 | 247 |
|
208 | 248 | comps.sort()
|
|
0 commit comments