Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Tenzer committed Aug 16, 2014
0 parents commit 94253e5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Default.sublime-commands
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"caption": "Python Output Format",
"command": "python_output_format"
}
]
44 changes: 44 additions & 0 deletions PythonOutputFormat.py
Original file line number Diff line number Diff line change
@@ -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.