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
  • 11 years ago
  • 2 hours ago
  • 12 years ago

Installs

  • Total 4K
  • Win 1K
  • Mac 2K
  • Linux 1K
Jul 27 Jul 26 Jul 25 Jul 24 Jul 23 Jul 22 Jul 21 Jul 20 Jul 19 Jul 18 Jul 17 Jul 16 Jul 15 Jul 14 Jul 13 Jul 12 Jul 11 Jul 10 Jul 9 Jul 8 Jul 7 Jul 6 Jul 5 Jul 4 Jul 3 Jul 2 Jul 1 Jun 30 Jun 29 Jun 28 Jun 27 Jun 26 Jun 25 Jun 24 Jun 23 Jun 22 Jun 21 Jun 20 Jun 19 Jun 18 Jun 17 Jun 16 Jun 15 Jun 14 Jun 13
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?