Beispiel #1
0
    def on_navigate(self, url):
        if self.view.is_popup_visible():
            self.view.hide_popup()
            if url[0:4] == 'http':
                webbrowser.open(url)
            elif url[0:8] == 'autofix:':
                rgn = symbols.Region.from_str(url[8:])
                errs = ParseOutput.errors_for_view(self.view)
                # Give err_text, err_rgn scope other than the loop.
                err_text = ''
                err_rgn = None
                repl_text = None

                for err in errs:
                    if err.correction is not None and err.correction.corrector.region == rgn:
                        err.erase_from_view()
                        ParseOutput.ERRORS.remove(err)
                        errs.remove(err)
                        ParseOutput.mark_messages_in_view(errs, self.view)

                        corrector = err.correction.corrector
                        err_rgn = corrector.to_region(self.view)
                        err_text = corrector.contents

                        repl_text = {'text': err_text,
                                     'begin': err_rgn.begin(),
                                     'end': err_rgn.end()}
                        sublime.set_timeout(lambda: self.view.run_command('sublime_haskell_replace_text', repl_text), 0)
            elif url[0:7] == "import:":
                decl_name = urllib.parse.unquote(url[7:])
                self.view.run_command('sublime_haskell_insert_import_for_symbol',
                                      {'filename': self.view.file_name(),
                                       'decl': decl_name})
            else:
                self.view.window().open_file(url, sublime.ENCODED_POSITION | sublime.TRANSIENT)
Beispiel #2
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)
Beispiel #3
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)