Created
August 31, 2023 07:59
-
-
Save johanhalse/47f670463b31cb9861d2782e57b0dc2c to your computer and use it in GitHub Desktop.
Rubocop autofix command for Sublime Text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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