Skip to content

Commit

Permalink
Fix error when using tabs
Browse files Browse the repository at this point in the history
Changes to readme
  • Loading branch information
Craig Williams committed Dec 9, 2014
1 parent 0a672ea commit 4338ca2
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 11 deletions.
1 change: 0 additions & 1 deletion BeautifyRuby.sublime-settings
Expand Up @@ -2,7 +2,6 @@
// Would you prefer a tab or two spaces to represent a tab
// The default is two spaces represented by 'space'
// anything else will use one tab character
"tab_or_space": "space",
"file_patterns": ["\\.html\\.erb", "\\.rb", "\\.rake", "Rakefile", "Gemfile"],
"html_erb_patterns": ["\\.html\\.erb"],
"run_on_save": false,
Expand Down
28 changes: 25 additions & 3 deletions README.md
Expand Up @@ -2,11 +2,17 @@

# BeautifyRuby

Beautifies Ruby code. This plugin uses the [Ruby Script Beautifier](http://www.arachnoid.com/ruby/rubyBeautifier.html) written by P.Lotus
Erb html templates uses [Paul Battley's htmlbeautifier gem](https://github.com/threedaymonk/htmlbeautifier). This (as well as rubygems) is assumed to be installed as seen by the ruby interpreter. Note that if you beautify and erb file but `htmlbeautifier` is not found, the error message is 'check your ruby interpreter settings', do not be misled.

I made very little modification to get it to work with a Sublime Text 2 plugin.
### Interpreter settings

Erb html templates uses [Paul Battley's htmlbeautifier gem](https://github.com/threedaymonk/htmlbeautifier). This (as well as rubygems) is assumed to be installed as seen by the ruby interpreter. Note that if you beautify and erb file but `htmlbeautifier` is not found, the error message is 'check your ruby interpreter settings', do not be misled.
If an error is encountered while processing the file, Python receives and empty string and the following message is displayed but may have nothing to do with your Ruby settings.

```
check your ruby interpreter settings
```

### Hooks

This package offers a pre-save hook, i.e., your ruby and erb files will be reformatted automatically before saving. To activate this feature, set:

Expand Down Expand Up @@ -43,6 +49,22 @@ If you use project-specific rubies and gem sets managed with `rvm`, then simply

and then the `htmlbeautifier` gem is found even if it is only installed for this project.

### Tabs or Spaces

By default, Sublime does not translate tabs to spaces. If you wish to use tabs you will not need to change your settings. If you wish to use spaces, add the following setting.

```
"translate_tabs_to_spaces": true
```

### Tab size

Sublime's default `tab_size` is set to 4. Override this setting to change the number of spaces to use when using spaces instead of tabs.

```
"tab_size": 2
```

### Key Binding

```
Expand Down
5 changes: 2 additions & 3 deletions beautify_ruby.py
Expand Up @@ -11,6 +11,8 @@ def on_pre_save(self, view):
class BeautifyRubyCommand(sublime_plugin.TextCommand):
def run(self, edit, error=True, save=True):
self.load_settings()
self.view.settings().set('translate_tabs_to_spaces', self.settings.get('translate_tabs_to_spaces'))
self.view.settings().set('tab_size', self.settings.get('tab_size'))
self.filename = self.view.file_name()
self.fname = os.path.basename(self.filename)
self.erb = self.is_erb_file()
Expand Down Expand Up @@ -58,9 +60,6 @@ def cmd(self, path = "-"):

args = ["'" + str(path) + "'"] + self.config_params()

if self.settings.get('tab_or_space') != "space":
args.insert(0, '-t')

return ruby_interpreter + " '" + ruby_script + "' " + ' '.join(args)

def finalize_output(self, text):
Expand Down
4 changes: 3 additions & 1 deletion lib/rbeautify.rb
Expand Up @@ -25,6 +25,7 @@
require_relative './rbeautify/block_matcher.rb'
require_relative './rbeautify/language.rb'
require_relative './rbeautify/line.rb'
require_relative './rbeautify/tab_size.rb'
require_relative './rbeautify/config/ruby.rb'

module RBeautify
Expand All @@ -38,7 +39,8 @@ def beautify_string(language, source, config)
language = RBeautify::Language.language(language)
end

language.indent_size = config["tab_size"].to_i
config['tab_size'] = RBeautify::TabSize.new(config).tab_size
language.indent_size = config['tab_size']

source.force_encoding("UTF-8").split("\n").each_with_index do |line_content, line_number|
line = RBeautify::Line.new(language, line_content, line_number, block, config)
Expand Down
2 changes: 1 addition & 1 deletion lib/rbeautify/line.rb
Expand Up @@ -4,7 +4,7 @@ class Line
attr_reader :language, :content, :line_number, :original_block, :block, :indent_character

def initialize(language, content, line_number, original_block = nil, config)
@tab_size = config["tab_size"].to_i
@tab_size = config["tab_size"]
@use_tabs = config["translate_tabs_to_spaces"] == 'False'
@language = language
@content = content
Expand Down
14 changes: 14 additions & 0 deletions lib/rbeautify/tab_size.rb
@@ -0,0 +1,14 @@
module RBeautify
class TabSize
DEFAULT_TAB_SIZE = 6

def initialize(config)
@tab_size = config['tab_size'].to_i
end

def tab_size
@tab_size == 0 ? DEFAULT_TAB_SIZE : @tab_size
end

end
end
4 changes: 2 additions & 2 deletions license.txt
@@ -1,6 +1,6 @@
All of BeautifyRuby is licensed under the MIT license.

Copyright (c) 2011 Craig Williams <cwilliams.allancraig@gmail.com>
Copyright (c) 2011 Craig Williams <craigwilliamsdev@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THE SOFTWARE.

0 comments on commit 4338ca2

Please sign in to comment.