-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathformat_code.py
More file actions
71 lines (55 loc) · 2.3 KB
/
format_code.py
File metadata and controls
71 lines (55 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import sublime
import sublime_plugin
import os.path
import subprocess
class FormatCodeCommand(sublime_plugin.TextCommand):
def __init__(self, view):
self.settings = sublime.load_settings("Rubyfmt.sublime-settings")
self.view = view
def run(self, edit):
syntax = self.view.settings().get('syntax').lower()
if 'ruby' in syntax:
self.format_view(edit)
def format_view(self, edit):
region = sublime.Region(0, self.view.size())
unformatted_bytes = self.view.substr(region)
formatted_bytes = self.format_bytes(unformatted_bytes)
if formatted_bytes:
if unformatted_bytes != formatted_bytes:
self.view.replace(edit, region, formatted_bytes)
else:
syntax_error = self.check_syntax(unformatted_bytes)
if(syntax_error):
window = self.view.window()
window.set_status_bar_visible(True)
window.status_message(syntax_error)
def check_syntax(self, bytes):
ruby_executable = self.settings.get("ruby_executable", "ruby")
command = [ruby_executable, "-c"]
formatter = subprocess.Popen(command,
stdin=subprocess.PIPE,
stderr=subprocess.PIPE)
_, stderr = formatter.communicate(bytes.encode())
if formatter.returncode != 0:
return stderr.decode()
def format_bytes(self, bytes):
ruby_executable = self.settings.get("ruby_executable", "ruby")
rubyfmt_executable = self.settings.get("rubyfmt_executable", "rubyfmt")
command = [rubyfmt_executable]
formatter = subprocess.Popen(command,
stdout=subprocess.PIPE,
stdin=subprocess.PIPE,
stderr=subprocess.PIPE)
formatted, stderr = formatter.communicate(bytes.encode())
if formatter.returncode == 0:
return formatted.decode()
else:
syntax_error = self.check_syntax(bytes)
if not syntax_error:
print("Rubyfmt failed:")
print(stderr.decode())
class FormatOnSave(sublime_plugin.EventListener):
def on_pre_save(self, view):
settings = sublime.load_settings("Rubyfmt.sublime-settings")
if settings.get("format_on_save", False):
view.run_command("format_code")