This repository was archived by the owner on Aug 4, 2022. It is now read-only.
forked from SublimeHaskell/SublimeHaskell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindenters.py
73 lines (60 loc) · 3.32 KB
/
indenters.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
# -*- coding: UTF-8 -*-
import traceback
import sublime
import SublimeHaskell.cmdwin_types as CommandWin
import SublimeHaskell.internals.proc_helper as ProcHelper
import SublimeHaskell.internals.settings as Settings
import SublimeHaskell.sublime_haskell_common as Common
FILTER_OUTPUT_PANEL_NAME = 'haskell_run_output'
def do_prettify(view, edit, indenter, indenter_options):
try:
Common.hide_panel(view.window(), panel_name=FILTER_OUTPUT_PANEL_NAME)
regions = []
for region in view.sel():
regions.append(sublime.Region(region.a, region.b))
selection = region if not region.empty() else sublime.Region(0, view.size())
# Newline conversion seems dubious here, but... leave it alone for the time being.
sel_str = view.substr(selection).replace('\r\n', '\n')
with ProcHelper.ProcHelper(indenter + indenter_options) as proc:
if proc.process is not None:
_, out, err = proc.wait(sel_str)
# stylish-haskell does not have a non-zero exit code if it errors out! (Surprise!)
# Not sure about hindent, but this seems like a safe enough test.
#
# Also test if the contents actually changed so break the save-indent-save-indent-... loop if
# the user enabled prettify_on_save.
if not err:
if out not in [selection, sel_str]:
view.replace(edit, selection, out)
else:
indent_err = ' '.join(indenter)
stderr_out = '\n'.join(["{0} failed, stderr contents:".format(indent_err), "-" * 40, ""]) + err
report_error(view, stderr_out)
else:
report_error(view, proc.process_err)
view.sel().clear()
# Questionable whether regions should be re-activated: stylish-haskell usually adds whitespace, which makes
# the selection nonsensical.
#
# However, there are other plugins that get fired after SublimeHaskell that don't like it when you kill all of the
# selection regions from underneath their feet.
for region in regions:
view.sel().add(region)
except OSError:
report_error(view, 'Exception executing {0}'.format(' '.join(indenter)))
traceback.print_exc()
def report_error(view, errmsg):
window = view.window()
output_view = Common.output_panel(window, FILTER_OUTPUT_PANEL_NAME)
output_view.run_command('sublime_haskell_output_text', {'text': errmsg, 'clear': 'yes'})
Common.show_panel(window, FILTER_OUTPUT_PANEL_NAME)
output_view.sel().clear()
output_view.sel().add(sublime.Region(0, 0))
class SublimeHaskellStylish(CommandWin.SublimeHaskellTextCommand):
def run(self, edit, **_kwargs):
stylish_options = Settings.get_project_setting(self.view, 'stylish_options', Settings.PLUGIN.stylish_options)
do_prettify(self.view, edit, ['stylish-haskell'], stylish_options)
class SublimeHaskellHindent(CommandWin.SublimeHaskellTextCommand):
def run(self, edit, **_kwargs):
hindent_options = Settings.get_project_setting(self.view, 'hindent_options', Settings.PLUGIN.hindent_options)
do_prettify(self.view, edit, ['hindent'], hindent_options)