Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

Support for formatting/beautifying Ruby files #56

Closed
HusseinElMotayam opened this issue Sep 12, 2016 · 40 comments
Closed

Support for formatting/beautifying Ruby files #56

HusseinElMotayam opened this issue Sep 12, 2016 · 40 comments
Labels
feature-request Adds currently unsupported functionality

Comments

@HusseinElMotayam
Copy link

HusseinElMotayam commented Sep 12, 2016

Your environment

  • vscode-ruby version: 0.5.6
  • Ruby version: 2.3.0
  • VS Code version: 1.5.1
  • Operating System: Ubuntu 16.04

Expected behavior

It'd be great to have some formatting/beautifying functionality in the vscode-ruby plugin.

Actual behavior

As for now, there is no way to format/beautify .rb and .erb files inside vscode in a proper way.

Workaround

I worked around this by:

  • Installing htmlbeautifier and rbeautify gems.
  • Editing my Tasks file as follows:
{
    "version": "0.1.0",
    "command": "sh",
    "args": ["-c"],
    "isShellCommand": false,    
    "showOutput": "never",
    "suppressTaskName": true,
    "tasks": [        
        {
            "taskName": "Ruby format",
            "args": ["rbeautify ${file}"]
        },
        {
            "taskName": "ERB format",
            "args": ["htmlbeautifier ${file}"]
        }
    ]   
}
  • Now, selecting the "Ruby format" task for .rb files or the "ERB format" task for .erb files formats the files in place.
  • Hotkeys can also be assigned to quickly invoke both tasks.
@HusseinElMotayam HusseinElMotayam changed the title Support for formatting Ruby files Support for formatting/beautifying Ruby files Sep 12, 2016
@rebornix
Copy link
Member

@HusseinElMotayam great idea! I'll put it on the list.

@HookyQR
Copy link
Contributor

HookyQR commented Sep 21, 2016

@HusseinElMotayam, this seems like an easy win. I'll look at spinning up an option to use either rbeautify or the auto-correct feature of rubocop (which is a bit harsh - but would align well for those using rubocop for linting).

For erb you can use my beautifier extension. You'll have to make some changes to the settings to get it to work 'by default' for erb files. It won't beautify the ruby inside though.

@ghost
Copy link

ghost commented Sep 22, 2016

I'd be interested in implementing auto-correct functionality on the plugin.

Rubocop supports the -a flag that automatically corrects some issues. Since the existing linter functionality works by passing the data through standard input, it's not as easy as making a linter flag, but it shouldn't be too hard. Any ideas on how this should be implemented?

I'm thinking that I can pretty much copy the way linters are implemented for now, and mostly s/linters/formatters/, with the caveat that a) formatters must run before linters and b) they must edit the actual file instead of taking standard input. Still, probably not very hard? Would this be an acceptable way to do a first iteration of formatters? I haven't actually coded any Visual Studio Code plugins so all help/tips are appreciated.

@rebornix
Copy link
Member

@slapresta IMO there are basically two ways of doing formatting or correction. The first one is Rubocop (or whatever library) telling us what part of content should be updated. The second one is Rubocop updates the local file and VS Code picks up the local update.

I don't know whether Rubocop support the first solution but the second one concerns me as what if Rubocop changes local content and users change the buffer in VS Code as well? I don't have a clear idea about this but definitely you can give it a try, in whatever way.

I like the idea of copying the way how linters do, you don't need to make this feature perfect at the very beginning :)

@khuongduybui
Copy link

any updates?

@HookyQR
Copy link
Contributor

HookyQR commented Nov 19, 2016

@khuongduybui: Formatting is provided, as long as you have RuboCop installed. It doesn't touch the file on disk, so there is no need to worry about tainted editors or committing unsaved changes to disk.

I've just made an update to the mechanics as well, so it will support .rubocop.yml settings correctly. Note that, by default, incorrect end indentation is not auto corrected. Checkout the RuboCop documentation for what it can, can't and will automatically do.

@HookyQR
Copy link
Contributor

HookyQR commented Nov 19, 2016

@HusseinElMotayam, have you tried out my suggestion using my beautify extension for ERB? Also, are you happy with were the formatting for Ruby is at in this extension?

@khuongduybui
Copy link

@HookyQR how do I use that "formatting" feature? Using the normal Alt Shift F keyboard shortcut does not work. Ctrl Shift P does not reveal any "format" command either (with or without selection).

@HookyQR
Copy link
Contributor

HookyQR commented Nov 19, 2016

If you right click on the document and the menu doesn't show "Format Document" then the extension doesn't know where to find rubocop. There should be an error displayed in the dev tools (Help > Toggle Developer Tools).

If you can't run rubocop with just rubocop it may be a path issue. Do you get any highlights from the rubocop linter? (Note that it's disabled by default, you'll have to turn it on in your settings.)

@HookyQR
Copy link
Contributor

HookyQR commented Nov 19, 2016

If you're getting linter responses, but can't use the formatting function, it's my fault. I've made a change today that should fix it, we'll have to wait for @rebornix to publish.

@khuongduybui
Copy link

@HookyQR yes, in fact I do get linter responses, along with my specific settings (such as ignoring some rules my team don't care about).

@HookyQR
Copy link
Contributor

HookyQR commented Nov 20, 2016

Yep, that's my bad then. The previous version didn't check for the rubocop executable properly, and didn't use the path settings of the linter. This version should. PR #89 has been merged and 0.10.3 released. Hopefully it will fix the problem. Let me know.

@khuongduybui
Copy link

I updated to latest release and was able to get formatting to work.

@HusseinElMotayam
Copy link
Author

@HookyQR I noticed that Rubocop formatting didn't work before when inside vscode, that is why I logged this FR.
In case it is back working again, then I'd be happy with the current formatting behavior.

@HookyQR
Copy link
Contributor

HookyQR commented Nov 20, 2016

It wasn't a thing when you opened this @HusseinElMotayam. Let me know if it's working for you. If the Format Document function is available in the (right click) context menu on a ruby file, it's working.

@HusseinElMotayam
Copy link
Author

HusseinElMotayam commented Nov 20, 2016

Well, the context menu is there and it is indeed formating.
But honestly speaking, the resulting formatting is totally incorrect for .rb files in terms of indentation. I'm sticking with manually running rbeautify at the moment.

I'm currently using Ruby vscode extension version 0.10.3
My Rubocop version is 0.42.0

@mydoghasworms
Copy link

👍 +1
Thanking you in advance! 😉

@HookyQR
Copy link
Contributor

HookyQR commented Dec 5, 2016

@HusseinElMotayam, the format that gets applied is dependant on the settings you have in your .rubocop.yml file. By default, it doesn't touch indenting. There are a large number of settings for formatting with rubocop, but many of them are off by default. Take a look at http://rubocop.readthedocs.io/en/latest/cops_lint/ (and the rest of that site) for how and what to use to get what you need.

@tnmto
Copy link

tnmto commented Jan 29, 2017

Any update on this?

@khuongduybui
Copy link

In the latest version I don't see the ruby.formatting option anymore.

@Dashue
Copy link

Dashue commented Feb 7, 2017

I'd be interested in a simpler way about this as well.

@suan
Copy link

suan commented Apr 18, 2017

Just wanna chime in that this doesn't work for me either - among other things, my project's .rubocop.yml has

Lint/DefEndAlignment:
  Description: "Makes sure def/ends line up"
  Enabled: true

but when I attempt to format I get "the key combo cmd+K, cmd+F is not a command", suggesting that editorHasDocumentSelectionFormattingProvider is false. I know VSCode can see rubocop itself cause I get linter warnings - I just can't format.

@rebornix
Copy link
Member

@suan actually we don't support format selections, which means right this moment, you can only use shift+option+f to format the whole document.

@g5codyswartz
Copy link

The task is a little dated, we're on v2.0.0 now for tasks
I've got mine working but it's not very smooth

@rebornix rebornix added the feature-request Adds currently unsupported functionality label Aug 13, 2017
@irace
Copy link

irace commented Nov 7, 2017

@rebornix @HookyQR I’m still having an issue here – hoping you might be able to help. I installed the extension and added:

"ruby.lint": {
  "rubocop": true
}

to my settings. I see inline listing, but I don’t have the Format Document option for my ruby documents, and there are no rubicon errors in the Developer Console. Please advise.

@irace
Copy link

irace commented Nov 7, 2017

Is it possible to trigger Ruby formatting inside of VS Code without having my .rubocop.yml file configured to auto-format? I want auto-formatting personally, but don’t want to force the rest of my team.

@buncis
Copy link

buncis commented Dec 12, 2017

@HookyQR hi, I've used your beautify extensions but it's still bad for .html.erb, it better if I rename the file to .html

did you consider to add support for erb files like https://github.com/threedaymonk/htmlbeautifier?

@askarhu
Copy link

askarhu commented Dec 13, 2017

@buncismamen exactly, it's terrible when VSCode chews up our .html.erb files...hard to match proper closings, especially on larger files. Any updates @HookyQR ?

@awongCM
Copy link

awongCM commented Dec 15, 2017

I'm also very keen to hear more about this development in progress.

Any update so far?

@hackhowtofaq
Copy link

@rebornix any plans to support format selections?

@nolanpro
Copy link

@hackhowtofaq this is my PR for it microsoft/vscode#35594

@rebornix let me know if you have any suggestions or if I'm doing something non standard. I'm not too familiar with the vs code framework. Thanks

@interpegasus
Copy link

It would be great to have support for Ruby formatting. Meanwhile I am also sharing my tasks JSON with tasks that support Ruby and Rails development.
Running the task "configure" installs the required gems.

{
  // See https://go.microsoft.com/fwlink/?LinkId=733558
  // for the documentation about the tasks.json format
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Ruby Format",
      "type": "shell",
      "command": "rbeautify ${relativeFile}",
      "problemMatcher": [],
      "presentation": {
        "reveal": "never"
      }
    },
    {
      "label": "ERB Format",
      "type": "shell",
      "command": "htmlbeautifier ${relativeFile}",
      "problemMatcher": [],
      "presentation": {
        "reveal": "never"
      }
    },
    {
      "label": "rubocop",
      "type": "shell",
      "command": "rubocop --auto-correct ${relativeFile}",
      "problemMatcher": [],
      "presentation": {
        "reveal": "never"
      }
    },
    {
      "label": "rspec",
      "type": "shell",
      "command": "bundle exec rspec",
      "problemMatcher": [],
      "presentation": {
        "reveal": "always"
      }
    },
    {
      "label": "rspec file",
      "type": "shell",
      "command": "bundle exec rspec ${relativeFile}",
      "problemMatcher": [],
      "presentation": {
        "reveal": "always"
      }
    },
    {
      "label": "rails c",
      "type": "shell",
      "command": "bundle exec rails console",
      "problemMatcher": [],
      "presentation": {
        "reveal": "always"
      }
    },
    {
      "label": "rails s",
      "type": "shell",
      "command": "bundle exec rails server",
      "problemMatcher": [],
      "presentation": {
        "reveal": "always"
      }
    },
    {
      "label": "configure",
      "type": "shell",
      "command": "gem install rbeautify ruby-debug-ide:0.6.0 debase:0.2.2.beta10 rcodetools rubocop fastri htmlbeautifier --no-ri --no-rdoc",
      "problemMatcher": [],
      "presentation": {
        "reveal": "always"
      }
    }
  ]
}

@gurgeous
Copy link
Contributor

Hey @interpegasus, thanks for sharing. These are neat. Keep in mind that vscode-ruby includes great support for reformatting with rubocop. Once set up it'll run rubocop -a for you automatically whenever you save a file. Just like your task, only using the builtin vscode formatting framework. Have fun!

@aliariff
Copy link

Hey I just created a extension for that so that we can format it naturally using vscode provided API.
Feel free to try it.
https://marketplace.visualstudio.com/items?itemName=aliariff.vscode-erb-beautify

@idoo
Copy link

idoo commented Oct 20, 2018

@aliariff would be nice if you can do it with ruby as well ;)

@aliariff
Copy link

@idoo there is rubocop for that vscode-ruby extension already do that.

@idoo
Copy link

idoo commented Oct 20, 2018

@aliariff try to call Format Code from menu, you'll see that error will be displayed

@aliariff
Copy link

@idoo which errror?

@wingrunr21
Copy link
Collaborator

@idoo That's likely due to how you've got it configured. I just merged #384 which aimed to improve that documentation.

In addition, the branch multi_root_and_linters has code that improves (I hope) the automatic environment detection so the extension isn't as picky on how you launch VSCode and/or what your environment looks like.

@wingrunr21
Copy link
Collaborator

This should be working correctly on v0.22.2 and up with language server based linting

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature-request Adds currently unsupported functionality
Projects
None yet
Development

No branches or pull requests