craigwilliams / beautifyruby Goto Github PK
View Code? Open in Web Editor NEWSublime Text 2 & 3 Plugin to Beautify Ruby
License: Other
Sublime Text 2 & 3 Plugin to Beautify Ruby
License: Other
I like to follow GitHub's ruby guidelines. So I'm curious how I change the rulesets for code cleanup
For example I'd like it to be formatted like this...
attr_accessible :id,
:name
:description
Not like this...
attr_accessible :id,
:name
:description
I've installed BeautifyRuby with Package Control for Sublime Text 3. I've changed my user settings to:
{
"ruby": "ruby"
}
But after trying to beautify a ruby file I get this error-message: "Check your ruby interpreter settings". What can I do with this?
With mulitple files open, if you change one file but then mouse over another file while doing a save all, the changed file gets the content of the file that has mouse focus.
Only happens when format on save is true.
Fortunately I found this out on files checked into git:)
in our rspec suite, we have a lot of contexts like context "#cache_key" do
this works fine until you change double quotes to single quotes.
when using single quotes like context '#cache_key' do
the next line does not get intended properly!
i tried to find the line for this in the plugin code, but there is so much stuff going on there...
If you have a regex like:
expression = /([0-9]*)/
The beautifier chokes and adds extra indents after it. You can work around this by commenting out the regex line, then beautifying, then uncommenting.
When a file is edited but not yet saved, pressing the hotkey will try to beautify previous code.
This can lead to unintended consequencies if someone does not pay attention.
My line ending in SB3 are set to windows. When I run CTRL+ALT+K the formatting works fine but shows a CR at the end of every line. How do I get rid of the CR formatting mark showing up on every beautify_ruby command?
I have tried linux line endings as well and the same thing happens.
I am running windows 7, sublime text 3 and the latest BeautifyRuby gem.
Unless I missed something, beautifyruby converts tabs to spaces. It seems like if I used tabs, it would assume I considered them beautiful :) In any case, an option to disable this would be welcomed.
Hey,
I just installed the plugin on Sublime 3 via the package manager.
which ruby
/usr/local/opt/ruby/bin/ruby
Then I went to preferences > package settings > beautify ruby > settings default
but I'm not able to edit this file.
So I added
{
"ruby": "/usr/local/opt/ruby/bin/ruby"
}
to the user settings of the package but when I try to use it I still get "check your ruby interpreter settings"
What am I doing wrong?
The command beautifies the whole file. Any way to beautify just the selected text? Thanks.
We are using tabs of width for, cause we don't like how it can be hard at times to see the differences in indentation of 2
class A
def foo
"abc" =~ /\w+/
puts $`
end
def bar
end
end
After BeautifyRuby runs, it turns out to be:
class A
def foo
"abc" =~ /\w+/
puts $`
end
def bar
end #### <---- wrong!
end
A temporary workaround is:
class A
def foo
"abc" =~ /\w+/
puts $` #` <------ workaround!
end
def bar
end
end
Some other special ruby variables, such as
As a pity, BeautifyRuby does not leave here_docs untouched. This makes BeautifyRuby hard to use in this case.
Example:
module FOO
def foo
config =<<-eos
### This file is authorization file for #{@project_name}.
# do not remove * = at [/]
[groups]
observer=
developer=#{@project_admin_user}
[/]
@observer=r
@developer=rw
* =
eos
end
end```
converts to
```ruby
module FOO
def foo
config =<<-eos
### This file is authorization file for #{@project_name}.
# do not remove * = at [/]
[groups]
observer=
developer=#{@project_admin_user}
[/]
@observer=r
@developer=rw
* =
eos
end
end
or this example
template = %{
<html>
<head><title>Ruby Toys -- <%= @name %></title></head>
<body>
<h1><%= @name %> (<%= @code %>)</h1>
<p><%= @desc %></p>
<ul>
<% @features.each do |f| %>
<li><b><%= f %></b></li>
<% end %>
</ul>
<p>
<% if @cost < 10 %>
<b>Only <%= @cost %>!!!</b>
<% else %>
Call for a price, today!
<% end %>
</p>
</body>
</html>
}.gsub(/^ /, '')
is converted to
template = %{
<html>
<head><title>Ruby Toys -- <%= @name %></title></head>
<body>
<h1><%= @name %> (<%= @code %>)</h1>
<p><%= @desc %></p>
<ul>
<% @features.each do |f| %>
<li><b><%= f %></b></li>
<% end %>
</ul>
<p>
<% if @cost < 10 %>
<b>Only <%= @cost %>!!!</b>
<% else %>
Call for a price, today!
<% end %>
</p>
</body>
</html>
}.gsub(/^ /, '')
Sublime Text 2 will lost cursor focus after beautify ruby. It's difficult to find last working line, especially when the file has lots of lines. I prefer the way that 'Reindent Lines' does.
When I run BeautifyRuby over a file, it does not place a new line character at the end of the file. It also removes any existing new line characters.
Hi,
I want to format my VagrantFIle which is not a .rb file, but contains text in ruby syntax. I encountered the error: Error: This is not a Ruby file. The validation should be removed
Carlo
I added a comment block to a model definition and when i run beautify it indents the =begin part making the model definition not being commented out
It would be REALLY nice if this provided a menu option, so you could get to it using Cmd+P. It's really unintuitive having to find a documentation page for a package to find out what the keyboard shortcut is when you install it from the Package Manager.
Great work though.
Getting this:
Traceback (most recent call last):
File "/Applications/Sublime Text.app/Contents/MacOS/sublime_plugin.py", line 382, in run_
return self.run(edit)
File "/Users/kain/Library/Application Support/Sublime Text 3/Packages/BeautifyRuby/beautify_ruby.py", line 21, in run
self.update_view(self.beautify_buffer())
File "/Users/kain/Library/Application Support/Sublime Text 3/Packages/BeautifyRuby/beautify_ruby.py", line 36, in beautify_buffer
beautifier = subprocess.Popen(self.cmd(), shell=True, cwd=working_dir, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
File "/Users/kain/Library/Application Support/Sublime Text 3/Packages/BeautifyRuby/beautify_ruby.py", line 72, in cmd
args = ["'" + unicode(path) + "'"]
NameError: global name 'unicode' is not defined
The linux shortcut is missing, maybe adding a Default (Linux).sublime-keymap will help.
Hi, great package! Although one issue, every time I write x%[] command and then beautify, code gets shifted one indent per each %x[] command..
I have a few eruby files that end only with .erb and not .html.erb
How can i configure BeautifyRuby to check for the .erb files too?
I used which ruby to find the best option for the ruby interpreter settings.
so for rbenv on Ubuntu its.
"ruby": "/home/sayth/.rbenv/shims/ruby",
However this still asks to check the interpreter. For Rbenv what should this be?
Here's what I'm getting: 4 spaces indent, instead of two:
until first_section_number do
current_line = Or.next_nonblank_line(lines: lines, start: current_line)
aLine = lines[current_line].strip
if aLine =~ /^([1-9]\w*\.[0-9]\w*) +(.+)$/ then
first_section_number = $1
first_section_title = $2.strip
else
if aLine =~ /^[^(].+[^)]$/ then # Not a sub-sub-chapter
first_subchapter_name = aLine.titlecase
end
end
end
I'm looking at the code to see how I'd fix this, if it's actually a bug.
Hey, thanks for this awesome plugin!
If you have a ruby block comment:
=begin
yada yada yada
=end
it indents it to
=begin
...
=end
This breaks on most ruby 1.9.3s
the beautifier should not touch =begin and =end, they must remain totally left justified
Running Beautify Ruby command always produces this error in ST2 Python console (ST2 Beta 2181, OSX 10.6.8):
Writing file /Users/vb/Library/Application Support/Sublime Text 2/Packages/BeautifyRuby/ruby/beautifier.rb with encoding UTF-8
Exception in thread Thread-6:
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threading.py", line 522, in __bootstrap_inner
self.run()
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threading.py", line 477, in run
self.__target(*self.__args, **self.__kwargs)
File "./history.py", line 69, in run
EOFError
Seems like there is some problem with child process that the command spawns (popen?). I have zero Python fu so no idea where to start fixing it...
I installed BeautifyRuby on my Mac and everything works perfectly with the keyboard shortcut. I then opened my 'Setting - User' and added the line "run_on_save" : true, and restarted ST2, but it does not work. Did I configure it correctly? Did I put the config line in the wrong file? Any ideas?
Hello, and thanks for you work. It really useful.
Here's the indentation BR gives to me
desc "blabla"
task :blabla => :environment do
filename_pattern = File.join(LOCAL_MERGE_FOLDER, "*.#{FILE_BASE_NAME}*.gz")
Dir.glob(filename_pattern).sort.each do |filename|
puts "Merging #{filename}"
and here's what it should be :
desc "blabla"
task :blabla => :environment do
filename_pattern = File.join(LOCAL_MERGE_FOLDER, "*.#{FILE_BASE_NAME}*.gz")
Dir.glob(filename_pattern).sort.each do |filename|
puts "Merging #{filename}"
It seems to be related to the 'Dir.glob'.
After installation on sublime tekst 3 (manual installation), with ruby: ruby changed for windows, the command and keyboard shortcut do show up, but don't seem to do anything. No blank screen, no error. Is there a log file that I can consult? Any other ideas?
It would be nice if indent size (set here https://github.com/CraigWilliams/BeautifyRuby/blob/master/lib/rbeautify/config/ruby.rb#L10)
were configurable in the settings file, personally I would like 4 spaces even though it is against the grain of 2 spaces.
before_save O.new("v"),
if: Proc.new { |p| [154, 1217].include?(p.account_id) }
after_save O.new("v"),
if: Proc.new { |p| [154, 1217].include?(p.account_id) }
after_find O.new("v"),
if: Proc.new { |p| p.attributes.include?("v") && [154, 1217].include?(p.account_id) }
becomes:
before_save O.new("v"),
if: Proc.new { |p| [154, 1217].include?(p.account_id) }
after_save O.new("v"),
if: Proc.new { |p| [154, 1217].include?(p.account_id) }
after_find O.new("v"),
if: Proc.new { |p| proposal.attributes.include?("v") && [154, 1217].include?(p.account_id) }
the top code block is correct.
I think it's seeing the "if" keywoard, and just auto-indenting the line without looking at what if is
a condition starting with if:
isn't actually valid.
should support encoding reading the first line, for example
if source have chars like Italians accent (for example: è à ù)
blank file
class OrlandoDns < Dns
attr_reader :srv_records
DATA_CENTER = 'orl'
def initialize
@srv_records = parse_srv_records('orl')
super
end
def to_inbound_orl
sort_srv_records('orl')
end
end
becomes
class OrlandoDns < Dns
attr_reader :srv_records, :site
DATA_CENTER = 'orl'
def initialize
@srv_records = parse_srv_records('orl')
super
end
def to_inbound_orl
sort_srv_records('orl')
end
end
Notice now @srv_records and DATA_CENTER are on column 0, when they should be on column 4 and 2, respectfully
This is on STS3
-John
The following code is formatted incorrectly (the if in line 5 is put as a new line)
Original code:
def _findLoginFormsByActions(doc)
inputs=doc.xpath("//form").select { |n|
bits=[]
%w(action name).each {|x|
bits << n[x].downcase if n[x]
}
bits.any? {|b|
INTERESTING_WORDS.any? {|word| not (b.index word).nil? }
}
}
end
Formatted code:
def _findLoginFormsByActions(doc)
inputs=doc.xpath("//form").select { |n|
bits=[]
%w(action name).each {|x|
bits << n[x].downcase
if n[x]
}
bits.any? {|b|
INTERESTING_WORDS.any? {|word| not (b.index word).nil? }
}
}
end
I would like to prevent the deleting of empty lines, especially in erb files its sometimes more comfortable to read parts if there are empty lines.
at the moment all empty lines are removed..
Could you maybe add an option?
thanks!
Before
p = FactoryGirl.create(:content, {
user: @user,
account: @account,
category_id: c.id,
visibility: Shared::PUBLIC
})
After
p = FactoryGirl.create(:content, {
user: @user,
account: @account,
category_id: c.id,
visibility: Shared::PUBLIC
})
Expected
p = FactoryGirl.create(:content, {
user: @user,
account: @account,
category_id: c.id,
visibility: Shared::PUBLIC
})
Is there a way to configure this? or is this a bug?
Even if I save the file before running the script, it wipes the entire file when run.
I lost my work about 15 times.
When trying to beautify html.erb docs, I get the following error from Sublime Text:
check your ruby interpreter settings
I have the htmlbeautifier
gem installed.
i noticed today, that BeautifyRuby changed behavior for indention after calling private
in a class.
unfortunately i can't say when this change appeared, but i think that this must be new somehow. i added a failing test for this: phoet@375d758
is it supposed to be that way?
Under the new OS X Mavericks with Ruby 2.0.0-p247, while the ruby source file has unicode characters like Chinese words, rbeautify.rb
Line 41 would raise an "invalid byte sequence in US-ASCII" exception:
41| source.split("\n").each_with_index do |line_content, line_number|
42| line = RBeautify::Line.new(language, line_content, line_number, block, use_tabs)
43| dest += line.format + "\n"
44| block = line.block
45| end
The exception is raised by source.split("\n")
because something weird happened to the source string and it's not a UTF-8 string. Workaround for this is either:
source.force_encoding("UTF-8")
. This should be safe because all the source string is delivered by Sublime python runtime and it uses subprocess.communicate(body.encode("UTF-8"))
{
"ruby": "/usr/bin/env ruby --encoding='utf-8'",
// ...
}
Yes, force the ruby VM to cheat all strings as UTF-8.
Currently I can only use workaround 2 but I strongly suggest add a ```force_encoding``` call to the rbeautify.rb source code. Thanks.
I use rbenv and can't seem to get this to work no matter what I configure the ruby path to be. Any suggestions?
$ which ruby
/Users/bhilkert/.rbenv/shims/ruby
Should run show_at_center after beautify the ruby code. Or keep the editor scoll position untouched. But right now it scrolls to the top of the file, not keeping my current editing position.
When I use BeautifyRuby command the content of the file is erased...
With windows line endings, I don't know exactly what happens but I get one line ending added on each line, so this script:
class PagesController < ApplicationController
layout 'web_client'
def home
@event = true # Max 1
@news = News.find(:all) # Max 3
@books = [1,2] # Max 2
@events = [1,2,3,4,5,6] # Max 6
@initiatives = [1,2,3] # Max 3
@videos = [1,2,3] # Max 3 [1, 2, 3]
@suggested_links = [1,2,3,4] # Max 4
respond_to do |format|
format.html { render } # index.html.erb
end
end
def about
end
def contacts
end
end
becomes this:
class PagesController < ApplicationController
layout 'web_client'
def home
@event = true # Max 1
@news = News.find(:all) # Max 3
@books = [1,2] # Max 2
@events = [1,2,3,4,5,6] # Max 6
@initiatives = [1,2,3] # Max 3
@videos = [1,2,3] # Max 3 [1, 2, 3]
@suggested_links = [1,2,3,4] # Max 4
respond_to do |format|
format.html { render } # index.html.erb
end
end
def about
end
def contacts
end
end
Very boring
Unless I'm missing something, reformatting is applied to the entire file rather than just the current selection.
Is this a bug or expected behaviour?
(Keep up the good work by the way - great package)
When I run the command on sublime 2 Windows 7 - the file content disappears and the file also gets saved.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.