def complete(cursor_offset, line): """Construct a full list of possibly completions for imports.""" tokens = line.split() if 'from' not in tokens and 'import' not in tokens: return None result = lineparts.current_word(cursor_offset, line) if result is None: return None if lineparts.current_from_import_from(cursor_offset, line) is not None: if lineparts.current_from_import_import(cursor_offset, line) is not None: # `from a import <b|>` completion return (module_matches(lineparts.current_from_import_import(cursor_offset, line)[2], lineparts.current_from_import_from(cursor_offset, line)[2]) + attr_matches(lineparts.current_from_import_import(cursor_offset, line)[2], lineparts.current_from_import_from(cursor_offset, line)[2])) else: # `from <a|>` completion return (module_attr_matches(lineparts.current_from_import_from(cursor_offset, line)[2]) + module_matches(lineparts.current_from_import_from(cursor_offset, line)[2])) elif lineparts.current_import(cursor_offset, line): # `import <a|>` completion return (module_matches(lineparts.current_import(cursor_offset, line)[2]) + module_attr_matches(lineparts.current_import(cursor_offset, line)[2])) else: return None
def complete(cursor_offset, line): """Construct a full list of possibly completions for imports.""" tokens = line.split() if 'from' not in tokens and 'import' not in tokens: return None result = current_word(cursor_offset, line) if result is None: return None from_import_from = current_from_import_from(cursor_offset, line) if from_import_from is not None: import_import = current_from_import_import(cursor_offset, line) if import_import is not None: # `from a import <b|>` completion matches = module_matches(import_import[2], from_import_from[2]) matches.update(attr_matches(import_import[2], from_import_from[2])) else: # `from <a|>` completion matches = module_attr_matches(from_import_from[2]) matches.update(module_matches(from_import_from[2])) return matches cur_import = current_import(cursor_offset, line) if cur_import is not None: # `import <a|>` completion matches = module_matches(cur_import[2]) matches.update(module_attr_matches(cur_import[2])) return matches else: return None
def complete(cursor_offset, line): """Construct a full list of possibly completions for imports.""" # TODO if this is done in a thread (as it prob will be in Windows) we'll need this # if not fully_loaded: # return [] tokens = line.split() if 'from' not in tokens and 'import' not in tokens: return None result = lineparts.current_word(cursor_offset, line) if result is None: return None def module_matches(cw, prefix=''): """Modules names to replace cw with""" full = '%s.%s' % (prefix, cw) if prefix else cw matches = [ name for name in modules if (name.startswith(full) and name.find('.', len(full)) == -1) ] if prefix: return [match[len(prefix) + 1:] for match in matches] else: return matches def attr_matches(cw, prefix='', only_modules=False): """Attributes to replace name with""" full = '%s.%s' % (prefix, cw) if prefix else cw module_name, _, name_after_dot = full.rpartition('.') if module_name not in sys.modules: return [] module = sys.modules[module_name] if only_modules: matches = [ name for name in dir(module) if name.startswith(name_after_dot) and '%s.%s' % (module_name, name) in sys.modules ] else: matches = [ name for name in dir(module) if name.startswith(name_after_dot) ] module_part, _, _ = cw.rpartition('.') if module_part: return ['%s.%s' % (module_part, m) for m in matches] return matches def module_attr_matches(name): """Only attributes which are modules to replace name with""" return attr_matches(name, prefix='', only_modules=True) if lineparts.current_from_import_from(cursor_offset, line) is not None: if lineparts.current_from_import_import(cursor_offset, line) is not None: # `from a import <b|>` completion return (module_matches( lineparts.current_from_import_import(cursor_offset, line)[2], lineparts.current_from_import_from(cursor_offset, line)[2]) + attr_matches( lineparts.current_from_import_import( cursor_offset, line)[2], lineparts.current_from_import_from( cursor_offset, line)[2])) else: # `from <a|>` completion return (module_attr_matches( lineparts.current_from_import_from(cursor_offset, line)[2]) + module_matches( lineparts.current_from_import_from( cursor_offset, line)[2])) elif lineparts.current_import(cursor_offset, line): # `import <a|>` completion return ( module_matches(lineparts.current_import(cursor_offset, line)[2]) + module_attr_matches( lineparts.current_import(cursor_offset, line)[2])) else: return None