Esempio n. 1
0
    def generate_completions_cache(self, project_name, file_name, contents=None):
        def log_result(result):
            retval = result or []
            if Settings.COMPONENT_DEBUG.completions:
                print('completions: {0}'.format(len(retval)))
            return retval

        comps = []
        update_cabal = False
        update_sources = False
        with self.cache as cache_:
            if file_name in cache_.files:
                del cache_.files[file_name]
            else:
                update_cabal = not cache_.cabal
                update_sources = not cache_.sources

        ## Not sure what these were supposed to do -- the actual methods are no-ops.
        if update_cabal:
            self.update_cabal_completions()
        if update_sources:
            self.update_sources_completions()

        with self.cache as cache_:
            comps = cache_.global_completions()

        import_names = []

        if file_name:
            if Settings.COMPONENT_DEBUG.completions:
                print('preparing completions for {0} ({1})'.format(project_name, file_name))

            backend = BackendManager.active_backend()
            comps = make_completions(backend.complete(Common.QualifiedSymbol(''), file_name, contents=contents))
            current_module = Utils.head_of(backend.module(project_name, file_name))

            if Settings.COMPONENT_DEBUG.completions:
                print('current_module {0}'.format(current_module))

            if current_module:
                # Get import names
                #
                # Note, that if module imported with 'as', then it can be used only with its synonym
                # instead of full name
                import_names.extend([('{0}\tmodule {1}'.format(i.import_as, i.module), i.import_as)
                                     for i in current_module.imports if i.import_as])
                import_names.extend([('{0}\tmodule'.format(i.module), i.module)
                                     for i in current_module.imports if not i.import_as])

                comps.extend(import_names)
                sort_completions(comps)

            with self.cache as cache_:
                cache_.files[file_name] = comps
                return log_result(cache_.files[file_name])
        else:
            return log_result(comps)
Esempio n. 2
0
    def get_completions(self, view, locations):
        "Get all the completions related to the current file."

        current_file_name = view.file_name()
        if not current_file_name:
            return []

        if Settings.COMPONENT_DEBUG.completions:
            print('AutoCompleter.get_completions.')

        self.current_filename = current_file_name
        _, project_name = Common.locate_cabal_project_from_view(view)

        line_contents = Common.get_line_contents(view, locations[0])
        qsymbol = Common.get_qualified_symbol(line_contents)
        prefix_qsymbol = Common.QualifiedSymbol('', qsymbol.module, qsymbol.module_as, qsymbol.is_import_list, qsymbol.is_operator)
        immutable_prefix = prefix_qsymbol.qualified_name()
        qualified_prefix = qsymbol.qualified_name()

        if Settings.COMPONENT_DEBUG.completions:
            print('qsymbol {0}'.format(qsymbol))
            print('current_file_name {0} qualified_prefix {1}'.format(current_file_name, qualified_prefix))
            print('immutable prefix: {0}'.format(immutable_prefix))

        view_settings = view.settings()
        wide = view_settings.get('subhask_wide_completion')
        backend = BackendManager.active_backend()

        suggestions = []
        completions = []
        if qsymbol.module:
            if qsymbol.is_import_list:
                current_module = Utils.head_of(backend.module(project_name, current_file_name))
                if current_module and current_module.location.project:
                    # Search for declarations of qsymbol.module within current project
                    q_module = Utils.head_of(backend.scope_modules(project_name, current_file_name, lookup=qsymbol.module,
                                                                   search_type='exact'))
                    if q_module is not None:
                        if q_module.by_source():
                            proj_module = backend.module(None, file=q_module.location.filename)
                            if proj_module:
                                suggestions = proj_module.exports
                        elif q_module.by_cabal():
                            cabal_module = Utils.head_of(backend.module(project_name, lookup=q_module.name, search_type='exact',
                                                                        package=q_module.location.package.name))
                            if cabal_module:
                                suggestions = cabal_module.exports
            else:
                if Settings.COMPONENT_DEBUG.completions:
                    print('completions: querying module-specific completions')
                suggestions = backend.complete(prefix_qsymbol, current_file_name, wide=wide)

            completions = make_completions(suggestions)
        else:
            with self.cache as cache_:
                if wide:
                    if Settings.COMPONENT_DEBUG.completions:
                        print('completions: returning global completions')
                    completions += cache_.global_completions()
                else:
                    if Settings.COMPONENT_DEBUG.completions:
                        print('completions: returning file-specific completions')
                    completions += cache_.files.get(current_file_name, cache_.global_completions())

        completions += self.keyword_completions(qsymbol.name)
        completions = cut_completions_prefix(completions, prefix=immutable_prefix)
        sort_completions(completions)
        return completions