Skip to content

Commit

Permalink
Temporary replacement for removed crystal tool format --format json (
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite authored and vladfaust committed Feb 7, 2019
1 parent 737b1dd commit 67b2c97
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions crystal_format.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import subprocess
import sys
import re

import sublime_plugin
import sublime
Expand Down Expand Up @@ -29,7 +30,8 @@ def run(self, edit):
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", "-", "--format", "json"]
command = [settings.get("crystal_cmd"), "tool", "format", "-", "--no-color"]

popen_args = dict(args=command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# Prevent flashing terminal windows
Expand Down Expand Up @@ -59,13 +61,32 @@ def run(self, edit):
window.run_command("hide_panel")

else:
error = json.loads(stderr)
error_pos = self.view.text_point(error[0]["line"] - 1, error[0]["column"] - 1)
line_region = self.view.full_line(error_pos)
self.view.add_regions('crystal_errors', [line_region], 'comment', 'dot', sublime.HIDDEN)
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'])
})
# 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"})

0 comments on commit 67b2c97

Please sign in to comment.