Skip to content

Commit 8fdd8fb

Browse files
authored
share the parsing result between the REPL styling passes to not parse the same input many times (#59868)
1 parent c4f2878 commit 8fdd8fb

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

stdlib/REPL/src/StylingPasses.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,29 @@ function merge_annotations(annotated_strings::Vector{<:AnnotatedString})
3737
return result
3838
end
3939

40-
function apply_style(pass::StylingPass, input::String, context::StylingContext)
41-
return pass(input, context)::AnnotatedString{String}
40+
function apply_style(pass::StylingPass, input::String, ast, context::StylingContext)
41+
return pass(input, ast, context)::AnnotatedString{String}
4242
end
4343

4444
function apply_styling_passes(input::String, passes::Vector{StylingPass}, context::StylingContext)
4545
if isempty(passes)
4646
return AnnotatedString(input)
4747
end
4848

49-
results = [apply_style(pass, input, context) for pass in passes]
49+
# Parse once and share AST across all passes
50+
ast = JuliaSyntax.parseall(JuliaSyntax.GreenNode, input; ignore_errors=true)
51+
52+
results = [apply_style(pass, input, ast, context) for pass in passes]
5053
return merge_annotations(results)
5154
end
5255

5356
# Applies Julia syntax highlighting
5457
struct SyntaxHighlightPass <: StylingPass end
5558

56-
function (::SyntaxHighlightPass)(input::String, ::StylingContext)
59+
function (::SyntaxHighlightPass)(input::String, ast, ::StylingContext)
5760
try
58-
return JuliaSyntaxHighlighting.highlight(input)
61+
return JuliaSyntaxHighlighting.highlight(input, ast)
5962
catch e
60-
e isa InterruptException && rethrow()
6163
@error "Error in SyntaxHighlightPass" exception=(e, catch_backtrace()) maxlog=1
6264
return AnnotatedString(input)
6365
end
@@ -66,7 +68,7 @@ end
6668
# Applies inverse video styling to the selected region
6769
struct RegionHighlightPass <: StylingPass end
6870

69-
function (::RegionHighlightPass)(input::String, context::StylingContext)
71+
function (::RegionHighlightPass)(input::String, ::Any, context::StylingContext)
7072
result = AnnotatedString(input)
7173

7274
if context.region_start > 0 && context.region_stop >= context.region_start
@@ -86,23 +88,21 @@ end
8688

8789
EnclosingParenHighlightPass() = EnclosingParenHighlightPass(Face(weight=:bold, underline=true))
8890

89-
function (pass::EnclosingParenHighlightPass)(input::String, context::StylingContext)
91+
function (pass::EnclosingParenHighlightPass)(input::String, ast, context::StylingContext)
9092
result = AnnotatedString(input)
9193

9294
if isempty(input) || context.cursor_pos < 1
9395
return result
9496
end
9597

9698
try
97-
ast = JuliaSyntax.parseall(JuliaSyntax.GreenNode, input; ignore_errors=true)
9899
paren_pairs = find_enclosing_parens(input, ast, context.cursor_pos)
99100

100101
for (open_pos, close_pos) in paren_pairs
101102
annotate!(result, open_pos:open_pos, :face, pass.face)
102103
annotate!(result, close_pos:close_pos, :face, pass.face)
103104
end
104105
catch e
105-
e isa InterruptException && rethrow()
106106
@error "Error in EnclosingParenHighlightPass" exception=(e, catch_backtrace()) maxlog=1
107107
end
108108

0 commit comments

Comments
 (0)