From 5a0c15eb556f13871abce2bb7990feaac070dada Mon Sep 17 00:00:00 2001 From: Miles Cranmer Date: Wed, 17 Jan 2024 21:39:52 +0000 Subject: [PATCH 1/3] Create overloadable function for strings --- TypedSyntax/src/show.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/TypedSyntax/src/show.jl b/TypedSyntax/src/show.jl index f3bfbe05..9c6d9559 100644 --- a/TypedSyntax/src/show.jl +++ b/TypedSyntax/src/show.jl @@ -113,12 +113,14 @@ function type_annotation_mode(node, @nospecialize(T); type_annotations::Bool, hi return type_annotate, pre, pre2, post end +type_string(T) = string(T) + function show_annotation(io, @nospecialize(T), post, node, position; iswarn::Bool) diagnostics = get(io, :diagnostics, nothing) inlay_hints = get(io, :inlay_hints, nothing) print(io, post) - T_str = string(T) + T_str = type_string(T) if iswarn && is_type_unstable(T) color = is_small_union_or_tunion(T) ? :yellow : :red printstyled(io, "::", T_str; color) From eaa8ae8444547c2077df49fb8bdb259536dcf157 Mon Sep 17 00:00:00 2001 From: Miles Cranmer Date: Wed, 17 Jan 2024 21:43:35 +0000 Subject: [PATCH 2/3] Document `type_string` and make available in `Cthulhu` --- TypedSyntax/src/show.jl | 7 +++++++ src/Cthulhu.jl | 1 + 2 files changed, 8 insertions(+) diff --git a/TypedSyntax/src/show.jl b/TypedSyntax/src/show.jl index 9c6d9559..1709f248 100644 --- a/TypedSyntax/src/show.jl +++ b/TypedSyntax/src/show.jl @@ -113,6 +113,13 @@ function type_annotation_mode(node, @nospecialize(T); type_annotations::Bool, hi return type_annotate, pre, pre2, post end +""" + type_string(type) + +Overload this function to implement custom annotations for types. For example, +`type_string(::Type{Float32}) = "F32"` would result in `F32` being used to annotate +any instance of a `Float32`. +""" type_string(T) = string(T) function show_annotation(io, @nospecialize(T), post, node, position; iswarn::Bool) diff --git a/src/Cthulhu.jl b/src/Cthulhu.jl index 46b98bdc..fa1855c0 100644 --- a/src/Cthulhu.jl +++ b/src/Cthulhu.jl @@ -9,6 +9,7 @@ using REPL: REPL, AbstractTerminal using JuliaSyntax using JuliaSyntax: SyntaxNode, AbstractSyntaxNode, child, children using TypedSyntax +using TypedSyntax: type_string using WidthLimitedIO using Core: MethodInstance, MethodMatch From 2629d7b6f1d485170b6bb63ac7b35582e18d0cab Mon Sep 17 00:00:00 2001 From: MilesCranmer Date: Thu, 18 Jan 2024 05:04:06 +0000 Subject: [PATCH 3/3] Recurse in `type_string` --- TypedSyntax/src/show.jl | 21 ++++++++++++++++++++- test/test_codeview.jl | 27 +++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/TypedSyntax/src/show.jl b/TypedSyntax/src/show.jl index 1709f248..8eec4dad 100644 --- a/TypedSyntax/src/show.jl +++ b/TypedSyntax/src/show.jl @@ -120,7 +120,26 @@ Overload this function to implement custom annotations for types. For example, `type_string(::Type{Float32}) = "F32"` would result in `F32` being used to annotate any instance of a `Float32`. """ -type_string(T) = string(T) +function type_string(T) + if T isa DataType + if isempty(T.parameters) + return string(T) + else + wrapper = Base.typename(T).wrapper + return string(wrapper, '{', join(map(type_string, T.parameters), ','), '}') + end + elseif T isa Union + params = union_to_tuple(T) + return string("Union{" * join(map(type_string, params), ",") * "}") + elseif T isa UnionAll + return string(type_string(T.body), " where ", string(T.var)) + end + return string(T) +end +union_to_tuple(T) = (T,) +function union_to_tuple(U::Union) + return (union_to_tuple(U.a)..., union_to_tuple(U.b)...) +end function show_annotation(io, @nospecialize(T), post, node, position; iswarn::Bool) diagnostics = get(io, :diagnostics, nothing) diff --git a/test/test_codeview.jl b/test/test_codeview.jl index 8e531011..48159251 100644 --- a/test/test_codeview.jl +++ b/test/test_codeview.jl @@ -1,6 +1,7 @@ module test_codeview using Cthulhu, Test, Revise +import Cthulhu: type_string include("setup.jl") @@ -113,4 +114,30 @@ end end end +@testset "type aliases" begin + let + @gensym MyType + (; src, infos, mi, rt, exct, effects, slottypes) = @eval Module() begin + import Cthulhu: type_string + struct $MyType{T} + a::T + end + type_string(::Type{$MyType{T}}) where {T} = "Abracadabra{" * type_string(T) * ",5}" + $cthulhu_info() do + x = Union{$MyType{Float64},Missing}[] + return x + end + end + function prints(; kwargs...) + io = IOBuffer() + Cthulhu.cthulhu_typed(io, :none, src, rt, exct, effects, mi; kwargs...) + return String(take!(io)) + end + let + s = prints() + @test occursin("Abracadabra{Float64,5}", s) + end + end +end + end # module test_codeview