Julia Syntax Highlighting
The JuliaSyntaxHighlighting library serves as a small convenience package to syntax highlight Julia code using JuliaSyntax and StyledStrings.
It is intended for use across the standard library, and the wider ecosystem.
Functions
JuliaSyntaxHighlighting.highlight — Functionhighlight(content::Union{AbstractString, IO},
ast::JuliaSyntax.GreenNode = <parsed content>;
syntax_errors::Bool = false) -> AnnotatedString{String}Apply syntax highlighting to content using JuliaSyntax.
By default, JuliaSyntax.parseall is used to generate to ast with the ignore_errors keyword argument set to true. Alternatively, one may provide a pre-generated ast.
When syntax_errors is set, the julia_error face is applied to detected syntax errors.
Note that the particular faces used by JuliaSyntax, and the way they are applied, is subject to change.
Examples
julia> JuliaSyntaxHighlighting.highlight("sum(1:8)")
"sum(1:8)"
julia> JuliaSyntaxHighlighting.highlight("sum(1:8)") |> Base.annotations
5-element Vector{@NamedTuple{region::UnitRange{Int64}, label::Symbol, value}}:
@NamedTuple{region::UnitRange{Int64}, label::Symbol, value}((1:3, :face, :julia_funcall))
@NamedTuple{region::UnitRange{Int64}, label::Symbol, value}((4:4, :face, :julia_rainbow_paren_1))
@NamedTuple{region::UnitRange{Int64}, label::Symbol, value}((5:5, :face, :julia_number))
@NamedTuple{region::UnitRange{Int64}, label::Symbol, value}((7:7, :face, :julia_number))
@NamedTuple{region::UnitRange{Int64}, label::Symbol, value}((8:8, :face, :julia_rainbow_paren_1))JuliaSyntaxHighlighting.highlight! — Functionhighlight!(content::Union{AnnotatedString, SubString{AnnotatedString}},
ast::JuliaSyntax.GreenNode = <parsed content>;
syntax_errors::Bool = false) -> contentModify content by applying syntax highlighting using JuliaSyntax.
By default, JuliaSyntax.parseall is used to generate to ast with the ignore_errors keyword argument set to true. Alternatively, one may provide a pre-generated ast.
When syntax_errors is set, the julia_error face is applied to detected syntax errors.
Note that the particular faces used by JuliaSyntax, and the way they are applied, is subject to change.
Examples
julia> str = Base.AnnotatedString("sum(1:8)")
"sum(1:8)"
julia> JuliaSyntaxHighlighting.highlight!(str)
"sum(1:8)"
julia> Base.annotations(str)
5-element Vector{@NamedTuple{region::UnitRange{Int64}, label::Symbol, value}}:
@NamedTuple{region::UnitRange{Int64}, label::Symbol, value}((1:3, :face, :julia_funcall))
@NamedTuple{region::UnitRange{Int64}, label::Symbol, value}((4:4, :face, :julia_rainbow_paren_1))
@NamedTuple{region::UnitRange{Int64}, label::Symbol, value}((5:5, :face, :julia_number))
@NamedTuple{region::UnitRange{Int64}, label::Symbol, value}((7:7, :face, :julia_number))
@NamedTuple{region::UnitRange{Int64}, label::Symbol, value}((8:8, :face, :julia_rainbow_paren_1))Faces
The highlight/highlight! methods work by applying custom faces to Julia code. As part of the standard library, these faces use privileged face names, of the form julia_*. These can be re-used in other packages, and customised with faces.toml configuration.
The particular faces used by JuliaSyntaxHighlighting are liable to change without warning in point releases. As the syntax highlighting rules are refined over time, changes should become less and less frequent though.
The current set of faces, and their default values are as follows:
julia_macro: magentajulia_symbol: magentajulia_singleton_identifier: inherits fromjulia_symboljulia_type: yellowjulia_typedec: bright bluejulia_comment: greyjulia_string: greenjulia_regex: inherits fromjulia_stringjulia_backslash_literal: magenta, inherits fromjulia_stringjulia_string_delim: bright greenjulia_cmdstring: inherits fromjulia_stringjulia_char: inherits fromjulia_stringjulia_char_delim: inherits fromjulia_string_delimjulia_number: bright magentajulia_bool: inherits fromjulia_numberjulia_funcall: cyanjulia_broadcast: bright blue, boldjulia_builtin: bright bluejulia_operator: bluejulia_comparator: inherits fromjulia_operatorjulia_assignment: bright redjulia_keyword: redjulia_parentheses: unstyledjulia_unpaired_parentheses: inherit fromjulia_errorandjulia_parenthesesjulia_error: red backgroundjulia_rainbow_paren_1: bright green, inherits fromjulia_parenthesesjulia_rainbow_paren_2: bright blue, inherits fromjulia_parenthesesjulia_rainbow_paren_3: bright red, inherits fromjulia_parenthesesjulia_rainbow_paren_4: inherits fromjulia_rainbow_paren_1julia_rainbow_paren_5: inherits fromjulia_rainbow_paren_2julia_rainbow_paren_6: inherits fromjulia_rainbow_paren_3julia_rainbow_bracket_1: blue, inherits fromjulia_parenthesesjulia_rainbow_bracket_2: brightmagenta, inherits from `juliaparentheses`julia_rainbow_bracket_3: inherits fromjulia_rainbow_bracket_1julia_rainbow_bracket_4: inherits fromjulia_rainbow_bracket_2julia_rainbow_bracket_5: inherits fromjulia_rainbow_bracket_1julia_rainbow_bracket_6: inherits fromjulia_rainbow_bracket_2julia_rainbow_curly_1: bright yellow, inherits fromjulia_parenthesesjulia_rainbow_curly_2: yellow, inherits fromjulia_parenthesesjulia_rainbow_curly_3: inherits fromjulia_rainbow_curly_1julia_rainbow_curly_4: inherits fromjulia_rainbow_curly_2julia_rainbow_curly_5: inherits fromjulia_rainbow_curly_1julia_rainbow_curly_6: inherits fromjulia_rainbow_curly_2