Skip to content

Commit

Permalink
fallback to internal fmt if fmt_cmd fails
Browse files Browse the repository at this point in the history
  • Loading branch information
DisposaBoy committed May 3, 2016
1 parent d3600fd commit c1721a4
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 33 deletions.
3 changes: 2 additions & 1 deletion GoSublime.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
// the command will be passed, to its stdin, the contents of the file
// it must output the new file contents
//
// You might need to increase `ipc_timeout` below if you use this setting
// You might need to increase `ipc_timeout` below if you use this setting.
// If `fmt_cmd` takes too long, the internal MarGo fmt will be tried instead.
"fmt_cmd": [],

// ipc_timeout sets the maximum amount of time time in seconds to wait for a blocking ipc call to MarGo.
Expand Down
63 changes: 40 additions & 23 deletions gosubl/mg9.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,28 +302,43 @@ def _complete_opts(fn, src, pos, builtins):
}

def fmt(fn, src):
st = gs.settings_dict()
x = st.get('fmt_cmd')
if x:
env = sh.env()
x = [string.Template(s).safe_substitute(env) for s in x]
res, err = bcall('sh', {
'Env': env,
'Cmd': {
'Name': x[0],
'Args': x[1:],
'Input': src or '',
},
})
return res.get('out', ''), (err or res.get('err', ''))

fn = fn or ''
src = src or ''
fmt_cmd = gs.settings_dict().get('fmt_cmd')
if not fmt_cmd:
return _mg_fmt(fn, src)

env = sh.env()
fmt_cmd = [string.Template(s).safe_substitute(env) for s in fmt_cmd]
cmd_name = fmt_cmd[0]
cmd_args = fmt_cmd[1:]
res, err = bcall('sh', {
'Env': env,
'Cmd': {
'Name': cmd_name,
'Args': cmd_args,
'Input': src,
},
}, err_title=cmd_name)
err = err or res.get('err') or ''
cmd_src = '' if err else (res.get('out') or '')
if err:
mg_src, mg_err = _mg_fmt(fn, src)
if mg_src and not mg_err:
err = 'Used MarGo fmt because %s failed:\n\n%s' % (cmd_name, err)
cmd_src = mg_src

return cmd_src, err

def _mg_fmt(fn, src):
res, err = bcall('fmt', {
'Fn': fn or '',
'Src': src or '',
'TabIndent': st.get('fmt_tab_indent'),
'TabWidth': st.get('fmt_tab_width'),
'Fn': fn,
'Src': src,
})
return res.get('src', ''), err
if err:
return '', err

return (res.get('src') or ''), ''

def import_paths(fn, src, f):
tid = gs.begin(DOMAIN, 'Fetching import paths')
Expand Down Expand Up @@ -420,9 +435,11 @@ def share(src, f):
def acall(method, arg, cb):
gs.mg9_send_q.put((method, arg, cb))

def bcall(method, arg):
def bcall(method, arg, err_title=''):
err_title = err_title or method

if _inst_state() != "done":
return {}, 'Blocking call(%s) aborted: Install is not done' % method
return {}, 'Blocking call(%s) aborted: Install is not done' % err_title

q = gs.queue.Queue()
acall(method, arg, lambda r,e: q.put((r, e)))
Expand All @@ -431,7 +448,7 @@ def bcall(method, arg):
res, err = q.get(True, timeout)
return res, err
except:
return {}, 'Blocking Call(%s) Timed out after %0.3f second(s). You might need to increase the `ipc_timeout` setting' % (method, timeout)
return {}, 'Blocking Call(%s) timed out after %0.3f second(s). You might need to increase the `ipc_timeout` setting' % (err_title, timeout)

def expand_jdata(v):
if gs.is_a(v, {}):
Expand Down
16 changes: 7 additions & 9 deletions gscommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ def run(self, edit):
src, err = mg9.fmt(self.view.file_name(), src)

if not err and not src.strip():
err = "it appears to be empty"
err = "the fmt'd file result appears to be empty"

if err:
err = "Cannot fmt file. Error: `%s'" % err
short_err = '%s: %s... error logged to console' % (domain, err[:15])

def clear_status():
Expand All @@ -53,14 +52,13 @@ def clear_status():
sublime.set_timeout(clear_status, 10000)

if 'ipc_timeout' in err:
self.view.show_popup("%s: <b>%s</b>" % (domain, err))
self.view.show_popup("%s: %s" % (domain, err.replace('\n', '<br>')))

return

_, err = gspatch.merge(self.view, vsize, src, edit)
if err:
msg = 'PANIC: Cannot fmt file. Check your source for errors (and maybe undo any changes).'
sublime.error_message("%s: %s: Merge failure: `%s'" % (domain, msg, err))
if src:
_, err = gspatch.merge(self.view, vsize, src, edit)
if err:
msg = 'PANIC: Cannot fmt file. Check your source for errors (and maybe undo any changes).'
sublime.error_message("%s: %s: Merge failure: `%s'" % (domain, msg, err))

class GsFmtSaveCommand(sublime_plugin.TextCommand):
def is_enabled(self):
Expand Down

0 comments on commit c1721a4

Please sign in to comment.