-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
repl: add a bunch of types to structs and methods #22377
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
8566385
5431b5e
ce44871
f9a4b39
8181394
b3e6589
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ using ..LineEdit | |
using ..REPLCompletions | ||
|
||
export | ||
AbstractREPL, | ||
BasicREPL, | ||
LineEditREPL, | ||
StreamREPL | ||
|
@@ -172,7 +173,7 @@ struct REPLBackendRef | |
response_channel::Channel | ||
end | ||
|
||
function run_repl(repl::AbstractREPL, consumer = x->nothing) | ||
function run_repl(repl::AbstractREPL, consumer::Function = x->nothing) | ||
repl_channel = Channel(1) | ||
response_channel = Channel(1) | ||
backend = start_repl_backend(repl_channel, response_channel) | ||
|
@@ -254,8 +255,8 @@ mutable struct LineEditREPL <: AbstractREPL | |
in_help::Bool | ||
envcolors::Bool | ||
waserror::Bool | ||
specialdisplay | ||
interface | ||
specialdisplay::Union{Void,Display} | ||
interface::ModalInterface | ||
backendref::REPLBackendRef | ||
LineEditREPL(t,hascolor,prompt_color,input_color,answer_color,shell_color,help_color,history_file,in_shell,in_help,envcolors) = | ||
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 | |
specialdisplay(r::AbstractREPL) = nothing | ||
terminal(r::LineEditREPL) = r.t | ||
|
||
LineEditREPL(t::TextTerminal, envcolors = false) = LineEditREPL(t, | ||
true, | ||
Base.text_colors[:green], | ||
Base.input_color(), | ||
Base.answer_color(), | ||
Base.text_colors[:red], | ||
Base.text_colors[:yellow], | ||
false, false, false, envcolors) | ||
|
||
mutable struct REPLCompletionProvider <: CompletionProvider; end | ||
|
||
mutable struct ShellCompletionProvider <: CompletionProvider; end | ||
LineEditREPL(t::TextTerminal, envcolors::Bool=false) = | ||
LineEditREPL(t, true, | ||
Base.text_colors[:green], | ||
Base.input_color(), | ||
Base.answer_color(), | ||
Base.text_colors[:red], | ||
Base.text_colors[:yellow], | ||
false, false, false, envcolors | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Odd indentation? Perhaps better the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is my preferred style for writing this kind of code. Clearly not universally agreed upon, but until we have an official style guide for this, I'm going to write it the way I prefer 😬 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also happens to be my and Invenia's preferred style :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair enough :). |
||
|
||
struct LatexCompletions <: CompletionProvider; end | ||
mutable struct REPLCompletionProvider <: CompletionProvider end | ||
mutable struct ShellCompletionProvider <: CompletionProvider end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems you can remove the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried that – they have finalizers, so it doesn't work. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah ok thanks There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need these two |
||
struct LatexCompletions <: CompletionProvider end | ||
|
||
beforecursor(buf::IOBuffer) = String(buf.data[1:buf.ptr-1]) | ||
|
||
|
@@ -305,16 +305,15 @@ function complete_line(c::LatexCompletions, s) | |
return ret, partial[range], should_complete | ||
end | ||
|
||
|
||
mutable struct REPLHistoryProvider <: HistoryProvider | ||
history::Array{String,1} | ||
history_file | ||
history_file::Union{Void,IO} | ||
start_idx::Int | ||
cur_idx::Int | ||
last_idx::Int | ||
last_buffer::IOBuffer | ||
last_mode | ||
mode_mapping | ||
last_mode::Union{Void,Prompt} | ||
mode_mapping::Dict | ||
modes::Array{Symbol,1} | ||
end | ||
REPLHistoryProvider(mode_mapping) = | ||
|
@@ -619,7 +618,9 @@ end | |
|
||
backend(r::AbstractREPL) = r.backendref | ||
|
||
send_to_backend(ast, backend::REPLBackendRef) = send_to_backend(ast, backend.repl_channel, backend.response_channel) | ||
send_to_backend(ast, backend::REPLBackendRef) = | ||
send_to_backend(ast, backend.repl_channel, backend.response_channel) | ||
|
||
function send_to_backend(ast, req, rep) | ||
put!(req, (ast, 1)) | ||
return take!(rep) # (val, bt) | ||
|
@@ -661,7 +662,7 @@ function prepare_next(repl::LineEditREPL) | |
println(terminal(repl)) | ||
end | ||
|
||
function mode_keymap(julia_prompt) | ||
function mode_keymap(julia_prompt::Prompt) | ||
AnyDict( | ||
'\b' => function (s,o...) | ||
if isempty(s) || position(LineEdit.buffer(s)) == 0 | ||
|
@@ -689,7 +690,11 @@ repl_filename(repl, hp) = "REPL" | |
const JL_PROMPT_PASTE = Ref(true) | ||
enable_promptpaste(v::Bool) = JL_PROMPT_PASTE[] = v | ||
|
||
function setup_interface(repl::LineEditREPL; hascolor = repl.hascolor, extra_repl_keymap = Dict{Any,Any}[]) | ||
function setup_interface( | ||
repl::LineEditREPL; | ||
hascolor::Bool = repl.hascolor, | ||
extra_repl_keymap::Vector{<:Dict} = Dict{Any,Any}[] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. did you mean There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I could do that, but in practice only Dicts get stuck in there There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then why not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I see what you're saying. Yes, that makes sense, not sure what I was thinking here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, that's funny. Pedantic question: do we say There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh I got confused, no 100% familiar yet with this |
||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is my preferred style, although obviously not everyone agrees. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also happens to be my and Invenia's preferred style :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Glad to know I'm not the only one 😁 |
||
### | ||
# | ||
# This function returns the main interface that describes the REPL | ||
|
@@ -932,7 +937,7 @@ function setup_interface(repl::LineEditREPL; hascolor = repl.hascolor, extra_rep | |
ModalInterface([julia_prompt, shell_mode, help_mode, search_prompt, prefix_prompt]) | ||
end | ||
|
||
function run_frontend(repl::LineEditREPL, backend) | ||
function run_frontend(repl::LineEditREPL, backend::REPLBackendRef) | ||
d = REPLDisplay(repl) | ||
dopushdisplay = repl.specialdisplay === nothing && !in(d,Base.Multimedia.displays) | ||
dopushdisplay && pushdisplay(d) | ||
|
@@ -975,7 +980,7 @@ input_color(r::StreamREPL) = r.input_color | |
|
||
# heuristic function to decide if the presence of a semicolon | ||
# at the end of the expression was intended for suppressing output | ||
function ends_with_semicolon(line) | ||
function ends_with_semicolon(line::AbstractString) | ||
match = rsearch(line, ';') | ||
if match != 0 | ||
# 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) | |
dopushdisplay && popdisplay(d) | ||
end | ||
|
||
function start_repl_server(port) | ||
function start_repl_server(port::Int) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's an internal function and if the value is too large, this won't work anyway, so Int is ok. |
||
listen(port) do server, status | ||
client = accept(server) | ||
run_repl(client) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
module Terminals | ||
|
||
export | ||
AbstractTerminal, | ||
TextTerminal, | ||
UnixTerminal, | ||
TerminalBuffer, | ||
|
@@ -33,11 +34,15 @@ import Base: | |
read, | ||
readuntil | ||
|
||
## AbstractTerminal: abstract supertype of all terminals ## | ||
|
||
abstract type AbstractTerminal <: Base.AbstractPipe end | ||
|
||
## TextTerminal ## | ||
|
||
abstract type TextTerminal <: Base.AbstractPipe end | ||
abstract type TextTerminal <: AbstractTerminal end | ||
|
||
# INTERFACE | ||
# Terminal interface: | ||
pipe_reader(::TextTerminal) = error("Unimplemented") | ||
pipe_writer(::TextTerminal) = error("Unimplemented") | ||
displaysize(::TextTerminal) = error("Unimplemented") | ||
|
@@ -95,7 +100,7 @@ pipe_reader(t::UnixTerminal) = t.in_stream | |
pipe_writer(t::UnixTerminal) = t.out_stream | ||
|
||
mutable struct TerminalBuffer <: UnixTerminal | ||
out_stream::Base.IO | ||
out_stream::IO | ||
end | ||
|
||
mutable struct TTYTerminal <: UnixTerminal | ||
|
@@ -113,22 +118,17 @@ cmove_right(t::UnixTerminal, n) = write(t.out_stream, "$(CSI)$(n)C") | |
cmove_left(t::UnixTerminal, n) = write(t.out_stream, "$(CSI)$(n)D") | ||
cmove_line_up(t::UnixTerminal, n) = (cmove_up(t, n); cmove_col(t, 1)) | ||
cmove_line_down(t::UnixTerminal, n) = (cmove_down(t, n); cmove_col(t, 1)) | ||
cmove_col(t::UnixTerminal, n) = (write(t.out_stream, '\r'); n > 1 && cmove_right(t, n - 1)) | ||
cmove_col(t::UnixTerminal, n) = (write(t.out_stream, '\r'); n > 1 && cmove_right(t, n-1)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why change this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be consistent with the formatting in the rest of the file, which doesn't have spaces in small arithmetic expressions anywhere else. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, thanks for clarifying |
||
|
||
if is_windows() | ||
function raw!(t::TTYTerminal,raw::Bool) | ||
check_open(t.in_stream) | ||
if Base.ispty(t.in_stream) | ||
run(if raw | ||
`stty raw -echo onlcr -ocrnl opost` | ||
else | ||
`stty sane` | ||
end,t.in_stream,t.out_stream,t.err_stream) | ||
run((raw ? `stty raw -echo onlcr -ocrnl opost` : `stty sane`), | ||
t.in_stream, t.out_stream, t.err_stream) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 :) |
||
true | ||
else | ||
ccall(:jl_tty_set_mode, | ||
Int32, (Ptr{Void},Int32), | ||
t.in_stream.handle, raw) != -1 | ||
ccall(:jl_tty_set_mode, Int32, (Ptr{Void},Int32), t.in_stream.handle, raw) != -1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing space separating tuple arguments? |
||
end | ||
end | ||
else | ||
|
@@ -148,9 +148,7 @@ end | |
@eval clear_line(t::UnixTerminal) = write(t.out_stream, $"\r$(CSI)0K") | ||
#beep(t::UnixTerminal) = write(t.err_stream,"\x7") | ||
|
||
function Base.displaysize(t::UnixTerminal) | ||
return displaysize(t.out_stream) | ||
end | ||
Base.displaysize(t::UnixTerminal) = displaysize(t.out_stream) | ||
|
||
if is_windows() | ||
hascolor(t::TTYTerminal) = true | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -360,10 +360,10 @@ using .I18n | |
|
||
# frontend | ||
include("initdefs.jl") | ||
include("Terminals.jl") | ||
include("LineEdit.jl") | ||
include("REPLCompletions.jl") | ||
include("REPL.jl") | ||
include("repl/Terminals.jl") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. using joinpath if possible may avoid mixed forward and backslashes in backtraces on windows There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I finally understand why I see people use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd kind of prefer if we used forward slashes for joinpath and everything else even on Windows but that might be unlikely. Most Windows programs can handle forward slashes in paths just fine, but maybe not all. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should probably introduce Path types and have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This matches the style of all other includes in |
||
include("repl/LineEdit.jl") | ||
include("repl/REPLCompletions.jl") | ||
include("repl/REPL.jl") | ||
include("client.jl") | ||
|
||
# Stack frames and traces | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why comment the type annotation here? (similar question in other places, e.g. with
CompletionProvider
)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because that's the type in practice, but
HistoryPrompt
doesn't exist yet when this is defined. The reverse dependency is unfortunate and something that should be fixed, but that kind of refactoring is beyond the scope of this PR.