def on_filename(filename): if filename: text = self.view.substr(sublime.Region(0, self.view.size())) changes = {filename: {'content': text}} new_gist = update_gist(gist['url'], changes) gistify_view(self.view, new_gist, filename) sublime.status_message("File added to Gist")
def test_gistify_view(self, mocked_gist_title): mocked_gist_title.return_value = ['some gist title'] view = sublime.View() gist_filename = 'some filename' gist = { 'html_url': 'some html url', 'description': 'some description', 'url': 'some url' } gist_helpers.gistify_view(view, gist, gist_filename) self.assertEqual(view.file_name(), 'some filename') self.assertEqual(view.settings().get('gist_html_url'), 'some html url') self.assertEqual(view.settings().get('gist_description'), 'some description') self.assertEqual(view.settings().get('gist_url'), 'some url') self.assertEqual(view.settings().get('gist_filename'), 'some filename') self.assertEqual(view._status.get('Gist'), 'Gist: some gist title') view = sublime.View() gist_filename = 'some another filename' view.set_name('some view filename') gist_helpers.gistify_view(view, gist, gist_filename) self.assertEqual(view._status.get('Gist'), 'Gist: some gist title (some another filename)')
def open_gist(gist_url): gist = api_request(gist_url) files = sorted(gist['files'].keys()) for gist_filename in files: allowed_types = ['text', 'application'] media_type = gist['files'][gist_filename]['type'].split('/')[0] if media_type not in allowed_types: continue view = sublime.active_window().new_file() gistify_view(view, gist, gist_filename) view.run_command('append', { 'characters': gist['files'][gist_filename]['content'], }) if settings.get('supress_save_dialog'): view.set_scratch(True) if settings.get('update_on_save'): view.retarget(os.path.join(tempfile.gettempdir(), gist_filename)) # Save over it (to stop us reloading from that file in case it exists) # But don't actually do a gist update view.settings().set('do-update', False) view.run_command('save') set_syntax(view, gist['files'][gist_filename])
def on_filename(filename): if filename and filename != old_filename: text = self.view.substr(sublime.Region(0, self.view.size())) file_changes = {old_filename: {'filename': filename, 'content': text}} new_gist = update_gist(self.gist_url(), file_changes) gistify_view(self.view, new_gist, filename) sublime.status_message('Gist file renamed')
def on_gist_filename(filename): # We need to figure out the filenames. Right now, the following logic is used: # If there's only 1 selection, just pass whatever the user typed to Github. # It'll rename empty files for us. # If there are multiple selections and user entered a filename, rename the files from foo.js to # foo (1).js, foo (2).js, etc. # If there are multiple selections and user didn't enter anything, post the files as # $SyntaxName 1, $SyntaxName 2, etc. if len(region_data) == 1: gist_data = {filename: region_data[0]} else: if filename: (namepart, extpart) = os.path.splitext(filename) make_filename = lambda num: "%s (%d)%s" % (namepart, num, extpart) else: syntax_name, _ = os.path.splitext(os.path.basename(self.view.settings().get('syntax'))) make_filename = lambda num: "%s %d" % (syntax_name, num) gist_data = dict((make_filename(idx), data) for idx, data in enumerate(region_data, 1)) gist = create_gist(self.public, description, gist_data) if not gist: return gist_html_url = gist['html_url'] sublime.set_clipboard(gist_html_url) sublime.status_message("%s Gist: %s" % (self.mode(), gist_html_url)) if gistify: gistify_view(self.view, gist, list(gist['files'].keys())[0])
def on_gist_description(description): if description and description != self.gist_description(): gist_url = self.gist_url() new_gist = update_gist(gist_url, {}, new_description=description) for window in sublime.windows(): for view in window.views(): if view.settings().get('gist_url') == gist_url: gistify_view(view, new_gist, view.settings().get('gist_filename')) sublime.status_message('Gist description changed')
def on_filename(filename): if filename and filename != old_filename: text = self.view.substr(sublime.Region(0, self.view.size())) file_changes = { old_filename: { 'filename': filename, 'content': text } } new_gist = update_gist(self.gist_url(), file_changes) gistify_view(self.view, new_gist, filename) sublime.status_message('Gist file renamed')
def on_gist_filename(filename): # We need to figure out the filenames. Right now, the following logic is used: # If there's only 1 selection, just pass whatever the user typed to Github. # It'll rename empty files for us. # If there are multiple selections and user entered a filename, rename the files from foo.js to # foo (1).js, foo (2).js, etc. # If there are multiple selections and user didn't enter anything, post the files as # $SyntaxName 1, $SyntaxName 2, etc. if len(region_data) == 1: gist_data = {filename: region_data[0]} else: if filename: (namepart, extpart) = os.path.splitext(filename) make_filename = lambda num: "%s (%d)%s" % ( namepart, num, extpart) else: syntax_name, _ = os.path.splitext( os.path.basename( self.view.settings().get('syntax'))) make_filename = lambda num: "%s %d" % (syntax_name, num ) gist_data = dict( (make_filename(idx), data) for idx, data in enumerate(region_data, 1)) gist = create_gist(self.public, description, gist_data) if not gist: return gist_html_url = gist['html_url'] sublime.set_clipboard(gist_html_url) sublime.status_message("%s Gist: %s" % (self.mode(), gist_html_url)) if gistify: gistify_view(self.view, gist, list(gist['files'].keys())[0])