Skip to content

Commit

Permalink
Improve help grammar for field hints (#51178)
Browse files Browse the repository at this point in the history
  • Loading branch information
LilithHafner authored Sep 16, 2023
1 parent b2dfa1d commit 9ccf346
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
5 changes: 3 additions & 2 deletions stdlib/REPL/src/docview.jl
Original file line number Diff line number Diff line change
Expand Up @@ -643,8 +643,9 @@ function fielddoc(binding::Binding, field::Symbol)
end
end
end
fields = join(["`$f`" for f in fieldnames(resolve(binding))], ", ", ", and ")
fields = isempty(fields) ? "no fields" : "fields $fields"
fs = fieldnames(resolve(binding))
fields = isempty(fs) ? "no fields" : (length(fs) == 1 ? "field " : "fields ") *
join(("`$f`" for f in fs), ", ", ", and ")
Markdown.parse("`$(resolve(binding))` has $fields.")
end

Expand Down
50 changes: 32 additions & 18 deletions stdlib/REPL/test/docview.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,17 @@ using Test
import REPL, REPL.REPLCompletions
import Markdown

@testset "symbol completion" begin
@test startswith(let buf = IOBuffer()
Core.eval(Main, REPL.helpmode(buf, "α"))
String(take!(buf))
end, "\"α\" can be typed by \\alpha<tab>\n")

@test startswith(let buf = IOBuffer()
Core.eval(Main, REPL.helpmode(buf, "🐨"))
String(take!(buf))
end, "\"🐨\" can be typed by \\:koala:<tab>\n")
function get_help_io(input)
buf = IOBuffer()
eval(REPL.helpmode(buf, input))
String(take!(buf))
end
get_help_standard(input) = string(eval(REPL.helpmode(IOBuffer(), input)))

@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")
@testset "symbol completion" begin
@test startswith(get_help_io("α"), "\"α\" can be typed by \\alpha<tab>\n")
@test startswith(get_help_io("🐨"), "\"🐨\" can be typed by \\:koala:<tab>\n")
@test startswith(get_help_io("ᵞ₁₂₃¹²³α"), "\"ᵞ₁₂₃¹²³α\" can be typed by \\^gamma<tab>\\_123<tab>\\^123<tab>\\alpha<tab>\n")

# Check that all symbols with several completions have a canonical mapping (#39148)
symbols = values(REPLCompletions.latex_symbols)
Expand All @@ -27,10 +23,7 @@ import Markdown
end

@testset "quoting in doc search" begin
str = let buf = IOBuffer()
Core.eval(Main, REPL.helpmode(buf, "mutable s"))
String(take!(buf))
end
str = get_help_io("mutable s")
@test occursin("'mutable struct'", str)
@test occursin("Couldn't find 'mutable s'", str)
end
Expand Down Expand Up @@ -75,6 +68,27 @@ end
@test REPL.summarize(b, Tuple{}) isa Markdown.MD
end

@testset "Struct field help (#51178)" begin
struct StructWithNoFields end
struct StructWithOneField
field1
end
struct StructWithTwoFields
field1
field2
end
struct StructWithThreeFields
field1
field2
field3
end

@test endswith(get_help_standard("StructWithNoFields.not_a_field"), "StructWithNoFields` has no fields.\n")
@test endswith(get_help_standard("StructWithOneField.not_a_field"), "StructWithOneField` has field `field1`.\n")
@test endswith(get_help_standard("StructWithTwoFields.not_a_field"), "StructWithTwoFields` has fields `field1`, and `field2`.\n")
@test endswith(get_help_standard("StructWithThreeFields.not_a_field"), "StructWithThreeFields` has fields `field1`, `field2`, and `field3`.\n")
end

module InternalWarningsTests

module A
Expand Down

0 comments on commit 9ccf346

Please sign in to comment.