【问题标题】:Sublime text comment indentation issue with RubyRuby 的 Sublime 文本注释缩进问题
【发布时间】:2013-04-19 06:24:11
【问题描述】:

我使用 SublimeText 几个月以来一直在使用 ruby​​,但我遇到了评论自动缩进的问题。缩进使用注释的缩进,并使用此缩进缩进以下所有代码。我希望自动缩进忽略(至少)或设置先前代码的缩进(最好),但根本不接受评论的缩进:

我所有使用这个编辑器的同事都有同样的问题 这是一个由 SublimeText 重新缩进的示例代码

class Test
  def method1
  end

    #Bad indentation
    def method2
      somecode
    end

    def method3
      somecode
    end

  end

想要:

class Test
  def method1
  end

  #Bad indentation
  def method2
    somecode
  end

  def method3
    somecode
  end

end

我做了一个快速修复 ~/.config/sublime-text-2/Packages/Default/Indentation Rules - Comments.tmPreferences

更换

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

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

但它会影响默认行为,我更喜欢只影响 Ruby 的行为。 有没有人有更好的解决方案?

【问题讨论】:

  • 很遗憾没有人回答这个问题。我真的很想知道。 :|
  • 好伤心,这么多年了,我们面临同样的问题,但我们还是没有解决办法......
  • TuxRacer 的回答是对的,beautifyruby 真的可以解决这个问题,我的错。

标签: ruby sublimetext2 auto-indent


【解决方案1】:

我建议您使用BeautifyRuby ST2 包。您还需要安装htmlbeautifier gem。不仅你的 cmets,你的代码也会很好地缩进。

如果您使用 rvm,您可能需要更改 BeautifyRuby.sublime-settings 以使用 rvm 安装的 ruby​​,而不是系统安装的 ruby​​。要找出您正在使用的 ruby​​ 的路径,请在 shell 提示符下键入 which ruby。将此路径粘贴为名为 "ruby" 的键的值,例如:

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

beautifyruby 可以用于:edit->beautify ruby​​

或者您可以更改它的键绑定:

    { 
  "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" }
  },

【讨论】:

  • 这个问题没用:标签关注评论
  • 对不起,小姐,beautifyruby 真的可以解决这个问题。
【解决方案2】:

以下是缩进的关键设置

"tab_size": 2,
"translate_tabs_to_spaces": true 

这对我有用

{
"caret_style": "solid",
"color_scheme": "Packages/Color Scheme - Default/Monokai.tmTheme",
"enable_tab_scrolling": false,
"folder_exclude_patterns":
[
    ".git",
    ".hg",
    ".sass-cache",
    "log",
    "tmp",
    "script",
    "vendor",

],
"font_size": 10,
"highlight_line": true,
"highlight_modified_tabs": true,
"ignored_packages":
[
    "Vintage"
],
"rulers":
[
    100
],
"scroll_past_end": false,
"tab_size": 2,
"translate_tabs_to_spaces": true
}

【讨论】:

  • 这个问题没用:标签关注评论
【解决方案3】:

对于 ST3, 首选项 -> 设置 - 用户

添加以下代码: { “标签大小”:2 }

默认情况下,SublimeText 使用 4 个字符长的硬制表符。

【讨论】:

  • 这个问题没用:标签关注评论
【解决方案4】:

我把它放在我的用户设置文件中:

首选项 -> 设置 - 用户:

{
"color_scheme": "Packages/Theme - Refined/Color Schemes/Danro.tmTheme",
"ensure_newline_at_eof_on_save": true,
"font_size": 18.0,
"hot_exit": false,
"ignored_packages":
[
    "Vintage"
],
"remember_open_files": false,
"save_on_focus_lost": true,
"tab_size": 2,
"translate_tabs_to_spaces": true

}

我试图重现您的问题,但无法强制执行这些设置。

【讨论】:

  • 这个问题没用:标签关注评论
猜你喜欢
  • 1970-01-01
  • 2014-07-18
  • 2016-02-20
  • 2016-11-09
  • 2014-05-26
  • 1970-01-01
  • 2011-06-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多