Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Begin to attempt to add trailing zeros to Float literals #66

Merged
merged 2 commits into from
Sep 15, 2019
Merged

Begin to attempt to add trailing zeros to Float literals #66

merged 2 commits into from
Sep 15, 2019

Conversation

odow
Copy link
Contributor

@odow odow commented Sep 14, 2019

So I wanted to implement adding trailing zeros to Float literals (#61), but I quickly got stuck.

I haven't quite got my head around PTrees, so I'm stumbling around in the dark.

Is this the correct way to implement this? Or do I need to add a node to the tree like you did here:
8944eaa#diff-9915f66306b42c958fcd076dc0eee523R1228

At the moment I get

julia> JuliaFormatter.format_text("1.")
"1.0"

julia> JuliaFormatter.format_text("1. + 1")
ERROR: Indexing range 1 - 6, index used = 7
Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] cursor_loc at /Users/oscar/.julia/dev/JuliaFormatter/src/JuliaFormatter.jl:100 [inlined]
 [3] cursor_loc at /Users/oscar/.julia/dev/JuliaFormatter/src/JuliaFormatter.jl:102 [inlined]
 [4] p_literal(::CSTParser.EXPR, ::JuliaFormatter.State) at /Users/oscar/.julia/dev/JuliaFormatter/src/pretty.jl:386
 [5] pretty(::CSTParser.EXPR, ::JuliaFormatter.State) at /Users/oscar/.julia/dev/JuliaFormatter/src/pretty.jl:192
 [6] #p_binarycall#11(::Bool, ::Bool, ::Function, ::CSTParser.EXPR, ::JuliaFormatter.State) at /Users/oscar/.julia/dev/JuliaFormatter/src/pretty.jl:1139
 [7] p_binarycall(::CSTParser.EXPR, ::JuliaFormatter.State) at /Users/oscar/.julia/dev/JuliaFormatter/src/pretty.jl:1091
 [8] pretty(::CSTParser.EXPR, ::JuliaFormatter.State) at /Users/oscar/.julia/dev/JuliaFormatter/src/pretty.jl:252
 [9] pretty(::CSTParser.EXPR, ::JuliaFormatter.State) at /Users/oscar/.julia/dev/JuliaFormatter/src/pretty.jl:307
 [10] #format_text#52(::Int64, ::Int64, ::Function, ::String) at /Users/oscar/.julia/dev/JuliaFormatter/src/JuliaFormatter.jl:135
 [11] format_text(::String) at /Users/oscar/.julia/dev/JuliaFormatter/src/JuliaFormatter.jl:123
 [12] top-level scope at none:0

@domluna
Copy link
Owner

domluna commented Sep 15, 2019

@odow This looks like it's on the right track. https://github.com/julia-vscode/CSTParser.jl/blob/master/src/spec.jl is the best reference for node types. The only new types introduced in the formatter are PLeaf types.

You shouldn't be editing the CST from CSTParser, it's just used to create the PTree. We probably also want to add an additional check besides just val[end] == '.'. I'm not sure if this occurs in other cases besides floats without a trailing zero.

This should work:

        val = x.val
        if x.kind === Tokens.FLOAT && val[end] == '.'
            val *= '0'
        end
        return PTree(x, loc[1], loc[1], val)

Token types are found in https://github.com/JuliaLang/Tokenize.jl/blob/master/src/token_kinds.jl

@domluna
Copy link
Owner

domluna commented Sep 15, 2019

BTW Does the trailing zero come up at all with complex numbers or would this already handle that?

@domluna
Copy link
Owner

domluna commented Sep 15, 2019

LGTM. Let me know if it's ready to merge in or if you're still making changes.

@odow
Copy link
Contributor Author

odow commented Sep 15, 2019

Thanks for you help. I was missing the fact that the x.val argument to the PTree didn't have to be x.val.

I believe Tokens.FLOAT is the only case where there can be a trailing .. I've added tests with complex number examples.

What's the policy on running format("src") and format("test") on a PR? Because there seemed to be a lot of indentation changes, but I didn't include them because they seemed unrelated.

@domluna
Copy link
Owner

domluna commented Sep 15, 2019

No problem. I need to add more documentation to this thing so it's kind of my fault 🙃 .

Generally I do format("src") and format("test/runtests.jl").

@odow
Copy link
Contributor Author

odow commented Sep 15, 2019

I guess one hack would be to add

format(joinpath(dirname(@__DIR__, "src")))
format(@__FILE__)

to the bottom of test/runtests.jl so that anyone running the tests also auto-formats things.

@domluna
Copy link
Owner

domluna commented Sep 15, 2019

that or GH support can get back to me so I can get this to work https://github.com/domluna/JuliaFormatter-action 😄

Anyway I'm gonna merge this in, thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants