Sublime text2のAuto Encoding for Rubyで既に別形式のエンコード指定があるのに自動で挿入されてしまう。

ちょっと困ったので中開けて書き換え。

Ruby1.9系からエンコード指定が必須になって実行時にファイルのエンコードが原因で動作が怪しくなるとかそういうことは減ったのですが、いかんせん面倒くさい。

普段書いててちょっと困ってたのでcmd+shift+pでいつものCommand Palleteを起動して漁って見ることにしたら、素晴らしいことにauto-encoding-for-ruby(リンクはgithubのページ)とかいう俺のためのものとしか思えないような素晴らしい奴が見つかった。

で、反射的にインストールしたのだが・・・・

実は既に書いていたコードが別形式のエンコード形式で指定していたため気持ちの悪いことに・・・

#encoding: utf-8

# -*- coding: utf-8 -*-
class MyAntenaController < ApplicationController

なんかちょっとゾワワと来たので解決策を考えてみる。

1. 諦めてこれはこれでいいとする。
まぁ僕一人だったら良いんですけどね・・・
一番最初の行に入ってるかチェックしているだけなのでもしも他の人のエディタのプラグインも同じ仕様だと・・・
上のエンコード指定が繰り返し発生することにもなりかねないです。

2. プラグインの中を開けて書き換えてしまう。
正直言うと余りやりたくはない。
が、背に腹は代えられないということで覗いてみて見て簡単に対応できるかどうか確かめてみることにしました。

sublime text2->preferences->Browse Packages
プラグインのインストールディレクトリを開きauto-encoding-for-rubyディレクトリに移動します。

こんな中身で意外とシンプルだった。
sublime text2のプラグインpythonで記述がされているのですが、実はというほどでもありませんが、私はpythonを書けませんし読めません(`・∀・´)エッヘン!!(威張るこっちゃ無い)。
ですが、明らかにAuto Encoding for Ruby.sublime-settingsが怪しそうだなと思うぐらいの知性は持ち合わせているので、pythonを読む前に取り敢えずコレを開いて見ることに・・・

// Don't change any setting on this file. If you want to override the default
// behaviour, change the settings values on your
// Packages/User/Auto Encoding for Ruby.sublime-settings file.
{
  // Syntaxes where Auto Encoding for Ruby will be activated.
  "allowed_syntaxes":
  [
    "Packages/Ruby/Ruby.tmLanguage",
    "Packages/Rails/Ruby on Rails.tmLanguage",
    "Packages/RSpec/RSpec.tmLanguage"
  ],

  // This is the encoding declaration that will be inserted on the top of your files.
  "encoding_declaration": "#encoding: utf-8\n\n",

  // The first line of your files will be checked against this regex to verify if it
  // already has an encoding declaration. So if you change the default encoding
  // declaration above, you must change this accordingly. Don't forget to escape
  // character classes as "\s" to "\\s", for example.
  "encoding_declaration_regex": "^\\s*#\\s*encoding\\s*:\\s*utf-8\\s*$",

  // Set to false if you want to keep encoding declaration even it became not need
  "remove_encoding_declaration": true,

  // Set to false if you don't want to checking encoding on every keystroke
  "checking_encoding_on_pre_save_only": false
}

一番上にもしデフォルトを変えたければコレを書き換えるんじゃなくてPackages/User/の下にAuto Encoding for Ruby.sublime-settingsファイルを作ってねって書いてあったね。ゴメンナサイ・・・この記事かいてる時に気が付いちゃった(・ω<)。

というわけで、素直に従ってPackage/Userの中にAuto Encoding for Ruby.sublime-settingsを作成しました。
で、取り敢えず上に貼った初期設定をコピペして、そこからスタートしてみる。
で取り敢えず見てみると,

// The first line of your files will be checked against this regex to verify if it
  // already has an encoding declaration. So if you change the default encoding
  // declaration above, you must change this accordingly. Don't forget to escape
  // character classes as "\s" to "\\s", for example.
  "encoding_declaration_regex": "^\\s*#\\s*encoding\\s*:\\s*utf-8\\s*$",

とか有るわけですね、はいこれは既にエンコードが入ってるかどうかのチェックを下の正規表現で行ってるってことなんだけど・・・
取り敢えず、今回の場合この正規表現に今までのエンコード表現を追加すれば回避できそう?な気がするので変更してみる。
というわけで下のようにガリガリと賢くない正規表現を力技で実装しました。

  // The first line of your files will be checked against this regex to verify if it
  // already has an encoding declaration. So if you change the default encoding
  // declaration above, you must change this accordingly. Don't forget to escape
  // character classes as "\s" to "\\s", for example.
  "encoding_declaration_regex": "^\\s*#\\s*(encoding\\s*:\\s*utf-8|-\\*-\\s*coding:\\s*utf-8\\s*-\\*-)\\s*$",


正規表現に関しては自分で書いててこれは酷いwとか思うレベルですが、コレでも一応はじめの目的は満たせました。

詳説 正規表現をちゃんとやろうと決意した日でした・・・

因みに挿入される形式自体を統一したいことなど有ると思いますが、変更したい場合には

  // This is the encoding declaration that will be inserted on the top of your files.
  "encoding_declaration": "#encoding: utf-8\n\n",

を書き換えるだけすげー楽ちん。

結論
auto-encoding-for-rubyは便利