Skip to content

Commit a41f5e1

Browse files
authored
Generic sourcefile() function (#469)
* Having a generic version of this is useful for other syntax tree types such as `JuliaLowering.SyntaxTree` * Function to get the `SourceFile` for a syntax object * Generic `highlight()` implementation in terms of this * Cleanup: Remove internal `interpolate_literal()` function which accidentally has still survived from early prototyping.
1 parent dbe9e5d commit a41f5e1

File tree

3 files changed

+36
-20
lines changed

3 files changed

+36
-20
lines changed

src/expr.jl

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -519,19 +519,27 @@ function build_tree(::Type{Expr}, stream::ParseStream;
519519
only(_fixup_Expr_children!(SyntaxHead(K"None",EMPTY_FLAGS), loc, Any[entry.ex]))
520520
end
521521

522+
"""
523+
Get the source file for a given syntax object
524+
"""
525+
function sourcefile(node::SyntaxNode)
526+
node.source
527+
end
528+
522529
function _to_expr(node::SyntaxNode)
530+
file = sourcefile(node)
523531
if !haschildren(node)
524-
offset, txtbuf = _unsafe_wrap_substring(sourcetext(node.source))
525-
return _leaf_to_Expr(node.source, txtbuf, head(node), byte_range(node) .+ offset, node)
532+
offset, txtbuf = _unsafe_wrap_substring(sourcetext(file))
533+
return _leaf_to_Expr(file, txtbuf, head(node), byte_range(node) .+ offset, node)
526534
end
527535
cs = children(node)
528536
args = Any[_to_expr(c) for c in cs]
529-
_internal_node_to_Expr(node.source, byte_range(node), head(node), byte_range.(cs), head.(cs), args)
537+
_internal_node_to_Expr(file, byte_range(node), head(node), byte_range.(cs), head.(cs), args)
530538
end
531539

532540
function Base.Expr(node::SyntaxNode)
533541
ex = _to_expr(node)
534-
loc = source_location(LineNumberNode, node.source, first(byte_range(node)))
542+
loc = source_location(LineNumberNode, sourcefile(node), first_byte(node))
535543
only(_fixup_Expr_children!(SyntaxHead(K"None",EMPTY_FLAGS), loc, Any[ex]))
536544
end
537545

src/source_files.jl

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,26 @@ function _print_marker_line(io, prefix_str, str, underline, singleline, color,
193193
end
194194
end
195195

196+
function highlight(io::IO, x; kws...)
197+
highlight(io, sourcefile(x), byte_range(x); kws...)
198+
end
199+
196200
"""
197-
Print the lines of source code surrounding the given byte `range`, which is
198-
highlighted with background `color` and markers in the text.
201+
highlight(io::IO, source::SourceFile, range::UnitRange;
202+
color, note, notecolor,
203+
context_lines_before, context_lines_inner, context_lines_after,
204+
highlight(io, x; kws...)
205+
206+
Print the lines of source code `source` surrounding the given byte `range`
207+
which is highlighted with background `color` and underlined with markers in the
208+
text. A `note` in `notecolor` may be provided as annotation.
209+
210+
In the second form, `x` is an object with `sourcefile(x)` and `byte_range(x)`
211+
implemented.
212+
213+
The context arguments `context_lines_before`, etc, refer to the number of
214+
lines of code which will be printed as context before and after, with `inner`
215+
referring to context lines inside a multiline region.
199216
"""
200217
function highlight(io::IO, source::SourceFile, range::UnitRange;
201218
color=(120,70,70), context_lines_before=2,

src/syntax_tree.jl

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -137,21 +137,16 @@ byte_range(ex) = first_byte(ex):last_byte(ex)
137137
Get the full source text of a node.
138138
"""
139139
function sourcetext(node::AbstractSyntaxNode)
140-
view(node.source, byte_range(node))
140+
view(sourcefile(node), byte_range(node))
141141
end
142142

143-
source_line(node::AbstractSyntaxNode) = source_line(node.source, node.position)
144-
source_location(node::AbstractSyntaxNode) = source_location(node.source, node.position)
145-
146-
function interpolate_literal(node::SyntaxNode, val)
147-
@assert kind(node) == K"$"
148-
SyntaxNode(node.source, node.raw, node.position, node.parent, true, val)
149-
end
143+
source_line(node::AbstractSyntaxNode) = source_line(sourcefile(node), node.position)
144+
source_location(node::AbstractSyntaxNode) = source_location(sourcefile(node), node.position)
150145

151146
function _show_syntax_node(io, current_filename, node::AbstractSyntaxNode,
152147
indent, show_byte_offsets)
153-
fname = node.source.filename
154-
line, col = source_location(node.source, node.position)
148+
fname = sourcefile(node).filename
149+
line, col = source_location(node)
155150
posstr = "$(lpad(line, 4)):$(rpad(col,3))"
156151
if show_byte_offsets
157152
posstr *= "$(lpad(first_byte(node),6)):$(rpad(last_byte(node),6))"
@@ -301,10 +296,6 @@ function child_position_span(node::SyntaxNode, path::Int...)
301296
n, n.position, span(n)
302297
end
303298

304-
function highlight(io::IO, node::SyntaxNode; kws...)
305-
highlight(io, node.source, byte_range(node); kws...)
306-
end
307-
308299
function highlight(io::IO, source::SourceFile, node::GreenNode, path::Int...; kws...)
309300
_, p, span = child_position_span(node, path...)
310301
q = p + span - 1

0 commit comments

Comments
 (0)