@@ -163,10 +163,10 @@ The `range` is a `UnitRange{Int}` that indexes into `ctx.content` and
163163
164164This 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
171171end
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"""
183183function _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
355356Apply syntax highlighting to `content` using `JuliaSyntax`.
356357
357358By default, `JuliaSyntax.parseall` is used to generate to `ast` with the
358359`ignore_errors` keyword argument set to `true`. Alternatively, one may provide a
359360pre-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"""
381384function 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
398403Modify `content` by applying syntax highlighting using `JuliaSyntax`.
399404
400405By default, `JuliaSyntax.parseall` is used to generate to `ast` with the
401406`ignore_errors` keyword argument set to `true`. Alternatively, one may provide a
402407pre-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
432439end
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