-
Notifications
You must be signed in to change notification settings - Fork 18
/
crystal_format.py
96 lines (80 loc) · 3.25 KB
/
crystal_format.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import json
import subprocess
import sys
import re
import os
import sublime_plugin
import sublime
from .diff_match_patch import diff_match_patch
class CrystalPluginListener(sublime_plugin.EventListener):
def on_pre_save(self, view):
settings = sublime.load_settings('Crystal.sublime-settings')
if settings.get('auto_format'):
view.run_command('crystal_format')
class CrystalFormatCommand(sublime_plugin.TextCommand):
def is_enabled(self):
caret = self.view.sel()[0].a
syntax_name = self.view.scope_name(caret)
return "source.crystal" in syntax_name
def has_redo(self):
cmd, args, repeat = self.view.command_history(1)
return cmd != ''
def run(self, edit):
vsize = self.view.size()
region = sublime.Region(0, vsize)
src = self.view.substr(region)
window = self.view.window()
settings = sublime.load_settings('Crystal.sublime-settings')
#command = [settings.get("crystal_cmd"), "tool", "format", "-", "--format", "json"]
command = [settings.get("crystal_cmd"), "tool", "format", "-", "--no-color"]
# for Windows Subsystem for Linux
if os.name == "nt": command.insert(0, "wsl")
popen_args = dict(args=command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# Prevent flashing terminal windows
if sys.platform.startswith('win'):
popen_args['startupinfo'] = subprocess.STARTUPINFO()
popen_args['startupinfo'].dwFlags |= subprocess.STARTF_USESHOWWINDOW
proc = subprocess.Popen(**popen_args)
stdout, stderr = proc.communicate(src.encode('utf-8'))
stdout = stdout.decode('utf-8')
stderr = stderr.decode('utf-8')
exit = proc.returncode
pos = 0
if exit == 0:
if not self.has_redo():
for op, text in diff_match_patch().diff_main(src, stdout):
if op == diff_match_patch.DIFF_DELETE:
self.view.erase(edit, sublime.Region(pos, pos + len(text)))
if op == diff_match_patch.DIFF_INSERT:
self.view.insert(edit, pos, text)
pos += len(text)
if op == diff_match_patch.DIFF_EQUAL:
pos += len(text)
self.view.erase_regions('crystal_errors')
window.run_command("hide_panel")
else:
error_pos = None
pattern = r"Error: Syntax error in .+?:(\d+): (.+)"
match = re.match(pattern, stderr)
if match:
error_pos = int(match.group(1))
error = match.group(2)
else:
error_pos = None
error = stderr
# error = json.loads(stderr)
# error_pos = self.view.text_point(error[0]["line"] - 1, error[0]["column"] - 1)
if error_pos:
line_region = self.view.full_line(error_pos)
self.view.add_regions('crystal_errors', [line_region], 'comment', 'dot', sublime.HIDDEN)
error_panel = window.create_output_panel('crystal_errors')
# error_panel.run_command("append", {"characters":
# "Error at line %d, column %d: %s" % (error[0]["line"], error[0]["column"], error[0]['message'])
# })
if error_pos:
error_panel.run_command("append", {"characters":
"Error at line %d: %s" % (error_pos, error)
})
else:
error_panel.run_command("append", {"characters": error})
window.run_command("show_panel", {"panel": "output.crystal_errors"})