SublimeLinter / SublimeLinter-rubocop

SublimeLinter 3 plugin for Ruby, using rubocop.
MIT License
159 stars 40 forks source link

Autocorrect? #19

Closed ProGM closed 8 years ago

ProGM commented 9 years ago

This plugin works very well, but there is no way to call the rubocop autocorrect function, isn't it?

If so, do you plan to add it?

reconbot commented 9 years ago

It doesn't currently exist but it would be a nice feature. I'm not sure if the linter framework supports having commands. On Apr 3, 2015 5:31 AM, "Piero Dotti" notifications@github.com wrote:

This plugin works very well, but there is no way to call the rubocop autocorrect function, isn't it?

If so, do you plan to add it?

— Reply to this email directly or view it on GitHub https://github.com/SublimeLinter/SublimeLinter-rubocop/issues/19.

ProGM commented 9 years ago

Uhm... Probably not. What about adding it externally from SublimeLinter?

joshmfrankel commented 8 years ago

Was hoping this was as simple as

"rubocop": {
  "@disable": false,
  "args": [
    "--auto-correct"
  ],
  "excludes": []
},

in the SublimeLinter - User Settings, but unfortunately adding this only displays the text [corrected] on lines that rubocop would be able to correct. It doesn't actually correct them. This might require the creation of another plugin.

joshmfrankel commented 8 years ago

@ProGM The Sublime_Rubocop plugin allows for autocorrect functionality from the sublime command menu. https://github.com/pderichs/sublime_rubocop. Have just tried it and it works pretty well. Add this to the RuboCop.sublime_settings to allow Sublime linter to still manage file linting.

{
  "mark_issues_in_view": false,
}
ProGM commented 8 years ago

@joshmfrankel Thank you, I'm trying it :)

reconbot commented 8 years ago

That's great!

peresleguine commented 5 years ago

Another way would be just call rubocop -a on an editable file. To run a shell command from ST3 I’ve used Shell Exec package and added the following command to Default.sublime-commands file under Packages folder:

[
  {
    "caption": "RuboCop: autocorrect",
    "command": "shell_exec_run",
    "args": {
      "command": "rubocop -a ${file}"
    }
  }
]

The above command will then appear in the Command Palette.

joshmfrankel commented 5 years ago

For Guard users I've found a lot of success with using the guard-rubocop plugin to automatically run fixing on save. Simple as:

guard :rubocop, all_on_start: false, cli: "--fix" do
  watch(%r{.+\.rb$})
end
fnando commented 4 years ago

Plugin for running rubocop on save:

import sublime
import sublime_plugin
import os
import subprocess

class RubyFormatOnSave(sublime_plugin.EventListener):
  def on_post_save_async(self, view):
    filename = view.file_name()

    if view.match_selector(0, "source.ruby"):
      subprocess.call(
        ["rubocop", "-a", filename],
        cwd=os.path.dirname(filename)
      )

Just copy it over your Packages/User folder.

drewB commented 4 years ago

@fnando I created a new file with the code from above and put in my Packages/User folder but saving doesn't seem to cause the autocorrect. Is there something else I need to do to enable it?

fnando commented 4 years ago

@drewB I extracted this script into a plugin a while ago: https://github.com/fnando/sublime-rubocop-formatter

drewB commented 4 years ago

Thanks will check it out.

amazing-jay commented 3 years ago

@fnando the link you posted above is broken, but your repos look cool. https://github.com/fnando & https://sublime.fnando.com/

I tried a couple, but installation isn't quite working. Can you help? Am interested in: