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
  • 52 minutes ago
  • 12 years ago

Installs

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