vscode-ruby version: 0.5.6It'd be great to have some formatting/beautifying functionality in the vscode-ruby plugin.
As for now, there is no way to format/beautify .rb and .erb files inside vscode in a proper way.
I worked around this by:
htmlbeautifier and rbeautify gems.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}"]
}
]
}
@HusseinElMotayam great idea! I'll put it on the list.
@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.
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.
@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 :)
any updates?
@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.
@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?
@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).
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.)
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.
@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).
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.
I updated to latest release and was able to get formatting to work.
@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.
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.
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
👍 +1
Thanking you in advance! 😉
@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.
Any update on this?
In the latest version I don't see the ruby.formatting option anymore.
I'd be interested in a simpler way about this as well.
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.
@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.
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 @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.
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.
@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?
@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 ?
I'm also very keen to hear more about this development in progress.
Any update so far?
@rebornix any plans to support format selections?
@hackhowtofaq this is my PR for it https://github.com/Microsoft/vscode/pull/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
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"
}
}
]
}
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!
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
@aliariff would be nice if you can do it with ruby as well ;)
@idoo there is rubocop for that vscode-ruby extension already do that.
@aliariff try to call Format Code from menu, you'll see that error will be displayed
@idoo which errror?
@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.
This should be working correctly on v0.22.2 and up with language server based linting
Most helpful comment
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.