evancz / elm-format-on-save

Sublime Text plugin to run elm-format on save
https://packagecontrol.io/packages/Elm%20Format%20on%20Save
BSD 3-Clause "New" or "Revised" License
23 stars 3 forks source link

Viewport position changes on format #4

Open dpinn opened 3 years ago

dpinn commented 3 years ago

Whenever I save the file, and thereby actuate elm-format-on-save, the window scrolls all the way to the right, hiding most of the code.

Could be related to https://github.com/jonlabelle/SublimeJsPrettier/pull/171.

The format class from elm_format.py would look something like this:

class ElmFormatCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        elm_format = find_elm_format(self.view)

        if elm_format == None:
            return

        region = sublime.Region(0, self.view.size())
        content = self.view.substr(region)
        previous_position = self.view.viewport_position()

        stdout, stderr = subprocess.Popen(
            [elm_format, '--stdin', '--yes', '--elm-version=0.19'],
            stdin=subprocess.PIPE,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            shell=os.name=="nt").communicate(input=bytes(content, 'UTF-8'))

        if stderr.strip():
            open_panel(self.view, re.sub('\x1b\[\d{1,2}m', '', stderr.strip().decode()))
        else:
            self.view.replace(edit, region, stdout.decode('UTF-8'))
            self.view.set_viewport_position((0, 0), False)
            self.view.set_viewport_position(previous_position, False)
            self.view.window().run_command("hide_panel", {"panel": "output.elm_format"})