def run(self): while True: fn = self.popq() fr = ref(fn, False) if fr: reports = {} res, _ = mg9.bcall('lint', { 'dir': (os.path.dirname(fn) if fn else ''), 'fn': fn, 'src': fr.src, 'filter': gs.setting('lint_filter', []), }) res = gs.dval(res, {}) for r in gs.dval(res.get('reports'), []): if fn and fn != '<stdin>' and r.get('Fn') != fn: continue kind = r.get('Kind', '') row = r.get('Row', 0) col = r.get('Col', 0) msg = r.get('Message', '') msg = '%s: %s' % (kind, msg) if row >= 0 and msg: reports[row] = Report(row, col, msg) fr = ref(fn, False) if fr: with sem: fr.state = 1 fr.reports = reports file_refs[fn] = fr
def cb(res, err): if tid: gs.end(tid) res = gs.dval(res.get("Candidates"), []) calltip_cache.set(fn, src, pos, res, err) f(res, err)
def complete(fn, src, pos): builtins = ( gs.setting("autocomplete_builtins") is True or gs.setting("complete_builtins") is True ) res, err = bcall("gocode_complete", _complete_opts(fn, src, pos, builtins)) res = gs.dval(res.get("Candidates"), []) return res, err
def run(self): while True: fn = self.popq() fr = ref(fn, False) if fr: reports = {} res, _ = mg9.bcall('lint', {'fn': fn, 'src': fr.src, 'filter': gs.setting('lint_filter', [])}) res = gs.dval(res, {}) for r in gs.dval(res.get('reports'), []): row = r.get('Row', 0) col = r.get('Col', 0) msg = r.get('Message', '') if row >= 0 and msg: reports[row] = Report(row, col, msg) fr = ref(fn, False) if fr: with sem: fr.state = 1 fr.reports = reports file_refs[fn] = fr
def run(self): while True: fn = self.popq() fr = ref(fn, False) if fr: reports = {} res, _ = mg9.bcall('lint', {'fn': fn, 'src': fr.src}) res = gs.dval(res, {}) for r in gs.dval(res.get('reports'), []): row = r.get('row', 0) col = r.get('col', 0) msg = r.get('message', '') if row >= 0 and msg: reports[row] = Report(row, col, msg) fr = ref(fn, False) if fr: with sem: fr.state = 1 fr.reports = reports file_refs[fn] = fr
def do_sync_active_view(view): fn = view.file_name() if fn: gs.set_attr('last_active_fn', fn) if fn.lower().endswith('.go'): gs.set_attr('last_active_go_fn', fn) if gs.is_pkg_view(view): m = {} psettings = view.settings().get('GoSublime') if psettings and gs.is_a(psettings, {}): m = gs.mirror_settings(psettings) gs.set_attr('last_active_project_settings', gs.dval(m, {}))
def on_query_completions(self, view, prefix, locations): pos = gs.sel(view).begin() if view.score_selector(pos, 'text.9o') == 0: return [] cl = set() hkey = _hkey(view.settings().get('9o.wd', '')) cl.update((k, k + ' ') for k in gs.dval(gs.aso().get(hkey), [])) cl.update((k, k + ' ') for k in aliases()) cl.update((k, k + ' ') for k in builtins()) cl.update(DEFAULT_CL) return ([cl_esc(e) for e in sorted(cl)], AC_OPTS)
def on_query_completions(self, view, prefix, locations): pos = gs.sel(view).begin() if view.score_selector(pos, 'text.9o') == 0: return [] cl = set() hkey = _hkey(view.settings().get('9o.wd', '')) cl.update((k, k+' ') for k in gs.dval(gs.aso().get(hkey), [])) cl.update((k, k+' ') for k in aliases()) cl.update((k, k+' ') for k in builtins()) cl.update(DEFAULT_CL) return ([cl_esc(e) for e in sorted(cl)], AC_OPTS)
def do_sync_active_view(view): fn = view.file_name() or '' gs.set_attr('active_fn', fn) if fn: gs.set_attr('last_active_fn', fn) if fn.lower().endswith('.go'): gs.set_attr('last_active_go_fn', fn) if gs.is_pkg_view(view): m = {} psettings = view.settings().get('GoSublime') if psettings and gs.is_a(psettings, {}): m = gs.mirror_settings(psettings) gs.set_attr('last_active_project_settings', gs.dval(m, {}))
def on_query_completions(self, view, prefix, locations): pos = gs.sel(view).begin() if view.score_selector(pos, 'text.9o') == 0: return [] cl = [] hkey = '9o.hist.%s' % view.settings().get('9o.wd', '') for i, cmd in enumerate(reversed(gs.dval(gs.aso().get(hkey), []))): if not cmd in cl: cl.append(('^%d %s' % (i+1, cmd), cmd+' ')) cl.extend(DEFAULT_CL) return (cl, AC_OPTS)
def on_query_completions(self, view, prefix, locations): pos = gs.sel(view).begin() if view.score_selector(pos, 'text.9o') == 0: return [] cl = [] hkey = '9o.hist.%s' % view.settings().get('9o.wd', '') for i, cmd in enumerate(reversed(gs.dval(gs.aso().get(hkey), []))): if not cmd in cl: cl.append(('^%d %s' % (i + 1, cmd), cmd + ' ')) cl.extend(DEFAULT_CL) return (cl, AC_OPTS)
def do_sync_active_view(view): fn = view.file_name() or '' gs.set_attr('active_fn', fn) if fn: gs.set_attr('last_active_fn', fn) if fn.lower().endswith('.go'): gs.set_attr('last_active_go_fn', fn) win = view.window() if win is not None and view in win.views(): m = {} psettings = view.settings().get('GoSublime') if psettings and gs.is_a(psettings, {}): m = gs.mirror_settings(psettings) gs.set_attr('last_active_project_settings', gs.dval(m, {}))
def do_sync_active_view(view): fn = view.file_name() or '' gs.set_attr('active_fn', fn) if fn: gs.set_attr('last_active_fn', fn) if fn.lower().endswith('.go'): gs.set_attr('last_active_go_fn', fn) win = view.window() if win is not None and view in win.views(): m = {} psettings = view.settings().get('GoSublime') if psettings and gs.is_a(psettings, {}): psettings['env']['GOPATH'] = expand_template(psettings['env']['GOPATH']) m = gs.mirror_settings(psettings) gs.set_attr('last_active_project_settings', gs.dval(m, {}))
def complete(fn, src, pos): home = gs.home_path() builtins = (gs.setting('autocomplete_builtins') is True or gs.setting('complete_builtins') is True) res, err = bcall('gocode_complete', { 'Dir': gs.basedir_or_cwd(fn), 'Builtins': builtins, 'Fn': fn or '', 'Src': src or '', 'Pos': pos or 0, 'Home': home, 'Env': gs.env({ 'XDG_CONFIG_HOME': home, }), }) res = gs.dval(res.get('completions'), []) return res, err
def cmd_hist(view, edit, args, wd, rkey): aso = gs.aso() hkey = _hkey(wd) s = 'hist: invalid args: %s' % args if len(args) == 0: hist = gs.dval(aso.get(hkey), []) hist.reverse() hlen = len(hist) s = '\n'.join('^%d: %s' % (i + 1, v) for i, v in enumerate(hist)) elif len(args) == 1: if args[0] == 'erase': aso.erase(hkey) gs.save_aso() s = '' push_output(view, rkey, s)
def cmd_hist(view, edit, args, wd, rkey): aso = gs.aso() hkey = _hkey(wd) s = 'hist: invalid args: %s' % args if len(args) == 0: hist = gs.dval(aso.get(hkey), []) hist.reverse() hlen = len(hist) s = '\n'.join('^%d: %s' % (i+1, v) for i,v in enumerate(hist)) elif len(args) == 1: if args[0] == 'erase': aso.erase(hkey) gs.save_aso() s = '' push_output(view, rkey, s)
def on_query_completions(self, view, _, locations): pos = locations[0] if view.score_selector(pos, 'text.9o') == 0: return [] if view.substr(locations[0] - 2) == '$': return ([('$' + k, '\$' + k + ' ') for k in sh.env()], AC_OPTS) cl = set() slash = os.path.sep # the prefix passed tho us by definition doesn't contain slash because it's not a word char p = view.substr(sublime.Region(view.line(pos).begin(), pos)) p = p.split()[-1].lstrip(' #') file_only_comp = p.startswith('.') or slash in p if not p.startswith(('.', slash)): p = '.' + slash + p rm = '' m = word_sep_pat.match(p) if m: rm = m.group(1) try: for fn in glob.iglob(p + '*'): space = ' ' if os.path.isdir(fn): space = '' fn += '/' cl.add((fn, fn[len(rm):] + space)) except Exception: pass if not file_only_comp: hkey = _hkey(view.settings().get('9o.wd', '')) cl.update((k, k + ' ') for k in gs.dval(gs.aso().get(hkey), [])) cl.update((k, k + ' ') for k in builtins()) cl.update(DEFAULT_CL) return ([cl_esc(e) for e in sorted(cl)], AC_OPTS)
def on_query_completions(self, view, _, locations): pos = locations[0] if view.score_selector(pos, 'text.9o') == 0: return [] if view.substr(locations[0]-2) == '$': return ([('$'+k, '\$'+k+' ') for k in sh.env()], AC_OPTS) cl = set() slash = os.path.sep # the prefix passed tho us by definition doesn't contain slash because it's not a word char p = view.substr(sublime.Region(view.line(pos).begin(), pos)) p = p.split()[-1].lstrip(' #') file_only_comp = p.startswith('.') or slash in p if not p.startswith(('.', slash)): p = '.'+slash+p rm = '' m = word_sep_pat.match(p) if m: rm = m.group(1) try: for fn in glob.iglob(p+'*'): space = ' ' if os.path.isdir(fn): space = '' fn += '/' cl.add((fn, fn[len(rm):]+space)) except Exception: pass if not file_only_comp: hkey = _hkey(view.settings().get('9o.wd', '')) cl.update((k, k+' ') for k in gs.dval(gs.aso().get(hkey), [])) cl.update((k, k+' ') for k in builtins()) cl.update(DEFAULT_CL) return ([cl_esc(e) for e in sorted(cl)], AC_OPTS)
def run(self, edit, up): view = self.view pos = gs.sel(view).begin() if view.score_selector(pos, 'prompt.9o') <= 0: return aso = gs.aso() vs = view.settings() wd = vs.get('9o.wd') hkey = _hkey(wd) hist = [s for s in gs.dval(aso.get(hkey), []) if s.strip()] if not hist: return r = view.extract_scope(pos) cmd = view.substr(r).strip('#').strip() try: idx = hist.index(cmd) + (-1 if up else 1) found = True except Exception: idx = -1 found = False if cmd and not found: hist.append(cmd) aso.set(hkey, hist) gs.save_aso() if idx >= 0 and idx < len(hist): cmd = hist[idx] elif up: if not found: cmd = hist[-1] else: cmd = '' view.replace(edit, r, '# %s \n' % cmd) n = view.line(r.begin()).end() view.sel().clear() view.sel().add(sublime.Region(n, n))
def complete(fn, src, pos): res, err = bcall('gocode_complete', _complete_opts(fn, src, pos)) res = gs.dval(res.get('completions'), []) return res, err
def completion_options(m={}): res, err = bcall("gocode_options", {}) res = gs.dval(res.get("options"), {}) return res, err
def run(self, edit, save_hist=False): view = self.view pos = gs.sel(view).begin() line = view.line(pos) wd = view.settings().get('9o.wd') try: os.chdir(wd) except Exception: gs.error_traceback(DOMAIN) ln = view.substr(line).split('#', 1) if len(ln) == 2: cmd = ln[1].strip() if cmd: vs = view.settings() aso = gs.aso() hkey = _hkey(wd) hist = gs.dval(aso.get(hkey), []) m = HIST_EXPAND_PAT.match(cmd) if m: pfx = m.group(1) hl = len(hist) idx = hl - int(m.group(2)) cmd = '' if idx >= 0 and idx < hl: cmd = hist[idx] if pfx == '^' or not cmd: view.replace(edit, line, ('%s# %s' % (ln[0], cmd))) return elif save_hist: try: hist.remove(cmd) except ValueError: pass hist.append(cmd) aso.set(hkey, hist) gs.save_aso() if not cmd: view.run_command('gs9o_init') return view.replace(edit, line, (u'[ `%s` %s ]' % (cmd, HOURGLASS))) rkey = '9o.exec.%s' % uuid.uuid4() view.add_regions(rkey, [sublime.Region(line.begin(), view.size())], '') view.run_command('gs9o_init') nv = sh.env() anv = nv.copy() seen = {} am = aliases() while True: cli = cmd.split(' ', 1) nm = cli[0] if not nm: break ag = cli[1].strip() if len(cli) == 2 else '' alias = am.get(nm, '') if not alias: break if alias in seen: gs.error(DOMAIN, 'recursive alias detected: `%s`' % alias) break seen[alias] = True anv['_args'] = ag cmd = string.Template(alias).safe_substitute(anv) if nm != 'sh': f = builtins().get(nm) if f: args = [] if ag: args = [_exparg(s, nv) for s in shlex.split(gs.astr(ag))] f(view, edit, args, wd, rkey) return if nm == 'sh': args = sh.cmd(ag) else: args = sh.cmd(cmd) cmd_sh(view, edit, args, wd, rkey) else: view.insert(edit, gs.sel(view).begin(), '\n')
def _exec(view, edit, save_hist=False): pos = gs.sel(view).begin() line = view.line(pos) wd = view.settings().get('9o.wd') try: os.chdir(wd) except Exception: ui.trace(DOMAIN) ln = view.substr(line).split('#', 1) if len(ln) == 2: cmd = ln[1].strip() if cmd: vs = view.settings() aso = gs.aso() hkey = _hkey(wd) hist = gs.dval(aso.get(hkey), []) m = HIST_EXPAND_PAT.match(cmd) if m: pfx = m.group(1) hl = len(hist) idx = hl - int(m.group(2)) cmd = '' if idx >= 0 and idx < hl: cmd = hist[idx] if pfx == '^' or not cmd: view.replace(edit, line, ('%s# %s' % (ln[0], cmd))) return elif save_hist: try: hist.remove(cmd) except ValueError: pass hist.append(cmd) aso.set(hkey, hist) gs.save_aso() if not cmd: view.run_command('gs9o_init') return line = view.full_line(pos) ctx = '9o.exec.%s' % gs.uid() view.replace(edit, line, ('[`%s`]\n' % cmd)) view.run_command('gs9o_init') ep = view.full_line(line.begin()).end() view.add_regions(ctx, [sublime.Region(ep, ep)], '') hellip = u'[ \u22EF ]' ep += 1 view.insert(edit, ep, hellip + '\n\n') view.add_regions(ctx + '.done', [sublime.Region(ep, ep + len(hellip))], '') cli = cmd.split(' ', 1) if cli[0] == 'sh': a = cli[1].strip() if len(cli) == 2 else '' mk_cmd(view, wd, ctx, sh.cmd(a)).start() return nv = sh.env() a = [_exparg(s, nv) for s in shlex.split(gs.astr(cmd))] f = builtins().get(a[0]) if f: f(view, edit, a[1:], wd, ctx) else: mk_cmd(view, wd, ctx, a).start() else: view.insert(edit, gs.sel(view).begin(), '\n')
def complete(fn, src, pos): builtins = (gs.setting('autocomplete_builtins') is True or gs.setting('complete_builtins') is True) res, err = bcall('gocode_complete', _complete_opts(fn, src, pos, builtins)) res = gs.dval(res.get('Candidates'), []) return res, err
def run(self, edit, save_hist=False): view = self.view pos = gs.sel(view).begin() line = view.line(pos) wd = view.settings().get('9o.wd') ln = view.substr(line).split('#', 1) if len(ln) == 2: cmd = ln[1].strip() if cmd: vs = view.settings() aso = gs.aso() hkey = '9o.hist.%s' % wd hist = gs.dval(aso.get(hkey), []) m = HIST_EXPAND_PAT.match(cmd) if m: pfx = m.group(1) hl = len(hist) idx = hl - int(m.group(2)) cmd = '' if idx >= 0 and idx < hl: cmd = hist[idx] if pfx == '^' or not cmd: view.replace(edit, line, ('%s# %s' % (ln[0], cmd))) return elif save_hist: try: hist.remove(cmd) except ValueError: pass hist.append(cmd) aso.set(hkey, hist) gs.save_aso() if not cmd: view.run_command('gs9o_init') return view.replace(edit, line, (u'[ `%s` %s ]' % (cmd, HOURGLASS))) rkey = '9o.exec.%s' % uuid.uuid4() view.add_regions(rkey, [sublime.Region(line.begin(), view.size())], '') view.run_command('gs9o_init') cli = cmd.split(' ', 1) # todo: move this into margo if cli[0] == 'sh': def on_done(c): out = gs.ustr('\n'.join(c.consume_outq())) sublime.set_timeout(lambda: push_output(view, rkey, out), 0) c = gsshell.Command(cmd=cli[1], shell=True, cwd=wd) c.on_done = on_done c.start() return f = globals().get('cmd_%s' % cli[0]) if f: args = shlex.split(gs.astr(cli[1])) if len(cli) == 2 else [] f(view, edit, args, wd, rkey) else: push_output(view, rkey, 'Invalid command %s' % cli) else: view.insert(edit, gs.sel(view).begin(), '\n')
def cb(res, err): if tid: gs.end(tid) res = gs.dval(res.get('Candidates'), []) f(res, err)
def _exec(view, edit, save_hist=False): pos = gs.sel(view).begin() line = view.line(pos) wd = view.settings().get('9o.wd') try: os.chdir(wd) except Exception: ui.trace(DOMAIN) ln = view.substr(line).split('#', 1) if len(ln) == 2: cmd = ln[1].strip() if cmd: vs = view.settings() aso = gs.aso() hkey = _hkey(wd) hist = gs.dval(aso.get(hkey), []) m = HIST_EXPAND_PAT.match(cmd) if m: pfx = m.group(1) hl = len(hist) idx = hl - int(m.group(2)) cmd = '' if idx >= 0 and idx < hl: cmd = hist[idx] if pfx == '^' or not cmd: view.replace(edit, line, ('%s# %s' % (ln[0], cmd))) return elif save_hist: try: hist.remove(cmd) except ValueError: pass hist.append(cmd) aso.set(hkey, hist) gs.save_aso() if not cmd: view.run_command('gs9o_init') return line = view.full_line(pos) ctx = '9o.exec.%s' % gs.uid() view.replace(edit, line, ('[`%s`]\n' % cmd)) view.run_command('gs9o_init') ep = view.full_line(line.begin()).end() view.add_regions(ctx, [sublime.Region(ep, ep)], '') hellip = u'[ \u22EF ]' ep += 1 view.insert(edit, ep, hellip+'\n\n') view.add_regions(ctx+'.done', [sublime.Region(ep, ep+len(hellip))], '') cli = cmd.split(' ', 1) if cli[0] == 'sh': a = cli[1].strip() if len(cli) == 2 else '' mk_cmd(view, wd, ctx, sh.cmd(a)).start() return nv = sh.env() a = [_exparg(s, nv) for s in shlex.split(gs.astr(cmd))] f = builtins().get(a[0]) if f: f(view, edit, a[1:], wd, ctx) else: mk_cmd(view, wd, ctx, a).start() else: view.insert(edit, gs.sel(view).begin(), '\n')
def cb(res, err): if tid: gs.end(tid) res = gs.dval(res.get('calltips'), []) f(res, err)
def completion_options(m={}): res, err = bcall('gocode_options', {}) res = gs.dval(res.get('options'), {}) return res, err
def complete(fn, src, pos): res, err = bcall("gocode_complete", _complete_opts(fn, src, pos)) res = gs.dval(res.get("completions"), []) return res, err
def run(self, edit, save_hist=False): view = self.view pos = gs.sel(view).begin() line = view.line(pos) wd = view.settings().get('9o.wd') try: os.chdir(wd) except Exception: gs.error_traceback(DOMAIN) ln = view.substr(line).split('#', 1) if len(ln) == 2: cmd = ln[1].strip() if cmd: vs = view.settings() aso = gs.aso() hkey = _hkey(wd) hist = gs.dval(aso.get(hkey), []) m = HIST_EXPAND_PAT.match(cmd) if m: pfx = m.group(1) hl = len(hist) idx = hl - int(m.group(2)) cmd = '' if idx >= 0 and idx < hl: cmd = hist[idx] if pfx == '^' or not cmd: view.replace(edit, line, ('%s# %s' % (ln[0], cmd))) return elif save_hist: try: hist.remove(cmd) except ValueError: pass hist.append(cmd) aso.set(hkey, hist) gs.save_aso() if not cmd: view.run_command('gs9o_init') return view.replace(edit, line, (u'[ `%s` %s ]' % (cmd, HOURGLASS))) rkey = '9o.exec.%s' % uuid.uuid4() view.add_regions(rkey, [sublime.Region(line.begin(), view.size())], '') view.run_command('gs9o_init') nv = sh.env() anv = nv.copy() seen = {} am = aliases() while True: cli = cmd.split(' ', 1) nm = cli[0] if not nm: break ag = cli[1].strip() if len(cli) == 2 else '' alias = am.get(nm, '') if not alias: break if alias in seen: gs.error(DOMAIN, 'recursive alias detected: `%s`' % alias) break seen[alias] = True anv['_args'] = ag cmd = string.Template(alias).safe_substitute(anv) if nm != 'sh': f = builtins().get(nm) if f: args = [] if ag: args = [ _exparg(s, nv) for s in shlex.split(gs.astr(ag)) ] f(view, edit, args, wd, rkey) return if nm == 'sh': args = sh.cmd(ag) else: args = sh.cmd(cmd) cmd_sh(view, edit, args, wd, rkey) else: view.insert(edit, gs.sel(view).begin(), '\n')
def run(self, edit, save_hist=False): view = self.view pos = gs.sel(view).begin() line = view.line(pos) wd = view.settings().get('9o.wd') ln = view.substr(line).split('#', 1) if len(ln) == 2: cmd = ln[1].strip() if cmd: vs = view.settings() aso = gs.aso() hkey = _hkey(wd) hist = gs.dval(aso.get(hkey), []) m = HIST_EXPAND_PAT.match(cmd) if m: pfx = m.group(1) hl = len(hist) idx = hl - int(m.group(2)) cmd = '' if idx >= 0 and idx < hl: cmd = hist[idx] if pfx == '^' or not cmd: view.replace(edit, line, ('%s# %s' % (ln[0], cmd))) return elif save_hist: try: hist.remove(cmd) except ValueError: pass hist.append(cmd) aso.set(hkey, hist) gs.save_aso() if not cmd: view.run_command('gs9o_init') return view.replace(edit, line, (u'[ `%s` %s ]' % (cmd, HOURGLASS))) rkey = '9o.exec.%s' % uuid.uuid4() view.add_regions(rkey, [sublime.Region(line.begin(), view.size())], '') view.run_command('gs9o_init') cli = cmd.split(' ', 1) nm = cli[0] ag = cli[1].strip() if len(cli) == 2 else '' if nm == "cd": args = [ag] if ag else [] cmd_cd(view, edit, args, wd, rkey) return # todo: move this into margo if nm == 'sh': def on_done(c): out = gs.ustr('\n'.join(c.consume_outq())) sublime.set_timeout(lambda: push_output(view, rkey, out), 0) c = gsshell.Command(cmd=ag, shell=True, cwd=wd) c.on_done = on_done c.start() return f = gs.gs9o.get(nm) or globals().get('cmd_%s' % nm) if f: args = shlex.split(gs.astr(ag)) if ag else [] f(view, edit, args, wd, rkey) else: push_output(view, rkey, 'Invalid command %s' % cli) else: view.insert(edit, gs.sel(view).begin(), '\n')