Пример #1
0
    def jump_to_imports(self):
        view = gs.active_valid_go_view()
        if not view:
            return

        last_import = gs.attr('last_import_path.%s' % vu.V(view).vfn(), '')
        r = None
        if last_import:
            offset = len(last_import) + 2
            last_import = re.escape(last_import)
            pat = '(?s)import.*?(?:"%s"|`%s`)' % (last_import, last_import)
            r = view.find(pat, 0)

        if not r:
            offset = 1
            pat = '(?s)import.*?["`]'
            r = view.find(pat, 0)

        if not r:
            ui.error(DOMAIN, "cannot find import declarations")
            return

        pt = r.end() - offset
        row, col = view.rowcol(pt)
        loc = Loc(view.file_name(), row, col)
        self.jump_to((view, loc))
Пример #2
0
    def run(self, edit, set_status=False):
        view = self.view
        vv = vu.V(view)
        fn = vv.vfn()
        src = vv.src()
        pos = vv.sel().begin()

        def f(cl, err):
            def f2(cl, err):
                c = {}
                if len(cl) == 1:
                    c = cl[0]

                if set_status:
                    intel, _ = mg9.bcall('intel', {
                        'Fn': fn,
                        'Src': src,
                        'Pos': pos,
                    })

                    s = ''
                    if c:
                        pfx = 'func('
                        typ = c['type']
                        if typ.startswith(pfx):
                            s = 'func %s(%s' % (c['name'], typ[len(pfx):])
                        else:
                            s = '%s: %s' % (c['name'], typ)

                    func = intel.get('Func')
                    if func:
                        s = u'%s \u00B7 %s > %s' % (s, intel.get('Pkg'), func)

                    if s:
                        view.set_status(HINT_KEY, s)
                    else:
                        view.erase_status(HINT_KEY)
                else:
                    if c:
                        s = '%s %s\n%s' % (c['name'], c['class'], c['type'])
                    else:
                        s = '// %s' % (err or 'No calltips found')

                    gs.show_output(HINT_KEY,
                                   s,
                                   print_output=False,
                                   syntax_file='GsDoc')

            sublime.set_timeout(lambda: f2(cl, err), 0)

        mg9.calltip(fn, src, pos, set_status, f)
Пример #3
0
	def run(self, edit, pos, content, added_path=''):
		pos = int(pos) # un-f*****g-believable
		view = self.view
		dirty, err = gspatch.merge(view, pos, content, edit)
		if err:
			ui.error(DOMAIN, err)
			if dirty:
				sublime.set_timeout(lambda: view.run_command('undo'), 0)
		elif dirty:
			k = 'last_import_path.%s' % vu.V(view).vfn()
			if added_path:
				gs.set_attr(k, added_path)
			else:
				gs.del_attr(k)
Пример #4
0
    def palette_declarations(self, view, direct=False):
        def f(res, err):
            added = 0
            if err:
                ui.note('GsDeclarations', err)
            else:
                decls = res.get('file_decls', [])
                for i, v in enumerate(decls):
                    loc = Loc(v['fn'], v['row'], v['col'])
                    s = '%s %s' % (v['kind'], (v['repr'] or v['name']))
                    self.add_item(s, self.jump_to, (view, loc))
                    added += 1

            if added < 1:
                self.add_item(['', 'No declarations found'])

            self.do_show_panel()

        vv = vu.V(view)
        mg9.declarations(vv.vfn(), vv.src(), '', f)
Пример #5
0
 def present_current(self):
     vv = vu.V(gs.active_valid_go_view(win=self.window, strict=False))
     self.present(vv.vfn(), vv.src(), vv.dir())
Пример #6
0
    def on_query_completions(self, view, prefix, locations):
        pos = locations[0]
        scopes = view.scope_name(pos).split()
        if ('source.go' not in scopes) or (gs.setting('gscomplete_enabled',
                                                      False) is not True):
            return []

        if view.score_selector(pos, 'comment.build-constraint.go') > 0:
            return (BUILD_CL, AC_OPTS)

        if not scope_ok(view, pos):
            return ([], AC_OPTS)

        try:
            if basename(view.file_name()) == "main.go":
                default_pkgname = 'main'
            else:
                default_pkgname = basename(dirname(view.file_name()))
        except Exception:
            default_pkgname = ''

        if not REASONABLE_PKGNAME_PAT.match(default_pkgname):
            default_pkgname = ''

        offset = pos - len(prefix)
        vv = vu.V(view)
        src = vv.src()

        if not src:
            return ([], AC_OPTS)

        intel, _ = mg9.bcall('intel', {
            'Fn': vv.vfn(),
            'Src': src,
            'Pos': pos,
        })

        pkgname = intel.get('Pkg')
        if not default_pkgname:
            default_pkgname = pkgname if pkgname else 'main'

        types = intel.get('Types') or []
        ctx = {
            'global': intel.get('Global'),
            'local': not intel.get('Global'),
            'pkgname': pkgname,
            'types': types or [''],
            'has_types': len(types) > 0,
            'default_pkgname': default_pkgname,
            'fn': vv.fn(),
        }

        if not pkgname:
            return (resolve_snippets(ctx),
                    AC_OPTS) if cfg.autocomplete_snippets else ([], AC_OPTS)

        nc = view.substr(sublime.Region(pos, pos + 1))
        cl = self.complete(vv.fn() or '<stdin>', offset, src,
                           nc.isalpha() or nc == "(")

        pc = view.substr(sublime.Region(pos - 1, pos))
        if cfg.autocomplete_snippets and (pc.isspace() or pc.isalpha()):
            if scopes[-1] == 'source.go':
                cl.extend(resolve_snippets(ctx))
            elif scopes[-1] == 'meta.block.go' and (
                    'meta.function.plain.go' in scopes
                    or 'meta.function.receiver.go' in scopes):
                cl.extend(resolve_snippets(ctx))
        return (cl, AC_OPTS)