Skip to content
Permalink
Browse files

Initial commit

  • Loading branch information...
Tenzer committed Aug 16, 2014
0 parents commit 94253e5d885bb1bdc94f66a2313d06c73b43c9d8
Showing with 50 additions and 0 deletions.
  1. +6 −0 Default.sublime-commands
  2. +44 −0 PythonOutputFormat.py
@@ -0,0 +1,6 @@
[
{
"caption": "Python Output Format",
"command": "python_output_format"
}
]
@@ -0,0 +1,44 @@
import sublime, sublime_plugin
import tokenize
from io import BytesIO


class PythonOutputFormatCommand(sublime_plugin.TextCommand):
indentation_level = 0

def indent(self):
return (tokenize.INDENT, ' ' * 4 * self.indentation_level)

def run(self, edit):
NEWLINE = (tokenize.NL, '\n')
result = []
entire_document = sublime.Region(0, self.view.size())
data_input = tokenize.tokenize(BytesIO(self.view.substr(entire_document).encode('utf-8')).readline)

for token in data_input:
if token.type == tokenize.OP:
if token.exact_type in [tokenize.LBRACE, tokenize.LSQB]:
result.append(self.indent())
result.append((token.exact_type, token.string))
result.append(NEWLINE)
self.indentation_level += 1
elif token.exact_type in [tokenize.RBRACE, tokenize.RSQB]:
if result[-1] != NEWLINE:
result.append(NEWLINE)
self.indentation_level -= 1
result.append(self.indent())
result.append((token.exact_type, token.string))
elif token.exact_type == tokenize.COLON:
result.append((token.exact_type, token.string))
result.append((tokenize.STRING, ' '))
elif token.exact_type == tokenize.COMMA:
result.append((token.exact_type, token.string))
result.append(NEWLINE)
else:
result.append((token.exact_type, token.string))
elif token.type != tokenize.NL:
result.append(self.indent())
result.append((token.exact_type, token.string))

result.append(NEWLINE)
self.view.replace(edit, entire_document, tokenize.untokenize(result).decode('utf-8'))

0 comments on commit 94253e5

Please sign in to comment.
You can’t perform that action at this time.