Skip to content

Instantly share code, notes, and snippets.

@johanhalse
Created August 31, 2023 07:59
Show Gist options
  • Save johanhalse/47f670463b31cb9861d2782e57b0dc2c to your computer and use it in GitHub Desktop.
Save johanhalse/47f670463b31cb9861d2782e57b0dc2c to your computer and use it in GitHub Desktop.
Rubocop autofix command for Sublime Text
import sublime
import sublime_plugin
import subprocess
import shlex
class RubocopAutofixCommand(sublime_plugin.TextCommand):
def run(self, edit):
full_text = self.view.substr(sublime.Region(0, self.view.size()))
try:
command = "rubocop -c .rubocop.yml -A -s -o --stderr -ffi"
process = subprocess.Popen(shlex.split(command), cwd=self.view.window().folders()[0], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
stdout, stderr = process.communicate(input=full_text)
region = sublime.Region(0, self.view.size())
self.view.replace(edit, region, stdout.rsplit("====================\n", 1)[-1])
except Exception as e:
sublime.error_message(f"Error running RuboCop: {e}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment