Beispiel #1
0
    def context_completion(self, view, key, _operator, _operand, _matchall):
        # Completion context is the only branch here where a backend is needed.
        retval = False
        with self.backend_mgr:
            project_dir, project_name = Common.locate_cabal_project_from_view(
                view)
            region = view.sel()[0]
            if region.a == region.b:
                word_region = view.word(region)
                preline = Common.get_line_contents_before_region(
                    view, word_region)
                preline += self.COMPLETION_CHARS[key]
                qsymbol = Common.get_qualified_symbol(preline)
                if qsymbol.module:
                    mod_completions = self.autocompleter.get_current_module_completions(
                        project_name, project_dir)
                    if qsymbol.is_import_list:
                        retval = qsymbol.module in mod_completions
                    else:
                        retval = [
                            m for m in mod_completions
                            if m.startswith(qsymbol.module)
                        ] != []

        return retval
Beispiel #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 []

        Logging.log('AutoCompleter.get_completions.', Logging.LOG_DEBUG)

        self.current_filename = current_file_name
        project_name = Common.locate_cabal_project_from_view(view)[1]

        line_contents = Common.get_line_contents(view, locations[0])
        qsymbol = Common.get_qualified_symbol(line_contents)
        qualified_prefix = qsymbol.qualified_name()

        Logging.log('qsymbol {0}'.format(qsymbol), Logging.LOG_DEBUG)
        Logging.log('current_file_name {0} qualified_prefix {1}'.format(current_file_name, qualified_prefix), Logging.LOG_DEBUG)

        view_settings = view.settings()
        wide = view_settings.get('subhask_wide_completion')

        suggestions = []
        completions = []
        if qsymbol.module:
            if qsymbol.is_import_list:
                current_module = Utils.head_of(BackendManager.active_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(BackendManager.active_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 = BackendManager.active_backend().resolve(file=q_module.location.filename, exports=True)
                            if proj_module:
                                suggestions = proj_module.declarations.values()
                        elif q_module.by_cabal():
                            cabal_module = Utils.head_of(BackendManager.active_backend().module(project_name,
                                                                                                lookup=q_module.name,
                                                                                                search_type='exact',
                                                                                                package=q_module.location.package.name))
                            if cabal_module:
                                suggestions = cabal_module.declarations.values()
            else:
                suggestions = BackendManager.active_backend().complete(qualified_prefix, current_file_name, wide=wide)

            completions = make_completions(suggestions)
        else:
            with self.cache as cache_:
                if wide:
                    completions += cache_.global_completions()
                else:
                    completions += cache_.files.get(current_file_name, cache_.global_completions())

        completions += self.keyword_completions(qsymbol.name)
        sort_completions(completions)
        return completions
Beispiel #3
0
    def run(self, _edit, **kwargs):
        filename = kwargs.get('filename')
        module_name = kwargs.get('module_name')
        package_name = kwargs.get('package_name')
        symdb = kwargs.get('db')
        name = kwargs.get('name')
        qname = kwargs.get('qname')
        no_browse = kwargs.get('no_browse') or False

        if qname:
            self.full_name = qname
            self.current_file_name = self.view.file_name()
            # Try whois it, followed by file symbol and wider module searches
            self.candidates = self.collect_candidates(qname, name, filename, module_name, package_name, symdb)
        else:
            self.current_file_name = self.view.file_name()

            qsymbol = Common.get_qualified_symbol(qname) \
                      if qname \
                      else Common.get_qualified_symbol_at_region(self.view, self.view.sel()[0])
            module_word = qsymbol.module
            ident = qsymbol.name

            if ident is None:  # module
                if not no_browse:
                    self.view.window().run_command('sublime_haskell_browse_module', {'module_name': module_word,
                                                                                     'scope': self.current_file_name})
                return

            if not module_word and not ident:
                Common.sublime_status_message('No symbol selected')
                return

            self.whois_name = qsymbol.qualified_name()
            self.full_name = qsymbol.full_name()

            self.candidates = (BackendManager.active_backend().whois(self.whois_name, self.current_file_name) or [])[:1]

            if not self.candidates:
                self.candidates = BackendManager.active_backend().lookup(self.full_name, self.current_file_name)

            if not self.candidates:
                self.candidates = BackendManager.active_backend().symbol(lookup=self.full_name, search_type='exact')

        if not self.candidates:
            Common.sublime_status_message('Symbol {0} not found'.format(self.full_name))
        elif len(self.candidates) == 1:
            self.show_symbol_info(self.candidates[0])
        elif not no_browse:
            results = [[c.qualified_name(), c.defined_module().location.to_string()] for c in self.candidates]
            self.view.window().show_quick_panel(results, self.on_done)
Beispiel #4
0
    def on_query_context(self, view, key, _operator, _operand, _matchall):
        if Settings.COMPONENT_DEBUG.event_viewer:
            print('{0} key = {1}.'.format(
                type(self).__name__ + '.on_query_context', key))
        retval = None
        if key == 'haskell_autofix':
            retval = view.settings().get('autofix')
        elif key == 'auto_completion_popup':
            retval = Settings.PLUGIN.auto_completion_popup
        elif key == 'haskell_source':
            retval = Common.is_haskell_source(view)
        elif key == 'haskell_source_or_repl':
            retval = Common.is_haskell_source(view) or Common.is_haskell_repl(
                view)
        elif key == 'haskell_repl':
            retval = Common.is_haskell_repl(view)
        elif key == 'haskell_symbol_info':
            retval = Common.is_haskell_symbol_info(view)
        elif key == 'cabal_source':
            retval = Common.is_cabal_source(view)
        elif key == 'scanned_source':
            retval = self.is_scanned_source(view)
        elif key == 'in_project':
            retval = self.is_in_project(view)
        elif key == "is_module_completion" or key == "is_import_completion":
            # Completion context is the only branch here where a backend is needed.
            retval = False
            with self.backend_mgr:
                project_dir, project_name = Common.locate_cabal_project_from_view(
                    view)
                region = view.sel()[0]
                if region.a == region.b:
                    word_region = view.word(region)
                    preline = Common.get_line_contents_before_region(
                        view, word_region)
                    preline += self.COMPLETION_CHARS[key]
                    qsymbol = Common.get_qualified_symbol(preline)
                    if qsymbol.module:
                        mod_completions = self.autocompleter.get_current_module_completions(
                            project_name, project_dir)
                        if qsymbol.is_import_list:
                            retval = qsymbol.module in mod_completions
                        else:
                            retval = [
                                m for m in mod_completions
                                if m.startswith(qsymbol.module)
                            ] != []

        return retval
    def context_completion(self, view, key, _operator, _operand, _matchall):
        # Completion context is the only branch here where a backend is needed.
        retval = False
        with self.backend_mgr:
            project_dir, project_name = Common.locate_cabal_project_from_view(view)
            region = view.sel()[0]
            if region.a == region.b:
                word_region = view.word(region)
                preline = Common.get_line_contents_before_region(view, word_region)
                preline += self.COMPLETION_CHARS[key]
                qsymbol = Common.get_qualified_symbol(preline)
                if qsymbol.module:
                    mod_completions = self.autocompleter.get_current_module_completions(project_name, project_dir)
                    if qsymbol.is_import_list:
                        retval = qsymbol.module in mod_completions
                    else:
                        retval = [m for m in mod_completions if m.startswith(qsymbol.module)] != []

        return retval
    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)
        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))

        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.resolve(file=q_module.location.filename, exports=True)
                            if proj_module:
                                suggestions = proj_module.declarations.values()
                        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.declarations.values()
            else:
                if Settings.COMPONENT_DEBUG.completions:
                    print('completions: querying module-specific completions')
                suggestions = backend.complete(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)
        sort_completions(completions)
        return completions
Beispiel #7
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)
        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))

        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(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)
        sort_completions(completions)
        return completions