ctrl+shift+p filters: :st2 :st3 :win :osx :linux
Browse

Auto Encoding for Ruby

by elomarns ST2

Sublime Text 2 plugin to automatically include #encoding: utf-8 on Ruby files when needed.

Details

  • 2013.05.16.05.16.05
  • github.​com
  • github.​com
  • 12 years ago
  • 2 hours ago
  • 12 years ago

Installs

  • Total 4K
  • Win 1K
  • Mac 2K
  • Linux 1K
Jan 15 Jan 14 Jan 13 Jan 12 Jan 11 Jan 10 Jan 9 Jan 8 Jan 7 Jan 6 Jan 5 Jan 4 Jan 3 Jan 2 Jan 1 Dec 31 Dec 30 Dec 29 Dec 28 Dec 27 Dec 26 Dec 25 Dec 24 Dec 23 Dec 22 Dec 21 Dec 20 Dec 19 Dec 18 Dec 17 Dec 16 Dec 15 Dec 14 Dec 13 Dec 12 Dec 11 Dec 10 Dec 9 Dec 8 Dec 7 Dec 6 Dec 5 Dec 4 Dec 3 Dec 2
Windows 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Mac 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Linux 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Readme

Source
raw.​githubusercontent.​com

Auto Encoding for Ruby

I hate the need to include # encoding: utf-8 on every Ruby file with non-ASCII characters. So I've created this plugin to save me from this boring task. It automatically adds an encoding declaration on the top of Ruby files when needed, and remove it when it's not necessary anymore. Simple as that.

Installation

You have 3 options for installing Auto Encoding for Ruby: using Package Control, using Git, or just downloading it.

Package Control

Inside Sublime Text 2, open your command pallete (⌘ + ⇧ + P on OS X), and select "Package Control: Install Package". After this, search for "Auto Encoding for Ruby" and install it!

Git

Open your terminal application and go to your Packages directory, whose location depends on your operating system:

  • OS X:

    cd ~/Library/Application\ Support/Sublime\ Text\ 2/Packages
    
  • Linux:

    cd ~/.Sublime\ Text 2/Packages/
    
  • Windows:

    cd %APPDATA%/Sublime Text 2/Packages/
    

After this, you just need to clone this repository:

git clone git://github.com/elomarns/auto-encoding-for-ruby.git "Auto Encoding for Ruby"

Downloading

Click on the nice cloud icon above, and download the zip file containing this plugin. Then unzip the file and move the resulting folder to your Packages directory.

How to Use

Auto Encoding for Ruby will add an encoding declaration on top of Ruby files on these situations:

  • Just after you type the first non-ASCII character of the file;
  • When you open a file with non-ASCII characters and no encoding declaration.

It will also remove the encoding declaration on the following cases:

  • Just after you delete the last non-ASCII character;
  • When you open a file with an encoding declaration but without non-ASCII characters.

In other words, just write your code as you would without the plugin and you'll be fine.

Configuration

Although this plugin works out of the box, you can tweak it to your needs. Just for reference, this is the default settings, located on Packages/Auto Encoding for Ruby/Auto Encoding for Ruby.sublime-settings file:

{
  "allowed_syntaxes":
  [
    "Packages/Ruby/Ruby.tmLanguage",
    "Packages/Rails/Ruby on Rails.tmLanguage",
    "Packages/RSpec/RSpec.tmLanguage"
  ],

  "encoding_declaration": "# encoding: utf-8\n\n",
  "encoding_declaration_regex": "^\\s*#\\s*encoding\\s*:\\s*utf-8\\s*$",
  "remove_encoding_declaration": true,
  "check_encoding_on_load": true,
  "check_encoding_on_keystroke": true,
  "check_encoding_on_save": true
}

Allowed Syntaxes

By default, Auto Encoding for Ruby will works with Ruby, Ruby on Rails and RSpec syntaxes. But you can add or remove syntaxes by setting a new value on "allowed_syntaxes", on your Packages/User/Auto Encoding for Ruby.sublime-settings file. For example, if you want to remove the RSpec support, just update this setting to this:

{
  "allowed_syntaxes":
  [
    "Packages/Ruby/Ruby.tmLanguage",
    "Packages/Rails/Ruby on Rails.tmLanguage"
  ]
}

Encoding Declaration

The default encoding declaration is # encoding: #utf-8, but you can change it if you want. To do this, just set a new value to "encoding_declaration" setting on your Packages/User/Auto Encoding for Ruby.sublime-settings file. But if you change this setting, you must change "encoding_declaration_regex" too. This setting is used to check if your file already has an encoding declaration. So if you change only the encoding declaration but don't update the regex, the plugin won't be able to know if your file already has an encoding declaration, and it will add it infinitely.

This is an example of a changing in this setting:

{
  "encoding_declaration": "# -*- encoding : utf-8 -*-\n\n",
  "encoding_declaration_regex": "^\\s*#\\s*-\\*-\\s*encoding\\s*:\\s*utf-8\\s*-\\*-\\s*$",
  "remove_encoding_declaration": false,
  "check_encoding_on_load": true,
  "check_encoding_on_keystroke": true,
  "check_encoding_on_save": true
}

Don't forget to escape character classes on "encoding_declaration_regex" setting.

Always add encoding declaration

By default, this plug in only will add the encoding declaration to the file if it detects a non-ASCII character. If you want to always add the encoding declaration regardless of whether there is a non-ASCII character or not then you change the setting "always_generate_encoding_declaration" to true.

Licensing

You're free to do whatever you want with this plugin. How about this as a license?