Skip to content

Instantly share code, notes, and snippets.

@math2001
Last active January 2, 2017 23:02
Show Gist options
  • Save math2001/f0b52d8860f6b347301b74e15c0dfcca to your computer and use it in GitHub Desktop.
Save math2001/f0b52d8860f6b347301b74e15c0dfcca to your computer and use it in GitHub Desktop.
Remove the useless (and trouble makers) commas on sublime text
##
## THIS FILE IS NO LONGER UPDATED: SEE https://github.com/math2001/JSONComma
##
import sublime
import sublime_plugin
class JsonFixerCommand(sublime_plugin.TextCommand):
"""
fix json trailing comma.
"""
def run(self, edit):
v = self.view
if 'json' not in v.settings().get('syntax').lower():
return
regions = v.find_all(r',\s*[\]\}]')
v.selection = v.sel()
initial_colrows = []
for region in v.selection:
initial_colrows.append(v.rowcol(region.begin()))
v.selection.clear()
for region in regions:
if 'punctuation' not in v.scope_name(region.begin()):
continue
v.selection.add(region)
v.run_command('move', {"by": "characters", "forward": False})
v.run_command('right_delete')
v.selection.clear()
for col, row in initial_colrows:
line_length = len(v.substr(v.line(sublime.Region(v.text_point(col, 0)))))
if row > line_length:
row = line_length
v.selection.add(sublime.Region(v.text_point(col, row)))
sublime.status_message('You\'re welcome mate!')
class JsonFixerListener(sublime_plugin.EventListener):
def on_pre_save(self, view):
view.run_command('json_fixer')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment