Skip to content

Commit

Permalink
Strip trailing newlines
Browse files Browse the repository at this point in the history
  • Loading branch information
Keluaa committed Apr 1, 2024
1 parent bf0ff93 commit 48ecd70
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ top: ┃ top:
ret i64 %1 ⟪╋⟫ ret i64 %2
┣⟫ %1 = sext i8 %0 to i64
} ┃ }

julia> f2(a) = a - 1
f2 (generic function with 1 method)
Expand All @@ -33,7 +32,6 @@ top: ┃ top:
%1 = add i64 %0, 1 ⟪╋⟫ %1 = add i64 %0, -1
ret i64 %1 ┃ ret i64 %1
} ┃ }
```

## Supported languages
Expand Down
2 changes: 0 additions & 2 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ top: ┃ top:
ret i64 %1 ⟪╋⟫ ret i64 %2
┣⟫ %1 = sext i8 %0 to i64
} ┃ }
julia> f2(a) = a - 1
f2 (generic function with 1 method)
Expand All @@ -40,7 +39,6 @@ top: ┃ top:
%1 = add i64 %0, 1 ⟪╋⟫ %1 = add i64 %0, -1
ret i64 %1 ┃ ret i64 %1
} ┃ }
```

Setting the environment variable `"CODE_DIFFS_LINE_NUMBERS"` to `true` will display line
Expand Down
14 changes: 14 additions & 0 deletions src/compare.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ function compare_code(code₁::AbstractString, code₂::AbstractString, highligh
code₂_colored = code₂
end

if endswith(code₁, '\n') && endswith(code₂, '\n')
code₁ = rstrip(==('\n'), code₁)
code₁_colored = rstrip(==('\n'), code₁_colored)
code₂ = rstrip(==('\n'), code₂)
code₂_colored = rstrip(==('\n'), code₂_colored)
end

diff = CodeDiff(code₁, code₂, code₁_colored, code₂_colored)
optimize_line_changes!(diff)
return diff
Expand Down Expand Up @@ -134,6 +141,13 @@ function compare_show(code₁, code₂; color=true, force_no_ansi=false)
code₂_colored = code_str₂
end

if endswith(code_str₁, '\n') && endswith(code_str₂, '\n')
code_str₁ = rstrip(==('\n'), code_str₁)
code₁_colored = rstrip(==('\n'), code₁_colored)
code_str₂ = rstrip(==('\n'), code_str₂)
code₂_colored = rstrip(==('\n'), code₂_colored)

Check warning on line 148 in src/compare.jl

View check run for this annotation

Codecov / codecov/patch

src/compare.jl#L145-L148

Added lines #L145 - L148 were not covered by tests
end

diff = CodeDiff(code_str₁, code_str₂, code₁_colored, code₂_colored)
optimize_line_changes!(diff)
return diff
Expand Down
11 changes: 9 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -151,29 +151,36 @@ end
@testset "Typed" begin
diff = CodeDiffs.compare_code_typed(f₁, args₁, f₂, args₂; color=true)
@test findfirst(CodeDiffs.ANSI_REGEX, diff.before) === nothing
@test !endswith(diff.before, '\n') && !endswith(diff.after, '\n')
println("\nTyped: $(nameof(f₁)) vs. $(nameof(f₂))")
printstyled(display_str(diff; columns=120))
println()
end

@testset "LLVM" begin
diff = CodeDiffs.compare_code_llvm(f₁, args₁, f₂, args₂; color=true, debuginfo=:none)
@test findfirst(CodeDiffs.ANSI_REGEX, diff.before) === nothing
@test (@io2str InteractiveUtils.print_llvm(IOContext(::IO, :color => true), diff.before)) == diff.highlighted_before
@test !endswith(diff.before, '\n') && !endswith(diff.after, '\n')
@test rstrip(@io2str InteractiveUtils.print_llvm(IOContext(::IO, :color => true), diff.before)) == diff.highlighted_before
println("\nLLVM: $(nameof(f₁)) vs. $(nameof(f₂))")
printstyled(display_str(diff; columns=120))
println()
end

@testset "Native" begin
diff = CodeDiffs.compare_code_native(f₁, args₁, f₂, args₂; color=true, debuginfo=:none)
@test findfirst(CodeDiffs.ANSI_REGEX, diff.before) === nothing
@test (@io2str InteractiveUtils.print_native(IOContext(::IO, :color => true), diff.before)) == diff.highlighted_before
@test !endswith(diff.before, '\n') && !endswith(diff.after, '\n')
@test rstrip(@io2str InteractiveUtils.print_native(IOContext(::IO, :color => true), diff.before)) == diff.highlighted_before
println("\nNative: $(nameof(f₁)) vs. $(nameof(f₂))")
printstyled(display_str(diff; columns=120))
println()
end

@testset "Line numbers" begin
diff = CodeDiffs.compare_code_typed(f₁, args₁, f₂, args₂; color=false)
withenv("CODE_DIFFS_LINE_NUMBERS" => true) do
println("\nTyped + line numbers: $(nameof(f₁)) vs. $(nameof(f₂))")
printstyled(display_str(diff; color=false, columns=120))
println()
end
Expand Down

0 comments on commit 48ecd70

Please sign in to comment.