Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sublime text comment indentation issue with Ruby

I use SublimeText since a few months with ruby, and I have a issue with comment auto-indentation. Indentation uses the comment's indentation, and indent all the following code using this indentation. I expect auto-indentation to ignore(at least) or set indent of previous code (at best), but not to take comment's indentation at all :

All my colleagues who use this editor have the same issue Here's a sample code re-indented by SublimeText

class Test
  def method1
  end

    #Bad indentation
    def method2
      somecode
    end

    def method3
      somecode
    end

  end

Wanted :

class Test
  def method1
  end

  #Bad indentation
  def method2
    somecode
  end

  def method3
    somecode
  end

end

I did a quickfix on ~/.config/sublime-text-2/Packages/Default/Indentation Rules - Comments.tmPreferences

Replacing

<key>scope</key>
<string>comment</string>
<key>settings</key>
<dict>
    <key>preserveIndent</key>
    <true/>
</dict>

With

<key>scope</key>
<string>comment</string>
<key>settings</key>
<dict>
    <key>preserveIndent</key>
    <false/>
</dict>

But it affects the default behavior, and I do prefer only affect Ruby's behavior. Does anyone has greater solution ?

like image 725
user2131391 Avatar asked Apr 25 '13 11:04

user2131391


1 Answers

I suggest you use the BeautifyRuby ST2 package. You will also need to install the htmlbeautifier gem. Not only your comments, but also your code will be indented nicely.

If you use rvm, you might need to change the BeautifyRuby.sublime-settings to use the ruby installed by the rvm, instead of the system installed ruby. To find out the path of ruby you are using, type which ruby at the shell prompt. Paste this path as the value for the key named "ruby" such as:

"ruby": "/home/thetuxracer/.rvm/rubies/ruby-2.0.0-p247/bin/ruby"

and beautifyruby can be use with : edit->beautify ruby

or you can change it keybind:

    { 
  "keys": ["alt+tab"],
  "command": "beautify_ruby", 
  "context": { "key": "selector",
    "operator": "equal", 
    "operand": "source.rb, source.ruby" }

  },
{
  "keys": ["alt+tab"],
  "command": "reindent", 
  "args": {
    "single_line": false
    },
  "context": { "key": "selector",
    "operator": "not_equal", 
    "operand": "source.rb, source.ruby" }
  },
like image 163
theTuxRacer Avatar answered Nov 19 '22 22:11

theTuxRacer