Skip to content

Generic sourcefile() function #469

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

Merged
merged 1 commit into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -519,19 +519,27 @@ function build_tree(::Type{Expr}, stream::ParseStream;
only(_fixup_Expr_children!(SyntaxHead(K"None",EMPTY_FLAGS), loc, Any[entry.ex]))
end

"""
Get the source file for a given syntax object
"""
function sourcefile(node::SyntaxNode)
node.source
end

function _to_expr(node::SyntaxNode)
file = sourcefile(node)
if !haschildren(node)
offset, txtbuf = _unsafe_wrap_substring(sourcetext(node.source))
return _leaf_to_Expr(node.source, txtbuf, head(node), byte_range(node) .+ offset, node)
offset, txtbuf = _unsafe_wrap_substring(sourcetext(file))
return _leaf_to_Expr(file, txtbuf, head(node), byte_range(node) .+ offset, node)
end
cs = children(node)
args = Any[_to_expr(c) for c in cs]
_internal_node_to_Expr(node.source, byte_range(node), head(node), byte_range.(cs), head.(cs), args)
_internal_node_to_Expr(file, byte_range(node), head(node), byte_range.(cs), head.(cs), args)
end

function Base.Expr(node::SyntaxNode)
ex = _to_expr(node)
loc = source_location(LineNumberNode, node.source, first(byte_range(node)))
loc = source_location(LineNumberNode, sourcefile(node), first_byte(node))
only(_fixup_Expr_children!(SyntaxHead(K"None",EMPTY_FLAGS), loc, Any[ex]))
end

21 changes: 19 additions & 2 deletions src/source_files.jl
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,26 @@ function _print_marker_line(io, prefix_str, str, underline, singleline, color,
end
end

function highlight(io::IO, x; kws...)
highlight(io, sourcefile(x), byte_range(x); kws...)
end

"""
Print the lines of source code surrounding the given byte `range`, which is
highlighted with background `color` and markers in the text.
highlight(io::IO, source::SourceFile, range::UnitRange;
color, note, notecolor,
context_lines_before, context_lines_inner, context_lines_after,
highlight(io, x; kws...)

Print the lines of source code `source` surrounding the given byte `range`
which is highlighted with background `color` and underlined with markers in the
text. A `note` in `notecolor` may be provided as annotation.

In the second form, `x` is an object with `sourcefile(x)` and `byte_range(x)`
implemented.

The context arguments `context_lines_before`, etc, refer to the number of
lines of code which will be printed as context before and after, with `inner`
referring to context lines inside a multiline region.
"""
function highlight(io::IO, source::SourceFile, range::UnitRange;
color=(120,70,70), context_lines_before=2,
Expand Down
19 changes: 5 additions & 14 deletions src/syntax_tree.jl
Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,16 @@ byte_range(ex) = first_byte(ex):last_byte(ex)
Get the full source text of a node.
"""
function sourcetext(node::AbstractSyntaxNode)
view(node.source, byte_range(node))
view(sourcefile(node), byte_range(node))
end

source_line(node::AbstractSyntaxNode) = source_line(node.source, node.position)
source_location(node::AbstractSyntaxNode) = source_location(node.source, node.position)

function interpolate_literal(node::SyntaxNode, val)
@assert kind(node) == K"$"
SyntaxNode(node.source, node.raw, node.position, node.parent, true, val)
end
source_line(node::AbstractSyntaxNode) = source_line(sourcefile(node), node.position)
source_location(node::AbstractSyntaxNode) = source_location(sourcefile(node), node.position)

function _show_syntax_node(io, current_filename, node::AbstractSyntaxNode,
indent, show_byte_offsets)
fname = node.source.filename
line, col = source_location(node.source, node.position)
fname = sourcefile(node).filename
line, col = source_location(node)
posstr = "$(lpad(line, 4)):$(rpad(col,3))│"
if show_byte_offsets
posstr *= "$(lpad(first_byte(node),6)):$(rpad(last_byte(node),6))│"
Expand Down Expand Up @@ -301,10 +296,6 @@ function child_position_span(node::SyntaxNode, path::Int...)
n, n.position, span(n)
end

function highlight(io::IO, node::SyntaxNode; kws...)
highlight(io, node.source, byte_range(node); kws...)
end

function highlight(io::IO, source::SourceFile, node::GreenNode, path::Int...; kws...)
_, p, span = child_position_span(node, path...)
q = p + span - 1
Expand Down
Loading