Skip to content

Commit af768dd

Browse files
authored
Move overload of Base.range() to JuliaSyntax.byte_range() (#463)
The Base overload was a mistake - it doesn't really have the same semantics as compared to `Base.range()`. It's also not got the clearest name!
1 parent f99b76f commit af768dd

File tree

5 files changed

+24
-13
lines changed

5 files changed

+24
-13
lines changed

docs/src/api.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,18 @@ JuliaSyntax.flags
5454

5555
see also predicates related to `flags`.
5656

57-
## Syntax tree types
57+
## Syntax trees
58+
59+
Syntax tree types:
5860

5961
```@docs
6062
JuliaSyntax.SyntaxNode
6163
JuliaSyntax.GreenNode
6264
```
65+
66+
Functions applicable to syntax trees include everything in the sections on
67+
heads/kinds, and source file handling.
68+
69+
```@docs
70+
JuliaSyntax.byte_range
71+
```

src/diagnostics.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ end
4040
first_byte(d::Diagnostic) = d.first_byte
4141
last_byte(d::Diagnostic) = d.last_byte
4242
is_error(d::Diagnostic) = d.level === :error
43-
Base.range(d::Diagnostic) = first_byte(d):last_byte(d)
4443

4544
# Make relative path into a file URL
4645
function _file_url(filename)
@@ -89,7 +88,7 @@ function show_diagnostic(io::IO, diagnostic::Diagnostic, source::SourceFile)
8988
_printstyled(io, "# $prefix @ ", fgcolor=:light_black)
9089
_printstyled(io, "$locstr", fgcolor=:light_black, href=file_href)
9190
print(io, "\n")
92-
highlight(io, source, range(diagnostic),
91+
highlight(io, source, byte_range(diagnostic),
9392
note=diagnostic.message, notecolor=color,
9493
context_lines_before=1, context_lines_after=0)
9594
end

src/expr.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -522,16 +522,16 @@ end
522522
function _to_expr(node::SyntaxNode)
523523
if !haschildren(node)
524524
offset, txtbuf = _unsafe_wrap_substring(sourcetext(node.source))
525-
return _leaf_to_Expr(node.source, txtbuf, head(node), range(node) .+ offset, node)
525+
return _leaf_to_Expr(node.source, txtbuf, head(node), byte_range(node) .+ offset, node)
526526
end
527527
cs = children(node)
528528
args = Any[_to_expr(c) for c in cs]
529-
_internal_node_to_Expr(node.source, range(node), head(node), range.(cs), head.(cs), args)
529+
_internal_node_to_Expr(node.source, byte_range(node), head(node), byte_range.(cs), head.(cs), args)
530530
end
531531

532532
function Base.Expr(node::SyntaxNode)
533533
ex = _to_expr(node)
534-
loc = source_location(LineNumberNode, node.source, first(range(node)))
534+
loc = source_location(LineNumberNode, node.source, first(byte_range(node)))
535535
only(_fixup_Expr_children!(SyntaxHead(K"None",EMPTY_FLAGS), loc, Any[ex]))
536536
end
537537

src/syntax_tree.jl

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,17 +124,20 @@ span(node::AbstractSyntaxNode) = span(node.raw)
124124
first_byte(node::AbstractSyntaxNode) = node.position
125125
last_byte(node::AbstractSyntaxNode) = node.position + span(node) - 1
126126

127+
"""
128+
byte_range(ex)
129+
130+
Return the range of bytes which `ex` covers in the source text.
131+
"""
132+
byte_range(ex) = first_byte(ex):last_byte(ex)
133+
127134
"""
128135
sourcetext(node)
129136
130137
Get the full source text of a node.
131138
"""
132139
function sourcetext(node::AbstractSyntaxNode)
133-
view(node.source, range(node))
134-
end
135-
136-
function Base.range(node::AbstractSyntaxNode)
137-
(node.position-1) .+ (1:span(node))
140+
view(node.source, byte_range(node))
138141
end
139142

140143
source_line(node::AbstractSyntaxNode) = source_line(node.source, node.position)
@@ -299,7 +302,7 @@ function child_position_span(node::SyntaxNode, path::Int...)
299302
end
300303

301304
function highlight(io::IO, node::SyntaxNode; kws...)
302-
highlight(io, node.source, range(node); kws...)
305+
highlight(io, node.source, byte_range(node); kws...)
303306
end
304307

305308
function highlight(io::IO, source::SourceFile, node::GreenNode, path::Int...; kws...)

tools/check_all_packages.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Logging.with_logger(TerminalLogger()) do
3939
mismatch_count += 1
4040
failing_source = sprint(context=:color=>true) do io
4141
for c in reduce_tree(parseall(SyntaxNode, text))
42-
JuliaSyntax.highlight(io, c.source, range(c), context_lines_inner=5)
42+
JuliaSyntax.highlight(io, c.source, JuliaSyntax.byte_range(c), context_lines_inner=5)
4343
println(io, "\n")
4444
end
4545
end

0 commit comments

Comments
 (0)