indentr 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +3 -0
- data/README +4 -0
- data/Rakefile +53 -0
- data/lib/indentr.rb +78 -0
- data/lib/intentr/lang/ruby.rb +10 -0
- data/lib/intentr/version.rb +3 -0
- data/spec/indentr_spec.rb +56 -0
- metadata +85 -0
data/LICENSE
ADDED
data/README
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
#
|
2
|
+
# To change this template, choose Tools | Templates
|
3
|
+
# and open the template in the editor.
|
4
|
+
|
5
|
+
|
6
|
+
require 'rubygems'
|
7
|
+
require 'rake'
|
8
|
+
require 'rake/clean'
|
9
|
+
require 'rake/gempackagetask'
|
10
|
+
require 'rake/rdoctask'
|
11
|
+
require 'rake/testtask'
|
12
|
+
require 'spec/rake/spectask'
|
13
|
+
|
14
|
+
spec = Gem::Specification.new do |s|
|
15
|
+
s.name = 'indentr'
|
16
|
+
s.version = '0.0.1'
|
17
|
+
s.has_rdoc = true
|
18
|
+
s.extra_rdoc_files = ['README', 'LICENSE']
|
19
|
+
s.summary = 'simple ruby code indenter'
|
20
|
+
s.description = s.summary
|
21
|
+
s.author = 'YuZhenpin'
|
22
|
+
s.email = 'yuzhenpin@126.com'
|
23
|
+
# s.executables = ['your_executable_here']
|
24
|
+
s.files = %w(LICENSE README Rakefile) + Dir.glob("{bin,lib,spec}/**/*")
|
25
|
+
s.require_path = "lib"
|
26
|
+
s.bindir = "bin"
|
27
|
+
s.homepage = "https://yuzhenpin-nemo.googlecode.com/svn/trunk/indentr"
|
28
|
+
s.add_dependency(%q<coderay>, [">= 0"])
|
29
|
+
end
|
30
|
+
|
31
|
+
Rake::GemPackageTask.new(spec) do |p|
|
32
|
+
p.gem_spec = spec
|
33
|
+
p.need_tar = true
|
34
|
+
p.need_zip = true
|
35
|
+
end
|
36
|
+
|
37
|
+
Rake::RDocTask.new do |rdoc|
|
38
|
+
files =['README', 'LICENSE', 'lib/**/*.rb']
|
39
|
+
rdoc.rdoc_files.add(files)
|
40
|
+
rdoc.main = "README" # page to start on
|
41
|
+
rdoc.title = "indentr Docs"
|
42
|
+
rdoc.rdoc_dir = 'doc/rdoc' # rdoc output folder
|
43
|
+
rdoc.options << '--line-numbers'
|
44
|
+
end
|
45
|
+
|
46
|
+
Rake::TestTask.new do |t|
|
47
|
+
t.test_files = FileList['test/**/*.rb']
|
48
|
+
end
|
49
|
+
|
50
|
+
Spec::Rake::SpecTask.new do |t|
|
51
|
+
t.spec_files = FileList['spec/**/*.rb']
|
52
|
+
t.libs << Dir["lib"]
|
53
|
+
end
|
data/lib/indentr.rb
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "coderay"
|
3
|
+
require 'set'
|
4
|
+
|
5
|
+
class IndentR
|
6
|
+
def initialize(buffer, opt = {:indent => 2, :lang => :ruby})
|
7
|
+
@lang = opt[:lang] || :ruby
|
8
|
+
@indent = opt[:indent] || 2
|
9
|
+
@opening_and_modifier_tokens = %w[if unless until while].to_set
|
10
|
+
@opening_tokens = %w[begin case class def for module do {].to_set
|
11
|
+
@closing_tokens = %w[end }].to_set
|
12
|
+
@separator = [';', :operator]
|
13
|
+
|
14
|
+
@coderay_proxy = CodeRay.scan(buffer, @lang)
|
15
|
+
end
|
16
|
+
|
17
|
+
def buffer_tokens
|
18
|
+
buf_t = []
|
19
|
+
index = 0
|
20
|
+
slice = nil
|
21
|
+
@coderay_proxy.each do |item|
|
22
|
+
if 1 == index % 2
|
23
|
+
buf_t << [slice, item]
|
24
|
+
else
|
25
|
+
slice = item
|
26
|
+
end
|
27
|
+
index += 1
|
28
|
+
end
|
29
|
+
|
30
|
+
return buf_t
|
31
|
+
end
|
32
|
+
|
33
|
+
def code
|
34
|
+
|
35
|
+
indent_tokens = []
|
36
|
+
indent = 0
|
37
|
+
newline = false
|
38
|
+
buffer_tokens.each {|token, kind|
|
39
|
+
|
40
|
+
if kind == :keyword || kind == :operator
|
41
|
+
if @closing_tokens.include?(token)
|
42
|
+
indent -= 1
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
if kind == :space && token == "\n"
|
47
|
+
indent_tokens << token
|
48
|
+
newline = true
|
49
|
+
elsif kind == :space
|
50
|
+
if newline
|
51
|
+
indent_tokens << (" " * indent * @indent)
|
52
|
+
else
|
53
|
+
indent_tokens << token
|
54
|
+
end
|
55
|
+
|
56
|
+
newline = false
|
57
|
+
else
|
58
|
+
|
59
|
+
if newline
|
60
|
+
indent_tokens << (" " * indent * @indent) + token
|
61
|
+
else
|
62
|
+
indent_tokens << token
|
63
|
+
end
|
64
|
+
newline = false
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
if kind == :keyword || kind == :operator
|
69
|
+
if @opening_tokens.include?(token) || \
|
70
|
+
@opening_and_modifier_tokens.include?(token)
|
71
|
+
indent += 1
|
72
|
+
end
|
73
|
+
end
|
74
|
+
}
|
75
|
+
|
76
|
+
indent_tokens.join("")
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
class IndentR
|
2
|
+
class Ruby
|
3
|
+
def initialize
|
4
|
+
@opening_and_modifier_tokens = %w[if unless until while].to_set
|
5
|
+
@opening_tokens = %w[begin case class def for module do {].to_set
|
6
|
+
@closing_tokens = %w[end }].to_set
|
7
|
+
@separator = [';', :operator]
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# To change this template, choose Tools | Templates
|
2
|
+
# and open the template in the editor.
|
3
|
+
|
4
|
+
require "rubygems"
|
5
|
+
libpath = File.expand_path(File.dirname(__FILE__) + "/../lib")
|
6
|
+
$LOAD_PATH.unshift libpath unless $LOAD_PATH.include?(libpath)
|
7
|
+
require 'indentr'
|
8
|
+
|
9
|
+
describe "Indentr" do
|
10
|
+
before(:each) do
|
11
|
+
buffer = %Q{class A
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
def bbb
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
=begin
|
20
|
+
a
|
21
|
+
ab
|
22
|
+
c
|
23
|
+
d
|
24
|
+
e
|
25
|
+
f
|
26
|
+
=end
|
27
|
+
|
28
|
+
}
|
29
|
+
|
30
|
+
@indentr = IndentR.new(buffer)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should desc" do
|
34
|
+
# TODO
|
35
|
+
code = %{class A
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
def bbb
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
=begin
|
44
|
+
a
|
45
|
+
ab
|
46
|
+
c
|
47
|
+
d
|
48
|
+
e
|
49
|
+
f
|
50
|
+
=end
|
51
|
+
|
52
|
+
}
|
53
|
+
@indentr.code.should == code
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
metadata
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: indentr
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- YuZhenpin
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-05-03 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: coderay
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
34
|
+
description: simple ruby code indenter
|
35
|
+
email: yuzhenpin@126.com
|
36
|
+
executables: []
|
37
|
+
|
38
|
+
extensions: []
|
39
|
+
|
40
|
+
extra_rdoc_files:
|
41
|
+
- README
|
42
|
+
- LICENSE
|
43
|
+
files:
|
44
|
+
- LICENSE
|
45
|
+
- README
|
46
|
+
- Rakefile
|
47
|
+
- lib/indentr.rb
|
48
|
+
- lib/intentr/lang/ruby.rb
|
49
|
+
- lib/intentr/version.rb
|
50
|
+
- spec/indentr_spec.rb
|
51
|
+
homepage: https://yuzhenpin-nemo.googlecode.com/svn/trunk/indentr
|
52
|
+
licenses: []
|
53
|
+
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
hash: 3
|
65
|
+
segments:
|
66
|
+
- 0
|
67
|
+
version: "0"
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
hash: 3
|
74
|
+
segments:
|
75
|
+
- 0
|
76
|
+
version: "0"
|
77
|
+
requirements: []
|
78
|
+
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 1.8.22
|
81
|
+
signing_key:
|
82
|
+
specification_version: 3
|
83
|
+
summary: simple ruby code indenter
|
84
|
+
test_files: []
|
85
|
+
|