Rails 求助,有没有方法快速递格式化以下代码?

cisolarix · September 27, 2014 · Last by cisolarix replied at October 22, 2014 · 4278 hits
Topic has been selected as the excellent topic by the admin.

这是格式化之前的代码:

class CreateStudents < ActiveRecord::Migration
  def change
    create_table :students, :comment => "学生信息表" do |t|
      t.string :number, :comment => "学号"
      t.string :name, :comment => "姓名"
      t.date :birthday, :comment => "生日"
      t.string :gender, :comment => "性别"
      t.string :id_type, :comment => "证件类型"
      t.string :id_number, :comment => "证件号码"
      t.string :country, :comment => "国别"
      t.string :nation, :comment => "民族"
      t.string :birth_place, :comment => "籍贯"
      t.string :political_status, :comment => "政治面貌"
      t.string :education_level, :comment => "培养层次"
      t.string :enroll_method, :comment => "入学方式"
      t.date :enrolled_at, :comment => "入学时间"
      t.string :charge_type, :comment => "收费类别"
      t.string :enrolled_year, :comment => "学籍年度"
      t.string :enrolled_place, :comment => "生源所在地"
      t.string :bank_card_number, :comment => "银行卡号"
      t.string :bank_account_number,  :comment => "银行账号"
      t.boolean :is_active_duty, :default => false, :comment => "是否现役军人"
      t.boolean :is_equivalent_degree, :default => false, :comment => "是否同等学历"
      t.boolean :is_on_record, :default => true, :comment => "是否在籍"
      t.boolean :is_at_school, :default => true, :comment => "是否在校"

      t.timestamps
    end
  end
end

这是格式化之后的代码:

class CreateStudents < ActiveRecord::Migration
  def change
    create_table :students, :comment => "学生信息表" do |t|
      t.string  :number,               :comment => "学号"
      t.string  :name,                 :comment => "姓名"
      t.date    :birthday,             :comment => "生日"
      t.string  :gender,               :comment => "性别"
      t.string  :id_type,              :comment => "证件类型"
      t.string  :id_number,            :comment => "证件号码"
      t.string  :country,              :comment => "国别"
      t.string  :nation,               :comment => "民族"
      t.string  :birth_place,          :comment => "籍贯"
      t.string  :political_status,     :comment => "政治面貌"
      t.string  :education_level,      :comment => "培养层次"
      t.string  :enroll_method,        :comment => "入学方式"
      t.date    :enrolled_at,          :comment => "入学时间"
      t.string  :charge_type,          :comment => "收费类别"
      t.string  :enrolled_year,        :comment => "学籍年度"
      t.string  :enrolled_place,       :comment => "生源所在地"
      t.string  :bank_card_number,     :comment => "银行卡号"
      t.string  :bank_account_number,  :comment => "银行账号"
      t.boolean :is_active_duty,       :default => false,  :comment => "是否现役军人"
      t.boolean :is_equivalent_degree, :default => false,  :comment => "是否同等学历"
      t.boolean :is_on_record,         :default => true,   :comment => "是否在籍"
      t.boolean :is_at_school,         :default => true,   :comment => "是否在校"

      t.timestamps
    end
  end
end

vim easy align

Vim 用 Tabular plugin, 选中 block 内容,命令行:Tabular /:, s/: /:/g 就可以了。如果需要还可以随后用 q 手动调整一下间距。

总的来说呢,就是 ide 加插件,也有重量级 ide 自带插件可以搞定。例如 rubymine 的直接有。

完全没洁癖。估计你们看到我这种会很闹心。 当然有要求,我还是会这么做的。不喜欢是不喜欢,做是做。其实我也喜欢后一种,但我觉得不值得去做罢了。

#2 楼 @ericguo 是不是得用 Tab 才行?我平时喜用空格,不知道可行性如何。

#6 楼 @flowerwrong 请教一下,是哪个插件?找了半天也没找到。谢谢。

#7 楼 @yfractal 如果能很快地对齐的话,也无妨一试。如果太费事,就懒得格式化了

#2 楼 @ericguo 尝试了十来下,初步搞定简单的。如图:

#1 楼 @nightire #3 楼 @MrPasserby #4 楼 @billy #5 楼 @Yujing_Z 谢谢几位的建议,等我有空用 Vim 试下。

有 80 列洁癖的表示这种对齐容易出事

#15 楼 @ensonmj 现代显示器已经很大了,也基本上不会把 code 打印出来 背诵玩,所以 80 列慢慢就变为历史习惯了

sublime 对这种处理小 case

#18 楼 @cisolarix 指点不敢,你用 sublime 装上一个插件 AlignTab,安装成功后按住 command + shift + p 调出命令输入框,输入 AlignTab 按 enter 它会在底部弹出正则匹配条件,根据你得需求你只需要“ :"即空格加冒号,然后回车,就可以得到你想要的格式了

#16 楼 @huobazi 可以开两个 80 列啊,保持一行不太长还是有必要的

textmate 什么插件都不用装

cmd + opt + ]

#21 楼 @luikore 按你说的做之后:

class CreateStudents < ActiveRecord::Migration
  def change
    create_table :students, :comment            => "学生信息表" do |t|
      t.string :number, :comment                => "学号"
      t.string :name, :comment                  => "姓名"
      t.date :birthday, :comment                => "生日"
      t.string :gender, :comment                => "性别"
      t.string :id_type, :comment               => "证件类型"
      t.string :id_number, :comment             => "证件号码"
      t.string :country, :comment               => "国别"
      t.string :nation, :comment                => "民族"
      t.string :birth_place, :comment           => "籍贯"
      t.string :political_status, :comment      => "政治面貌"
      t.string :education_level, :comment       => "培养层次"
      t.string :enroll_method, :comment         => "入学方式"
      t.date :enrolled_at, :comment             => "入学时间"
      t.string :charge_type, :comment           => "收费类别"
      t.string :enrolled_year, :comment         => "学籍年度"
      t.string :enrolled_place, :comment        => "生源所在地"
      t.string :bank_card_number, :comment      => "银行卡号"
      t.string :bank_account_number,  :comment  => "银行账号"
      t.boolean :is_active_duty, :default       => false, :comment => "是否现役军人"
      t.boolean :is_equivalent_degree, :default => false, :comment => "是否同等学历"
      t.boolean :is_on_record, :default         => true, :comment => "是否在籍"
      t.boolean :is_at_school, :default         => true, :comment => "是否在校"

      t.timestamps
    end
  end
end

#22 楼 @cisolarix 你可以选中一段代码再按

#23 楼 @luikore 还是同样的效果

#24 楼 @cisolarix 哦我弄错你的意思了,你要 : 也对齐... sorry 这个不行

#25 楼 @luikore 比较罕见的需求吧?!

我是使用 vim 试验了一下你的问题,使用插件在命令模式下 Tab /:/r0 这样就可以了,意思是按分号对齐,右边留白是 0

Use the Ruby 1.9 hash literal syntax when your hash keys are symbols. https://github.com/bbatsov/ruby-style-guide#hash-literals

#28 楼 @hbin 新语法,也涉及到对齐的问题。

#27 楼 @bo6bear much better now, 虽然跟我的效果还是有些空格上的区别

看楼主这个帖子让我复习了一下 Tabularize,呵呵

#31 楼 @qhwa 挺不错,右下角 comment 那个部分左边的空白有点大

You need to Sign in before reply, if you don't have an account, please Sign up first.