Skip to content

Commit 04b2323

Browse files
committed
Support a syntax_errors keyword argument
1 parent 3fba08b commit 04b2323

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

src/JuliaSyntaxHighlighting.jl

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,10 @@ The `range` is a `UnitRange{Int}` that indexes into `ctx.content` and
163163
164164
This is a small wrapper around [`_hl_annotations!`](@ref) for convenience.
165165
"""
166-
function _hl_annotations(content::AbstractString, ast::GreenNode)
166+
function _hl_annotations(content::AbstractString, ast::GreenNode; syntax_errors::Bool = false)
167167
highlights = Vector{Tuple{UnitRange{Int}, Pair{Symbol, Any}}}()
168168
ctx = HighlightContext(content, zero(UInt), ast, ParenDepthCounter())
169-
_hl_annotations!(highlights, GreenLineage(ast, nothing), ctx)
169+
_hl_annotations!(highlights, GreenLineage(ast, nothing), ctx; syntax_errors)
170170
highlights
171171
end
172172

@@ -181,7 +181,7 @@ The `range` is a `UnitRange{Int}` that indexes into `ctx.content` and
181181
`annot` is a `Pair` of the form `:face => <face>`.
182182
"""
183183
function _hl_annotations!(highlights::Vector{Tuple{UnitRange{Int}, Pair{Symbol, Any}}},
184-
lineage::GreenLineage, ctx::HighlightContext)
184+
lineage::GreenLineage, ctx::HighlightContext; syntax_errors::Bool = false)
185185
(; node, parent) = lineage
186186
(; content, offset, lnode, pdepths) = ctx
187187
region = firstindex(content)+offset:node.span+offset
@@ -350,14 +350,17 @@ end
350350

351351
"""
352352
highlight(content::Union{AbstractString, IO},
353-
ast::JuliaSyntax.GreenNode = <parsed content>) -> AnnotatedString{String}
353+
ast::JuliaSyntax.GreenNode = <parsed content>;
354+
syntax_errors::Bool = false) -> AnnotatedString{String}
354355
355356
Apply syntax highlighting to `content` using `JuliaSyntax`.
356357
357358
By default, `JuliaSyntax.parseall` is used to generate to `ast` with the
358359
`ignore_errors` keyword argument set to `true`. Alternatively, one may provide a
359360
pre-generated `ast`.
360361
362+
When `syntax_errors` is set, the `julia_error` face is applied to detected syntax errors.
363+
361364
!!! warning
362365
Note that the particular faces used by `JuliaSyntax`, and the way they
363366
are applied, is subject to change.
@@ -380,27 +383,31 @@ julia> JuliaSyntaxHighlighting.highlight("sum(1:8)") |> Base.annotations
380383
"""
381384
function highlight end
382385

383-
highlight(str::AbstractString) =
384-
highlight(str, parseall(GreenNode, str, ignore_errors=true))
386+
highlight(str::AbstractString; syntax_errors::Bool = false) =
387+
highlight(str, parseall(GreenNode, str, ignore_errors=true); syntax_errors)
385388

386-
highlight(io::IO) = highlight(read(io, String))
389+
highlight(io::IO; syntax_errors::Bool = false) =
390+
highlight(read(io, String); syntax_errors)
387391

388-
highlight(io::IO, ast::GreenNode) =
389-
highlight(read(io, String), ast)
392+
highlight(io::IO, ast::GreenNode; syntax_errors::Bool = false) =
393+
highlight(read(io, String), ast; syntax_errors)
390394

391-
highlight(str::AbstractString, ast::GreenNode) =
392-
AnnotatedString(str, _hl_annotations(str, ast))
395+
highlight(str::AbstractString, ast::GreenNode; syntax_errors::Bool = false) =
396+
AnnotatedString(str, _hl_annotations(str, ast; syntax_errors))
393397

394398
"""
395399
highlight!(content::Union{AnnotatedString, SubString{AnnotatedString}},
396-
ast::JuliaSyntax.GreenNode = <parsed content>)
400+
ast::JuliaSyntax.GreenNode = <parsed content>;
401+
syntax_errors::Bool = false) -> content
397402
398403
Modify `content` by applying syntax highlighting using `JuliaSyntax`.
399404
400405
By default, `JuliaSyntax.parseall` is used to generate to `ast` with the
401406
`ignore_errors` keyword argument set to `true`. Alternatively, one may provide a
402407
pre-generated `ast`.
403408
409+
When `syntax_errors` is set, the `julia_error` face is applied to detected syntax errors.
410+
404411
!!! warning
405412
Note that the particular faces used by `JuliaSyntax`, and the way they
406413
are applied, is subject to change.
@@ -424,16 +431,16 @@ julia> Base.annotations(str)
424431
(8:8, :face => :julia_rainbow_paren_1)
425432
```
426433
"""
427-
function highlight!(str::AnnotatedString)
428-
for (range, annot) in _hl_annotations(str.string, parseall(GreenNode, str.string, ignore_errors=true))
434+
function highlight!(str::AnnotatedString; syntax_errors::Bool = false)
435+
for (range, annot) in _hl_annotations(str.string, parseall(GreenNode, str.string, ignore_errors=true); syntax_errors)
429436
annotate!(str, range, annot)
430437
end
431438
str
432439
end
433440

434-
function highlight!(str::SubString{AnnotatedString{S}}) where {S}
441+
function highlight!(str::SubString{AnnotatedString{S}}; syntax_errors::Bool = false) where {S}
435442
plainstr = SubString{S}(str.string.string, str.offset, str.ncodeunits, Val(:noshift))
436-
for (range, annot) in _hl_annotations(plainstr, parseall(GreenNode, plainstr, ignore_errors=true))
443+
for (range, annot) in _hl_annotations(plainstr, parseall(GreenNode, plainstr, ignore_errors=true); syntax_errors)
437444
annotate!(str, range, annot)
438445
end
439446
str

0 commit comments

Comments
 (0)