Skip to content

Commit

Permalink
Resolve Faces when applying styling
Browse files Browse the repository at this point in the history
This is the JuliaSyntaxHighlighting counterpart to:
JuliaLang/StyledStrings.jl#99
  • Loading branch information
topolarity committed Oct 16, 2024
1 parent 19bd57b commit eb70d00
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/JuliaSyntaxHighlighting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module JuliaSyntaxHighlighting

import Base: JuliaSyntax, AnnotatedString, annotate!
import Base.JuliaSyntax: @K_str, Kind, GreenNode, parseall, kind, flags
using StyledStrings: Face, addface!
using StyledStrings: Face, addface!, getface

public highlight, highlight!

Expand Down Expand Up @@ -238,7 +238,7 @@ function _hl_annotations!(highlights::Vector{@NamedTuple{region::UnitRange{Int},
elseif nkind == K"`" || nkind == K"```"; :julia_cmdstring
elseif nkind == K"Char"
kind(lnode) == K"'" && !isempty(highlights) &&
(highlights[end] = (highlights[end][1], :face, :julia_char_delim))
(highlights[end] = (highlights[end][1], :face, getface(:julia_char_delim)))
:julia_char
elseif nkind == K"'" && kind(lnode) == K"Char"; :julia_char_delim
elseif nkind == K"true" || nkind == K"false"; :julia_bool
Expand All @@ -247,8 +247,8 @@ function _hl_annotations!(highlights::Vector{@NamedTuple{region::UnitRange{Int},
if nkind == K"="
ifelse(ppkind == K"for", :julia_keyword, :julia_assignment)
else # updating for <op>=
push!(highlights, (firstindex(content)+offset:node.span+offset-1, :face, :julia_operator))
push!(highlights, (node.span+offset:node.span+offset, :face, :julia_assignment))
push!(highlights, (firstindex(content)+offset:node.span+offset-1, :face, getface(:julia_operator)))
push!(highlights, (node.span+offset:node.span+offset, :face, getface(:julia_assignment)))
nothing
end
elseif nkind == K";" && pkind == K"parameters" && pnode == lnode
Expand Down Expand Up @@ -321,19 +321,19 @@ function _hl_annotations!(highlights::Vector{@NamedTuple{region::UnitRange{Int},
end
end
!isnothing(face) &&
push!(highlights, (region, :face, face))
push!(highlights, (region, :face, getface(face)))
if nkind == K"Comment"
for match in eachmatch(
r"(?:^|[(\[{[:space:]-])`([^[:space:]](?:.*?[^[:space:]])?)`(?:$|[!,\-.:;?\[\][:space:]])",
regionstr)
code = first(match.captures)
push!(highlights, (firstindex(content)+offset+code.offset:firstindex(content)+offset+code.offset+code.ncodeunits-1,
:face, :code))
:face, getface(:code)))
end
elseif nkind == K"String"
for match in eachmatch(r"\\.", regionstr)
push!(highlights, (firstindex(content)+offset+match.offset-1:firstindex(content)+offset+match.offset+ncodeunits(match.match)-2,
:face, :julia_backslash_literal))
:face, getface(:julia_backslash_literal)))
end
end
isempty(node.args) && return
Expand Down

0 comments on commit eb70d00

Please sign in to comment.