format 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/Gemfile +19 -0
- data/Gemfile.lock +34 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +19 -0
- data/Rakefile +57 -16
- data/VERSION +1 -0
- data/bin/format +26 -12
- data/format.gemspec +59 -0
- data/lib/format.rb +179 -62
- data/test/helper.rb +18 -0
- data/test/test.java +18 -0
- data/test/test.java.correct_ugly +27 -0
- data/test/test_format.rb +17 -0
- metadata +91 -47
- data/tests/tc_format.rb +0 -16
data/.document
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
|
9
|
+
group :test do
|
10
|
+
gem "shoulda", "~> 3.1.1"
|
11
|
+
end
|
12
|
+
|
13
|
+
group :development do
|
14
|
+
# gem "rdoc", "~> 3.12"
|
15
|
+
gem "yard", "~> 0.8.2.1"
|
16
|
+
gem "bundler", "~> 1.1.4"
|
17
|
+
gem "jeweler", "~> 1.8.4"
|
18
|
+
# gem "rcov", ">= 0"
|
19
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
activesupport (3.2.8)
|
5
|
+
i18n (~> 0.6)
|
6
|
+
multi_json (~> 1.0)
|
7
|
+
git (1.2.5)
|
8
|
+
i18n (0.6.1)
|
9
|
+
jeweler (1.8.4)
|
10
|
+
bundler (~> 1.0)
|
11
|
+
git (>= 1.2.5)
|
12
|
+
rake
|
13
|
+
rdoc
|
14
|
+
json (1.7.5)
|
15
|
+
multi_json (1.3.6)
|
16
|
+
rake (0.9.2.2)
|
17
|
+
rdoc (3.12)
|
18
|
+
json (~> 1.4)
|
19
|
+
shoulda (3.1.1)
|
20
|
+
shoulda-context (~> 1.0)
|
21
|
+
shoulda-matchers (~> 1.2)
|
22
|
+
shoulda-context (1.0.0)
|
23
|
+
shoulda-matchers (1.3.0)
|
24
|
+
activesupport (>= 3.0.0)
|
25
|
+
yard (0.8.2.1)
|
26
|
+
|
27
|
+
PLATFORMS
|
28
|
+
ruby
|
29
|
+
|
30
|
+
DEPENDENCIES
|
31
|
+
bundler (~> 1.1.4)
|
32
|
+
jeweler (~> 1.8.4)
|
33
|
+
shoulda (~> 3.1.1)
|
34
|
+
yard (~> 0.8.2.1)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Arcterus
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
= format
|
2
|
+
|
3
|
+
A formatter for several popular indentation/bracketing styles.
|
4
|
+
|
5
|
+
== Contributing to format
|
6
|
+
|
7
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
8
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
9
|
+
* Fork the project.
|
10
|
+
* Start a feature/bugfix branch.
|
11
|
+
* Commit and push until you are happy with your contribution.
|
12
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
13
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2012 Arcterus. See LICENSE.txt for
|
18
|
+
further details.
|
19
|
+
|
data/Rakefile
CHANGED
@@ -1,27 +1,68 @@
|
|
1
|
-
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
|
-
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
4
13
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "format"
|
18
|
+
gem.homepage = "http://github.com/Arcterus/format"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{A formatter for the UNIX, Java, and Allman indentation/bracketing styles.}
|
21
|
+
gem.description = %Q{A formatter for the UNIX, Java, and Allman indentation/bracketing styles.}
|
22
|
+
gem.email = "arcterus@mail.com"
|
23
|
+
gem.authors = ["Arcterus"]
|
24
|
+
gem.executables = 'format'
|
25
|
+
# dependencies defined in Gemfile
|
9
26
|
end
|
27
|
+
Jeweler::RubygemsDotOrgTasks.new
|
10
28
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
29
|
+
require 'rake/testtask'
|
30
|
+
Rake::TestTask.new(:test) do |test|
|
31
|
+
test.libs << '.' << '../test'
|
32
|
+
test.pattern = '../test/**/test_*.rb'
|
33
|
+
test.verbose = true
|
15
34
|
end
|
16
35
|
|
17
|
-
|
18
|
-
|
36
|
+
=begin
|
37
|
+
require 'rcov/rcovtask'
|
38
|
+
Rcov::RcovTask.new do |test|
|
39
|
+
test.libs << 'test'
|
40
|
+
test.pattern = 'test/**/test_*.rb'
|
41
|
+
test.verbose = true
|
42
|
+
test.rcov_opts << '--exclude "gems/*"'
|
19
43
|
end
|
44
|
+
=end
|
20
45
|
|
21
|
-
task :
|
22
|
-
|
46
|
+
task :default => :test
|
47
|
+
|
48
|
+
=begin
|
49
|
+
require 'rdoc/task'
|
50
|
+
Rake::RDocTask.new do |rdoc|
|
51
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
52
|
+
|
53
|
+
rdoc.rdoc_dir = 'rdoc'
|
54
|
+
rdoc.title = "format #{version}"
|
55
|
+
rdoc.rdoc_files.include('README*')
|
56
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
23
57
|
end
|
58
|
+
=end
|
59
|
+
|
60
|
+
require 'yard'
|
61
|
+
require 'yard/rake/yardoc_task'
|
62
|
+
|
63
|
+
YARD::Rake::YardocTask.new do |yard|
|
64
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ''
|
24
65
|
|
25
|
-
|
26
|
-
|
66
|
+
yard.files = ['lib/**/*.rb', 'README*']
|
67
|
+
yard.options = ['--no-private', '--protected', '--title', "\"format #{version}\"", '-o', 'doc']
|
27
68
|
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/bin/format
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
+
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
|
4
|
+
|
3
5
|
require 'format'
|
4
6
|
|
5
|
-
|
6
|
-
|
7
|
-
indent = nil
|
7
|
+
indent = nil
|
8
|
+
if ARGV[0] == 'java' || ARGV[0] == 'sun' || ARGV[0] == 'unix' then
|
8
9
|
case ARGV[1]
|
9
10
|
when 'tab'
|
10
11
|
indent = "\t"
|
@@ -21,26 +22,39 @@ when 'uglify'
|
|
21
22
|
indent = ' '
|
22
23
|
ARGV.insert(1, nil)
|
23
24
|
end
|
24
|
-
ARGV.
|
25
|
+
ARGV.delete_at(1)
|
26
|
+
end
|
27
|
+
case ARGV[0]
|
28
|
+
when 'java' || 'sun'
|
29
|
+
ARGV.shift
|
25
30
|
ARGV.each {|arg|
|
26
|
-
Format.
|
31
|
+
Format.format(arg, :style => Format.JAVA, :indentation => indent)
|
27
32
|
}
|
28
|
-
when '
|
33
|
+
when 'unix'
|
29
34
|
ARGV.shift
|
30
35
|
ARGV.each {|arg|
|
31
|
-
Format.
|
36
|
+
Format.format(arg, :style => Format.UNIX, :indentation => indent)
|
32
37
|
}
|
38
|
+
when 'allman'
|
39
|
+
ARGV.shift
|
40
|
+
ARGV.each {|arg|
|
41
|
+
Format.format(arg, :style => Format.ALLMAN)
|
42
|
+
}
|
43
|
+
when 'unix'
|
44
|
+
|
33
45
|
when 'help'
|
34
46
|
puts "\nHelp Menu\n---------"
|
35
|
-
puts 'format
|
47
|
+
puts 'format command [options] files'
|
36
48
|
puts "\nCommands\n--------"
|
37
|
-
puts '
|
38
|
-
puts '
|
39
|
-
puts 'help
|
40
|
-
puts "\nOptions (for '
|
49
|
+
puts 'java / sun => brackets on separate line'
|
50
|
+
puts 'allman => brackets on same line'
|
51
|
+
puts 'help => this menu'
|
52
|
+
puts "\nOptions (for 'java' / 'sun')\n----------------------------"
|
41
53
|
puts 'two_spaces => two space indentation'
|
42
54
|
puts 'three_spaces => three space indentation'
|
43
55
|
puts 'four_spaces (default) => four space indentation'
|
44
56
|
puts 'eight_spaces => eight space indentation'
|
45
57
|
puts "tab => tab indentation\n\n"
|
58
|
+
else
|
59
|
+
puts 'Invalid command. You may want to view the help menu (format help)'
|
46
60
|
end
|
data/format.gemspec
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "format"
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Arcterus"]
|
12
|
+
s.date = "2012-09-12"
|
13
|
+
s.description = "A formatter for the UNIX, Java, and Allman indentation/bracketing styles."
|
14
|
+
s.email = "arcterus@mail.com"
|
15
|
+
s.executables = ["format"]
|
16
|
+
s.extra_rdoc_files = [
|
17
|
+
"LICENSE.txt"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
"Gemfile",
|
22
|
+
"Gemfile.lock",
|
23
|
+
"LICENSE.txt",
|
24
|
+
"README.rdoc",
|
25
|
+
"Rakefile",
|
26
|
+
"VERSION",
|
27
|
+
"bin/format",
|
28
|
+
"format.gemspec",
|
29
|
+
"lib/format.rb",
|
30
|
+
"test/helper.rb",
|
31
|
+
"test/test.java",
|
32
|
+
"test/test.java.correct_ugly",
|
33
|
+
"test/test_format.rb"
|
34
|
+
]
|
35
|
+
s.homepage = "http://github.com/Arcterus/format"
|
36
|
+
s.licenses = ["MIT"]
|
37
|
+
s.require_paths = ["lib"]
|
38
|
+
s.rubygems_version = "1.8.24"
|
39
|
+
s.summary = "A formatter for the UNIX, Java, and Allman indentation/bracketing styles."
|
40
|
+
|
41
|
+
if s.respond_to? :specification_version then
|
42
|
+
s.specification_version = 3
|
43
|
+
|
44
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
45
|
+
s.add_development_dependency(%q<yard>, ["~> 0.8.2.1"])
|
46
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.1.4"])
|
47
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
|
48
|
+
else
|
49
|
+
s.add_dependency(%q<yard>, ["~> 0.8.2.1"])
|
50
|
+
s.add_dependency(%q<bundler>, ["~> 1.1.4"])
|
51
|
+
s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
|
52
|
+
end
|
53
|
+
else
|
54
|
+
s.add_dependency(%q<yard>, ["~> 0.8.2.1"])
|
55
|
+
s.add_dependency(%q<bundler>, ["~> 1.1.4"])
|
56
|
+
s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
data/lib/format.rb
CHANGED
@@ -1,39 +1,41 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
1
|
+
#
|
2
|
+
# Improvements to the String class.
|
3
|
+
#
|
4
|
+
# @author Arcterus
|
5
|
+
# @version 2012-09-11
|
6
|
+
#
|
8
7
|
class String
|
9
|
-
|
8
|
+
#
|
10
9
|
# Determines whether the String is entirely blank.
|
11
10
|
#
|
11
|
+
# @return [Boolean] whether or not the String is blank
|
12
|
+
#
|
12
13
|
def is_space
|
13
|
-
|
14
|
-
return true
|
15
|
-
else
|
16
|
-
return false
|
17
|
-
end
|
14
|
+
self =~ /^[[:blank:]]+$/ ? true : false
|
18
15
|
end
|
19
16
|
end
|
20
17
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
18
|
+
#
|
19
|
+
# Allows for C-like source code to be formatted using several
|
20
|
+
# popular bracketing styles.
|
21
|
+
#
|
22
|
+
# @author Arcterus
|
23
|
+
# @version 2012-09-11
|
24
|
+
#
|
29
25
|
class Format
|
30
26
|
private
|
31
27
|
|
32
28
|
@@keywords = ['else', 'while']
|
29
|
+
@@version = nil
|
33
30
|
|
34
|
-
|
35
|
-
# Get the leading whitespace (the indentation)
|
36
|
-
#
|
31
|
+
#
|
32
|
+
# Get the leading whitespace (the indentation) from the
|
33
|
+
# input string.
|
34
|
+
#
|
35
|
+
# @param [String] str the string from which the
|
36
|
+
# whitespace will be taken
|
37
|
+
#
|
38
|
+
# @return [String] the leading whitespace
|
37
39
|
#
|
38
40
|
def self.get_leading_space(str)
|
39
41
|
result = ''
|
@@ -47,33 +49,56 @@ class Format
|
|
47
49
|
return result
|
48
50
|
end
|
49
51
|
|
50
|
-
public
|
51
|
-
|
52
|
-
##
|
53
|
-
# Formats the input file according to the
|
54
|
-
# uglier (in my opinion) of the two styles.
|
55
52
|
#
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
53
|
+
# Format the input file using the Allman bracketing
|
54
|
+
# style.
|
55
|
+
#
|
56
|
+
# @param [String] file the name of the input file
|
57
|
+
# @param [File] output the output file
|
58
|
+
# @param [String] indent the string to be used for
|
59
|
+
# indentation
|
60
|
+
#
|
61
|
+
def self.allman(file, output, indent)
|
62
|
+
typedef = false
|
63
63
|
IO.readlines(file).each {|line|
|
64
|
-
indentlevel = self.get_leading_space(line).scan(/#{indent}/).size
|
65
|
-
if /^[[:blank:]]*}
|
64
|
+
indentlevel = self.get_leading_space(line).scan(/#{Regexp.quote(indent)}/).size
|
65
|
+
if line =~ /^[[:blank:]]*}.*$/ then
|
66
66
|
indentlevel.times do
|
67
67
|
output.print(indent)
|
68
68
|
end
|
69
69
|
output.puts('}')
|
70
70
|
line.sub!(/}[[:blank:]]*/, '')
|
71
|
-
if line.lstrip == ''
|
72
|
-
|
71
|
+
next if line.lstrip == ''
|
72
|
+
end
|
73
|
+
if line =~ /[[:graph:]][[:blank:]]*{[[:blank:]]*$/ then
|
74
|
+
output.puts(line.rstrip.chop.rstrip)
|
75
|
+
indentlevel.times do
|
76
|
+
output.print(indent)
|
73
77
|
end
|
78
|
+
output.puts('{')
|
79
|
+
else
|
80
|
+
output.puts(line)
|
74
81
|
end
|
75
|
-
|
76
|
-
|
82
|
+
}
|
83
|
+
end
|
84
|
+
|
85
|
+
#
|
86
|
+
# Format the input file using the UNIX bracketing
|
87
|
+
# style.
|
88
|
+
#
|
89
|
+
# @param [String] file the name of the input file
|
90
|
+
# @param [File] output the output file
|
91
|
+
# @param [String] indent the string to be used for
|
92
|
+
# indentation
|
93
|
+
#
|
94
|
+
def self.unix(file, output, indent)
|
95
|
+
java_output = File.new(file + '.unix.tmp', 'w+')
|
96
|
+
self.java(file, java_output, indent)
|
97
|
+
java_output.seek(0, IO::SEEK_SET)
|
98
|
+
IO.readlines(java_output).each {|line|
|
99
|
+
indentlevel = self.get_leading_space(line).scan(/#{Regexp.quote(indent)}/).size
|
100
|
+
if line =~ /^[[:blank:]]*[[:alpha:]][[:alnum:]]*[[:blank:]]+.*\(.*\)[[:blank:]]*\{[[:blank:]]*$/ then
|
101
|
+
output.puts(line.rstrip.chop.rstrip)
|
77
102
|
indentlevel.times do
|
78
103
|
output.print(indent)
|
79
104
|
end
|
@@ -82,47 +107,139 @@ class Format
|
|
82
107
|
output.puts(line)
|
83
108
|
end
|
84
109
|
}
|
85
|
-
|
110
|
+
java_output.close
|
111
|
+
File.delete(file + '.unix.tmp')
|
86
112
|
end
|
87
113
|
|
88
|
-
##
|
89
|
-
# Formats the input file according to the
|
90
|
-
# prettier (in my opinion) of the two styles.
|
91
114
|
#
|
92
|
-
|
93
|
-
|
115
|
+
# Format the input file using the Java bracketing
|
116
|
+
# style.
|
117
|
+
#
|
118
|
+
# @param [String] file the name of the input file
|
119
|
+
# @param [File] output the output file
|
120
|
+
# @param [String] indent the string to be used for
|
121
|
+
# indentation
|
122
|
+
#
|
123
|
+
def self.java(file, output, indent)
|
94
124
|
prev_line = 'IGNORE THIS LINE'
|
95
125
|
data = IO.readlines(file)
|
96
126
|
index = 0
|
97
127
|
data.each {|line|
|
98
|
-
if /^[[:blank:]]*{[[:blank:]]
|
99
|
-
!(/^[[:blank:]]*}[[:blank:]]
|
128
|
+
if line =~ /^[[:blank:]]*{[[:blank:]]*$/ and
|
129
|
+
!(prev_line =~ /^[[:blank:]]*}[[:blank:]]*$/) then
|
100
130
|
prev_line = prev_line.rstrip + ' {'
|
101
131
|
line = 'IGNORE THIS LINE'
|
102
132
|
else
|
103
133
|
@@keywords.each {|key|
|
104
|
-
if /^[[:blank:]]*#{Regexp.quote(key)}([[:blank:]]|\()
|
105
|
-
/^[[:blank:]]*}[[:blank:]]
|
134
|
+
if line =~ /^[[:blank:]]*#{Regexp.quote(key)}([[:blank:]]|\()?.*$/ and
|
135
|
+
prev_line =~ /^[[:blank:]]*}[[:blank:]]*$/ then
|
106
136
|
prev_line = prev_line.rstrip + ' ' + line.lstrip
|
107
|
-
if index + 1 < data.length
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
end
|
137
|
+
if index + 1 < data.length and
|
138
|
+
data[index + 1] =~ /^[[:blank:]]*{[[:blank:]]*$/ then
|
139
|
+
prev_line = prev_line.rstrip + ' {'
|
140
|
+
data[index + 1] = 'IGNORE THIS LINE'
|
112
141
|
end
|
113
142
|
line = 'IGNORE THIS LINE'
|
114
143
|
break
|
115
144
|
end
|
116
145
|
}
|
117
146
|
end
|
118
|
-
if prev_line != 'IGNORE THIS LINE'
|
119
|
-
output.puts(prev_line)
|
120
|
-
end
|
147
|
+
output.puts(prev_line) if prev_line != 'IGNORE THIS LINE'
|
121
148
|
prev_line = line
|
122
149
|
index += 1
|
123
150
|
}
|
124
|
-
if prev_line != 'IGNORE THIS LINE'
|
125
|
-
|
151
|
+
output.puts(prev_line) if prev_line != 'IGNORE THIS LINE'
|
152
|
+
end
|
153
|
+
|
154
|
+
def self.whitesmith(file, output, indent)
|
155
|
+
|
156
|
+
end
|
157
|
+
|
158
|
+
public
|
159
|
+
|
160
|
+
#
|
161
|
+
# Allman bracketing style
|
162
|
+
#
|
163
|
+
# @return [Integer] the number designating the
|
164
|
+
# Allman bracketing style
|
165
|
+
#
|
166
|
+
def self.ALLMAN
|
167
|
+
1
|
168
|
+
end
|
169
|
+
|
170
|
+
#
|
171
|
+
# UNIX bracketing style
|
172
|
+
#
|
173
|
+
# @return [Integer] the number designating the
|
174
|
+
# UNIX bracketing style
|
175
|
+
#
|
176
|
+
def self.UNIX
|
177
|
+
2
|
178
|
+
end
|
179
|
+
|
180
|
+
#
|
181
|
+
# Java bracketing style
|
182
|
+
#
|
183
|
+
# @return [Integer] the number designating the
|
184
|
+
# Java bracketing style
|
185
|
+
#
|
186
|
+
def self.JAVA
|
187
|
+
3
|
188
|
+
end
|
189
|
+
|
190
|
+
#
|
191
|
+
# (see ::JAVA)
|
192
|
+
#
|
193
|
+
def self.SUN
|
194
|
+
self.JAVA
|
195
|
+
end
|
196
|
+
|
197
|
+
def self.WHITESMITH
|
198
|
+
4
|
199
|
+
end
|
200
|
+
|
201
|
+
#
|
202
|
+
# Gives the current version of format.
|
203
|
+
#
|
204
|
+
# @return [String] the version of format
|
205
|
+
#
|
206
|
+
def self.VERSION
|
207
|
+
if @@version == nil then
|
208
|
+
file = File.dirname(__FILE__) + '/../VERSION'
|
209
|
+
@@version = File.exists?(file) ? File.read(file) : ''
|
210
|
+
end
|
211
|
+
@@version
|
212
|
+
end
|
213
|
+
|
214
|
+
#
|
215
|
+
# Formats the input file according to the input
|
216
|
+
# options.
|
217
|
+
#
|
218
|
+
# @param [String] file the input file name
|
219
|
+
# @param [Hash] options the options that determine
|
220
|
+
# what is to happen to the
|
221
|
+
# input file
|
222
|
+
#
|
223
|
+
def self.format(file, options={})
|
224
|
+
indent = options[:indentation]
|
225
|
+
if indent == nil then
|
226
|
+
indent = ' '
|
227
|
+
end
|
228
|
+
output = nil
|
229
|
+
case options[:style]
|
230
|
+
when self.ALLMAN
|
231
|
+
output = File.new(file + '.allman', 'w+')
|
232
|
+
self.allman(file, output, indent)
|
233
|
+
when self.UNIX
|
234
|
+
output = File.new(file + '.unix', 'w+')
|
235
|
+
self.unix(file, output, indent)
|
236
|
+
when self.JAVA
|
237
|
+
output = File.new(file + '.sun', 'w+')
|
238
|
+
self.java(file, output, indent)
|
239
|
+
else
|
240
|
+
puts 'Defaulting to Java style formatting...'
|
241
|
+
output = File.new(file + '.sun', 'w+')
|
242
|
+
self.java(file, output, indent)
|
126
243
|
end
|
127
244
|
output.close
|
128
245
|
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'test/unit'
|
11
|
+
require 'shoulda'
|
12
|
+
|
13
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
14
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
15
|
+
require 'format'
|
16
|
+
|
17
|
+
class Test::Unit::TestCase
|
18
|
+
end
|
data/test/test.java
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
public class Test {
|
2
|
+
public static void main(String[] args) {
|
3
|
+
System.out.println("hi");
|
4
|
+
if(true == false) {
|
5
|
+
// hi
|
6
|
+
} else if(true == true) {
|
7
|
+
// test
|
8
|
+
} else {
|
9
|
+
// what
|
10
|
+
}
|
11
|
+
{
|
12
|
+
// what happens here?
|
13
|
+
}
|
14
|
+
do {
|
15
|
+
|
16
|
+
} while(false);
|
17
|
+
}
|
18
|
+
}
|
@@ -0,0 +1,27 @@
|
|
1
|
+
public class Test
|
2
|
+
{
|
3
|
+
public static void main(String[] args)
|
4
|
+
{
|
5
|
+
System.out.println("hi");
|
6
|
+
if(true == false)
|
7
|
+
{
|
8
|
+
// hi
|
9
|
+
}
|
10
|
+
else if(true == true)
|
11
|
+
{
|
12
|
+
// test
|
13
|
+
}
|
14
|
+
else
|
15
|
+
{
|
16
|
+
// what
|
17
|
+
}
|
18
|
+
{
|
19
|
+
// what happens here?
|
20
|
+
}
|
21
|
+
do
|
22
|
+
{
|
23
|
+
|
24
|
+
}
|
25
|
+
while(false);
|
26
|
+
}
|
27
|
+
}
|
data/test/test_format.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestFormat < Test::Unit::TestCase
|
4
|
+
def test_simple
|
5
|
+
Dir.chdir('lib') do
|
6
|
+
Format.format(Dir.pwd + '/../test/test.java', :indentation => ' ', :style => Format.ALLMAN)
|
7
|
+
Format.format(Dir.pwd + '/../test/test.java.allman', :style => Format.JAVA)
|
8
|
+
test = IO.read(Dir.pwd + '/../test/test.java')
|
9
|
+
allman = IO.read(Dir.pwd + '/../test/test.java.allman')
|
10
|
+
sun = IO.read(Dir.pwd + '/../test/test.java.allman.sun')
|
11
|
+
correct_ugly = IO.read(Dir.pwd + '/../test/test.java.correct_ugly')
|
12
|
+
assert_equal(test, sun)
|
13
|
+
assert_equal(allman, correct_ugly)
|
14
|
+
# TODO: add (more random) tests here
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
metadata
CHANGED
@@ -1,68 +1,112 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: format
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 0
|
9
|
-
- 1
|
10
|
-
version: 0.0.1
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Arcterus
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
12
|
+
date: 2012-09-12 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: yard
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.8.2.1
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 0.8.2.1
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: bundler
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 1.1.4
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 1.1.4
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: jeweler
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 1.8.4
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.8.4
|
62
|
+
description: A formatter for the UNIX, Java, and Allman indentation/bracketing styles.
|
22
63
|
email: arcterus@mail.com
|
23
|
-
executables:
|
24
|
-
|
64
|
+
executables:
|
65
|
+
- format
|
25
66
|
extensions: []
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
67
|
+
extra_rdoc_files:
|
68
|
+
- LICENSE.txt
|
69
|
+
files:
|
70
|
+
- .document
|
71
|
+
- Gemfile
|
72
|
+
- Gemfile.lock
|
73
|
+
- LICENSE.txt
|
74
|
+
- README.rdoc
|
30
75
|
- Rakefile
|
31
|
-
-
|
76
|
+
- VERSION
|
32
77
|
- bin/format
|
33
|
-
-
|
34
|
-
|
35
|
-
|
36
|
-
|
78
|
+
- format.gemspec
|
79
|
+
- lib/format.rb
|
80
|
+
- test/helper.rb
|
81
|
+
- test/test.java
|
82
|
+
- test/test.java.correct_ugly
|
83
|
+
- test/test_format.rb
|
84
|
+
homepage: http://github.com/Arcterus/format
|
85
|
+
licenses:
|
86
|
+
- MIT
|
37
87
|
post_install_message:
|
38
88
|
rdoc_options: []
|
39
|
-
|
40
|
-
require_paths:
|
89
|
+
require_paths:
|
41
90
|
- lib
|
42
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
91
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
92
|
none: false
|
44
|
-
requirements:
|
45
|
-
- -
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
|
48
|
-
segments:
|
93
|
+
requirements:
|
94
|
+
- - ! '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
segments:
|
49
98
|
- 0
|
50
|
-
|
51
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
hash: -172312612135061892
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
101
|
none: false
|
53
|
-
requirements:
|
54
|
-
- -
|
55
|
-
- !ruby/object:Gem::Version
|
56
|
-
|
57
|
-
segments:
|
58
|
-
- 0
|
59
|
-
version: "0"
|
102
|
+
requirements:
|
103
|
+
- - ! '>='
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
60
106
|
requirements: []
|
61
|
-
|
62
107
|
rubyforge_project:
|
63
|
-
rubygems_version: 1.8.
|
108
|
+
rubygems_version: 1.8.24
|
64
109
|
signing_key:
|
65
110
|
specification_version: 3
|
66
|
-
summary:
|
67
|
-
test_files:
|
68
|
-
- tests/tc_format.rb
|
111
|
+
summary: A formatter for the UNIX, Java, and Allman indentation/bracketing styles.
|
112
|
+
test_files: []
|
data/tests/tc_format.rb
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
require '../format'
|
2
|
-
require 'test/unit'
|
3
|
-
|
4
|
-
class TestFormat < Test::Unit::TestCase
|
5
|
-
def test_simple
|
6
|
-
Format.uglify('test.java', :indentation => ' ')
|
7
|
-
Format.prettify('test.java.ugly')
|
8
|
-
test = IO.read('test.java')
|
9
|
-
ugly = IO.read('test.java.ugly')
|
10
|
-
pretty = IO.read('test.java.ugly.pretty')
|
11
|
-
correct_ugly = IO.read('test.java.correct_ugly')
|
12
|
-
assert_equal(test, pretty)
|
13
|
-
assert_equal(ugly, correct_ugly)
|
14
|
-
# TODO: add (more random) tests here
|
15
|
-
end
|
16
|
-
end
|