def cmd_9(view, edit, args, wd, r): if len(args) == 1 and args[0] == "play": dmn = '%s: 9 play' % DOMAIN cid = '9play-%s' % uuid.uuid4() def cancel(): mg9.acall('kill', {'cid': cid}, None) tid = gs.begin(dmn, "9 play", set_status=False, cancel=cancel) def cb(res, err): out = '\n'.join(s for s in (res.get('out'), res.get('err')) if s) if not out: out = err def f(): gs.end(tid) view.insert(edit, r.end(), '\n%s' % out) view.run_command('gs_commander_init') sublime.set_timeout(f, 0) a = { 'cid': cid, 'env': gs.env(), 'dir': wd, } av = sublime.active_window().active_view() if av and not av.file_name() and gs.is_go_source_view(av, False): a['src'] = av.substr(sublime.Region(0, av.size())) mg9.acall('play', a, cb) else: view.insert(edit, r.end(), ('Invalid args %s' % args)) view.run_command('gs_commander_init')
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)
def run(self, edit): view = self.view if gs.is_go_source_view(view): if not gstest.handle_action(view, "left-click"): view.run_command("gs_doc", {"mode": "goto"}) elif view.score_selector(gs.sel(view).begin(), "text.9o") > 0: view.window().run_command("gs9o_open_selection")
def cmd_9(view, edit, args, wd, rkey): if len(args) == 0 or args[0] not in ('run', 'replay', 'build'): push_output(view, rkey, ('9: invalid args %s' % args)) return subcmd = args[0] cid = '' if subcmd == 'replay': cid = '9replay-%s' % wd cid, cb = _9_begin_call(subcmd, view, edit, args, wd, rkey, cid) a = { 'cid': cid, 'env': gs.env(), 'dir': wd, 'args': args[1:], 'build_only': (subcmd == 'build'), } win = view.window() if win is not None: av = win.active_view() if av is not None: fn = av.file_name() if fn: _save_all(win, wd) else: if gs.is_go_source_view(av, False): a['src'] = av.substr(sublime.Region(0, av.size())) mg9.acall('play', a, cb)
def cmd_9(view, edit, args, wd, rkey): if len(args) == 0 or args[0] not in ('play', 'build'): push_output(view, rkey, ('9: invalid args %s' % args)) return subcmd = args[0] cid, cb = _9_begin_call(subcmd, view, edit, args, wd, rkey) a = { 'cid': cid, 'env': gs.env(), 'dir': wd, 'args': args[1:], 'build_only': (subcmd == 'build'), } win = view.window() if win is not None: av = win.active_view() if av is not None: fn = av.file_name() if fn: basedir = gs.basedir_or_cwd(fn) for v in win.views(): try: fn = v.file_name() if fn and v.is_dirty() and fn.endswith('.go') and os.path.dirname(fn) == basedir: v.run_command('gs_fmt_save') except Exception: gs.println(gs.traceback()) else: if gs.is_go_source_view(av, False): a['src'] = av.substr(sublime.Region(0, av.size())) mg9.acall('play', a, cb)
def run(self, _, mode=''): view = self.view if (not gs.is_go_source_view(view)) or (mode not in ['goto', 'hint']): return pt = view.sel()[0].begin() r = view.word(pt) r2 = None if r.begin() > 1 and view.substr(sublime.Region(r.begin()-1, r.begin())) == ".": r2 = view.word(r.begin()-2) if not r2 and view.substr(sublime.Region(r.end(), r.end()+1)) == ".": r2 = view.word(r.end()+2) if r2: r = sublime.Region(min(r.begin(), r2.begin()), max(r.end(), r2.end())) expr = view.substr(r) src = view.substr(sublime.Region(0, view.size())) docs, err = margo.doc(view.file_name(), src, pt, expr) if err: gs.notice(DOMAIN, err) elif docs: if mode == "goto": fn = '' flags = 0 for d in docs: fn = d.get('fn', '') row = d.get('row', 0) col = d.get('col', 0) if row > 0: flags = sublime.ENCODED_POSITION fn = '%s:%d:%d' % (fn, row+1, col+1) if fn: view.window().open_file(fn, flags) else: gs.notice(DOMAIN, "cannot find definition for `%s'" % expr) elif mode == "hint": s = [] for d in docs: s.append('%s\n\n%s' % (d.get('decl'), d.get('doc'))) s = '\n\n\n\n'.join(s) panel = view.window().get_output_panel(DOMAIN) edit = panel.begin_edit() try: panel.set_read_only(False) panel.sel().clear() panel.replace(edit, sublime.Region(0, panel.size()), s) panel.set_read_only(True) finally: panel.end_edit(edit) view.window().run_command("show_panel", {"panel": "output.%s" % DOMAIN}) else: gs.notice(DOMAIN, "no docs found for `%s'" % expr) else: gs.notice(DOMAIN, 'cannot find a valid name: currently supports only pkg.Func')
def run(self, _, mode=''): view = self.view if (not gs.is_go_source_view(view)) or (mode not in ['goto', 'hint']): return pt = gs.sel(view).begin() src = view.substr(sublime.Region(0, view.size())) def f(docs, err): doc = '' if err: self.show_output('// Error: %s' % err) elif docs: if mode == "goto": fn = '' flags = 0 if len(docs) > 0: d = docs[0] fn = d.get('fn', '') row = d.get('row', 0) col = d.get('col', 0) if fn: gs.println('opening %s:%s:%s' % (fn, row, col)) gs.focus(fn, row, col) return self.show_output("%s: cannot find definition" % DOMAIN) elif mode == "hint": s = [] for d in docs: name = d.get('name', '') if name: kind = d.get('kind', '') pkg = d.get('pkg', '') if pkg: name = '%s.%s' % (pkg, name) src = d.get('src', '') if src: src = '\n//\n%s' % src doc = '// %s %s%s' % (name, kind, src) s.append(doc) doc = '\n\n\n'.join(s).strip() self.show_output(doc or "// %s: no docs found" % DOMAIN) margo.call( path='/doc', args={ 'fn': view.file_name(), 'src': src, 'offset': pt, }, default=[], cb=f, message='fetching docs' )
def run(self, _, mode=''): view = self.view if (not gs.is_go_source_view(view)) or (mode not in ['goto', 'hint']): return pt = view.sel()[0].begin() src = view.substr(sublime.Region(0, view.size())) def f(docs, err): doc = '' if err: self.show_output('// Error: %s' % err) elif docs: if mode == "goto": fn = '' flags = 0 if len(docs) > 0: d = docs[0] fn = d.get('fn', '') row = d.get('row', 0) col = d.get('col', 0) if fn: gs.println('opening %s:%s:%s' % (fn, row, col)) gs.focus(fn, row, col) return self.show_output("%s: cannot find definition" % DOMAIN) elif mode == "hint": s = [] for d in docs: name = d.get('name', '') if name: kind = d.get('kind', '') pkg = d.get('pkg', '') if pkg: name = '%s.%s' % (pkg, name) src = d.get('src', '') if src: src = '\n//\n%s' % src doc = '// %s %s%s' % (name, kind, src) s.append(doc) doc = '\n\n\n'.join(s).strip() self.show_output(doc or "// %s: no docs found" % DOMAIN) margo.call( path='/doc', args={ 'fn': view.file_name(), 'src': src, 'offset': pt, }, default=[], cb=f, message='fetching docs' )
def on_close(self, view): if gs.is_go_source_view(view): vid = view.id() def cb(): try: del gs.l_vsyncs[vid] del gs.l_lsyncs[vid] del gs.l_errors[vid] except: pass set_timeout(cb, 0)
def run(self, _, mode=""): view = self.view if (not gs.is_go_source_view(view)) or (mode not in ["goto", "hint"]): return pt = gs.sel(view).begin() src = view.substr(sublime.Region(0, view.size())) def f(docs, err): doc = "" if err: self.show_output("// Error: %s" % err) elif docs: if mode == "goto": fn = "" flags = 0 if len(docs) > 0: d = docs[0] fn = d.get("fn", "") row = d.get("row", 0) col = d.get("col", 0) if fn: gs.println("opening %s:%s:%s" % (fn, row, col)) gs.focus(fn, row, col) return self.show_output("%s: cannot find definition" % DOMAIN) elif mode == "hint": s = [] for d in docs: name = d.get("name", "") if name: kind = d.get("kind", "") pkg = d.get("pkg", "") if pkg: name = "%s.%s" % (pkg, name) src = d.get("src", "") if src: src = "\n//\n%s" % src doc = "// %s %s%s" % (name, kind, src) s.append(doc) doc = "\n\n\n".join(s).strip() self.show_output(doc or "// %s: no docs found" % DOMAIN) margo.call( path="/doc", args={"fn": view.file_name(), "src": src, "offset": pt}, default=[], cb=f, message="fetching docs", )
def run(self, edit): if not (gs.setting('fmt_enabled', False) is True and gs.is_go_source_view(self.view)): return vsize = self.view.size() src = self.view.substr(sublime.Region(0, vsize)) if not src.strip(): return src, err = margo.fmt(self.view.file_name(), src) if err or not src.strip(): return dirty, err = gspatch.merge(self.view, vsize, src) if err: gs.notice_undo(DOMAIN, err, self.view, dirty)
def run(self, _, mode=''): view = self.view if (not gs.is_go_source_view(view)) or (mode not in ['goto', 'hint']): return pt = view.sel()[0].begin() src = view.substr(sublime.Region(0, view.size())) docs, err = margo.doc(view.file_name(), src, pt) if err: self.show_output('// Error: %s' % err) elif docs: if mode == "goto": fn = '' flags = 0 for d in docs: fn = d.get('fn', '') row = d.get('row', 0) col = d.get('col', 0) if row > 0: flags = sublime.ENCODED_POSITION fn = '%s:%d:%d' % (fn, row+1, col+1) if fn: view.window().open_file(fn, flags) else: self.show_output("%s: cannot find definition" % DOMAIN) elif mode == "hint": s = [] for d in docs: decl = d.get('decl', '').strip() doc = d.get('doc', '').strip() src = d.get('src', '').strip() if doc: doc = '// %s' % doc.replace('\n', '\n// ') if decl: doc = '%s\n\n%s' % (decl, doc) if src: doc = src s.append(doc.strip() or ('// %s no docs' % DOMAIN)) s = '\n\n\n\n'.join(s) self.show_output(s) else: self.show_output("%s: no docs found" % DOMAIN)
def cmd_9(view, edit, args, wd, rkey): if len(args) == 0 or args[0] not in ('run', 'replay', 'build'): push_output(view, rkey, ('9: invalid args %s' % args)) return subcmd = args[0] cid = '' if subcmd == 'replay': cid = '9replay-%s' % wd cid, cb = _9_begin_call(subcmd, view, edit, args, wd, rkey, cid) a = { 'cid': cid, 'env': gs.env(), 'dir': wd, 'args': args[1:], 'build_only': (subcmd == 'build'), } win = view.window() if win is not None: av = win.active_view() if av is not None: fn = av.file_name() if fn: basedir = gs.basedir_or_cwd(fn) for v in win.views(): try: fn = v.file_name() if fn and v.is_dirty() and fn.endswith( '.go') and os.path.dirname(fn) == basedir: v.run_command('gs_fmt_save') except Exception: gs.println(gs.traceback()) else: if gs.is_go_source_view(av, False): a['src'] = av.substr(sublime.Region(0, av.size())) mg9.acall('play', a, cb)
def is_enabled(self): return gs.is_go_source_view(self.view)
def run(self, edit): if gs.is_go_source_view(self.view): self.view.run_command("gs_fmt") sublime.set_timeout(lambda: self.view.run_command("save"), 0)
def run(self, edit): if gs.is_go_source_view(self.view): self.view.run_command('gs_doc', {"mode": "hint"})
def is_enabled(self): return gs.setting('fmt_enabled', False) is True and gs.is_go_source_view(self.view)
def run(self, edit): if gs.is_go_source_view(self.view): self.view.run_command('gs_doc', {"mode": "goto"}) elif self.view.score_selector(gs.sel(self.view).begin(), "text.9o") > 0: self.view.window().run_command("gs9o_open_selection")
def is_enabled(self): return gs.is_go_source_view(self.window.active_view())
def check_depends(view): global dep_check_done if dep_check_done: return if not view or not view.window(): sublime.set_timeout(lambda: check_depends(view), 1000) return if not gs.is_go_source_view(view): return dep_check_done = True e = gs.env() if not (e.get('GOROOT') and e.get('GOPATH')): gs.notice(DOMAIN, "GOPATH and/or GOROOT appear to be unset") if not call_cmd(['go', '--help']): gs.notice(DOMAIN, 'The `go` command cannot be found') return missing = [] cmds = [ ['gocode', '--help'], ['MarGo', '--help'], ] for cmd in cmds: if not call_cmd(cmd): missing.append(cmd[0]) if missing: def cb(i): if i == 0: run_go_get(view) items = [[ 'GoSublime depends on gocode and MarGo', 'Install %s (using `go get`)' % ', '.join(missing), 'gocode repo: %s' % GOCODE_REPO, 'MarGo repo: %s' % MARGO_REPO, ]] view.window().show_quick_panel(items, cb) return changelog_fn = os.path.join(sublime.packages_path(), 'GoSublime', "CHANGELOG.md") try: with open(changelog_fn) as f: s = f.read() except IOError: gs.notice(DOMAIN, traceback.format_exc()) return changes = split_changes(s) if changes: win = sublime.active_window() if win: settings_fn = 'GoSublime-GsDepends.sublime-settings' settings = sublime.load_settings(settings_fn) new_rev = changes[-1][0] old_rev = settings.get('tracking_rev', '') def on_panel_close(i): if i == 1 or i == 2: view = win.open_file(changelog_fn) if i == 1: run_go_get(view) settings.set('tracking_rev', new_rev) sublime.save_settings(settings_fn) if new_rev > old_rev: items = [ [ " ", "GoSublime updated to %s" % new_rev, " ", ], [ "Install/Update dependencies: Gocode, MarGo", "go get -u %s" % GOCODE_REPO, "go get -u %s" % MARGO_REPO, ], [ "View changelog", "Packages/GoSublime/CHANGELOG.md" " ", ] ] win.show_quick_panel(items, on_panel_close) return gsq.dispatch(hello)
def run(self, edit): view = self.view if gs.is_go_source_view(view): if not gstest.handle_action(view, "right-click"): view.run_command("gs_doc", {"mode": "hint"})
def on_load(self, view): global dep_check_done sublime.set_timeout(gs.sync_settings, 0) if not dep_check_done and gs.is_go_source_view(view): dep_check_done = True margo.dispatch(check_depends, 'checking dependencies')