Skip to content

Commit

Permalink
Merge pull request #7 from corvisa/fix-completions
Browse files Browse the repository at this point in the history
Fix completions when multiple options are possible
  • Loading branch information
aphistic committed Jan 31, 2015
2 parents 1538f0d + 68c625d commit fa7f7fa
Showing 1 changed file with 53 additions and 13 deletions.
66 changes: 53 additions & 13 deletions completions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,17 @@
import json

import_regex = r"""^(local\s+)?([\w\d_]+)\s*=\s*require\s*\(?['"]?(.+?)['"]?\)?$"""
assign_regex = r"""^[ \t]*(?:local\s+)?([\w\d_.,]+)\s*=\s*([\w\d_.:]+)\s*(?:\(.*\)\s*)?$"""

# assign_regex matches multiple types of lua assignments
# examples:
#
# local setrow = chain:insert(value) -- with an ending comment
# setrow = chain:insert(value) -- with an ending comment
# local db = datastore.get_table(name, type)
# db = datastore.get_table(name, type)
# somevar = tbl.some_field
# somevar = tbl.some_field -- with a comment
assign_regex = r"""^[ \t]*(?:local\s+)?([\w\d_.,]+)\s*=\s*([\w\d_.:]+)\s*(?:\(.*\)\s*)?(?:--.*)?$"""


def is_summit_file(view):
Expand Down Expand Up @@ -33,6 +43,8 @@ class SummitCompletions:
obj_types = {}

def __init__(self):
self.completions = []
self.obj_types = {}
self.load_completions()

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

rhs_types = []
for found_type in found_types:
type_opts = self.obj_types[found_type]

type_funcs = type_opts['functions']
type_fields = type_opts['fields']

if target_name in type_funcs:
if 'returns' in type_funcs[target_name]:
return type_funcs[target_name]['returns']
if target_name in type_fields:
if 'returns' in type_fields[target_name]:
return type_fields[target_name]['returns']
# found_types are the potential types that obj_name
# can be, so iterate over them and try to get the
# function and field data for each of those types
type_opts = self.obj_types.get(found_type)
if type_opts:
# found the type we were looking for, so see if we find
# a match for the target (such as a function call) and
# then add the return values for that function/field
# to the correct set of returns
type_funcs = type_opts['functions']
if target_name in type_funcs:
returns = type_funcs[target_name].get('returns')
if returns:
ret_types_diff = len(returns) - len(rhs_types)
for _ in range(0, ret_types_diff):
# Add any missing return sets to the list
rhs_types.append(set())

for type_idx, ret_types in enumerate(returns):
ret_idx_types = rhs_types[type_idx]
for ret_type in ret_types:
ret_idx_types.add(ret_type)

type_fields = type_opts['fields']
if target_name in type_fields:
returns = type_fields[target_name].get('returns')
if returns:
ret_types_diff = len(returns) - len(rhs_types)
for _ in range(0, ret_types_diff):
# Add any missing return sets to the list
rhs_types.append(set())

for type_idx, ret_types in enumerate(returns):
ret_idx_types = rhs_types[type_idx]
for ret_type in ret_types:
ret_idx_types.add(ret_type)

return rhs_types

return None

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

comps.sort()
Expand Down

0 comments on commit fa7f7fa

Please sign in to comment.