Skip to content

Commit

Permalink
add some support for public keyword (#886)
Browse files Browse the repository at this point in the history
* add some support for public keyword

* fix the kset memberships for `public`

* add a simple test
  • Loading branch information
exaexa authored Nov 28, 2024
1 parent bc87301 commit 5f83396
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/fst.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
Const,
Import,
Export,
Public,
Using,
File,
Quotenode,
Expand Down Expand Up @@ -425,7 +426,7 @@ is_opener(t::JuliaSyntax.GreenNode) = kind(t) in KSet"{ ( ["
function is_iterable(t::JuliaSyntax.GreenNode)
if !(
kind(t) in
KSet"parens tuple vect vcat braces curly comprehension typed_comprehension macrocall ref typed_vcat import using export"
KSet"parens tuple vect vcat braces curly comprehension typed_comprehension macrocall ref typed_vcat import using export public"
)
is_func_call(t)
else
Expand All @@ -447,7 +448,7 @@ function is_named_iterable(x::FST)
end

function is_import_expr(x::FST)
return x.typ in (Import, Using, Export)
return x.typ in (Import, Using, Export, Public)
end

"""
Expand Down
13 changes: 12 additions & 1 deletion src/styles/default/nest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ function nest!(
n_import!(style, fst, s, lineage)
elseif fst.typ === Export
n_export!(style, fst, s, lineage)
elseif fst.typ === Public
n_public!(style, fst, s, lineage)
elseif fst.typ === Using
n_using!(style, fst, s, lineage)
elseif fst.typ === Where
Expand Down Expand Up @@ -242,7 +244,7 @@ function n_do!(
return nested
end

# Import,Using,Export
# Import,Using,Export,Public
function n_using!(
ds::AbstractStyle,
fst::FST,
Expand Down Expand Up @@ -303,6 +305,15 @@ function n_export!(
n_using!(ds, fst, s, lineage)
end

function n_public!(
ds::AbstractStyle,
fst::FST,
s::State,
lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}},
)
n_using!(ds, fst, s, lineage)
end

function n_import!(
ds::AbstractStyle,
fst::FST,
Expand Down
18 changes: 16 additions & 2 deletions src/styles/default/pretty.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ function pretty(
p_import(style, node, s, ctx, lineage)
elseif k === K"export"
p_export(style, node, s, ctx, lineage)
elseif k === K"public"
p_public(style, node, s, ctx, lineage)
elseif k === K"using"
p_using(style, node, s, ctx, lineage)
elseif k === K"importpath"
Expand Down Expand Up @@ -2598,14 +2600,14 @@ function p_import(
end

for a in children(cst)
if kind(a) in KSet"import export using"
if kind(a) in KSet"import export public using"
add_node!(t, pretty(style, a, s, ctx, lineage), s; join_lines = true)
add_node!(t, Whitespace(1), s)
elseif kind(a) === K":" && haschildren(a)
nodes = children(a)
for n in nodes
add_node!(t, pretty(style, n, s, ctx, lineage), s; join_lines = true)
if kind(n) in KSet"import export using"
if kind(n) in KSet"import export public using"
add_node!(t, Whitespace(1), s)
elseif kind(n) in KSet", :"
add_node!(t, Placeholder(1), s)
Expand Down Expand Up @@ -2633,6 +2635,18 @@ function p_export(
t
end

function p_public(
ds::AbstractStyle,
cst::JuliaSyntax.GreenNode,
s::State,
ctx::PrettyContext,
lineage::Vector{Tuple{JuliaSyntax.Kind,Bool,Bool}},
)
t = p_import(ds, cst, s, ctx, lineage)
t.typ = Public
t
end

function p_using(
ds::AbstractStyle,
cst::JuliaSyntax.GreenNode,
Expand Down
1 change: 1 addition & 0 deletions src/styles/sciml/nest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ for f in [
:n_import!,
:n_using!,
:n_export!,
:n_public!,
:n_vcat!,
:n_ncat!,
:n_typedvcat!,
Expand Down
1 change: 1 addition & 0 deletions src/styles/sciml/pretty.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ for f in [
:p_import,
:p_using,
:p_export,
:p_public,
:p_vcat,
:p_ncat,
:p_typedvcat,
Expand Down
8 changes: 8 additions & 0 deletions src/styles/yas/nest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,14 @@ function n_export!(
)
n_using!(ys, fst, s, lineage)
end
function n_public!(
ys::YASStyle,
fst::FST,
s::State,
lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}},
)
n_using!(ys, fst, s, lineage)
end
function n_import!(
ys::YASStyle,
fst::FST,
Expand Down
16 changes: 14 additions & 2 deletions src/styles/yas/pretty.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ function p_import(
end

for a in children(cst)
if kind(a) in KSet"import export using"
if kind(a) in KSet"import export using public"
add_node!(t, pretty(style, a, s, ctx, lineage), s; join_lines = true)
add_node!(t, Whitespace(1), s)
elseif kind(a) === K":" && haschildren(a)
nodes = children(a)
for n in nodes
add_node!(t, pretty(style, n, s, ctx, lineage), s; join_lines = true)
if kind(n) in KSet"import export using :"
if kind(n) in KSet"import export using public :"
add_node!(t, Whitespace(1), s)
elseif kind(n) in KSet","
add_node!(t, Placeholder(1), s)
Expand Down Expand Up @@ -116,6 +116,18 @@ function p_export(
t
end

function p_public(
ys::YASStyle,
cst::JuliaSyntax.GreenNode,
s::State,
ctx::PrettyContext,
lineage::Vector{Tuple{JuliaSyntax.Kind,Bool,Bool}},
)
t = p_import(ys, cst, s, ctx, lineage)
t.typ = Public
t
end

function p_curly(
ys::YASStyle,
cst::JuliaSyntax.GreenNode,
Expand Down
17 changes: 17 additions & 0 deletions test/default_style.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4741,6 +4741,23 @@ some_function(
@test fmt(str, 4, 27; join_lines_based_on_source = true) == str
end

@testset "public keyword support" begin
str_ = """
public a,b,
c
"""
str = """
public a, b, c
"""
@test fmt(str_, 4, 14) == str
str = """
public a,
b,
c
"""
@test fmt(str_, 4, 1) == str
end

@testset "block automatically assume nested when join_lines_based_on_source" begin
str_ = """
let y = a, z = b
Expand Down

0 comments on commit 5f83396

Please sign in to comment.