From 5a14e88e221eec0dfecc4b86783144996244aeb5 Mon Sep 17 00:00:00 2001 From: Zentrik Date: Thu, 15 Jun 2023 20:55:08 +0100 Subject: [PATCH] Clean up code --- TypedSyntax/src/TypedSyntax.jl | 2 +- TypedSyntax/src/show.jl | 22 ++++------------------ TypedSyntax/src/vscode.jl | 15 ++++++++++++++- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/TypedSyntax/src/TypedSyntax.jl b/TypedSyntax/src/TypedSyntax.jl index ac9d4569..40522dad 100644 --- a/TypedSyntax/src/TypedSyntax.jl +++ b/TypedSyntax/src/TypedSyntax.jl @@ -11,7 +11,7 @@ using CodeTracking export TypedSyntaxNode include("node.jl") -include("show.jl") include("vscode.jl") +include("show.jl") end diff --git a/TypedSyntax/src/show.jl b/TypedSyntax/src/show.jl index 256e961c..9728b4a7 100644 --- a/TypedSyntax/src/show.jl +++ b/TypedSyntax/src/show.jl @@ -1,18 +1,5 @@ ## Extensions of JuliaSyntax to cover TypedSyntaxNode -# Structs used when using VSCode -struct WarnUnstable - path::String - line::Int - severity::Int # 0: Error, 1: Warning, 2: Information, 3: Hint -end -struct InlayHint - line::Int - column::Int - label::String - kind::Union{Nothing, Int} -end - function Base.show(io::IO, ::MIME"text/plain", node::TypedSyntaxNode; show_byte_offsets=false) println(io, "line:col│$(show_byte_offsets ? " byte_range │" : "") tree │ type") JuliaSyntax._show_syntax_node(io, Ref{Union{Nothing,String}}(nothing), node, "", show_byte_offsets) @@ -65,14 +52,13 @@ function _printstyled(io::IO, rootnode::MaybeTypedSyntaxNode, catchup(io, rootnode, position, nd, idxend+1) # finish the node return nothing end -function Base.printstyled(io::IO, rootnode::MaybeTypedSyntaxNode; +function Base.printstyled(io::IO, rootnode::MaybeTypedSyntaxNode, + type_hints = Dict{String, Vector{InlayHint}}(), + warn_diagnostics = WarnUnstable[]; type_annotations::Bool=true, iswarn::Bool=true, hide_type_stable::Bool=true, with_linenumber::Bool=true, idxend = last_byte(rootnode), vscode_integration=true) - if vscode_integration && isdefined(Main, :VSCodeServer) && Main.VSCodeServer isa Module - type_hints = Dict{String, Vector{InlayHint}}() - warn_diagnostics = WarnUnstable[] - + if vscode_integration && isvscode() _printstyled(io, rootnode, type_hints, warn_diagnostics; type_annotations, iswarn, hide_type_stable, with_linenumber, idxend) display(Main.VSCodeServer.InlineDisplay(false), warn_diagnostics) diff --git a/TypedSyntax/src/vscode.jl b/TypedSyntax/src/vscode.jl index 005b97fa..6b8676a8 100644 --- a/TypedSyntax/src/vscode.jl +++ b/TypedSyntax/src/vscode.jl @@ -1,3 +1,10 @@ +isvscode() = isdefined(Main, :VSCodeServer) && Main.VSCodeServer isa Module + +struct WarnUnstable + path::String + line::Int + severity::Int # 0: Error, 1: Warning, 2: Information, 3: Hint +end function Base.show(io::IO, ::MIME"application/vnd.julia-vscode.diagnostics", warn_diagnostics::AbstractVector{WarnUnstable}) return ( source = "Cthulhu", @@ -15,8 +22,14 @@ const InlayHintKinds = ( Type = 1, Parameter = 2 ) +struct InlayHint + line::Int + column::Int + label::String + kind::Union{Nothing, Int} +end function Base.show(io::IO, ::MIME"application/vnd.julia-vscode.inlayHints", type_hints_by_file::Dict{String, Vector{InlayHint}}) - if isdefined(Main, :VSCodeServer) && Main.VSCodeServer isa Module && isdefined(Main.VSCodeServer, :INLAY_HINTS_ENABLED) + if isvscode() && isdefined(Main.VSCodeServer, :INLAY_HINTS_ENABLED) return Dict(filepath => map(x -> (position=(x.line, x.column), label=x.label, kind=x.kind), type_hints) for (filepath, type_hints) in type_hints_by_file) end end