Browse Source

Replace tabs with spaces

pull/23/head
Fredrik Ekre 1 year ago
parent
commit
bdbe60e230
No known key found for this signature in database
GPG Key ID: DE82E6D5E364C0A2
  1. 6
      src/Runic.jl
  2. 17
      src/runestone.jl
  3. 5
      test/runtests.jl

6
src/Runic.jl

@ -39,8 +39,8 @@ struct Node
tags::TagType tags::TagType
end end
function Node(head::JuliaSyntax.SyntaxHead, span::Integer) function Node(head::JuliaSyntax.SyntaxHead, span::Integer, tags::Integer = 0)
return Node(head, span % UInt32, (), 0 % TagType) return Node(head, span % UInt32, (), tags % TagType)
end end
function Node(head::JuliaSyntax.SyntaxHead, kids::Vector{Node}) function Node(head::JuliaSyntax.SyntaxHead, kids::Vector{Node})
@ -78,6 +78,7 @@ JuliaSyntax.span(node::Node) = span(node)
# Matching JuliaSyntax.(head|span|flags|kind) # Matching JuliaSyntax.(head|span|flags|kind)
head(node::Node) = node.head head(node::Node) = node.head
span(node::Node) = node.span span(node::Node) = node.span
tags(node::Node) = node.tags
flags(node::Node) = JuliaSyntax.flags(node) flags(node::Node) = JuliaSyntax.flags(node)
kind(node::Node) = JuliaSyntax.kind(node) kind(node::Node) = JuliaSyntax.kind(node)
@ -298,6 +299,7 @@ function format_node!(ctx::Context, node::Node)::Union{Node, Nothing, NullNode}
# Go through the runestone and apply transformations. # Go through the runestone and apply transformations.
ctx.call_depth += 1 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 no_leading_and_single_trailing_newline(ctx, node)
@return_something max_three_consecutive_newlines(ctx, node) @return_something max_three_consecutive_newlines(ctx, node)
@return_something insert_delete_mark_newlines(ctx, node) @return_something insert_delete_mark_newlines(ctx, node)

17
src/runestone.jl

@ -25,6 +25,23 @@ function trim_trailing_whitespace(ctx::Context, node::Node)
return node′ return node′
end 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) function format_hex_literals(ctx::Context, node::Node)
kind(node) === K"HexInt" || return nothing kind(node) === K"HexInt" || return nothing
@assert flags(node) == 0 @assert flags(node) == 0

5
test/runtests.jl

@ -769,3 +769,8 @@ end
@test format_string("$(nl)"; filemode = true) == "\n" @test format_string("$(nl)"; filemode = true) == "\n"
end end
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

Loading…
Cancel
Save