Skip to content

Commit 5853103

Browse files
authored
Finish adding types to fields in REPL (#36281)
This finishes the work started in #22377
1 parent 4331b24 commit 5853103

File tree

2 files changed

+33
-33
lines changed

2 files changed

+33
-33
lines changed

stdlib/REPL/src/LineEdit.jl

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
module LineEdit
44

55
import ..REPL
6-
using ..Terminals
6+
using REPL: AbstractREPL
77

8+
using ..Terminals
89
import ..Terminals: raw!, width, height, cmove, getX,
910
getY, clear_line, beep
1011

@@ -13,6 +14,8 @@ using Base: something
1314

1415
abstract type TextInterface end
1516
abstract type ModeState end
17+
abstract type HistoryProvider end
18+
abstract type CompletionProvider end
1619

1720
export run_interface, Prompt, ModalInterface, transition, reset_state, edit_insert, keymap
1821

@@ -31,11 +34,11 @@ mutable struct Prompt <: TextInterface
3134
# Same as prefix except after the prompt
3235
prompt_suffix::Union{String,Function}
3336
keymap_dict::Dict{Char}
34-
repl # ::AbstractREPL
35-
complete # ::REPLCompletionProvider
37+
repl::Union{AbstractREPL,Nothing}
38+
complete::CompletionProvider
3639
on_enter::Function
3740
on_done::Function
38-
hist # ::REPLHistoryProvider
41+
hist::HistoryProvider
3942
sticky::Bool
4043
end
4144

@@ -141,9 +144,6 @@ function input_string_newlines_aftercursor(s::PromptState)
141144
return count(c->(c == '\n'), rest)
142145
end
143146

144-
abstract type HistoryProvider end
145-
abstract type CompletionProvider end
146-
147147
struct EmptyCompletionProvider <: CompletionProvider end
148148
struct EmptyHistoryProvider <: HistoryProvider end
149149

@@ -1602,9 +1602,16 @@ const escape_defaults = merge!(
16021602
AnyDict("\e[$(c)l" => nothing for c in 1:20)
16031603
)
16041604

1605+
mutable struct HistoryPrompt <: TextInterface
1606+
hp::HistoryProvider
1607+
complete::CompletionProvider
1608+
keymap_dict::Dict{Char,Any}
1609+
HistoryPrompt(hp) = new(hp, EmptyCompletionProvider())
1610+
end
1611+
16051612
mutable struct SearchState <: ModeState
16061613
terminal::AbstractTerminal
1607-
histprompt # ::HistoryPrompt
1614+
histprompt::HistoryPrompt
16081615
#rsearch (true) or ssearch (false)
16091616
backward::Bool
16101617
query_buffer::IOBuffer
@@ -1617,6 +1624,8 @@ mutable struct SearchState <: ModeState
16171624
new(terminal, histprompt, backward, query_buffer, response_buffer, false, InputAreaState(0,0))
16181625
end
16191626

1627+
init_state(terminal, p::HistoryPrompt) = SearchState(terminal, p, true, IOBuffer(), IOBuffer())
1628+
16201629
terminal(s::SearchState) = s.terminal
16211630

16221631
function update_display_buffer(s::SearchState, data)
@@ -1654,18 +1663,20 @@ function reset_state(s::SearchState)
16541663
nothing
16551664
end
16561665

1657-
mutable struct HistoryPrompt <: TextInterface
1658-
hp # ::HistoryProvider
1659-
complete # ::CompletionProvider
1666+
# a meta-prompt that presents itself as parent_prompt, but which has an independent keymap
1667+
# for prefix searching
1668+
mutable struct PrefixHistoryPrompt <: TextInterface
1669+
hp::HistoryProvider
1670+
parent_prompt::Prompt
1671+
complete::CompletionProvider
16601672
keymap_dict::Dict{Char,Any}
1661-
HistoryPrompt(hp) = new(hp, EmptyCompletionProvider())
1673+
PrefixHistoryPrompt(hp, parent_prompt) =
1674+
new(hp, parent_prompt, EmptyCompletionProvider())
16621675
end
16631676

1664-
init_state(terminal, p::HistoryPrompt) = SearchState(terminal, p, true, IOBuffer(), IOBuffer())
1665-
16661677
mutable struct PrefixSearchState <: ModeState
16671678
terminal::AbstractTerminal
1668-
histprompt # ::HistoryPrompt
1679+
histprompt::PrefixHistoryPrompt
16691680
prefix::String
16701681
response_buffer::IOBuffer
16711682
ias::InputAreaState
@@ -1678,6 +1689,8 @@ mutable struct PrefixSearchState <: ModeState
16781689
new(terminal, histprompt, prefix, response_buffer, InputAreaState(0,0), 0)
16791690
end
16801691

1692+
init_state(terminal, p::PrefixHistoryPrompt) = PrefixSearchState(terminal, p, "", IOBuffer())
1693+
16811694
function show(io::IO, s::PrefixSearchState)
16821695
print(io, "PrefixSearchState ", isdefined(s,:parent) ?
16831696
string("(", s.parent, " active)") : "(no parent)", " for ",
@@ -1696,19 +1709,6 @@ end
16961709

16971710
input_string(s::PrefixSearchState) = String(take!(copy(s.response_buffer)))
16981711

1699-
# a meta-prompt that presents itself as parent_prompt, but which has an independent keymap
1700-
# for prefix searching
1701-
mutable struct PrefixHistoryPrompt <: TextInterface
1702-
hp # ::HistoryProvider
1703-
parent_prompt::Prompt
1704-
complete # ::CompletionProvider
1705-
keymap_dict::Dict{Char,Any}
1706-
PrefixHistoryPrompt(hp, parent_prompt) =
1707-
new(hp, parent_prompt, EmptyCompletionProvider())
1708-
end
1709-
1710-
init_state(terminal, p::PrefixHistoryPrompt) = PrefixSearchState(terminal, p, "", IOBuffer())
1711-
17121712
write_prompt(terminal, s::PrefixSearchState) = write_prompt(terminal, s.histprompt.parent_prompt)
17131713
prompt_string(s::PrefixSearchState) = prompt_string(s.histprompt.parent_prompt.prompt)
17141714

stdlib/REPL/src/REPL.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ import Base:
3636
include("Terminals.jl")
3737
using .Terminals
3838

39+
abstract type AbstractREPL end
40+
3941
include("LineEdit.jl")
4042
using .LineEdit
4143
import ..LineEdit:
@@ -66,8 +68,6 @@ function __init__()
6668
Base.REPL_MODULE_REF[] = REPL
6769
end
6870

69-
abstract type AbstractREPL end
70-
7171
answer_color(::AbstractREPL) = ""
7272

7373
const JULIA_PROMPT = "julia> "
@@ -496,15 +496,15 @@ function complete_line(c::LatexCompletions, s)
496496
end
497497

498498
mutable struct REPLHistoryProvider <: HistoryProvider
499-
history::Array{String,1}
499+
history::Vector{String}
500500
history_file::Union{Nothing,IO}
501501
start_idx::Int
502502
cur_idx::Int
503503
last_idx::Int
504504
last_buffer::IOBuffer
505505
last_mode::Union{Nothing,Prompt}
506-
mode_mapping::Dict
507-
modes::Array{Symbol,1}
506+
mode_mapping::Dict{Symbol,Prompt}
507+
modes::Vector{Symbol}
508508
end
509509
REPLHistoryProvider(mode_mapping) =
510510
REPLHistoryProvider(String[], nothing, 0, 0, -1, IOBuffer(),

0 commit comments

Comments
 (0)