Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: crystal-lang/crystal
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 8387828f0dfe
Choose a base ref
...
head repository: crystal-lang/crystal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 3db3586498f7
Choose a head ref
  • 2 commits
  • 3 files changed
  • 1 contributor

Commits on Mar 3, 2017

  1. Copy the full SHA
    957c701 View commit details
  2. Compiler: fix wrong method call

    Ary Borenszweig committed Mar 3, 2017
    Copy the full SHA
    3db3586 View commit details
Showing with 43 additions and 25 deletions.
  1. +7 −0 spec/compiler/formatter/formatter_spec.cr
  2. +1 −1 src/compiler/crystal/codegen/types.cr
  3. +35 −24 src/compiler/crystal/tools/formatter.cr
7 changes: 7 additions & 0 deletions spec/compiler/formatter/formatter_spec.cr
Original file line number Diff line number Diff line change
@@ -1007,4 +1007,11 @@ describe Crystal::Formatter do

assert_format "<<-HEREDOC\n \#{foo}\n H\#{bar}\n HEREDOC"
assert_format "foo[a, b: 2]"

assert_format "def a\n {\n 1, # x\n # y\n }\nend"
assert_format "def a\n [\n 1, # x\n # y\n ]\nend"
assert_format "def a\n b(\n 1, # x\n # y\n )\nend"
assert_format "def a\n b(\n 1, # x\n # y\n 2\n )\nend"
assert_format "def a\n b(\n a: 1, # x\n # y\n b: 2\n )\nend"
assert_format "def a\n b(\n 1, # x\n # y\n a: 1, # x\n # y\n b: 2 # z\n )\nend"
end
2 changes: 1 addition & 1 deletion src/compiler/crystal/codegen/types.cr
Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@ module Crystal
# `malloc` in `Pointer.malloc` for a tiny performance boost.
def has_inner_pointers?
case self
when void?
when .void?
# We consider Void to have pointers, so doing
# Pointer(Void).malloc(...).as(ReferenceType)
# will consider potential inner pointers as such.
59 changes: 35 additions & 24 deletions src/compiler/crystal/tools/formatter.cr
Original file line number Diff line number Diff line change
@@ -738,13 +738,13 @@ module Crystal

last = last?(i, elements)

found_comment = skip_space(write_comma: last && has_newlines)
found_comment = skip_space(offset, write_comma: last && has_newlines)

if @token.type == :","
write "," unless last || found_comment
slash_is_regex!
next_token
found_comment = skip_space(write_comma: last && has_newlines)
found_comment = skip_space(offset, write_comma: last && has_newlines)
if @token.type == :NEWLINE
if last && !found_comment
write ","
@@ -1172,10 +1172,10 @@ module Crystal
indent(@column, node.cond)
end

indent(@indent + 2) { skip_space }
skip_space(@indent + 2)
skip_semicolon
format_nested node.then
indent(@indent + 2) { skip_space_or_newline last: true }
skip_space_or_newline(@indent + 2, last: true)
jump_semicolon

node_else = node.else
@@ -1184,7 +1184,7 @@ module Crystal
write_indent
write "else"
next_token
indent(@indent + 2) { skip_space }
skip_space(@indent + 2)
skip_semicolon
format_nested node.else
elsif node_else.is_a?(If) && @token.keyword?(:elsif)
@@ -1244,7 +1244,7 @@ module Crystal
end

def format_nested_with_end(node, column = @indent, write_end_line = true)
indent(column + 2) { skip_space }
skip_space(column + 2)

if @token.type == :";"
if node.is_a?(Nop)
@@ -1263,7 +1263,7 @@ module Crystal
end

def format_end(column)
indent(column + 2) { skip_space_or_newline last: true }
skip_space_or_newline(column + 2, last: true)
check_end
write_indent(column)
write "end"
@@ -1358,7 +1358,7 @@ module Crystal
if @token.type == :NEWLINE
write_line
write_indent(prefix_size)
indent(prefix_size) { skip_space_or_newline }
skip_space_or_newline(prefix_size)
end

if double_splat
@@ -1408,7 +1408,7 @@ module Crystal
next_token_skip_space
if @token.type == :NEWLINE
write_line
indent(prefix_size) { skip_space_or_newline }
skip_space_or_newline(prefix_size)
next_needs_indent = true
end
skip_space_or_newline
@@ -2318,7 +2318,7 @@ module Crystal
end

if named_args
has_newlines, named_args_found_comment, needed_indent = format_named_args(args, named_args, needed_indent) if named_args
has_newlines, named_args_found_comment, needed_indent = format_named_args(args, named_args, needed_indent)
found_comment = true if args.empty? && named_args_found_comment
end

@@ -2365,7 +2365,7 @@ module Crystal
end
slash_is_regex!
write_token :","
found_comment = skip_space
found_comment = skip_space(needed_indent)
if found_comment
write_indent(needed_indent)
else
@@ -2385,15 +2385,16 @@ module Crystal
end

def format_named_args(args, named_args, needed_indent)
skip_space
skip_space(needed_indent)

named_args_column = needed_indent

if args.empty?
else
write_token :","
skip_space
if @token.type == :NEWLINE
found_comment = skip_space(needed_indent)
if found_comment || @token.type == :NEWLINE
write_indent(needed_indent)
else
write " "
end
@@ -2428,8 +2429,8 @@ module Crystal
if has_parentheses
if @token.type == :","
next_token
skip_space(write_comma: true)
skip_space_or_newline
found_comment |= skip_space(column + 2, write_comma: true)
found_comment |= skip_space_or_newline(column + 2)
if has_newlines
unless found_comment
write ","
@@ -3188,7 +3189,7 @@ module Crystal

node.whens.each_with_index do |a_when, i|
format_when(node, a_when, last?(i, node.whens))
indent(@indent + 2) { skip_space_or_newline }
skip_space_or_newline(@indent + 2)
end

skip_space_or_newline
@@ -3202,9 +3203,9 @@ module Crystal
write_line
next_token
end
indent(@indent + 2) { skip_space_or_newline }
skip_space_or_newline(@indent + 2)
format_nested(a_else, @indent)
indent(@indent + 2) { skip_space_or_newline }
skip_space_or_newline(@indent + 2)
else
while @token.type == :";"
next_token_skip_space
@@ -3962,7 +3963,7 @@ module Crystal
skip_space_or_newline
end

def skip_space(write_comma = false)
def skip_space(write_comma : Bool = false)
base_column = @column
has_space = false
while @token.type == :SPACE
@@ -3983,7 +3984,11 @@ module Crystal
end
end

def skip_space_or_newline(last = false, at_least_one = false)
def skip_space(indent : Int32, write_comma = false)
indent(indent) { skip_space(write_comma) }
end

def skip_space_or_newline(last : Bool = false, at_least_one : Bool = false)
just_wrote_line = @wrote_newline
base_column = @column
has_space = false
@@ -4023,6 +4028,10 @@ module Crystal
end
end

def skip_space_or_newline(indent : Int32, last : Bool = false, at_least_one : Bool = false)
indent(indent) { skip_space_or_newline(last, at_least_one) }
end

def slash_is_regex!
@lexer.slash_is_regex = true
end
@@ -4035,7 +4044,7 @@ module Crystal

def skip_nop(indent)
skip_space_write_line
indent(indent) { skip_space_or_newline }
skip_space_or_newline(indent)
end

def skip_semicolon
@@ -4144,8 +4153,9 @@ module Crystal

def indent
@indent += 2
yield
value = yield
@indent -= 2
value
end

def indent(node : ASTNode)
@@ -4155,8 +4165,9 @@ module Crystal
def indent(indent : Int)
old_indent = @indent
@indent = indent
yield
value = yield
@indent = old_indent
value
end

def indent(indent : Int, node : ASTNode | HashLiteral::Entry | NamedTupleLiteral::Entry)