Skip to content

Commit

Permalink
Refactored is? method into Token
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonl committed Jul 29, 2010
1 parent 0d12e9d commit 4734241
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
8 changes: 4 additions & 4 deletions lib/eden/formatters/block_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ def self.format( source_file )

source_file.each_line do |line|
line.tokens.each do |token|
if token.type == :lcurly
if token.is?( :lcurly )
next_token = line.next_token( token )
if next_token && next_token.content != space
if next_token.type == :whitespace
if next_token.is?(:whitespace)
next_token.content == space
else
new_space_token = Eden::Token.new(:whitespace, space)
line.insert_token_after(token, new_space_token)
end
end
elsif token.type == :rcurly
elsif token.is?( :rcurly )
prev_token = line.previous_token( token )
if prev_token && prev_token.content != space
if prev_token.type == :whitespace
if prev_token.is?( :whitespace )
prev_token.content == space
else
new_space_token = Eden::Token.new(:whitespace, space)
Expand Down
10 changes: 5 additions & 5 deletions lib/eden/formatters/indenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def self.calculate_pre_indent( line )
end
end

if t.type == :when
if t.is?( :when )
decrease_indent!
end
end
Expand All @@ -32,18 +32,18 @@ def self.calculate_post_indent( line )
if [:class, :def, :module, :do, :begin, :rescue, :if, :else, :elsif, :case].include?(t.type)
increase_indent!
line.tokens.each do |tok|
decrease_indent! if[:end].include?(tok.type)
decrease_indent! if tok.is?( :end )
end
end

if [:for, :until, :while].include?(t.type)
increase_indent!
line.tokens.each do |tok|
decrease_indent! if [:do].include?(tok.type)
decrease_indent! if tok.is?( :do )
end
end

if t.type == :when
if t.is?(:when)
increase_indent!
end
end
Expand All @@ -54,7 +54,7 @@ def self.adjust_indent( line )
if @current_indent == 0
line.tokens.delete_at(0) if line.tokens[0].type == :whitespace
else
if line.tokens[0].type == :whitespace
if line.tokens[0].is?( :whitespace )
line.tokens[0].content = indent_content
else
indent_token = Eden::Token.new( :whitespace, indent_content )
Expand Down
4 changes: 2 additions & 2 deletions lib/eden/formatters/white_space_cleaner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ def self.format( source_file )
source_file.each_line do |l|
i = l.tokens.size - 1
while( i >= 0 )
break unless (l.tokens[i].type == :whitespace || l.tokens[i].type == :newline)
l.tokens.delete_at(i) if l.tokens[i].type == :whitespace
break unless l.tokens[i].is?( :whitespace ) || l.tokens[i].is?( :newline )
l.tokens.delete_at(i) if l.tokens[i].is?( :whitespace )
i -= 1
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/eden/token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,9 @@ def binary_operator?
def keyword?
KEYWORDS.include?( type )
end

def is?( token_type )
@type == token_type
end
end
end

0 comments on commit 4734241

Please sign in to comment.