Skip to content

Commit

Permalink
bump version and add complete_julia function
Browse files Browse the repository at this point in the history
  • Loading branch information
MasonProtter committed Apr 8, 2021
1 parent 84792be commit 55363bc
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ReplMaker"
uuid = "b873ce64-0db9-51f5-a568-4457d8e49576"
authors = ["MasonProtter <[email protected]>"]
version = "0.2.4"
version = "0.2.5"

[deps]
REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
Expand Down
28 changes: 3 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,38 +61,16 @@ Next, we might notice that if we try to do a multiline expression, the REPL mode
Expr> function f(x)
:($(Expr(:incomplete, "incomplete: premature end of input")))
```
This is because we haven't told our REPL mode what constitues a valid, complete line. Since this REPL mode is just concerned with julia code, let's first make a function to detect if a string will parse to an `incomplete` expression.
```julia
julia> iscomplete(x) = true
iscomplete (generic function with 1 method)

julia> function iscomplete(ex::Expr)
if ex.head == :incomplete
false
else
true
end
end
iscomplete (generic function with 2 methods)
```
and then we can slurp up the string being stored in the REPL buffer, parse it and check if it is a complete expression:
```julia
julia> using REPL: LineEdit
This is because we haven't told our REPL mode what constitues a valid, complete line. ReplMaker.jl exports a function `complete_julia` that will tell you if a given expression is a complete julia-expression. If you are using ReplMaker.jl for a DSL that has different parsing semantics from julia, you may need to roll your own analogous function if you want to have multi-line inputs.

julia> function valid_julia(s)
input = String(take!(copy(LineEdit.buffer(s))))
iscomplete(Meta.parse(input))
end
valid_julia (generic function with 1 method)
```
Now all we have to do is redefine our REPL mode to use this completion checker:
To use `complete_julia` to check if an expression is complete, we just pass it as a keyword argument to to `initrepl`:
```julia
julia> initrepl(parse_to_expr,
prompt_text="Expr> ",
prompt_color = :blue,
start_key=')',
mode_name="Expr_mode",
valid_input_checker=valid_julia)
valid_input_checker=complete_julia)
┌ Warning: REPL key ')' overwritten.
└ @ ReplMaker ~/.julia/packages/ReplMaker/pwo5w/src/ReplMaker.jl:86
REPL mode Expr_mode initialized. Press ) to enter and backspace to exit.
Expand Down
16 changes: 15 additions & 1 deletion src/ReplMaker.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import REPL.LineEdit
import REPL.LineEditREPL
import Base.display

export initrepl, enter_mode!
export initrepl, enter_mode!, complete_julia

"""
```
Expand Down Expand Up @@ -189,4 +189,18 @@ function enablecustomdisplay(repl::LineEditREPL, replshow::Function=show, io::IO
return customrepl
end

function complete_julia(s)
input = String(take!(copy(LineEdit.buffer(s))))
iscomplete(Meta.parse(input))
end

iscomplete(x) = true
function iscomplete(ex::Expr)
if ex.head == :incomplete
false
else
true
end
end

end

2 comments on commit 55363bc

@MasonProtter
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/33876

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.5 -m "<description of version>" 55363bc8462451b0159eb7ad27ae8445b77353b8
git push origin v0.2.5

Please sign in to comment.