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

Installs

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