Skip to content

Commit 0a7fd04

Browse files
authored
Merge pull request #23637 from JuliaLang/rf/repl/options
REPL: add an Option type to handle REPL options
2 parents 7e143bb + 0245e2e commit 0a7fd04

File tree

3 files changed

+39
-19
lines changed

3 files changed

+39
-19
lines changed

base/repl/LineEdit.jl

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ mutable struct Prompt <: TextInterface
2727
# Same as prefix except after the prompt
2828
prompt_suffix::Union{String,Function}
2929
keymap_dict::Dict{Char}
30-
keymap_func_data # ::AbstractREPL
30+
repl # ::AbstractREPL
3131
complete # ::REPLCompletionProvider
3232
on_enter::Function
3333
on_done::Function
@@ -75,6 +75,8 @@ mutable struct PromptState <: ModeState
7575
indent::Int
7676
end
7777

78+
options(s::PromptState) = isdefined(s.p, :repl) ? s.p.repl.options : Base.REPL.Options()
79+
7880
setmark(s) = mark(buffer(s))
7981

8082
# the default mark is 0
@@ -116,7 +118,7 @@ terminal(s::PromptState) = s.terminal
116118

117119
for f in [:terminal, :on_enter, :add_history, :buffer, :(Base.isempty),
118120
:replace_line, :refresh_multi_line, :input_string, :update_display_buffer,
119-
:empty_undo, :push_undo, :pop_undo]
121+
:empty_undo, :push_undo, :pop_undo, :options]
120122
@eval ($f)(s::MIState, args...) = $(f)(state(s), args...)
121123
end
122124

@@ -549,9 +551,10 @@ end
549551
# align: delete up to 4 spaces to align to a multiple of 4 chars
550552
# adjust: also delete spaces on the right of the cursor to try to keep aligned what is
551553
# on the right
552-
function edit_backspace(s::PromptState, align::Bool=false, adjust=align)
554+
function edit_backspace(s::PromptState, align::Bool=options(s).backspace_align,
555+
adjust=options(s).backspace_adjust)
553556
push_undo(s)
554-
if edit_backspace(buffer(s), align)
557+
if edit_backspace(buffer(s), align, adjust)
555558
refresh_line(s)
556559
else
557560
pop_undo(s)
@@ -571,7 +574,7 @@ function endofline(buf, pos=position(buf))
571574
eol == 0 ? buf.size : pos + eol - 1
572575
end
573576

574-
function edit_backspace(buf::IOBuffer, align::Bool=false, adjust::Bool=align)
577+
function edit_backspace(buf::IOBuffer, align::Bool=false, adjust::Bool=false)
575578
!align && adjust &&
576579
throw(DomainError((align, adjust),
577580
"if `adjust` is `true`, `align` must be `true`"))
@@ -1650,7 +1653,7 @@ AnyDict(
16501653
end,
16511654
'\n' => KeyAlias('\r'),
16521655
# Backspace/^H
1653-
'\b' => (s,o...)->edit_backspace(s, true),
1656+
'\b' => (s,o...)->edit_backspace(s),
16541657
127 => KeyAlias('\b'),
16551658
# Meta Backspace
16561659
"\e\b" => (s,o...)->edit_delete_prev_word(s),
@@ -1861,14 +1864,14 @@ function Prompt(prompt;
18611864
prompt_prefix = "",
18621865
prompt_suffix = "",
18631866
keymap_dict = default_keymap_dict,
1864-
keymap_func_data = nothing,
1867+
repl = nothing,
18651868
complete = EmptyCompletionProvider(),
18661869
on_enter = default_enter_cb,
18671870
on_done = ()->nothing,
18681871
hist = EmptyHistoryProvider(),
18691872
sticky = false)
18701873

1871-
Prompt(prompt, prompt_prefix, prompt_suffix, keymap_dict, keymap_func_data,
1874+
Prompt(prompt, prompt_prefix, prompt_suffix, keymap_dict, repl,
18721875
complete, on_enter, on_done, hist, sticky)
18731876
end
18741877

@@ -1969,7 +1972,7 @@ end
19691972
edit_redo!(s) = nothing
19701973

19711974
keymap(s::PromptState, prompt::Prompt) = prompt.keymap_dict
1972-
keymap_data(s::PromptState, prompt::Prompt) = prompt.keymap_func_data
1975+
keymap_data(s::PromptState, prompt::Prompt) = prompt.repl
19731976
keymap(ms::MIState, m::ModalInterface) = keymap(state(ms), mode(ms))
19741977
keymap_data(ms::MIState, m::ModalInterface) = keymap_data(state(ms), mode(ms))
19751978

base/repl/REPL.jl

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,21 @@ function run_frontend(repl::BasicREPL, backend::REPLBackendRef)
241241
dopushdisplay && popdisplay(d)
242242
end
243243

244+
## User Options
245+
246+
mutable struct Options
247+
hascolor::Bool
248+
extra_keymap::Union{Dict,Vector{<:Dict}}
249+
backspace_align::Bool
250+
backspace_adjust::Bool
251+
end
252+
253+
Options(;
254+
hascolor = true,
255+
extra_keymap = AnyDict[],
256+
backspace_align = true, backspace_adjust = backspace_align) =
257+
Options(hascolor, extra_keymap, backspace_align, backspace_adjust)
258+
244259
## LineEditREPL ##
245260

246261
mutable struct LineEditREPL <: AbstractREPL
@@ -257,11 +272,12 @@ mutable struct LineEditREPL <: AbstractREPL
257272
envcolors::Bool
258273
waserror::Bool
259274
specialdisplay::Union{Void,Display}
275+
options::Options
260276
interface::ModalInterface
261277
backendref::REPLBackendRef
262278
LineEditREPL(t,hascolor,prompt_color,input_color,answer_color,shell_color,help_color,history_file,in_shell,in_help,envcolors) =
263279
new(t,true,prompt_color,input_color,answer_color,shell_color,help_color,history_file,in_shell,
264-
in_help,envcolors,false,nothing)
280+
in_help,envcolors,false,nothing, Options())
265281
end
266282
outstream(r::LineEditREPL) = r.t
267283
specialdisplay(r::LineEditREPL) = r.specialdisplay
@@ -703,8 +719,9 @@ enable_promptpaste(v::Bool) = JL_PROMPT_PASTE[] = v
703719

704720
function setup_interface(
705721
repl::LineEditREPL;
706-
hascolor::Bool = repl.hascolor,
707-
extra_repl_keymap::Union{Dict,Vector{<:Dict}} = Dict{Any,Any}[]
722+
# those keyword arguments may be deprecated eventually in favor of the Options mechanism
723+
hascolor::Bool = repl.options.hascolor,
724+
extra_repl_keymap::Union{Dict,Vector{<:Dict}} = repl.options.extra_keymap
708725
)
709726
###
710727
#
@@ -741,7 +758,7 @@ function setup_interface(
741758
prompt_prefix = hascolor ? repl.prompt_color : "",
742759
prompt_suffix = hascolor ?
743760
(repl.envcolors ? Base.input_color : repl.input_color) : "",
744-
keymap_func_data = repl,
761+
repl = repl,
745762
complete = replc,
746763
on_enter = return_callback)
747764

@@ -750,7 +767,7 @@ function setup_interface(
750767
prompt_prefix = hascolor ? repl.help_color : "",
751768
prompt_suffix = hascolor ?
752769
(repl.envcolors ? Base.input_color : repl.input_color) : "",
753-
keymap_func_data = repl,
770+
repl = repl,
754771
complete = replc,
755772
# When we're done transform the entered line into a call to help("$line")
756773
on_done = respond(Docs.helpmode, repl, julia_prompt))
@@ -760,7 +777,7 @@ function setup_interface(
760777
prompt_prefix = hascolor ? repl.shell_color : "",
761778
prompt_suffix = hascolor ?
762779
(repl.envcolors ? Base.input_color : repl.input_color) : "",
763-
keymap_func_data = repl,
780+
repl = repl,
764781
complete = ShellCompletionProvider(),
765782
# Transform "foo bar baz" into `foo bar baz` (shell quoting)
766783
# and pass into Base.repl_cmd for processing (handles `ls` and `cd`

test/lineedit.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ let buf = IOBuffer()
330330
@test position(buf) == 0
331331
LineEdit.edit_move_right(buf)
332332
@test nb_available(buf) == 0
333-
LineEdit.edit_backspace(buf)
333+
LineEdit.edit_backspace(buf, false, false)
334334
@test content(buf) == "a"
335335
end
336336

@@ -703,9 +703,9 @@ end
703703
@test edit!(edit_undo!) == "one two three"
704704

705705
LineEdit.move_line_end(s)
706-
LineEdit.edit_backspace(s)
707-
LineEdit.edit_backspace(s)
708-
@test edit!(LineEdit.edit_backspace) == "one two th"
706+
LineEdit.edit_backspace(s, false, false)
707+
LineEdit.edit_backspace(s, false, false)
708+
@test edit!(s->LineEdit.edit_backspace(s, false, false)) == "one two th"
709709
@test edit!(edit_undo!) == "one two thr"
710710
@test edit!(edit_undo!) == "one two thre"
711711
@test edit!(edit_undo!) == "one two three"

0 commit comments

Comments
 (0)