Exemplo n.º 1
0
def show_pkgfiles(dirname, o=None):
    ents = []
    m = {}

    try:
        dirname = os.path.abspath(dirname)
        for fn in gs.list_dir_tree(dirname, ext_filter,
                                   gs.setting('fn_exclude_prefixes', []), o):
            name = os.path.relpath(fn, dirname).replace('\\', '/')
            m[name] = fn
            ents.append(name)
    except Exception as ex:
        ui.error(DOMAIN, 'Error: %s' % ex)

    if ents:
        ents.sort(key=lambda a: a.lower())

        try:
            s = " ../  ( current: %s )" % dirname
            m[s] = os.path.join(dirname, "..")
            ents.insert(0, s)
        except Exception:
            pass

        def cb(i, win):
            if i >= 0:
                fn = m[ents[i]]
                if os.path.isdir(fn):
                    win.run_command("gs_browse_files", {"dir": fn})
                else:
                    vu.open(fn=fn, win=win)

        gs.show_quick_panel(ents, cb)
    else:
        gs.show_quick_panel([['', 'No files found']])
Exemplo n.º 2
0
def show_pkgfiles(dirname, o=None):
	ents = []
	m = {}

	try:
		dirname = os.path.abspath(dirname)
		for fn in gs.list_dir_tree(dirname, ext_filter, gs.setting('fn_exclude_prefixes', []), o):
			name = os.path.relpath(fn, dirname).replace('\\', '/')
			m[name] = fn
			ents.append(name)
	except Exception as ex:
		ui.error(DOMAIN, 'Error: %s' % ex)

	if ents:
		ents.sort(key = lambda a: a.lower())

		try:
			s = " ../  ( current: %s )" % dirname
			m[s] = os.path.join(dirname, "..")
			ents.insert(0, s)
		except Exception:
			pass

		def cb(i, win):
			if i >= 0:
				fn = m[ents[i]]
				if os.path.isdir(fn):
					win.run_command("gs_browse_files", {"dir": fn})
				else:
					vu.open(fn=fn, win=win)
		gs.show_quick_panel(ents, cb)
	else:
		gs.show_quick_panel([['', 'No files found']])
Exemplo n.º 3
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))
Exemplo n.º 4
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))
Exemplo n.º 5
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)
Exemplo n.º 6
0
		def f(res, err):
			if err:
				ui.error(DOMAIN, err)
				return

			fn = res.get('Fn')
			row = res.get('Line', -1) - 1
			col = res.get('Col', 0) - 1
			if not fn or row < 0:
				ui.note(DOMAIN, "no definition found")
				return

			gs.println('opening %s:%s:%s' % (fn, row, col))
			vu.open(fn=fn, row=row, col=col)
Exemplo n.º 7
0
        def f(res, err):
            if err:
                ui.error(DOMAIN, err)
                return

            fn = res.get('Fn')
            row = res.get('Line', -1) - 1
            col = res.get('Col', 0) - 1
            if not fn or row < 0:
                ui.note(DOMAIN, "no definition found")
                return

            gs.println('opening %s:%s:%s' % (fn, row, col))
            vu.open(fn=fn, row=row, col=col)
Exemplo n.º 8
0
		def f(res, err):
			if err:
				ui.error(DOMAIN, err)
				return

			ents, m = handle_pkgdirs_res(res)
			if ents:
				def cb(i, win):
					if i >= 0:
						dirname = gs.basedir_or_cwd(m[ents[i]])
						win.run_command('gs_browse_files', {'dir': dirname})
				gs.show_quick_panel(ents, cb)
			else:
				gs.show_quick_panel([['', 'No source directories found']])
Exemplo n.º 9
0
    def show_palette(self, palette, direct=False):
        view = gs.active_valid_go_view(self.window)
        if not view:
            return

        palette = palette.lower().strip()
        if palette == 'auto':
            palette = self.last_activate_palette
        elif palette == 'main':
            palette = ''

        pcb = None
        if palette:
            pcb = self.palettes.get(palette)
            if pcb:
                self.last_activate_palette = palette
            else:
                ui.error(DOMAIN, 'Invalid palette `%s`' % palette)
                palette = ''

        if not direct and len(self.bookmarks) > 0:
            loc = self.bookmarks[-1]
            line = 'line %d' % (loc.row + 1)
            if view.file_name() == loc.fn:
                fn = ''
            else:
                fn = relpath(loc.fn, dirname(loc.fn))
                if fn.startswith('..'):
                    fn = loc.fn
                fn = '%s ' % fn
            self.add_item(u'\u2190 Go Back (%s%s)' % (fn, line),
                          self.jump_back, None)

        if not direct and palette:
            self.add_item(u'@%s \u21B5' % palette.title(), self.show_palette,
                          'main')

        li1 = len(self.items)
        if pcb:
            pcb(view, direct)

        if not direct:
            for k in sorted(self.palettes.keys()):
                if k:
                    if k != palette:
                        ttl = '@' + k.title()
                        itm = ttl
                        self.add_item(itm, self.show_palette, k)
Exemplo n.º 10
0
        def f(res, err):
            if err:
                ui.error(DOMAIN, err)
                return

            ents, m = handle_pkgdirs_res(res)
            if ents:

                def cb(i, win):
                    if i >= 0:
                        dirname = gs.basedir_or_cwd(m[ents[i]])
                        win.run_command('gs_browse_files', {'dir': dirname})

                gs.show_quick_panel(ents, cb)
            else:
                gs.show_quick_panel([['', 'No source directories found']])
Exemplo n.º 11
0
        def f(im, err):
            if err:
                ui.error(DOMAIN, err)
                return

            delete_imports = []
            add_imports = []
            paths = im.get('paths', {})
            for path in paths:
                skipAdd = False
                for i in im.get('imports', []):
                    if i.get('path') == path:
                        skipAdd = True
                        name = i.get('name', '')
                        if not name:
                            name = basename(path)
                        if name == path:
                            delete_imports.append(
                                ('%sdelete: %s' % (indent, name), i))
                        else:
                            delete_imports.append(
                                ('%sdelete: %s ( %s )' % (indent, name, path),
                                 i))

                if not skipAdd:
                    s = '%s%s' % (indent, path)
                    m = {
                        'path': path,
                        'add': True,
                    }

                    nm = paths[path]
                    if nm and nm != path and not path.endswith('/%s' % nm):
                        s = '%s (%s)' % (s, nm)
                        if gs.setting('use_named_imports') is True:
                            m['name'] = nm

                    add_imports.append((s, m))

            for i in sorted(delete_imports):
                self.add_item(i[0], toggle_import, (view, i[1]))
            if len(delete_imports) > 0:
                self.add_item(' ', self.show_palette, 'imports')
            for i in sorted(add_imports):
                self.add_item(i[0], toggle_import, (view, i[1]))

            self.do_show_panel()
Exemplo n.º 12
0
	def show_palette(self, palette, direct=False):
		view = gs.active_valid_go_view(self.window)
		if not view:
			return

		palette = palette.lower().strip()
		if palette == 'auto':
			palette = self.last_activate_palette
		elif palette == 'main':
			palette = ''

		pcb = None
		if palette:
			pcb = self.palettes.get(palette)
			if pcb:
				self.last_activate_palette = palette
			else:
				ui.error(DOMAIN, 'Invalid palette `%s`' % palette)
				palette = ''

		if not direct and len(self.bookmarks) > 0:
			loc = self.bookmarks[-1]
			line = 'line %d' % (loc.row + 1)
			if view.file_name() == loc.fn:
				fn = ''
			else:
				fn = relpath(loc.fn, dirname(loc.fn))
				if fn.startswith('..'):
					fn = loc.fn
				fn = '%s ' % fn
			self.add_item(u'\u2190 Go Back (%s%s)' % (fn, line), self.jump_back, None)

		if not direct and palette:
			self.add_item(u'@%s \u21B5' % palette.title(), self.show_palette, 'main')

		li1 = len(self.items)
		if pcb:
			pcb(view, direct)

		if not direct:
			for k in sorted(self.palettes.keys()):
				if k:
					if k != palette:
						ttl = '@' + k.title()
						itm = ttl
						self.add_item(itm, self.show_palette, k)
Exemplo n.º 13
0
            def f(res, err):
                if err:
                    ui.error(DOMAIN, err)
                    return

                ents, m = handle_pkgdirs_res(res)
                if ents:
                    ents.insert(0, "Current Package")

                    def cb(i, win):
                        if i == 0:
                            self.present_current()
                        elif i >= 1:
                            self.present('', '', os.path.dirname(m[ents[i]]))

                    gs.show_quick_panel(ents, cb)
                else:
                    gs.show_quick_panel([['', 'No source directories found']])
Exemplo n.º 14
0
			def f(res, err):
				if err:
					ui.error(DOMAIN, err)
					return

				ents, m = handle_pkgdirs_res(res)
				if ents:
					ents.insert(0, "Current Package")

					def cb(i, win):
						if i == 0:
							self.present_current()
						elif i >= 1:
							self.present('', '', os.path.dirname(m[ents[i]]))

					gs.show_quick_panel(ents, cb)
				else:
					gs.show_quick_panel([['', 'No source directories found']])
Exemplo n.º 15
0
		def f(im, err):
			if err:
				ui.error(DOMAIN, err)
				return

			delete_imports = []
			add_imports = []
			paths = im.get('paths', {})
			for path in paths:
				skipAdd = False
				for i in im.get('imports', []):
					if i.get('path') == path:
						skipAdd = True
						name = i.get('name', '')
						if not name:
							name = basename(path)
						if name == path:
							delete_imports.append(('%sdelete: %s' % (indent, name), i))
						else:
							delete_imports.append(('%sdelete: %s ( %s )' % (indent, name, path), i))

				if not skipAdd:
					s = '%s%s' % (indent, path)
					m = {
						'path': path,
						'add': True,
					}

					nm = paths[path]
					if nm and nm != path and not path.endswith('/%s' % nm):
						s = '%s (%s)' % (s, nm)
						if gs.setting('use_named_imports') is True:
							m['name'] = nm

					add_imports.append((s, m))

			for i in sorted(delete_imports):
				self.add_item(i[0], toggle_import, (view, i[1]))
			if len(delete_imports) > 0:
				self.add_item(' ', self.show_palette, 'imports')
			for i in sorted(add_imports):
				self.add_item(i[0], toggle_import, (view, i[1]))

			self.do_show_panel()
Exemplo n.º 16
0
def toggle_import(a):
    view, decl = a
    im, err = mg9.imports(view.file_name(),
                          view.substr(sublime.Region(0, view.size())), [decl])

    if err:
        ui.error(DOMAIN, err)
    else:
        src = im.get('src', '')
        line_ref = im.get('lineRef', 0)
        r = view.full_line(view.text_point(max(0, line_ref - 1), 0))
        if not src or line_ref < 1 or not r:
            return

        view.run_command(
            'gs_patch_imports', {
                'pos': r.end(),
                'content': src,
                'added_path': (decl.get('path') if decl.get('add') else '')
            })
Exemplo n.º 17
0
def toggle_import(a):
	view, decl = a
	im, err = mg9.imports(
		view.file_name(),
		view.substr(sublime.Region(0, view.size())),
		[decl]
	)

	if err:
		ui.error(DOMAIN, err)
	else:
		src = im.get('src', '')
		line_ref = im.get('lineRef', 0)
		r = view.full_line(view.text_point(max(0, line_ref-1), 0))
		if not src or line_ref < 1 or not r:
			return

		view.run_command('gs_patch_imports', {
			'pos': r.end(),
			'content': src,
			'added_path': (decl.get('path') if decl.get('add') else '')
		})
Exemplo n.º 18
0
	def complete(self, fn, offset, src, func_name_only):
		comps = []
		autocomplete_tests = gs.setting('autocomplete_tests', False)
		autocomplete_closures = gs.setting('autocomplete_closures', False)
		res, err = mg9.complete(fn, src, offset)
		if err:
			ui.error(DOMAIN, err)

		ents = res['Candidates']
		if not ents and gs.setting('autocomplete_suggest_imports') is True:
			self.suggest(res['Suggestions'])

		name_fx = None
		name_fx_pat = gs.setting('autocomplete_filter_name')
		if name_fx_pat:
			try:
				name_fx = re.compile(name_fx_pat)
			except Exception as ex:
				ui.error(DOMAIN, 'Cannot filter completions: %s' % ex)

		in_iface = GoSublime.current_interface['name'] in GoSublime.INTERFACES and src[offset-1:offset] == '.'
		for ent in ents:
			if name_fx and name_fx.search(ent['name']):
				continue

			tn = ent['type']
			cn = ent['class']
			nm = ent['name']
			is_func = (cn == 'func')
			is_func_type = (cn == 'type' and tn.startswith('func('))
			
			if cn == 'type' and tn == 'interface' and nm not in GoSublime.INTERFACES:
				GoSublime.INTERFACES.append(nm)

			if in_iface:
				tmpl = '//Implementing %s·%s\nfunc (${1:this} ${2:*struct_name}) %s%s {\n\t$0\n}\n'
				if cn == 'func' and tn != 'built-in':
					sig = tmpl % (GoSublime.current_interface['name'], nm, nm, tn[4:])
					GoSublime.current_interface['funcs'].append(sig)

			if is_func:
				if nm in ('main', 'init'):
					continue

				if not autocomplete_tests and nm.startswith(('Test', 'Benchmark', 'Example')):
					continue

			if is_func or is_func_type:
				s_sfx = u'\u0282'
				t_sfx = gs.CLASS_PREFIXES.get('type', '')
				f_sfx = gs.CLASS_PREFIXES.get('func', '')
				params, ret = declex(tn)
				decl = []
				for i, p in enumerate(params):
					n, t = p
					if t.startswith('...'):
						n = '...'
					decl.append('${%d:%s}' % (i+1, n))
				decl = ', '.join(decl)
				ret = ret.strip('() ')

				if is_func:
					if func_name_only:
						comps.append((
							'%s\t%s %s' % (nm, ret, f_sfx),
							nm,
						))
					else:
						comps.append((
							'%s\t%s %s' % (nm, ret, f_sfx),
							'%s(%s)' % (nm, decl),
						))
				else:
					comps.append((
						'%s\t%s %s' % (nm, tn, t_sfx),
						nm,
					))

					if autocomplete_closures:
						comps.append((
							'%s {}\tfunc() {...} %s' % (nm, s_sfx),
							'%s {\n\t${0}\n}' % tn,
						))
			elif cn != 'PANIC':
				comps.append((
					'%s\t%s %s' % (nm, tn, self.typeclass_prefix(cn, tn)),
					nm,
				))
		if len(GoSublime.current_interface['funcs']) > 0 :
			comps.insert(0,('Implement Interface','\n'.join(GoSublime.current_interface['funcs']) + '\n'))
		else:
			self.not_in_scope = True
			# GoSublime.current_interface = []

		return comps
Exemplo n.º 19
0
	def complete(self, fn, offset, src, func_name_only):
		comps = []
		autocomplete_tests = gs.setting('autocomplete_tests', False)
		autocomplete_closures = gs.setting('autocomplete_closures', False)
		res, err = mg9.complete(fn, src, offset)
		if err:
			ui.error(DOMAIN, err)

		ents = res['Candidates']
		if not ents and gs.setting('autocomplete_suggest_imports') is True:
			self.suggest(res['Suggestions'])

		name_fx = None
		name_fx_pat = gs.setting('autocomplete_filter_name')
		if name_fx_pat:
			try:
				name_fx = re.compile(name_fx_pat)
			except Exception as ex:
				ui.error(DOMAIN, 'Cannot filter completions: %s' % ex)

		for ent in ents:
			if name_fx and name_fx.search(ent['name']):
				continue

			tn = ent['type']
			cn = ent['class']
			nm = ent['name']
			is_func = (cn == 'func')
			is_func_type = (cn == 'type' and tn.startswith('func('))

			if is_func:
				if nm in ('main', 'init'):
					continue

				if not autocomplete_tests and nm.startswith(('Test', 'Benchmark', 'Example')):
					continue

			if is_func or is_func_type:
				s_sfx = u'\u0282'
				t_sfx = gs.CLASS_PREFIXES.get('type', '')
				f_sfx = gs.CLASS_PREFIXES.get('func', '')
				params, ret = declex(tn)
				decl = []
				for i, p in enumerate(params):
					n, t = p
					if t.startswith('...'):
						n = '...'
					decl.append('${%d:%s}' % (i+1, n))
				decl = ', '.join(decl)
				ret = ret.strip('() ')

				if is_func:
					if func_name_only:
						comps.append((
							'%s\t%s %s' % (nm, ret, f_sfx),
							nm,
						))
					else:
						comps.append((
							'%s\t%s %s' % (nm, ret, f_sfx),
							'%s(%s)' % (nm, decl),
						))
				else:
					comps.append((
						'%s\t%s %s' % (nm, tn, t_sfx),
						nm,
					))

					if autocomplete_closures:
						comps.append((
							'%s {}\tfunc() {...} %s' % (nm, s_sfx),
							'%s {\n\t${0}\n}' % tn,
						))
			elif cn != 'PANIC':
				comps.append((
					'%s\t%s %s' % (nm, tn, self.typeclass_prefix(cn, tn)),
					nm,
				))
		return comps
Exemplo n.º 20
0
    def complete(self, fn, offset, src, func_name_only):
        comps = []
        autocomplete_tests = gs.setting('autocomplete_tests', False)
        autocomplete_closures = gs.setting('autocomplete_closures', False)
        res, err = mg9.complete(fn, src, offset)
        if err:
            ui.error(DOMAIN, err)

        ents = res['Candidates']
        if not ents and gs.setting('autocomplete_suggest_imports') is True:
            self.suggest(res['Suggestions'])

        name_fx = None
        name_fx_pat = gs.setting('autocomplete_filter_name')
        if name_fx_pat:
            try:
                name_fx = re.compile(name_fx_pat)
            except Exception as ex:
                ui.error(DOMAIN, 'Cannot filter completions: %s' % ex)

        for ent in ents:
            if name_fx and name_fx.search(ent['name']):
                continue

            tn = ent['type']
            cn = ent['class']
            nm = ent['name']
            is_func = (cn == 'func')
            is_func_type = (cn == 'type' and tn.startswith('func('))

            if is_func:
                if nm in ('main', 'init'):
                    continue

                if not autocomplete_tests and nm.startswith(
                    ('Test', 'Benchmark', 'Example')):
                    continue

            if is_func or is_func_type:
                s_sfx = u'\u0282'
                t_sfx = gs.CLASS_PREFIXES.get('type', '')
                f_sfx = gs.CLASS_PREFIXES.get('func', '')
                params, ret = declex(tn)
                decl = []
                for i, p in enumerate(params):
                    n, t = p
                    if t.startswith('...'):
                        n = '...'
                    decl.append('${%d:%s}' % (i + 1, n))
                decl = ', '.join(decl)
                ret = ret.strip('() ')

                if is_func:
                    if func_name_only:
                        comps.append((
                            '%s\t%s %s' % (nm, ret, f_sfx),
                            nm,
                        ))
                    else:
                        comps.append((
                            '%s\t%s %s' % (nm, ret, f_sfx),
                            '%s(%s)' % (nm, decl),
                        ))
                else:
                    comps.append((
                        '%s\t%s %s' % (nm, tn, t_sfx),
                        nm,
                    ))

                    if autocomplete_closures:
                        comps.append((
                            '%s {}\tfunc() {...} %s' % (nm, s_sfx),
                            '%s {\n\t${0}\n}' % tn,
                        ))
            elif cn != 'PANIC':
                comps.append((
                    '%s\t%s %s' % (nm, tn, self.typeclass_prefix(cn, tn)),
                    nm,
                ))
        return comps