Exemple #1
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
    def do_hover(self):
        if self.hover_zone == sublime.HOVER_TEXT:
            qsymbol = Common.get_qualified_symbol_at_point(self.view, self.point)
            ## print('hover: qualified symbol {0}'.format(qsymbol))
            module_word = qsymbol.module
            ident = qsymbol.name

            if module_word is not None and ident is None:
                # TODO: Any ideas for popup about module?
                pass
            elif ident is not None:
                whois_name = qsymbol.qualified_name()
                full_name = qsymbol.full_name()

                # Try get type of hovered symbol
                typed_expr = None
                if types.SourceHaskellTypeCache().has(self.filename):
                    typed_expr = self.get_type(types.SourceHaskellTypeCache().get(self.filename), whois_name)
                else:
                    project_name = Common.locate_cabal_project_from_view(self.view)[1]
                    point_rgn = sublime.Region(self.point, self.point)
                    typed_expr = self.get_type(types.get_type_view(self.view, project_name, point_rgn), whois_name)

                # Try whois
                suggest_import = False
                decl = Utils.head_of(BackendManager.active_backend().whois(whois_name, self.filename))
                if not decl:
                    suggest_import = True
                    decl = Utils.head_of(BackendManager.active_backend().lookup(full_name, self.filename))

                self.create_symbol_popup(typed_expr, decl, suggest_import)

        elif self.hover_zone == sublime.HOVER_GUTTER:
            errs = [err for err in ParseOutput.MARKER_MANAGER.marks_for_view(self.view) if err.region.start.line == self.line]
            if errs:
                popup_parts = [self.STYLES.gen_style(self.view.settings().get('color_scheme'))]
                for err in errs:
                    msg = UnicodeOpers.use_unicode_operators(symbols.escape_text(err.message))
                    # Decorate first word with style
                    decors = {
                        'Error': 'error',
                        'Warning': 'warning',
                        'Hint': 'hint'
                    }
                    for dec, dec_style in decors.items():
                        msg = msg.replace(dec, u'<span class="{0}">{1}</span>'.format(dec_style, dec))
                    popup_parts.append(u'<p>{0}</p>'.format(msg))
                    if err.correction is not None:
                        popup_parts.append(err.correction.popup())
                popup_text = u''.join(popup_parts)
                self.shown = True
                self.view.show_popup(popup_text, sublime.HIDE_ON_MOUSE_MOVE_AWAY, self.point, 600, 600,
                                     self.on_navigate, self.on_hide)
Exemple #3
0
    def do_hover(self):
        if self.hover_zone == sublime.HOVER_TEXT:
            qsymbol = Common.get_qualified_symbol_at_point(self.view, self.point)
            ## print('hover: qualified symbol {0}'.format(qsymbol))
            module_word = qsymbol.module
            ident = qsymbol.name

            if module_word is not None and ident is None:
                # TODO: Any ideas for popup about module?
                pass
            elif ident is not None:
                whois_name = qsymbol.qualified_name()
                full_name = qsymbol.full_name()

                # Try get type of hovered symbol
                typed_expr = None
                if types.SourceHaskellTypeCache().has(self.filename):
                    typed_expr = self.get_type(types.SourceHaskellTypeCache().get(self.filename), whois_name)
                else:
                    project_name = Common.locate_cabal_project_from_view(self.view)[1]
                    point_rgn = sublime.Region(self.point, self.point)
                    typed_expr = self.get_type(types.get_type_view(self.view, project_name, point_rgn), whois_name)

                # Try whois
                suggest_import = False
                decl = Utils.head_of(BackendManager.active_backend().whois(whois_name, self.filename))
                if not decl:
                    suggest_import = True
                    decl = Utils.head_of(BackendManager.active_backend().lookup(full_name, self.filename))

                self.create_symbol_popup(typed_expr, decl, suggest_import)

        elif self.hover_zone == sublime.HOVER_GUTTER:
            errs = [err for err in ParseOutput.errors_for_view(self.view) if err.region.start.line == self.line]
            if errs:
                popup_parts = [self.STYLES.gen_style(self.view.settings().get('color_scheme'))]
                for err in errs:
                    msg = UnicodeOpers.use_unicode_operators(symbols.escape_text(err.message))
                    # Decorate first word with style
                    decors = {
                        'Error': 'error',
                        'Warning': 'warning',
                        'Hint': 'hint'
                    }
                    for dec, dec_style in decors.items():
                        msg = msg.replace(dec, u'<span class="{0}">{1}</span>'.format(dec_style, dec))
                    popup_parts.append(u'<p>{0}</p>'.format(msg))
                    if err.correction is not None:
                        popup_parts.append(err.correction.popup())
                popup_text = u''.join(popup_parts)
                self.view.show_popup(popup_text, sublime.HIDE_ON_MOUSE_MOVE_AWAY, self.point, 600, 600,
                                     self.on_navigate, self.on_hide)
Exemple #4
0
 def contents_to_module(self, file, contents):
     self.set_file_contents(file, contents)
     m = self.module(file=file, header=True)
     proj = self.project(path=m.location.project)
     build_tool = proj['build-tool']
     self.scan_file(file=file, build_tool=build_tool, wait_complete=True)
     return Utils.head_of(self.module(None, file=file))
    def completions_for_module(self, project_name, module, filename):
        """
        Returns completions for module
        """
        retval = []
        backend = BackendManager.active_backend()
        if module:
            mods = backend.scope_modules(
                project_name, filename, lookup=module,
                search_type='exact') if filename else []

            mod_file = mods[0].location.filename if mods and mods[0].by_source(
            ) else None
            cache_db = mods[0].location.db if mods and mods[0].by_cabal(
            ) else None
            package = mods[0].location.package.name if mods and mods[
                0].by_cabal() else None

            mod_decls = Utils.head_of(
                backend.module(project_name,
                               lookup=module,
                               search_type='exact',
                               file=mod_file,
                               symdb=cache_db,
                               package=package))
            retval = make_completions(
                mod_decls.declarations.values()) if mod_decls else []

        return retval
Exemple #6
0
    def is_in_project(self, view):
        file_shown_in_view = Common.window_view_and_file(view)[2]
        if file_shown_in_view is None:
            return False

        src_module = Utils.head_of(BackendManager.active_backend().module(file=file_shown_in_view))
        return src_module is not None and src_module.location.project is not None
Exemple #7
0
    def run(self, edit, **kwargs):
        current_file_name = kwargs.get('filename', self.view.file_name())
        project_name = Common.locate_cabal_project_from_view(self.view)[1]
        backend = BackendManager.active_backend()

        imp_module = Utils.head_of(backend.module(project_name, file=current_file_name))
        if imp_module:
            imports = sorted(imp_module.imports, key=lambda i: i.position.line)

            supported, result = backend.clean_imports(current_file_name)
            print(result)
            if supported:
                if len(imports) == len(result):
                    Logging.log('replacing imports for {0}'.format(current_file_name), Logging.LOG_TRACE)
                    erased = 0
                    for imp, new_imp in zip(imports, result):
                        point = self.view.text_point(imp.position.line - 1 - erased, 0)
                        if new_imp.endswith('()'):
                            self.view.erase(edit, self.view.full_line(point))
                            erased = erased + 1
                        else:
                            self.view.replace(edit, self.view.line(point), new_imp)
                else:
                    Common.sublime_status_message('different number of imports: {0} and {1}'.format(len(imports), len(result)))
            else:
                if len(result) == 1:
                    Common.sublime_status_message(result[0])
                else:
                    sublime.message_dialog('\n'.join(result))
        else:
            Common.sublime_status_message('Clean Imports failed: module not scanned')
Exemple #8
0
    def completions_for_module(self, project_name, module, filename):
        """
        Returns completions for module
        """
        retval = []
        backend = BackendManager.active_backend()
        if module:
            mods = backend.scope_modules(
                project_name, filename, lookup=module,
                search_type='exact') if filename else []

            mod_file = mods[0].location.filename if mods and mods[0].by_source(
            ) else None
            package = mods[0].location.package.name if mods and mods[
                0].by_cabal() else None
            mod_id = mods[0] if mods else None

            mod_decls = Utils.head_of(
                list(
                    filter(
                        lambda m: mod_id == m,
                        backend.module(project_name,
                                       lookup=module,
                                       search_type='exact',
                                       file=mod_file,
                                       package=package))))
            retval = make_completions(mod_decls.exports) if mod_decls else []

        return retval
Exemple #9
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)
Exemple #10
0
    def run(self, _edit, **kwargs):
        module_name = kwargs.get('module_name')
        filename = kwargs.get('filename')
        symdb = kwargs.get('db')
        scope = kwargs.get('scope')

        self.candidates = []
        self.current_file_name = self.view.window().active_view().file_name()

        the_module = None
        project_name = Common.locate_cabal_project_from_view(self.view.window().active_view())[1]

        if filename:
            the_module = Utils.head_of(BackendManager.active_backend().module(project_name, file=filename))
            if not the_module:
                Common.sublime_status_message('Module {0} not found'.format(filename))
                return
        elif module_name:
            cand_mods = self.candidate_modules(project_name, module_name, scope, symdb)
            if not cand_mods:
                Common.sublime_status_message('Module {0} not found'.format(module_name))
                return
            elif len(cand_mods) == 1:
                the_module = self.get_module_info(project_name, cand_mods[0], module_name)
                if the_module:
                    the_module = Utils.head_of(the_module)
            else:
                self.candidates.extend([(m, [m.name, m.location.to_string()]) for m in cand_mods])
        else:
            if self.current_file_name:
                cand_mods = BackendManager.active_backend().scope_modules(project_name, self.current_file_name)
            else:
                symbol_db = symbols.PackageDb.from_string(symdb) if symdb else None
                cand_mods = BackendManager.active_backend().list_modules(symdb=symbol_db)
            self.candidates.extend([(m, [m.name, m.location.to_string()]) for m in cand_mods])

        if the_module:
            self.candidates = sorted(list(the_module.declarations.values()), key=lambda d: d.brief())
            results = [[decl.brief(use_unicode=False),
                        decl.docs.splitlines()[0] if decl.docs else ''] for decl in self.candidates]
            self.view.window().show_quick_panel(results, self.on_symbol_selected)
        else:
            self.candidates.sort(key=lambda c: c[1][0])
            self.view.window().show_quick_panel([c[1] for c in self.candidates], self.on_done)
Exemple #11
0
    def on_inspected(self, result):
        imp_module = None

        if self.view.is_dirty():
            # Use the buffer's contents
            pyresult = json.loads(result).get('result')
            if pyresult is not None:
                if Logging.is_log_level(Logging.LOG_DEBUG):
                    pprint.pprint(pyresult, width=80)
                modinfo = pyresult.get('module')
                if modinfo is not None:
                    imp_module = HsDevResultParse.parse_module(modinfo)
        else:
            # Otherwise, use the actual file
            imp_module = Utils.head_of(
                hsdev.client.module(file=self.current_file_name))

        if imp_module is not None:
            imports = sorted(imp_module.imports, key=lambda i: i.position.line)
            after = [i for i in imports if i.module > self.module_name]

            insert_line = 0
            insert_gap = False

            if len(after) > 0:
                # Insert before after[0]
                insert_line = after[0].position.line - 1
            elif len(imports) > 0:
                # Insert after all imports
                insert_line = imports[-1].position.line
            elif len(imp_module.declarations) > 0:
                # Insert before first declaration
                insert_line = min([
                    d.position.line for d in imp_module.declarations.values()
                ]) - 1
                insert_gap = True
            else:
                # Try to add the import just after the "where" of the module declaration
                contents = self.view.substr(sublime.Region(
                    0, self.view.size()))
                mod_decl = re.search('module.*where', contents, re.MULTILINE)
                if mod_decl is not None:
                    insert_line = mod_decl.end()
                    insert_gap = True
                else:
                    # Punt! Insert at the end of the file
                    insert_line = self.view.rowcol(self.view.size())[0]

            insert_text = 'import {0}\n'.format(
                self.module_name) + ('\n' if insert_gap else '')

            point = self.view.text_point(insert_line, 0)
            self.view.insert(self.edit, point, insert_text)

            Common.show_status_message(
                'Import {0} added'.format(self.module_name), True)
Exemple #12
0
    def get_types(self, filename=None, line=None, column=None):
        if not filename:
            filename = self.view.file_name()

        if (not line) or (not column):
            line, column = self.view.rowcol(self.view.sel()[0].b)

        module_name = Utils.head_of(hsdev.client.module(file=filename))

        return get_type(self.view, filename, module_name, line, column)
Exemple #13
0
def get_type_view(view, selection=None):
    filename = view.file_name()

    if selection is None:
        selection = view.sel()[0]

    line, column = view.rowcol(selection.b)
    module_name = Utils.head_of(hsdev.client.module(file=filename))

    return get_type(view, filename, module_name, line, column)
def get_type_view(view, project_name, selection=None):
    filename = view.file_name()
    project_name = Common.locate_cabal_project_from_view(view)[1]

    if selection is None:
        selection = view.sel()[0]

    line, column = view.rowcol(selection.b)
    module_name = Utils.head_of(BackendManager.active_backend().module(project_name, file=filename))

    return get_type(view, project_name, filename, module_name, line, column)
Exemple #15
0
def get_type_view(view, project_name, selection=None):
    filename = view.file_name()
    project_name = Common.locate_cabal_project_from_view(view)[1]

    if selection is None:
        selection = view.sel()[0]

    line, column = view.rowcol(selection.b)
    module_name = Utils.head_of(BackendManager.active_backend().module(project_name, file=filename))

    return get_type(view, project_name, filename, module_name, line, column)
Exemple #16
0
    def get_completions_async(self, project_name, file_name):
        def log_result(result):
            retval = result or []
            Logging.log('completions: {0}'.format(len(retval)), Logging.LOG_TRACE)
            return retval

        comps = []
        update_cabal = False
        update_sources = False
        with self.cache as cache_:
            if file_name in cache_.files:
                return log_result(cache_.files.get(file_name, []))
            else:
                update_cabal = not cache_.cabal
                update_sources = not cache_.sources
        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 is None:
            return log_result(comps)
        else:
            Logging.log('preparing completions for {0}/{1}'.format(project_name, file_name), Logging.LOG_DEBUG)
            comps = make_completions(BackendManager.active_backend().complete('', file_name))

            current_module = Utils.head_of(BackendManager.active_backend().module(project_name, file_name))
            Logging.log('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])
Exemple #17
0
    def rescan_source(self, project_name, filename, drop_all=True):
        if Settings.COMPONENT_DEBUG.inspection:
            print('{0}.rescan_source: {1}/{2}'.format(
                type(self).__name__, project_name, filename))
        with self.backend_mgr:
            if self.backend_mgr.active_backend().auto_rescan():
                if Utils.head_of(BackendManager.active_backend().module(
                        None, file=filename, header=True)) is not None:
                    return
            with self.backend_mgr.inspector() as insp:
                if not filename.endswith('.cabal'):
                    insp.mark_file_dirty(filename)
                else:
                    insp.mark_cabal_dirty(filename)

        EventCommon.update_completions_async(self.autocompleter, project_name,
                                             [filename], drop_all)
    def completions_for_module(self, project_name, module, filename):
        """
        Returns completions for module
        """
        retval = []
        backend = BackendManager.active_backend()
        if module:
            mods = backend.scope_modules(project_name, filename, lookup=module, search_type='exact') if filename else []

            mod_file = mods[0].location.filename if mods and mods[0].by_source() else None
            cache_db = mods[0].location.db if mods and mods[0].by_cabal() else None
            package = mods[0].location.package.name if mods and mods[0].by_cabal() else None

            mod_decls = Utils.head_of(backend.module(project_name, lookup=module, search_type='exact', file=mod_file,
                                                     symdb=cache_db, package=package))
            retval = make_completions(mod_decls.declarations.values()) if mod_decls else []

        return retval
Exemple #19
0
    def run(self, **kwargs):
        project = kwargs.get('project') or False
        decls = []

        if project:
            project_name = Common.locate_cabal_project_from_view(self.view)[1]
            modules = BackendManager.active_backend().module(project_name, file=self.current_filename)
            current_project = Utils.head_of(modules).location.project
            if not current_project:
                Common.sublime_status_message('File {0} is not in project'.format(self.current_filename))
                return

            decls = self.sorted_decls_name(BackendManager.active_backend().symbol(project=current_project))
            self.declarations = [[decl.brief(True, use_unicode=False), decl.module.name] for decl in decls]
        else:
            decls = self.sorted_decls_pos(BackendManager.active_backend().symbol(file=self.current_filename, local_names=True))
            self.declarations = [[(decl.position.column * ' ') + decl.brief(True, use_unicode=False)] for decl in decls]
        self.decls = decls[:]

        if decls:
            self.window.show_quick_panel(self.declarations,
                                         self.on_done, 0,
                                         self.closest_idx(decls),
                                         self.on_highlighted if not project else None)
Exemple #20
0
 def is_scanned_source(self, view):
     file_shown_in_view = Common.get_haskell_command_window_view_file_project(
         view)[2]
     return file_shown_in_view is not None and \
            Utils.head_of(BackendManager.active_backend().module(file=file_shown_in_view)) is not None
Exemple #21
0
 def get_types(self, project_name, filename, line, column):
     module_name = Utils.head_of(BackendManager.active_backend().module(
         project_name, file=filename))
     return get_type(self.view, project_name, filename, module_name, line,
                     column)
 def get_type(self, type_list, qual_name):
     filt_types = [t for t in type_list
                   if t.substr(self.view) == qual_name and t.region(self.view).contains(self.point)]
     return Utils.head_of(filt_types)
 def get_types(self, project_name, filename, line, column):
     module_name = Utils.head_of(BackendManager.active_backend().module(project_name, file=filename))
     return get_type(self.view, project_name, filename, module_name, line, column)
Exemple #24
0
    def on_hover(self, view, point, hover_zone):
        if not Common.is_haskell_source(view):
            return

        self.view = view
        self.current_file_name = self.view.file_name()
        # If the column is needed: (line, column) = self.view.rowcol(point) [remove subscript]
        line = self.view.rowcol(point)[0]
        self.decl = None
        self.typed_expr = None

        if hover_zone == sublime.HOVER_TEXT:
            qsymbol = Common.get_qualified_symbol_at_point(self.view, point)
            module_word = qsymbol.module
            ident = qsymbol.name

            if ident is None and module_word:  # TODO: Any ideas for popup about module?
                pass

            if ident:
                self.whois_name = qsymbol.qualified_name()
                self.full_name = qsymbol.full_name()

                # Try get type of hovered symbol
                self.point = point
                self.typed_expr = None
                if types.FILE_TYPES.has(self.current_file_name):
                    self.typed_expr = self.get_type(
                        types.FILE_TYPES.get(self.current_file_name))
                else:
                    types.get_types(self.current_file_name, self.on_types)

                # Try whois
                self.suggest_import = False
                self.decl = Utils.head_of(
                    hsdev.client.whois(self.whois_name,
                                       self.current_file_name))

                if not self.decl:
                    self.suggest_import = True
                    self.decl = Utils.head_of(
                        hsdev.client.lookup(self.full_name,
                                            self.current_file_name))

                self.create_symbol_popup()

        elif hover_zone == sublime.HOVER_GUTTER:
            self.view = view
            self.current_file_name = self.view.file_name()
            errs = list(
                filter(lambda e: e.region.start.line == line,
                       parseoutput.errors_for_view(self.view)))
            if errs:
                popup_parts = [
                    SUBHASK_STYLES.gen_style(
                        self.view.settings().get('color_scheme'))
                ]
                for err in errs:
                    msg = UnicodeOpers.use_unicode_operators(
                        symbols.escape_text(err.message))
                    # Decorate first word with style
                    decors = {
                        'Error': 'error',
                        'Warning': 'warning',
                        'Hint': 'hint'
                    }
                    for dec, dec_style in decors.items():
                        msg = msg.replace(
                            dec, u'<span class="{0}">{1}</span>'.format(
                                dec_style, dec))
                    popup_parts.append(u'<p>{0}</p>'.format(msg))
                    if err.correction is not None:
                        popup_parts.append(err.correction.popup())
                popup_text = u''.join(popup_parts)
                self.view.show_popup(popup_text,
                                     sublime.HIDE_ON_MOUSE_MOVE_AWAY, point,
                                     600, 600, self.on_navigate, self.on_hide)
Exemple #25
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
    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
Exemple #27
0
 def get_type(self, type_list, qual_name):
     filt_types = [
         t for t in type_list if t.substr(self.view) == qual_name
         and t.region(self.view).contains(self.point)
     ]
     return Utils.head_of(filt_types)
Exemple #28
0
 def is_scanned_source(self, view):
     file_shown_in_view = Common.window_view_and_file(view)[2]
     return file_shown_in_view is not None and \
            Utils.head_of(BackendManager.active_backend().module(None, file=file_shown_in_view, header=True)) is not None
 def is_scanned_source(self, view):
     file_shown_in_view = Common.window_view_and_file(view)[2]
     return file_shown_in_view is not None and \
            Utils.head_of(BackendManager.active_backend().module(file=file_shown_in_view)) is not None