Exemplo n.º 1
0
    def run(self, edit):
        scopes = self.view.scope_name(0).split()
        if 'source.go' not in scopes:
            return

        region = sublime.Region(0, self.view.size())
        src = self.view.substr(region)

        args = [gs.setting("gofmt_cmd", "gofmt"), "-d"]
        diff, err = gs.runcmd(args, src)
        if err:
            fn = self.view.file_name()
            err = err.replace('<standard input>', fn)
            def report_error():
                sublime.status_message('GsFmt: File %s contains errors' % fn)
            sublime.set_timeout(report_error, 0)
        elif diff:
            err = ''
            try:
                edit = self.view.begin_edit()
                ed = SublimeEditor(self.view, edit)
                err = thatcher.patch(ed, diff)
            except Exception as e:
                err = "%s\n\n%s" % (err, e)
            finally:
                self.view.end_edit(edit)
            if err:
                def report_err():
                    self.view.run_command('undo')
                    sublime.status_message("GsFmt: Could not patch the buffer: %s" % err)
                sublime.set_timeout(report_err, 0)
Exemplo n.º 2
0
    def run(self, edit):
        if not gs.is_go_source_view(self.view):
            return

        region = sublime.Region(0, self.view.size())
        src = self.view.substr(region)

        args = [gs.setting("gofmt_cmd", "gofmt"), "-d"]
        diff, err = gs.runcmd(args, src)
        if err:
            fn = self.view.file_name()
            err = err.replace('<standard input>', fn)
            gs.notice('GsFmt', 'File %s contains errors' % fn)
        elif diff:
            err = ''
            try:
                edit = self.view.begin_edit()
                ed = SublimeEditor(self.view, edit)
                err = thatcher.patch(ed, diff)
            except Exception as e:
                err = "%s\n\n%s" % (err, e)
            finally:
                self.view.end_edit(edit)
            
            if err:
                def cb():
                    if ed.dirty:
                        self.view.run_command('undo')
                    gs.notice("GsFmt", "Could not patch the buffer: %s" % err)
                sublime.set_timeout(cb, 0)