From 736b842194dfdf342d388fdc9114a50df3d88d48 Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Thu, 24 Jul 2025 23:52:40 +0200 Subject: [PATCH] Remove range format markers when using --lines and --diff together --- CHANGELOG.md | 5 +++++ Project.toml | 2 +- src/main.jl | 21 +++++++++++++++++++-- test/maintests.jl | 10 ++++++++++ 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e4df16..99aaf44 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [v1.4.5] - 2025-07-24 +### Fixed + - Fix `--diff` output in combination with `--lines`. Previously the internal makers used to + mark the lines to be formatted would show up in the diff output. ([#154]). + ## [v1.4.4] - 2025-07-24 ### Fixed - Fix another edgecase where adding a trailing comma after the last item in a list results diff --git a/Project.toml b/Project.toml index e5a2d74..1b944e2 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "Runic" uuid = "62bfec6d-59d7-401d-8490-b29ee721c001" -version = "1.4.4" +version = "1.4.5" [deps] JuliaSyntax = "70703baa-626e-46a2-a12c-08ffd08c73b4" diff --git a/src/main.jl b/src/main.jl index da889fc..66cf4c1 100644 --- a/src/main.jl +++ b/src/main.jl @@ -468,15 +468,32 @@ function main(argv) file = basename(inputfile) A = joinpath(a, file) B = joinpath(b, file) + src_str = ctx.src_str + # If we have ranges we need to remove the comment markers + # TODO: It isn't great that the source string has been modified to begin + # with, and to support --lines in the API functions this filtering + # needs to be moved to the `format_tree` function. + if !isempty(line_ranges) + io = IOBuffer(; sizehint = sizeof(src_str)) + for line in eachline(IOBuffer(src_str); keep = true) + if !( + occursin(RANGE_FORMATTING_BEGIN, line) || + occursin(RANGE_FORMATTING_END, line) + ) + write(io, line) + end + end + src_str = String(take!(io)) + end # juliac: `open(...) do` uses dynamic dispatch otherwise the following # blocks could be written as # ``` - # write(A, ctx.src_str) + # write(A, src_str) # write(B, seekstart(ctx.fmt_io)) # ``` let io = open(A, "w") try - write(io, ctx.src_str) + write(io, src_str) finally close(io) end diff --git a/test/maintests.jl b/test/maintests.jl index 7538012..9f0d584 100644 --- a/test/maintests.jl +++ b/test/maintests.jl @@ -543,6 +543,16 @@ function maintests(f::R) where {R} rc, fd1, fd2 = runic(["--lines=3:4", "."]) @test rc == 1 && isempty(fd1) @test occursin("option `--lines` can not be used together with multiple input files", fd2) + # --diff and --lines: no comment markers in diff output + if Sys.which("git") !== nothing + rc, fd1, fd2 = runic(["--lines=1:1", "--diff"], src) + @test rc == 0 + @test fd1 == "function f(a, b)\n return a+b\n end\n" + @test occursin("-function f(a,b)", fd2) + @test occursin("+function f(a, b)", fd2) + @test !occursin(Runic.RANGE_FORMATTING_BEGIN, fd2) + @test !occursin(Runic.RANGE_FORMATTING_END, fd2) + end end return