@@ -163,10 +163,10 @@ The `range` is a `UnitRange{Int}` that indexes into `ctx.content` and
163
163
164
164
This is a small wrapper around [`_hl_annotations!`](@ref) for convenience.
165
165
"""
166
- function _hl_annotations (content:: AbstractString , ast:: GreenNode )
166
+ function _hl_annotations (content:: AbstractString , ast:: GreenNode ; syntax_errors :: Bool = false )
167
167
highlights = Vector {Tuple{UnitRange{Int}, Pair{Symbol, Any}}} ()
168
168
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 )
170
170
highlights
171
171
end
172
172
@@ -181,7 +181,7 @@ The `range` is a `UnitRange{Int}` that indexes into `ctx.content` and
181
181
`annot` is a `Pair` of the form `:face => <face>`.
182
182
"""
183
183
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 )
185
185
(; node, parent) = lineage
186
186
(; content, offset, lnode, pdepths) = ctx
187
187
region = firstindex (content)+ offset: node. span+ offset
@@ -350,14 +350,17 @@ end
350
350
351
351
"""
352
352
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}
354
355
355
356
Apply syntax highlighting to `content` using `JuliaSyntax`.
356
357
357
358
By default, `JuliaSyntax.parseall` is used to generate to `ast` with the
358
359
`ignore_errors` keyword argument set to `true`. Alternatively, one may provide a
359
360
pre-generated `ast`.
360
361
362
+ When `syntax_errors` is set, the `julia_error` face is applied to detected syntax errors.
363
+
361
364
!!! warning
362
365
Note that the particular faces used by `JuliaSyntax`, and the way they
363
366
are applied, is subject to change.
@@ -380,27 +383,31 @@ julia> JuliaSyntaxHighlighting.highlight("sum(1:8)") |> Base.annotations
380
383
"""
381
384
function highlight end
382
385
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 )
385
388
386
- highlight (io:: IO ) = highlight (read (io, String))
389
+ highlight (io:: IO ; syntax_errors:: Bool = false ) =
390
+ highlight (read (io, String); syntax_errors)
387
391
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 )
390
394
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 ))
393
397
394
398
"""
395
399
highlight!(content::Union{AnnotatedString, SubString{AnnotatedString}},
396
- ast::JuliaSyntax.GreenNode = <parsed content>)
400
+ ast::JuliaSyntax.GreenNode = <parsed content>;
401
+ syntax_errors::Bool = false) -> content
397
402
398
403
Modify `content` by applying syntax highlighting using `JuliaSyntax`.
399
404
400
405
By default, `JuliaSyntax.parseall` is used to generate to `ast` with the
401
406
`ignore_errors` keyword argument set to `true`. Alternatively, one may provide a
402
407
pre-generated `ast`.
403
408
409
+ When `syntax_errors` is set, the `julia_error` face is applied to detected syntax errors.
410
+
404
411
!!! warning
405
412
Note that the particular faces used by `JuliaSyntax`, and the way they
406
413
are applied, is subject to change.
@@ -424,16 +431,16 @@ julia> Base.annotations(str)
424
431
(8:8, :face => :julia_rainbow_paren_1)
425
432
```
426
433
"""
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 )
429
436
annotate! (str, range, annot)
430
437
end
431
438
str
432
439
end
433
440
434
- function highlight! (str:: SubString{AnnotatedString{S}} ) where {S}
441
+ function highlight! (str:: SubString{AnnotatedString{S}} ; syntax_errors :: Bool = false ) where {S}
435
442
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 )
437
444
annotate! (str, range, annot)
438
445
end
439
446
str
0 commit comments