Last active
November 5, 2018 20:18
-
-
Save topher6345/4f921ca3e7938132563e to your computer and use it in GitHub Desktop.
Sublime Text RuboCop autocorrect selected text
This file contains hidden or 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
[ | |
{ "keys": ["super+shift+c"], "command": "rubo_fix"}, | |
{ "keys": ["super+k", "super+d"], "command": "rubo_fix" } | |
] |
This file contains hidden or 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
class RubocopAutocorrect: | |
# Method to run Rubocop from the shell | |
def rubocop(self): | |
try: | |
return self.view.settings().get("rubo_fix").get("rubocop") | |
except AttributeError: | |
return "rubocop" | |
def eval_as_ruby(self, script): | |
# Open Subprocess | |
proc = subprocess.Popen([self.rubocop(), "--cache", "false", "--stdin", "-a", "-f", "s", "foo"], | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE, | |
stdin=subprocess.PIPE) | |
output, error = proc.communicate(script.encode('utf-8')) | |
output = output.strip() | |
regex = r"={4,}" # which is the same as \W btw | |
pat = re.compile( regex ) | |
# output = pat.sub('', str(output, 'utf-8') ) | |
corrected_string = [] | |
b_point = False | |
lines = output.splitlines() | |
for line in lines: | |
if re.match(pat, line.decode()): | |
b_point = True | |
if b_point: | |
corrected_string.append(line.decode()) | |
corrected_string.reverse() | |
corrected_string.pop() | |
print(corrected_string.reverse()) | |
output = '\n'.join(corrected_string) | |
if proc.poll(): | |
output += "\n" + error.decode() | |
try: | |
return output | |
except NameError: | |
return unicode(output ,encoding='utf-8') | |
class RuboFixCommand(sublime_plugin.TextCommand, RubocopAutocorrect): | |
def run(self, edit, output_to_editor=True): | |
for region in self.view.sel(): | |
if region.size() == 0: | |
# eval line | |
region_of_line = self.view.line(region) | |
script = self.view.substr(region_of_line) | |
output = self.eval_as_ruby(script) | |
if output_to_editor: | |
self.insert_output(output, region, edit, region_of_line.b, "\n") | |
else: | |
pass # TODO | |
else: | |
# eval selected | |
script = self.view.substr(region) | |
output = self.eval_as_ruby(script) | |
start = max(region.a, region.b) | |
space = "" if script[-1] == "\n" else " " | |
if output_to_editor: | |
self.insert_output(output, region, edit, start, space) | |
else: | |
pass # TODO | |
def insert_output(self, output, region, edit, start, space): | |
i = self.view.insert(edit, start, space + output) | |
self.view.sel().subtract(region) | |
self.view.sel().add(sublime.Region(start + len(space), start + i)) |
This file contains hidden or 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
{ | |
"rubo_fix": | |
{ | |
"rubocop": "~/.rvm/gems/ruby-2.3.0/wrappers/rubocop" | |
}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This does not work