Add compat for JuliaSyntax v1#978
Conversation
|
@domluna all tests pass locally |
|
@penelopeysm can you review this? |
|
I could, but honestly, it would take me a bit of time! I haven't really gotten into the codebase as much as I'd like, and have been quite busy IRL too. If you don't mind waiting, I could try to get to it over the weekend? |
|
Sure no worries as long as we get this project moved forward again 🚂 |
pfitzseb
left a comment
There was a problem hiding this comment.
Ok, finally got a chance to take a proper look at this. It would generally be nice to get some more unit tests for the new utility functions if possible.
| function is_short_function_def(cst::JuliaSyntax.GreenNode) | ||
| kind(cst) === K"function" && haschildren(cst) || return false | ||
| idx = findfirst(n -> !JuliaSyntax.is_whitespace(n), children(cst)) | ||
| isnothing(idx) && return false | ||
| return kind(cst[idx]) !== K"function" | ||
| end |
There was a problem hiding this comment.
| function is_short_function_def(cst::JuliaSyntax.GreenNode) | |
| kind(cst) === K"function" && haschildren(cst) || return false | |
| idx = findfirst(n -> !JuliaSyntax.is_whitespace(n), children(cst)) | |
| isnothing(idx) && return false | |
| return kind(cst[idx]) !== K"function" | |
| end | |
| is_short_function_def(cst::JuliaSyntax.GreenNode) = JuliaSyntax.has_flags(cst, JuliaSyntax.SHORT_FORM_FUNCTION_FLAG) |
| elseif has_do_block_call(node) | ||
| p_do_call(style, node, s, ctx, lineage) |
There was a problem hiding this comment.
With this, does the
elseif k === K"do"
p_do(style, node, s, ctx, lineage)
ever get called? Seems like only having one of them should be enough (and specifically, why can't you re-use the existing case here)?
| return nothing | ||
| end | ||
|
|
||
| function is_source_operator(s::State, cst::JuliaSyntax.GreenNode, offset::Integer) |
There was a problem hiding this comment.
In theory this should be covered by the JuliaSyntax.is_operator_* calls no? Would be nice if we didn't have to look into the file for this.
| function source_kind(s::State, cst::JuliaSyntax.GreenNode, offset::Integer) | ||
| span(cst) == 0 && return nothing | ||
| offset = Int(offset) | ||
| n = Int(span(cst)) | ||
| val = getsrcval(s.doc, offset:(offset+n-1)) | ||
| try | ||
| return JuliaSyntax.Kind(val) | ||
| catch | ||
| return nothing | ||
| end | ||
| end | ||
|
|
||
| function source_operator_kind(s::State, cst::JuliaSyntax.GreenNode, offset::Integer) | ||
| if JuliaSyntax.is_operator(cst) && !haschildren(cst) | ||
| return kind(cst) | ||
| elseif kind(cst) === K"Identifier" && !haschildren(cst) | ||
| k = source_kind(s, cst, offset) | ||
| if !isnothing(k) && JuliaSyntax.is_operator(k) | ||
| return k | ||
| end | ||
| end | ||
| return nothing | ||
| end |
There was a problem hiding this comment.
We need this because of JuliaLang/JuliaSyntax.jl#591, right? Wonder whether this will get fixed upstream at some point...
| function source_op_kind(s::State, cst::JuliaSyntax.GreenNode, op_indices::Vector{Int}) | ||
| opkind = op_kind(cst) | ||
| opkind !== K"None" && opkind !== K"Identifier" && return opkind | ||
|
|
||
| offset = Int(s.offset) | ||
| childs = children(cst) | ||
| for (i, c) in enumerate(childs) | ||
| if i in op_indices | ||
| k = source_operator_kind(s, c, offset) | ||
| if !isnothing(k) && !(kind(cst) === K"dotcall" && k === K".") | ||
| return k | ||
| end | ||
| end | ||
| offset += Int(span(c)) | ||
| end | ||
| return opkind | ||
| end | ||
|
|
||
| function update_operator_indices(childs::Vector{JuliaSyntax.GreenNode{T}}) where {T} | ||
| args = findall(n -> !JuliaSyntax.is_whitespace(n), childs) | ||
| length(args) < 4 && return Int[] | ||
| return args[2:(end-1)] | ||
| end |
There was a problem hiding this comment.
Seems like the operator should be easy enough to find by checking in which position it occurs (JS.is_[prefix|infix|suffix]_op_call) and then directly accessing that argument.
I kept the changes as minimal as possible.