-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a40c605
commit e2a4176
Showing
4 changed files
with
79 additions
and
138 deletions.
There are no files selected for viewing
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 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,128 +1,72 @@ | ||
import sublime | ||
import sublime_plugin | ||
import re | ||
from os import system | ||
import textwrap | ||
from threading import Thread | ||
from subprocess import check_output as syscalloutput, CalledProcessError | ||
# from subprocess import check_call as syscall | ||
|
||
|
||
class FormatJsonCommand(sublime_plugin.TextCommand): | ||
def run_async(select, view): | ||
|
||
def run(self, edit, select=False): | ||
# self.view.run_command("expand_selection", {"to": "brackets"}) | ||
# self.view.run_command("expand_selection", {"to": "brackets"}) | ||
contents = self.view.substr(sublime.Region(0, self.view.size())) | ||
# clear file contents | ||
system(r"break > C:\Users\Ibraheem\Desktop\SublimeText\ffformat.py") | ||
if select: | ||
regions = self.view.sel() | ||
for region in regions: | ||
region_contents = self.view.substr(region).lstrip() | ||
self.view.run_command( | ||
"format_json_step1", { | ||
"select": select, | ||
"contents": region_contents, | ||
} | ||
) | ||
else: | ||
self.view.run_command( | ||
"format_json_step1", { | ||
"select": select, | ||
"contents": contents, | ||
} | ||
) | ||
sublime.set_timeout_async(lambda: self.delayedrun(select), 1500) | ||
# sublime.set_timeout_async( | ||
# lambda: self.view. | ||
# run_command("reindent", {"single_line": False}, 1500) | ||
# ) | ||
|
||
def delayedrun(self, select): | ||
self.view.run_command("format_json_step2", {"select": select}) | ||
def fixstr(contents): | ||
contents = contents.replace("//", "#").replace("res:#", "res://") | ||
contents = contents.replace("true", "True").replace("false", "False") | ||
return re.sub(r",(\s*[\)\]\}])", r"\1", contents) | ||
|
||
name = "C:\\Users\\Ibraheem\\Desktop\\SublimeText\\json-format-temp.py" | ||
style = "C:\\Users\\Ibraheem\\Desktop\\SublimeText\\json-style.txt" | ||
open(name, 'w').close() # clear file contents | ||
indents = [] | ||
with open(name, 'a') as file: | ||
if not select: | ||
self.view.window().run_command("save") | ||
|
||
|
||
class FormatJsonStep1Command(sublime_plugin.TextCommand): | ||
|
||
def run(self, edit, select, contents): | ||
contents = view.substr(sublime.Region(0, view.size())) | ||
file.write(fixstr(contents)) | ||
else: | ||
regions = view.sel() | ||
for region in regions: | ||
contents = view.substr(region) | ||
match = re.match(r"[\s\n]+", contents) | ||
indents.append(match.group() if match else None) | ||
file.write(fixstr(contents.lstrip())) | ||
file.write("\n# end region\n") | ||
|
||
contents = contents.replace("//", "#") | ||
contents = contents.replace("res:#", "res://") | ||
indexes = re.finditer(",\\s*[\\)\\]\\}]", contents) | ||
indexes = [i.start() for i in indexes] | ||
contents = "".join( | ||
[char for i, char in enumerate(contents) if i not in indexes] | ||
try: | ||
contents = syscalloutput("yapf " + name + " --style " + style, shell=True) | ||
except CalledProcessError: | ||
sublime.status_message( | ||
"JsonFormatter ran into an error while trying to format :'(" | ||
) | ||
contents = contents.replace("true", "True").replace("false", "False") | ||
name = "C:\\Users\\Ibraheem\\Desktop\\SublimeText\\ffformat.py" | ||
style = "C:\\Users\\Ibraheem\\Desktop\\SublimeText\\ssstyle.txt" | ||
command = "yapf " + name + " --in-place --style " + style | ||
self.view.window().run_command( | ||
"exec", { | ||
"shell_cmd": command, | ||
"show_panel": False, | ||
} | ||
return | ||
contents = contents.decode().replace("\r", "").replace("#", "//") | ||
contents = contents.replace("True", "true").replace("False", "false") | ||
sublime.set_timeout_async( | ||
lambda: view.run_command( | ||
"format_json_step2", | ||
{"select": select, "contents": contents, "indents": indents} | ||
) | ||
# with open(path + 'temporary.notpy', 'w') as file: | ||
with open(name, 'a') as file: | ||
file.write(contents) | ||
if select: | ||
file.write("\n# end region\n") | ||
) | ||
|
||
def is_enabled(self): | ||
return self.view.match_selector(0, "source.json") | ||
|
||
class FormatJsonCommand(sublime_plugin.WindowCommand): | ||
|
||
class FormatJsonStep2Command(sublime_plugin.TextCommand): | ||
def run(self, select=False): | ||
Thread(target=run_async, args=(select, self.window.active_view())).start() | ||
|
||
def run(self, edit, select): | ||
with open( | ||
"C:\\Users\\Ibraheem\\Desktop\\SublimeText\\ffformat.py", 'r' | ||
) as file: | ||
contents = file.read() | ||
contents = contents.replace("#", "//") | ||
contents = contents.replace("True", "true").replace("False", "false") | ||
|
||
# with open( | ||
# "C:\\Users\\Ibraheem\\Desktop\\SublimeText\\ffformatoutputcontent.sublime-keymap", | ||
# 'w' | ||
# ) as file: | ||
# file.write(contents) | ||
# if select: | ||
# file.write("\n# end region\n") | ||
class FormatJsonStep2Command(sublime_plugin.TextCommand): | ||
|
||
startindexes = sorted( | ||
[i.end() - 1 for i in re.finditer("keys\": \\[.*\\],\n", contents)], | ||
reverse=True | ||
) | ||
endindexes = sorted( | ||
[ | ||
i.end() - 1 | ||
for i in re.finditer("keys\": \\[.*\\],\n\\s*", contents) | ||
], | ||
reverse=True | ||
) | ||
contentslist = list(contents) | ||
for s, e in zip(startindexes, endindexes): | ||
contentslist[e] = " " | ||
del contentslist[s:e] | ||
contents = "".join(contentslist) | ||
def run(self, edit, select, contents, indents): | ||
print(indents) | ||
contents = re.sub(r'("keys": \[.*\],)\s*("command")', r'\1 \2', contents) | ||
contents = re.sub(r'([^\s])}', r'\1 }', contents) | ||
if select: | ||
contents = contents.split("\n// end region\n") | ||
contents = list(filter(None, contents.split("\n// end region\n"))) | ||
print(contents) | ||
regions = self.view.sel() | ||
for region, region_contents in zip(regions, contents): | ||
for region, region_contents, indent in zip(reversed(regions), reversed(contents),reversed(indents)): | ||
if indent: | ||
region_contents = textwrap.indent(region_contents, indent) | ||
self.view.replace(edit, region, region_contents) | ||
else: | ||
self.view.replace(edit, sublime.Region(0, self.view.size()), contents) | ||
|
||
# with open( | ||
# "C:\\Users\\Ibraheem\\Desktop\\SublimeText\\ffformatoutput.sublime-keymap", | ||
# 'w' | ||
# ) as file: | ||
# file.write(contents) | ||
# if select: | ||
# file.write("\n# end region\n") | ||
|
||
def is_enabled(self): | ||
return self.view.match_selector(0, "source.json") |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,29 @@ | ||
[style] | ||
# Default Settings: | ||
based_on_style = pep8 | ||
align_closing_bracket_with_visual_indent = False | ||
continuation_align_style = VALIGN-RIGHT | ||
dedent_closing_brackets = True | ||
indent_closing_brackets = False | ||
allow_multiline_dictionary_keys = True | ||
each_dict_entry_on_separate_line = False | ||
indent_dictionary_value = False | ||
allow_split_before_dict_value = False | ||
space_between_ending_comma_and_closing_bracket = False | ||
# User Settings: | ||
column_limit = 80 | ||
continuation_indent_width = 3 | ||
indent_width = 3 | ||
use_tabs = True | ||
spaces_around_dict_delimiters=True | ||
spaces_around_list_delimiters=True | ||
split_all_top_level_comma_separated_values = True | ||
# split_all_comma_separated_values = True | ||
# coalesce_brackets = True | ||
# this link can explain the options that are used in formatting | ||
# https://github.com/google/yapf/blob/master/yapf/yapflib/style.py#L412 | ||
[style] | ||
# Default Settings: | ||
based_on_style = pep8 | ||
align_closing_bracket_with_visual_indent = False | ||
continuation_align_style = VALIGN-RIGHT | ||
dedent_closing_brackets = True | ||
indent_closing_brackets = False | ||
allow_multiline_dictionary_keys = True | ||
each_dict_entry_on_separate_line = False | ||
indent_dictionary_value = False | ||
allow_split_before_dict_value = False | ||
space_between_ending_comma_and_closing_bracket = False | ||
|
||
|
||
# User Settings: | ||
column_limit = 81 | ||
continuation_indent_width = 3 | ||
indent_width = 3 | ||
use_tabs = True | ||
spaces_around_list_delimiters=False | ||
spaces_around_dict_delimiters=True | ||
split_all_top_level_comma_separated_values = True | ||
# split_all_comma_separated_values = True | ||
# coalesce_brackets = True | ||
|
||
|
||
|
||
# this link can explain the options that are used in formatting | ||
# https://github.com/google/yapf/blob/master/yapf/yapflib/style.py#L412 |