Skip to content
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

better sub/superscript tab completion #38649

Merged
merged 2 commits into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ Standard library changes
+ `suppress_output` (primarily a testing option) has been added as a keyword argument to `request`,
rather than a configuration option.

* Tab completion now supports runs of consecutive sub/superscript characters,
e.g. `\^(3)` tab-completes to `⁽³⁾` ([#38649]).

* Windows REPL now supports 24-bit colors, by correctly interpreting virtual terminal escapes.

#### SparseArrays
Expand Down
4 changes: 2 additions & 2 deletions doc/src/manual/variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ julia> 안녕하세요 = "Hello"

In the Julia REPL and several other Julia editing environments, you can type many Unicode math
symbols by typing the backslashed LaTeX symbol name followed by tab. For example, the variable
name `δ` can be entered by typing `\delta`-*tab*, or even `α̂` by `\alpha`-*tab*-`\hat`-
*tab*-`\_2`-*tab*. (If you find a symbol somewhere, e.g. in someone else's code,
name `δ` can be entered by typing `\delta`-*tab*, or even `α̂⁽²⁾` by `\alpha`-*tab*-`\hat`-
*tab*-`\^(2)`-*tab*. (If you find a symbol somewhere, e.g. in someone else's code,
that you don't know how to type, the REPL help will tell you: just type `?` and
then paste the symbol.)

Expand Down
11 changes: 11 additions & 0 deletions stdlib/REPL/src/REPLCompletions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,11 @@ const whitespace_chars = [" \t\n\r"...]
# bslash_completions function to try and complete on escaped characters in strings
const bslash_separators = [whitespace_chars..., "\"'`"...]

const subscripts = Dict(k[3]=>v[1] for (k,v) in latex_symbols if startswith(k, "\\_") && length(k)==3)
const subscript_regex = Regex("^\\\\_[" * join(isdigit(k) || isletter(k) ? "$k" : "\\$k" for k in keys(subscripts)) * "]+\\z")
const superscripts = Dict(k[3]=>v[1] for (k,v) in latex_symbols if startswith(k, "\\^") && length(k)==3)
const superscript_regex = Regex("^\\\\\\^[" * join(isdigit(k) || isletter(k) ? "$k" : "\\$k" for k in keys(superscripts)) * "]+\\z")

# Aux function to detect whether we're right after a
# using or import keyword
function afterusing(string::String, startpos::Int)
Expand All @@ -555,6 +560,12 @@ function bslash_completions(string::String, pos::Int)
latex = get(latex_symbols, s, "")
if !isempty(latex) # complete an exact match
return (true, (Completion[BslashCompletion(latex)], slashpos:pos, true))
elseif occursin(subscript_regex, s)
sub = map(c -> subscripts[c], s[3:end])
return (true, (Completion[BslashCompletion(sub)], slashpos:pos, true))
elseif occursin(superscript_regex, s)
sup = map(c -> superscripts[c], s[3:end])
return (true, (Completion[BslashCompletion(sup)], slashpos:pos, true))
end
emoji = get(emoji_symbols, s, "")
if !isempty(emoji)
Expand Down
23 changes: 22 additions & 1 deletion stdlib/REPL/src/docview.jl
Original file line number Diff line number Diff line change
Expand Up @@ -349,15 +349,36 @@ function repl_latex(io::IO, s::String)
print(io, "\"")
printstyled(io, s, color=:cyan)
print(io, "\" can be typed by ")
state = '\0'
with_output_color(:cyan, io) do io
for c in s
cstr = string(c)
if haskey(symbols_latex, cstr)
print(io, symbols_latex[cstr], "<tab>")
latex = symbols_latex[cstr]
if length(latex) == 3 && latex[2] in ('^','_')
# coalesce runs of sub/superscripts
if state != latex[2]
'\0' != state && print(io, "<tab>")
print(io, latex[1:2])
state = latex[2]
end
print(io, latex[3])
else
if '\0' != state
print(io, "<tab>")
state = '\0'
end
print(io, latex, "<tab>")
end
else
if '\0' != state
print(io, "<tab>")
state = '\0'
end
print(io, c)
end
end
'\0' != state && print(io, "<tab>")
end
println(io, '\n')
end
Expand Down
5 changes: 5 additions & 0 deletions stdlib/REPL/test/docview.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import Markdown
Core.eval(Main, REPL.helpmode(buf, "🐨"))
String(take!(buf))
end, "\"🐨\" can be typed by \\:koala:<tab>\n")

@test startswith(let buf = IOBuffer()
Core.eval(Main, REPL.helpmode(buf, "ᵞ₁₂₃¹²³α"))
String(take!(buf))
end, "\"ᵞ₁₂₃¹²³α\" can be typed by \\^gamma<tab>\\_123<tab>\\^123<tab>\\alpha<tab>\n")
end

@testset "Non-Markdown" begin
Expand Down
11 changes: 11 additions & 0 deletions stdlib/REPL/test/replcompletions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,17 @@ let s = "CompletionFoo.tuple."
@test isempty(c)
end

@testset "sub/superscripts" begin
@test "⁽¹²³⁾ⁿ" in test_complete("\\^(123)n")[1]
@test "ⁿ" in test_complete("\\^n")[1]
@test "ᵞ" in test_complete("\\^gamma")[1]
@test isempty(test_complete("\\^(123)nq")[1])
@test "₍₁₂₃₎ₙ" in test_complete("\\_(123)n")[1]
@test "ₙ" in test_complete("\\_n")[1]
@test "ᵧ" in test_complete("\\_gamma")[1]
@test isempty(test_complete("\\_(123)nq")[1])
end

# test Dicts
function test_dict_completion(dict_name)
s = "$dict_name[\"ab"
Expand Down