Skip to content

Commit 7affd0c

Browse files
Merge pull request #22377 from JuliaLang/sk/rerepl
repl: add a bunch of types to structs and methods
2 parents bd2878f + b3e6589 commit 7affd0c

File tree

8 files changed

+82
-82
lines changed

8 files changed

+82
-82
lines changed

base/LineEdit.jl renamed to base/repl/LineEdit.jl

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,32 @@ abstract type ModeState end
1515
export run_interface, Prompt, ModalInterface, transition, reset_state, edit_insert, keymap
1616

1717
struct ModalInterface <: TextInterface
18-
modes
18+
modes::Array{Base.LineEdit.TextInterface,1}
1919
end
2020

21+
mutable struct Prompt <: TextInterface
22+
prompt::String
23+
# A string or function to be printed before the prompt. May not change the length of the prompt.
24+
# This may be used for changing the color, issuing other terminal escape codes, etc.
25+
prompt_prefix::Union{String,Function}
26+
# Same as prefix except after the prompt
27+
prompt_suffix::Union{String,Function}
28+
keymap_dict::Dict{Char}
29+
keymap_func_data # ::AbstractREPL
30+
complete # ::REPLCompletionProvider
31+
on_enter::Function
32+
on_done::Function
33+
hist # ::REPLHistoryProvider
34+
sticky::Bool
35+
end
36+
37+
show(io::IO, x::Prompt) = show(io, string("Prompt(\"", x.prompt, "\",...)"))
38+
2139
mutable struct MIState
2240
interface::ModalInterface
23-
current_mode
41+
current_mode::TextInterface
2442
aborted::Bool
25-
mode_state
43+
mode_state::Dict
2644
kill_buffer::String
2745
previous_key::Array{Char,1}
2846
key_repeats::Int
@@ -33,31 +51,13 @@ function show(io::IO, s::MIState)
3351
print(io, "MI State (", s.current_mode, " active)")
3452
end
3553

36-
mutable struct Prompt <: TextInterface
37-
prompt
38-
# A string or function to be printed before the prompt. May not change the length of the prompt.
39-
# This may be used for changing the color, issuing other terminal escape codes, etc.
40-
prompt_prefix
41-
# Same as prefix except after the prompt
42-
prompt_suffix
43-
keymap_dict
44-
keymap_func_data
45-
complete
46-
on_enter
47-
on_done
48-
hist
49-
sticky::Bool
50-
end
51-
52-
show(io::IO, x::Prompt) = show(io, string("Prompt(\"", x.prompt, "\",...)"))
53-
5454
struct InputAreaState
5555
num_rows::Int64
5656
curs_row::Int64
5757
end
5858

5959
mutable struct PromptState <: ModeState
60-
terminal
60+
terminal::AbstractTerminal
6161
p::Prompt
6262
input_buffer::IOBuffer
6363
ias::InputAreaState
@@ -77,11 +77,8 @@ end
7777
abstract type HistoryProvider end
7878
abstract type CompletionProvider end
7979

80-
mutable struct EmptyCompletionProvider <: CompletionProvider
81-
end
82-
83-
mutable struct EmptyHistoryProvider <: HistoryProvider
84-
end
80+
struct EmptyCompletionProvider <: CompletionProvider end
81+
struct EmptyHistoryProvider <: HistoryProvider end
8582

8683
reset_state(::EmptyHistoryProvider) = nothing
8784

@@ -970,15 +967,15 @@ function write_response_buffer(s::PromptState, data)
970967
end
971968

972969
mutable struct SearchState <: ModeState
973-
terminal
974-
histprompt
970+
terminal::AbstractTerminal
971+
histprompt # ::HistoryPrompt
975972
#rsearch (true) or ssearch (false)
976973
backward::Bool
977974
query_buffer::IOBuffer
978975
response_buffer::IOBuffer
979976
ias::InputAreaState
980977
#The prompt whose input will be replaced by the matched history
981-
parent
978+
parent::Prompt
982979
SearchState(terminal, histprompt, backward, query_buffer, response_buffer) =
983980
new(terminal, histprompt, backward, query_buffer, response_buffer, InputAreaState(0,0))
984981
end
@@ -1015,7 +1012,7 @@ end
10151012

10161013
mutable struct HistoryPrompt{T<:HistoryProvider} <: TextInterface
10171014
hp::T
1018-
complete
1015+
complete # ::CompletionProvider
10191016
keymap_dict::Dict{Char,Any}
10201017
HistoryPrompt{T}(hp) where T<:HistoryProvider = new(hp, EmptyCompletionProvider())
10211018
end
@@ -1024,16 +1021,16 @@ HistoryPrompt(hp::T) where T<:HistoryProvider = HistoryPrompt{T}(hp)
10241021
init_state(terminal, p::HistoryPrompt) = SearchState(terminal, p, true, IOBuffer(), IOBuffer())
10251022

10261023
mutable struct PrefixSearchState <: ModeState
1027-
terminal
1028-
histprompt
1024+
terminal::AbstractTerminal
1025+
histprompt # ::HistoryPrompt
10291026
prefix::String
10301027
response_buffer::IOBuffer
10311028
ias::InputAreaState
10321029
indent::Int
10331030
# The modal interface state, if present
1034-
mi
1031+
mi::MIState
10351032
#The prompt whose input will be replaced by the matched history
1036-
parent
1033+
parent::Prompt
10371034
PrefixSearchState(terminal, histprompt, prefix, response_buffer) =
10381035
new(terminal, histprompt, prefix, response_buffer, InputAreaState(0,0), 0)
10391036
end
@@ -1055,7 +1052,7 @@ input_string(s::PrefixSearchState) = String(s.response_buffer)
10551052
mutable struct PrefixHistoryPrompt{T<:HistoryProvider} <: TextInterface
10561053
hp::T
10571054
parent_prompt::Prompt
1058-
complete
1055+
complete # ::CompletionProvider
10591056
keymap_dict::Dict{Char,Any}
10601057
PrefixHistoryPrompt{T}(hp, parent_prompt) where T<:HistoryProvider =
10611058
new(hp, parent_prompt, EmptyCompletionProvider())

base/REPL.jl renamed to base/repl/REPL.jl

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ using ..LineEdit
88
using ..REPLCompletions
99

1010
export
11+
AbstractREPL,
1112
BasicREPL,
1213
LineEditREPL,
1314
StreamREPL
@@ -172,7 +173,7 @@ struct REPLBackendRef
172173
response_channel::Channel
173174
end
174175

175-
function run_repl(repl::AbstractREPL, consumer = x->nothing)
176+
function run_repl(repl::AbstractREPL, consumer::Function = x->nothing)
176177
repl_channel = Channel(1)
177178
response_channel = Channel(1)
178179
backend = start_repl_backend(repl_channel, response_channel)
@@ -254,8 +255,8 @@ mutable struct LineEditREPL <: AbstractREPL
254255
in_help::Bool
255256
envcolors::Bool
256257
waserror::Bool
257-
specialdisplay
258-
interface
258+
specialdisplay::Union{Void,Display}
259+
interface::ModalInterface
259260
backendref::REPLBackendRef
260261
LineEditREPL(t,hascolor,prompt_color,input_color,answer_color,shell_color,help_color,history_file,in_shell,in_help,envcolors) =
261262
new(t,true,prompt_color,input_color,answer_color,shell_color,help_color,history_file,in_shell,
@@ -266,20 +267,19 @@ specialdisplay(r::LineEditREPL) = r.specialdisplay
266267
specialdisplay(r::AbstractREPL) = nothing
267268
terminal(r::LineEditREPL) = r.t
268269

269-
LineEditREPL(t::TextTerminal, envcolors = false) = LineEditREPL(t,
270-
true,
271-
Base.text_colors[:green],
272-
Base.input_color(),
273-
Base.answer_color(),
274-
Base.text_colors[:red],
275-
Base.text_colors[:yellow],
276-
false, false, false, envcolors)
277-
278-
mutable struct REPLCompletionProvider <: CompletionProvider; end
279-
280-
mutable struct ShellCompletionProvider <: CompletionProvider; end
270+
LineEditREPL(t::TextTerminal, envcolors::Bool=false) =
271+
LineEditREPL(t, true,
272+
Base.text_colors[:green],
273+
Base.input_color(),
274+
Base.answer_color(),
275+
Base.text_colors[:red],
276+
Base.text_colors[:yellow],
277+
false, false, false, envcolors
278+
)
281279

282-
struct LatexCompletions <: CompletionProvider; end
280+
mutable struct REPLCompletionProvider <: CompletionProvider end
281+
mutable struct ShellCompletionProvider <: CompletionProvider end
282+
struct LatexCompletions <: CompletionProvider end
283283

284284
beforecursor(buf::IOBuffer) = String(buf.data[1:buf.ptr-1])
285285

@@ -305,16 +305,15 @@ function complete_line(c::LatexCompletions, s)
305305
return ret, partial[range], should_complete
306306
end
307307

308-
309308
mutable struct REPLHistoryProvider <: HistoryProvider
310309
history::Array{String,1}
311-
history_file
310+
history_file::Union{Void,IO}
312311
start_idx::Int
313312
cur_idx::Int
314313
last_idx::Int
315314
last_buffer::IOBuffer
316-
last_mode
317-
mode_mapping
315+
last_mode::Union{Void,Prompt}
316+
mode_mapping::Dict
318317
modes::Array{Symbol,1}
319318
end
320319
REPLHistoryProvider(mode_mapping) =
@@ -619,7 +618,9 @@ end
619618

620619
backend(r::AbstractREPL) = r.backendref
621620

622-
send_to_backend(ast, backend::REPLBackendRef) = send_to_backend(ast, backend.repl_channel, backend.response_channel)
621+
send_to_backend(ast, backend::REPLBackendRef) =
622+
send_to_backend(ast, backend.repl_channel, backend.response_channel)
623+
623624
function send_to_backend(ast, req, rep)
624625
put!(req, (ast, 1))
625626
return take!(rep) # (val, bt)
@@ -661,7 +662,7 @@ function prepare_next(repl::LineEditREPL)
661662
println(terminal(repl))
662663
end
663664

664-
function mode_keymap(julia_prompt)
665+
function mode_keymap(julia_prompt::Prompt)
665666
AnyDict(
666667
'\b' => function (s,o...)
667668
if isempty(s) || position(LineEdit.buffer(s)) == 0
@@ -689,7 +690,11 @@ repl_filename(repl, hp) = "REPL"
689690
const JL_PROMPT_PASTE = Ref(true)
690691
enable_promptpaste(v::Bool) = JL_PROMPT_PASTE[] = v
691692

692-
function setup_interface(repl::LineEditREPL; hascolor = repl.hascolor, extra_repl_keymap = Dict{Any,Any}[])
693+
function setup_interface(
694+
repl::LineEditREPL;
695+
hascolor::Bool = repl.hascolor,
696+
extra_repl_keymap::Vector{<:Dict} = Dict{Any,Any}[]
697+
)
693698
###
694699
#
695700
# This function returns the main interface that describes the REPL
@@ -932,7 +937,7 @@ function setup_interface(repl::LineEditREPL; hascolor = repl.hascolor, extra_rep
932937
ModalInterface([julia_prompt, shell_mode, help_mode, search_prompt, prefix_prompt])
933938
end
934939

935-
function run_frontend(repl::LineEditREPL, backend)
940+
function run_frontend(repl::LineEditREPL, backend::REPLBackendRef)
936941
d = REPLDisplay(repl)
937942
dopushdisplay = repl.specialdisplay === nothing && !in(d,Base.Multimedia.displays)
938943
dopushdisplay && pushdisplay(d)
@@ -975,7 +980,7 @@ input_color(r::StreamREPL) = r.input_color
975980

976981
# heuristic function to decide if the presence of a semicolon
977982
# at the end of the expression was intended for suppressing output
978-
function ends_with_semicolon(line)
983+
function ends_with_semicolon(line::AbstractString)
979984
match = rsearch(line, ';')
980985
if match != 0
981986
# state for comment parser, assuming that the `;` isn't in a string or comment
@@ -1057,7 +1062,7 @@ function run_frontend(repl::StreamREPL, backend::REPLBackendRef)
10571062
dopushdisplay && popdisplay(d)
10581063
end
10591064

1060-
function start_repl_server(port)
1065+
function start_repl_server(port::Int)
10611066
listen(port) do server, status
10621067
client = accept(server)
10631068
run_repl(client)
File renamed without changes.

base/Terminals.jl renamed to base/repl/Terminals.jl

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
module Terminals
44

55
export
6+
AbstractTerminal,
67
TextTerminal,
78
UnixTerminal,
89
TerminalBuffer,
@@ -33,11 +34,15 @@ import Base:
3334
read,
3435
readuntil
3536

37+
## AbstractTerminal: abstract supertype of all terminals ##
38+
39+
abstract type AbstractTerminal <: Base.AbstractPipe end
40+
3641
## TextTerminal ##
3742

38-
abstract type TextTerminal <: Base.AbstractPipe end
43+
abstract type TextTerminal <: AbstractTerminal end
3944

40-
# INTERFACE
45+
# Terminal interface:
4146
pipe_reader(::TextTerminal) = error("Unimplemented")
4247
pipe_writer(::TextTerminal) = error("Unimplemented")
4348
displaysize(::TextTerminal) = error("Unimplemented")
@@ -95,7 +100,7 @@ pipe_reader(t::UnixTerminal) = t.in_stream
95100
pipe_writer(t::UnixTerminal) = t.out_stream
96101

97102
mutable struct TerminalBuffer <: UnixTerminal
98-
out_stream::Base.IO
103+
out_stream::IO
99104
end
100105

101106
mutable struct TTYTerminal <: UnixTerminal
@@ -113,22 +118,17 @@ cmove_right(t::UnixTerminal, n) = write(t.out_stream, "$(CSI)$(n)C")
113118
cmove_left(t::UnixTerminal, n) = write(t.out_stream, "$(CSI)$(n)D")
114119
cmove_line_up(t::UnixTerminal, n) = (cmove_up(t, n); cmove_col(t, 1))
115120
cmove_line_down(t::UnixTerminal, n) = (cmove_down(t, n); cmove_col(t, 1))
116-
cmove_col(t::UnixTerminal, n) = (write(t.out_stream, '\r'); n > 1 && cmove_right(t, n - 1))
121+
cmove_col(t::UnixTerminal, n) = (write(t.out_stream, '\r'); n > 1 && cmove_right(t, n-1))
117122

118123
if is_windows()
119124
function raw!(t::TTYTerminal,raw::Bool)
120125
check_open(t.in_stream)
121126
if Base.ispty(t.in_stream)
122-
run(if raw
123-
`stty raw -echo onlcr -ocrnl opost`
124-
else
125-
`stty sane`
126-
end,t.in_stream,t.out_stream,t.err_stream)
127+
run((raw ? `stty raw -echo onlcr -ocrnl opost` : `stty sane`),
128+
t.in_stream, t.out_stream, t.err_stream)
127129
true
128130
else
129-
ccall(:jl_tty_set_mode,
130-
Int32, (Ptr{Void},Int32),
131-
t.in_stream.handle, raw) != -1
131+
ccall(:jl_tty_set_mode, Int32, (Ptr{Void},Int32), t.in_stream.handle, raw) != -1
132132
end
133133
end
134134
else
@@ -148,9 +148,7 @@ end
148148
@eval clear_line(t::UnixTerminal) = write(t.out_stream, $"\r$(CSI)0K")
149149
#beep(t::UnixTerminal) = write(t.err_stream,"\x7")
150150

151-
function Base.displaysize(t::UnixTerminal)
152-
return displaysize(t.out_stream)
153-
end
151+
Base.displaysize(t::UnixTerminal) = displaysize(t.out_stream)
154152

155153
if is_windows()
156154
hascolor(t::TTYTerminal) = true
File renamed without changes.
File renamed without changes.

base/sysimg.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -360,10 +360,10 @@ using .I18n
360360

361361
# frontend
362362
include("initdefs.jl")
363-
include("Terminals.jl")
364-
include("LineEdit.jl")
365-
include("REPLCompletions.jl")
366-
include("REPL.jl")
363+
include("repl/Terminals.jl")
364+
include("repl/LineEdit.jl")
365+
include("repl/REPLCompletions.jl")
366+
include("repl/REPL.jl")
367367
include("client.jl")
368368

369369
# Stack frames and traces

doc/src/manual/interacting-with-julia.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ end
202202
atreplinit(customize_keys)
203203
```
204204

205-
Users should refer to `base/LineEdit.jl` to discover the available actions on key input.
205+
Users should refer to `LineEdit.jl` to discover the available actions on key input.
206206

207207
## Tab completion
208208

0 commit comments

Comments
 (0)