简体   繁体   English

Vim中格式错误的红宝石代码

[英]Bad formatting ruby code in Vim

When I format a PHP file in vim it's ok, but when I format a Ruby file, VIM format code bad. 当我在vim中格式化PHP文件是可以的,但是当我格式化Ruby文件时,VIM格式的代码不好。

For example: 例如:

class PostsController < ApplicationController

                skip_before_filter :authorize, :only => [ :index, :show ]

  def index
        @posts = Post.all
     end

  def show
    @post = Post.find(:first, :conditions => [ "id = ?", params[:id]], :include => [ :user, :category, :gallery ])
                  @photos = Photo.where(:gallery_id => @post.gallery.id).all
      end
   end

When I enter the command gg=G , I get. 当我输入命令gg = G时 ,我得到了。

class PostsController < ApplicationController

skip_before_filter :authorize, :only => [ :index, :show ]

def index
@posts = Post.all
end

def show
@post = Post.find(:first, :conditions => [ "id = ?", params[:id]], :include => [ :user, :category, :gallery ])
@photos = Photo.where(:gallery_id => @post.gallery.id).all
end
end

Please help me. 请帮我。

To get Ruby indenting working you need to provide indenting configuration. 要使Ruby缩进工作,您需要提供缩进配置。 Vim itself is not able to indent Ruby code, you could set the indentexpr variable to some similar language (like basic), but you wont be happy with results. Vim本身不能缩进Ruby代码,您可以将indentexpr变量设置为某种类似的语言(例如basic),但是您对结果不满意。 Check your smartindent and indentexpr variables: 检查您的smartindent和indentexpr变量:

:set si?
:set indentexpr?

In my case they are set: 就我而言,它们设置为:

nosmartindent
indentexpr=GetRubyIndent()

The best way to configure vim for ruby is to use vim-ruby plugin: https://github.com/vim-ruby/vim-ruby 为ruby配置vim的最佳方法是使用vim-ruby插件: https : //github.com/vim-ruby/vim-ruby

A more general formatting plugin exists, called vim-autoformat . 存在一个更通用的格式化插件,称为vim-autoformat Among others, it integrates rbeautify to provide stronger formatting than just fixing indentation. 除其他功能外,它还集成了rbeautify,以提供比固定缩进更强的格式。

I am not sure if I have any Vim plugins, because I use it at work (and was already installed). 我不确定我是否有任何Vim插件,因为我在工作中使用了它(并且已经安装了)。 However, for what it's worth, here's some of my .vimrc file. 但是,这是值得的,这是我的一些.vimrc文件。

syntax enable             " Enable syntax highlighting
syntax on
set expandtab             " Use spaces instead of tabs
set shiftwidth=2          " 1 tab == 4 spaces
set tabstop=2             " 1 tab == 4 spaces

I actually have set smartindent commented out in the file. 我实际上已经在文件中set smartindent注释。 [1] [1]

If you want replace any tab-character with spaces (set in the above .vimrc ), I suggest the following command in your working file: set :retab [2] 如果要用空格替换任何制表符(在上述.vimrc设置),建议在工作文件中使用以下命令: set :retab [2]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM