Last active
January 2, 2017 23:02
-
-
Save math2001/f0b52d8860f6b347301b74e15c0dfcca to your computer and use it in GitHub Desktop.
Remove the useless (and trouble makers) commas on sublime text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
## 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