ruby-beautify 0.92.2 → 0.93.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,23 +0,0 @@
1
- module RBeautify
2
-
3
- class BlockEnd
4
-
5
- attr_accessor :block_start, :offset, :match, :after_match
6
-
7
- def initialize(block_start, offset, match, after_match)
8
- self.block_start = block_start
9
- self.offset = offset
10
- self.match = match
11
- self.after_match = after_match
12
- end
13
-
14
- def end_offset
15
- offset + match.length
16
- end
17
-
18
- def end_can_also_be_start?
19
- block_start.block_matcher.end_can_also_be_start?
20
- end
21
- end
22
-
23
- end
@@ -1,153 +0,0 @@
1
- module RBeautify
2
-
3
- class BlockMatcher
4
-
5
- attr_reader :language, :name, :starts, :ends, :options
6
-
7
- def initialize(language, name, starts, ends, options = {})
8
- @language = language
9
- @name = name
10
- @starts = starts
11
- @ends = ends.nil? ? starts : ends
12
- @options = options
13
- end
14
-
15
- class << self
16
- def parse(language, original_block, line_number, string, current_offset)
17
- block_end = original_block && original_block.parse_block_end(string, current_offset)
18
-
19
- if (block_start = first_block_start(language,
20
- original_block,
21
- line_number,
22
- string,
23
- current_offset,
24
- block_end.nil? ? nil : block_end.offset))
25
-
26
- block_end = nil
27
- end
28
-
29
- if block_end
30
- # Check whether the section of the line which is a block end is also a block start
31
- if block_end.end_can_also_be_start? &&
32
- (block_start_candidate = first_block_start(language, block_end.block_start.parent, line_number, string, current_offset)) &&
33
- block_start_candidate.offset == block_end.offset
34
- block_start = block_start_candidate
35
- end
36
-
37
- end
38
-
39
- if block_start
40
- if debug
41
- puts "MATCH: '#{string.slice(0, string.length - block_start.match.length - block_start.after_match.length)}<START type=#{block_start.name}>#{block_start.match}</START>#{block_start.after_match}'"
42
- end
43
- parse(language, block_start, line_number, block_start.after_match, block_start.end_offset)
44
- elsif block_end
45
- if debug
46
- puts "MATCH: '#{string.slice(0, string.length - block_end.match.length - block_end.after_match.length)}<END>#{block_end.match}</END>#{block_end.after_match}'"
47
- end
48
- parse(language, block_end.block_start.parent, line_number, block_end.after_match, block_end.end_offset)
49
- else
50
- original_block
51
- end
52
- end
53
-
54
- def debug=(value)
55
- @debug = value
56
- end
57
-
58
- def debug
59
- @debug
60
- end
61
-
62
- private
63
- def first_block_start(language, parent_block, line_number, string, offset, maximum_offset = nil)
64
- first_block_start = nil
65
- language.matchers.each do |matcher|
66
- if matcher.can_nest?(parent_block)
67
- if (block_start_candidate = matcher.parse_block_start(string, parent_block, offset, line_number)) &&
68
- (maximum_offset.nil? || maximum_offset > block_start_candidate.offset)
69
- first_block_start = block_start_candidate
70
- maximum_offset = first_block_start.offset
71
- end
72
- end
73
- end
74
- first_block_start
75
- end
76
- end
77
-
78
- def parse_block_start(string, parent_block, offset, line_number)
79
- if !string.empty? && (match = starts.match(string))
80
- RBeautify::BlockStart.new(self, parent_block, line_number, offset + match.begin(0), match[0], match.post_match)
81
- end
82
- end
83
-
84
- def indent_end_line?(block)
85
- evaluate_option_for_block(:indent_end_line, block)
86
- end
87
-
88
- def indent_size(block)
89
- evaluate_option_for_block(:indent_size, block) || language.indent_size
90
- end
91
-
92
- # Look for blocks within the content of this one
93
- def parse_content?
94
- options[:parse_content] != false
95
- end
96
-
97
- # Indent the content of this block
98
- def format_content?
99
- options[:format_content] != false
100
- end
101
-
102
- def can_nest?(parent_block)
103
- return false unless parent_block.nil? || parent_block.parse_content?
104
-
105
- if options[:nest_only]
106
- parent_block && options[:nest_only].include?(parent_block.name)
107
- else
108
- parent_block.nil? ||
109
- options[:nest_except].nil? || !options[:nest_except].include?(parent_block.name)
110
- end
111
- end
112
-
113
- def ends?
114
- ends != false
115
- end
116
-
117
- def end_is_implicit?
118
- options[:end] == :implicit
119
- end
120
-
121
- def end_can_also_be_start?
122
- if ends == starts
123
- options[:end_can_also_be_start] == true
124
- else
125
- options[:end_can_also_be_start] != false
126
- end
127
- end
128
-
129
- def negate_ends_match?
130
- options[:negate_ends_match]
131
- end
132
-
133
- # True if blocks can contain the escape character \ which needs to be
134
- # checked for on end match
135
- def escape_character?
136
- options[:escape_character] == true
137
- end
138
-
139
- def inspect
140
- name
141
- end
142
-
143
- private
144
- def evaluate_option_for_block(key, block)
145
- if options[key] && options[key].respond_to?(:call)
146
- options[key].call(block)
147
- else
148
- options[key]
149
- end
150
- end
151
-
152
- end
153
- end
@@ -1,119 +0,0 @@
1
- module RBeautify
2
-
3
- class BlockStart
4
-
5
- attr_reader :block_matcher, :parent, :offset, :match, :after_match, :line_number
6
-
7
- class << self
8
- def first_common_ancestor(first, second)
9
- if first.nil? || second.nil?
10
- nil
11
- else
12
- (first.ancestors & second.ancestors).last
13
- end
14
- end
15
- end
16
-
17
- def initialize(block_matcher, parent, line_number, offset, match, after_match)
18
- @block_matcher = block_matcher
19
- @parent = parent
20
- @offset = offset
21
- @match = match
22
- @after_match = after_match
23
- @line_number = line_number
24
- end
25
-
26
- def end_offset
27
- offset + match.length
28
- end
29
-
30
- def parse_block_end(string, offset)
31
- block_end = parse_explicit_block_end(string, offset)
32
-
33
- # Handle case where end is implicit
34
- if block_end.nil? && end_is_implicit? && parent
35
- block_end = parent.parse_block_end(string, offset)
36
- end
37
-
38
- block_end
39
- end
40
-
41
- def format_content?
42
- block_matcher.format_content?
43
- end
44
-
45
- def parse_content?
46
- block_matcher.parse_content?
47
- end
48
-
49
- def indent_end_line?
50
- block_matcher.indent_end_line?(self)
51
- end
52
-
53
- def total_indent_size
54
- parent.nil? ? indent_size : parent.total_indent_size + indent_size
55
- end
56
-
57
- def indent_size
58
- block_matcher.indent_size(self)
59
- end
60
-
61
- def end_is_implicit?
62
- block_matcher.end_is_implicit?
63
- end
64
-
65
- def name
66
- block_matcher.name
67
- end
68
-
69
- # Returns true if strict ancestor of
70
- def strict_ancestor_of?(block_start)
71
- block_start && block_start.parent && (self == block_start.parent || strict_ancestor_of?(block_start.parent))
72
- end
73
-
74
- def ancestors
75
- if parent
76
- parent.ancestors + [self]
77
- else
78
- [self]
79
- end
80
- end
81
-
82
- private
83
- def ends?
84
- block_matcher.ends?
85
- end
86
-
87
- def negate_ends_match?
88
- block_matcher.negate_ends_match?
89
- end
90
-
91
- def escape_character?
92
- block_matcher.escape_character?
93
- end
94
-
95
- def parse_explicit_block_end(string, offset)
96
- block_end = nil
97
-
98
- if ends?
99
-
100
- if match = block_matcher.ends.match(string)
101
- unless negate_ends_match?
102
- if escape_character? &&
103
- ((escape_chars = match.pre_match.match(/\\*$/)) && (escape_chars[0].size % 2 == 1))
104
- # If there are an odd number of escape characters just before
105
- # the match then this match should be skipped
106
- return parse_explicit_block_end(match.post_match, offset + escape_chars[0].size + match[0].length)
107
- else
108
- return RBeautify::BlockEnd.new(self, offset + match.begin(0), match[0], match.post_match)
109
- end
110
- end
111
- elsif negate_ends_match?
112
- return RBeautify::BlockEnd.new(self, offset, '', string)
113
- end
114
-
115
- end
116
- end
117
-
118
- end
119
- end
@@ -1,131 +0,0 @@
1
- # define ruby language
2
-
3
- unless RBeautify::Language.language(:ruby)
4
-
5
- ruby = RBeautify::Language.add_language(:ruby)
6
-
7
- pre_keyword_boundary = '(^|[^a-z0-9A-Z:._])' # like \b but with : , . _ all added to list of exceptions
8
- start_statement_boundary = '(^|(;|=)\s*)'
9
- continue_statement_boundary = '(^|;\s*)'
10
- ruby.indent_size = 2
11
-
12
- ruby.add_matcher(:program_end, /^__END__$/, false, :format_content => false, :parse_content => false)
13
-
14
- ruby.add_matcher(:multiline_comment, /^=begin/, /^=end/, :format_content => false, :parse_content => false)
15
-
16
- ruby.add_matcher(:double_quote,
17
- /"/,
18
- /"/,
19
- :parse_content => true,
20
- :format_content => false,
21
- :escape_character => true,
22
- :nest_except => [:double_quote, :single_quote, :regex, :back_tick])
23
-
24
- # NEED TO MODIFY DOUBLE QUOTE TO BE FORMATTED to get this to work
25
- ruby.add_matcher(:interpolation,
26
- /#\{/,
27
- /\}/,
28
- :nest_only => [:double_quote, :regex, :backtick])
29
-
30
- ruby.add_matcher(:single_quote,
31
- /'/,
32
- /'/,
33
- :parse_content => false,
34
- :format_content => false,
35
- :escape_character => true,
36
- :nest_except => [:double_quote, :single_quote, :regex, :back_tick])
37
-
38
- ruby.add_matcher(:regex,
39
- /(^|((,|=|~)\s*))\//, # Try to distinguish it from division sign
40
- /\//,
41
- :format_content => false,
42
- :escape_character => true,
43
- :end_can_also_be_start => false,
44
- :nest_except => [:double_quote, :single_quote, :regex, :back_tick])
45
-
46
- ruby.add_matcher(:back_tick,
47
- /`/,
48
- /`/,
49
- :format_content => false,
50
- :escape_character => true,
51
- :nest_except => [:double_quote, :single_quote, :regex, :back_tick])
52
-
53
- ruby.add_matcher(:standard,
54
- /((#{start_statement_boundary}(module|class|def))|#{pre_keyword_boundary}do)\b/,
55
- /(((^|;|\s)end)|#{continue_statement_boundary}(rescue|ensure))\b/,
56
- :nest_except => [:double_quote, :regex, :backtick])
57
-
58
- ruby.add_matcher(:more,
59
- /#{start_statement_boundary}(until|for|while)\b/,
60
- /(((^|;|\s)end)|#{continue_statement_boundary}(rescue|ensure))\b/,
61
- :nest_except => [:double_quote, :regex, :backtick])
62
-
63
- ruby.add_matcher(:begin,
64
- /((#{start_statement_boundary}begin)|(#{continue_statement_boundary}(ensure|rescue)))\b/,
65
- /(((^|;|\s)end)|#{continue_statement_boundary}(rescue|ensure|else))\b/,
66
- :nest_except => [:double_quote, :regex, :backtick])
67
-
68
- ruby.add_matcher(:if,
69
- /((#{start_statement_boundary}(if|unless))|#{continue_statement_boundary}(then|elsif|else))\b/,
70
- /(((^|;|\s)end)|(#{continue_statement_boundary}(then|elsif|else)))\b/,
71
- :nest_except => [:case, :double_quote, :regex, :backtick])
72
-
73
- ruby.add_matcher(:case,
74
- /#{pre_keyword_boundary}case\b/,
75
- /(^|;|\s)end\b/,
76
- :nest_except => [:double_quote, :regex, :backtick],
77
- :indent_size => 0)
78
-
79
- ruby.add_matcher(:inner_case,
80
- /#{continue_statement_boundary}(when|else|then)\b/,
81
- /#{continue_statement_boundary}(when|else|then)\b/,
82
- :nest_only => [:case],
83
- :end => :implicit,
84
- :end_can_also_be_start => true,
85
- :nest_except => [:double_quote, :regex, :backtick])
86
-
87
- # TODO: Improve the check that this is not a block with arguments. Will
88
- # currently match any bracket followed by spaces and |.
89
- bracket_indent_end_line_proc = Proc.new { |block| !block.after_match.empty? && !block.after_match.match(/^\|/) }
90
- bracket_indent_size_proc = Proc.new do |block|
91
- if bracket_indent_end_line_proc.call(block)
92
- strict_ancestors_on_same_line = block.ancestors.select { |a| a != block && a.line_number == block.line_number }
93
- block.end_offset - strict_ancestors_on_same_line.inject(0) { |sum, a| sum + a.indent_size }
94
- end
95
- end
96
-
97
- ruby.add_matcher(:curly_bracket,
98
- /\{\s*/,
99
- /\}/,
100
- :indent_end_line => bracket_indent_end_line_proc,
101
- :indent_size => bracket_indent_size_proc,
102
- :nest_except => [:double_quote, :regex, :backtick])
103
-
104
- ruby.add_matcher(:round_bracket,
105
- /\(\s*/,
106
- /\)/,
107
- :indent_end_line => bracket_indent_end_line_proc,
108
- :indent_size => bracket_indent_size_proc,
109
- :nest_except => [:double_quote, :regex, :backtick])
110
-
111
- ruby.add_matcher(:square_bracket,
112
- /\[\s*/,
113
- /\]/,
114
- :indent_end_line => bracket_indent_end_line_proc,
115
- :indent_size => bracket_indent_size_proc,
116
- :nest_except => [:double_quote, :regex, :backtick])
117
-
118
- ruby.add_matcher(:comment, /(\s*)?#/,
119
- /$/,
120
- :parse_content => false,
121
- :format_content => false,
122
- :nest_except => [:double_quote, :single_quote, :regex, :back_tick])
123
-
124
- ruby.add_matcher(:continuing_line,
125
- /(,|\.|\+|-|=\>|=|&&|\|\||\\|==|\s\?|:|<<)(\s*)?(#.*)?$/,
126
- /(^|(,|\.|\+|-|=\>|=|&&|\|\||\\|==|\s\?|:|<<)(\s*)?)(#.*)?$/,
127
- :indent_end_line => true,
128
- :negate_ends_match => true,
129
- :nest_except => [:continuing_line, :curly_bracket, :round_bracket, :square_bracket, :double_quote, :single_quote, :regex, :back_tick])
130
-
131
- end
@@ -1,37 +0,0 @@
1
- module RBeautify
2
- class Language
3
-
4
- @@languages = {}
5
-
6
- attr_reader :matchers
7
- attr_accessor :indent_size
8
-
9
- class << self
10
-
11
- def language(name)
12
- languages[name]
13
- end
14
-
15
- def languages
16
- @@languages
17
- end
18
-
19
- def add_language(name)
20
- languages[name] = new()
21
- end
22
- end
23
-
24
- def initialize
25
- @matchers = []
26
- end
27
-
28
- def add_matcher(name, starts, ends, options = {})
29
- self.matchers << BlockMatcher.new(self, name, starts, ends, options)
30
- end
31
-
32
- def matcher(name)
33
- self.matchers.detect { |matcher| matcher.name == name}
34
- end
35
-
36
- end
37
- end