ruby_indentation 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +22 -0
- data/README +10 -0
- data/Rakefile +28 -0
- data/lib/ruby_indentation.rb +37 -0
- data/ruby_indentation.gemspec +17 -0
- metadata +64 -0
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT LICENSE
|
2
|
+
|
3
|
+
Copyright (c) 2011-2012 Jan Lelis
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
GEMSPEC = Dir['*.gemspec'].first
|
2
|
+
require 'fileutils'
|
3
|
+
|
4
|
+
def gemspec
|
5
|
+
@gemspec ||= eval(File.read(GEMSPEC), binding, GEMSPEC)
|
6
|
+
end
|
7
|
+
|
8
|
+
desc "Build the gem"
|
9
|
+
task :gem => :gemspec do
|
10
|
+
sh "gem build #{GEMSPEC}"
|
11
|
+
FileUtils.mkdir_p 'pkg'
|
12
|
+
FileUtils.mv "#{gemspec.name}-#{gemspec.version}.gem", 'pkg'
|
13
|
+
end
|
14
|
+
|
15
|
+
desc "Install the gem locally"
|
16
|
+
task :install => :gem do
|
17
|
+
sh %{gem install pkg/#{gemspec.name}-#{gemspec.version}}
|
18
|
+
end
|
19
|
+
|
20
|
+
desc "Generate the gemspec"
|
21
|
+
task :generate do
|
22
|
+
puts gemspec.to_ruby
|
23
|
+
end
|
24
|
+
|
25
|
+
desc "Validate the gemspec"
|
26
|
+
task :gemspec do
|
27
|
+
gemspec.validate
|
28
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'coderay'
|
2
|
+
require 'set'
|
3
|
+
|
4
|
+
module RubyIndentation
|
5
|
+
VERSION = '0.2.0'
|
6
|
+
|
7
|
+
def self.[](buffer)
|
8
|
+
opening_and_modifier_tokens = %w[if unless until while].to_set
|
9
|
+
opening_tokens = %w[begin case class def for module do {].to_set
|
10
|
+
closing_tokens = %w[end }].to_set
|
11
|
+
separator = [';', :operator]
|
12
|
+
indent = 0
|
13
|
+
|
14
|
+
# parse each token
|
15
|
+
buffer_tokens = [separator] + CodeRay.scan(buffer, :ruby).each_slice(2).select{|_, kind|
|
16
|
+
kind != :space
|
17
|
+
}
|
18
|
+
|
19
|
+
buffer_tokens.each_cons(2){ |(*old_pair), (token, kind)|
|
20
|
+
if kind == :keyword || kind == :operator
|
21
|
+
# modifiers cause trouble, so
|
22
|
+
# fix it in 9/10 cases
|
23
|
+
if opening_tokens.include?(token) ||
|
24
|
+
opening_and_modifier_tokens.include?(token) &&
|
25
|
+
( old_pair == separator || old_pair == ['=', :operator ] )
|
26
|
+
indent += 1
|
27
|
+
elsif closing_tokens.include?(token)
|
28
|
+
indent -= 1
|
29
|
+
end
|
30
|
+
end
|
31
|
+
}
|
32
|
+
# return a good value
|
33
|
+
indent < 0 ? 0 : indent
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# J-_-L
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: ascii -*-
|
2
|
+
require 'rubygems' unless defined? Gem
|
3
|
+
require File.dirname(__FILE__) + "/lib/ruby_indentation"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "ruby_indentation"
|
7
|
+
s.version = RubyIndentation::VERSION
|
8
|
+
s.authors = ["Jan Lelis"]
|
9
|
+
s.email = "mail@janlelis.de"
|
10
|
+
s.homepage = "https://gist.github.com/1603460"
|
11
|
+
s.summary = "Input: Ruby code. Output: Indentation level."
|
12
|
+
s.description = "Input: Ruby code. Output: Indentation level. Usage: RubyIndentation['# ruby code']"
|
13
|
+
s.add_dependency 'coderay', '~> 1.0.5'
|
14
|
+
s.files = Dir.glob(%w[{lib,test}/**/*.rb bin/* [A-Z]*.{txt,rdoc} ext/**/*.{rb,c}]) + %W[Rakefile #{__FILE__}]
|
15
|
+
s.extra_rdoc_files = ["README", "LICENSE"]
|
16
|
+
s.license = 'MIT'
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby_indentation
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jan Lelis
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-01-12 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: coderay
|
16
|
+
requirement: &18005920 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.0.5
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *18005920
|
25
|
+
description: ! 'Input: Ruby code. Output: Indentation level. Usage: RubyIndentation[''#
|
26
|
+
ruby code'']'
|
27
|
+
email: mail@janlelis.de
|
28
|
+
executables: []
|
29
|
+
extensions: []
|
30
|
+
extra_rdoc_files:
|
31
|
+
- README
|
32
|
+
- LICENSE
|
33
|
+
files:
|
34
|
+
- lib/ruby_indentation.rb
|
35
|
+
- Rakefile
|
36
|
+
- ruby_indentation.gemspec
|
37
|
+
- README
|
38
|
+
- LICENSE
|
39
|
+
homepage: https://gist.github.com/1603460
|
40
|
+
licenses:
|
41
|
+
- MIT
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ! '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
requirements: []
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 1.8.11
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: ! 'Input: Ruby code. Output: Indentation level.'
|
64
|
+
test_files: []
|