Skip to content

Commit

Permalink
Replace tabs with spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikekre committed Jul 20, 2024
1 parent 7604ae7 commit bdbe60e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Runic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ struct Node
tags::TagType
end

function Node(head::JuliaSyntax.SyntaxHead, span::Integer)
return Node(head, span % UInt32, (), 0 % TagType)
function Node(head::JuliaSyntax.SyntaxHead, span::Integer, tags::Integer = 0)
return Node(head, span % UInt32, (), tags % TagType)
end

function Node(head::JuliaSyntax.SyntaxHead, kids::Vector{Node})
Expand Down Expand Up @@ -78,6 +78,7 @@ JuliaSyntax.span(node::Node) = span(node)
# Matching JuliaSyntax.(head|span|flags|kind)
head(node::Node) = node.head
span(node::Node) = node.span
tags(node::Node) = node.tags
flags(node::Node) = JuliaSyntax.flags(node)
kind(node::Node) = JuliaSyntax.kind(node)

Expand Down Expand Up @@ -298,6 +299,7 @@ function format_node!(ctx::Context, node::Node)::Union{Node, Nothing, NullNode}

# Go through the runestone and apply transformations.
ctx.call_depth += 1
@return_something replace_tabs_with_four_spaces(ctx, node)
@return_something no_leading_and_single_trailing_newline(ctx, node)
@return_something max_three_consecutive_newlines(ctx, node)
@return_something insert_delete_mark_newlines(ctx, node)
Expand Down
17 changes: 17 additions & 0 deletions src/runestone.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@ function trim_trailing_whitespace(ctx::Context, node::Node)
return node′
end

function replace_tabs_with_four_spaces(ctx::Context, node::Node)
kind(node) in KSet"Whitespace NewlineWs" || return nothing
@assert is_leaf(node)
bytes = read_bytes(ctx, node)
tabidx = findfirst(x -> x == UInt8('\t'), bytes)
tabidx === nothing && return nothing
while tabidx !== nothing
bytes[tabidx] = UInt8(' ')
for _ in 1:3
insert!(bytes, tabidx, UInt8(' '))
end
tabidx = findnext(x -> x == UInt8('\t'), bytes, tabidx + 4)
end
nb = replace_bytes!(ctx, bytes, span(node))
return Node(head(node), nb, tags(node))
end

function format_hex_literals(ctx::Context, node::Node)
kind(node) === K"HexInt" || return nothing
@assert flags(node) == 0
Expand Down
5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -769,3 +769,8 @@ end
@test format_string("$(nl)"; filemode = true) == "\n"
end
end

@testset "https://youtu.be/SsoOG6ZeyUI?si=xpKpnczuqsOThtFP" begin
@test format_string("f(a,\tb)") == "f(a, b)"
@test format_string("begin\n\tx = 1\nend") == "begin\n x = 1\nend"
end

0 comments on commit bdbe60e

Please sign in to comment.