-
Notifications
You must be signed in to change notification settings - Fork 44
Integrate Cthulhu with VSCode - show types in source code #469
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
Changes from all commits
e48a8b5
40538f4
6414e3e
a7b4bcc
6f3568a
a30cf26
0f980a2
8f9877c
8bed8ec
fccc9ca
0cac4f7
d845ee3
621172f
315d34f
700f9a1
4a6982a
41f11d7
d4473ee
4da0775
1ab675d
6d3047a
acf9693
470dfba
7738e2a
ec8cb53
087ee56
f9a3cba
eb51cfa
3aac843
fd95d42
446bedd
15411cd
2f07187
6ad60a9
d123e95
fc3062f
22985c4
c37e281
1e27a37
fb45d2c
84c431b
e75e955
a5bcc27
8a2c5cc
1c21e25
b221c29
455a21d
1f17bab
5bef950
10a7660
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ using CodeTracking | |
| export TypedSyntaxNode | ||
|
|
||
| include("node.jl") | ||
| include("vscode.jl") | ||
| include("show.jl") | ||
|
|
||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| diagnostics_available_vscode() = isdefined(Main, :VSCodeServer) && Main.VSCodeServer isa Module && isdefined(Main.VSCodeServer, :DIAGNOSTICS_ENABLED) && Main.VSCodeServer.DIAGNOSTICS_ENABLED[] | ||
| inlay_hints_available_vscode() = isdefined(Main, :VSCodeServer) && Main.VSCodeServer isa Module && isdefined(Main.VSCodeServer, :INLAY_HINTS_ENABLED) && Main.VSCodeServer.INLAY_HINTS_ENABLED[] | ||
|
Comment on lines
+1
to
+2
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pfitzseb VSCodeServer is not registered right? Otherwise we could use package extensions here.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is not registered, yes. Do package extensions actually require that though? VSCodeServer is a package and is loaded as such, so the appropriate hooks should fire. |
||
|
|
||
| module DiagnosticKinds | ||
| @enum T Error=0 Warning=1 Information=2 Hint=3 | ||
| end | ||
|
|
||
| struct Diagnostic | ||
| path::String | ||
| line::Int # 1-based indexing | ||
| severity::DiagnosticKinds.T | ||
| msg::String | ||
| end | ||
|
|
||
| to_vscode_type(x::Diagnostic) = (msg=x.msg, path = x.path, line = x.line, severity = Int(x.severity)) | ||
| function Base.show(io::IO, ::MIME"application/vnd.julia-vscode.diagnostics", diagnostics::AbstractVector{Diagnostic}) | ||
| return ( | ||
| source = "Cthulhu", | ||
| items = to_vscode_type.(diagnostics), | ||
| ) | ||
| end | ||
|
|
||
| add_diagnostic!(::Nothing, node, position, severity) = nothing | ||
| function add_diagnostic!(diagnostics, node, position, severity) | ||
| file_path = node.filename | ||
| line = source_line(node, position) | ||
| push!(diagnostics, Diagnostic(file_path, line, severity, "Unstable Type")) | ||
| end | ||
|
|
||
| function clear_diagnostics_vscode() | ||
| if diagnostics_available_vscode() | ||
| display(Main.VSCodeServer.InlineDisplay(false), TypedSyntax.Diagnostic[]) | ||
| end | ||
| end | ||
|
|
||
| function display_diagnostics_vscode(diagnostics) | ||
| if diagnostics_available_vscode() && !isnothing(diagnostics) | ||
| # InlineDisplay(false) means we don't print to REPL | ||
| display(Main.VSCodeServer.InlineDisplay(false), diagnostics) | ||
| end | ||
| end | ||
| display_diagnostics_vscode(io::IO) = display_diagnostics_vscode(get(io, :diagnostics, nothing)) | ||
|
|
||
| const InlayHintKinds = (Type=1, Parameter=2, Nothing=nothing) | ||
|
|
||
| struct InlayHint | ||
| line::Int # 1-based indexing | ||
| column::Int # 1-based indexing | ||
| label::String | ||
| kind::Union{Nothing, Int} | ||
| end | ||
|
|
||
| to_vscode_type(x::InlayHint) = (position=(x.line, x.column), label=x.label, kind=x.kind) | ||
| function Base.show(io::IO, ::MIME"application/vnd.julia-vscode.inlayHints", inlay_hints_by_file::Dict{T, Vector{InlayHint}}) where T | ||
| if inlay_hints_available_vscode() | ||
| return Dict{T, Vector{NamedTuple{(:position, :label, :kind), Tuple{Tuple{Int, Int}, String, Union{Nothing, Int}}}}}( | ||
| filepath => to_vscode_type.(inlay_hints) for (filepath, inlay_hints) in inlay_hints_by_file | ||
| ) | ||
| end | ||
| return nothing | ||
| end | ||
|
|
||
| add_hint!(::Nothing, message, node, position; kind=InlayHintKinds.Type) = nothing | ||
| function add_hint!(inlay_hints, message, node, position; kind=InlayHintKinds.Type) | ||
| filepath = node.filename | ||
| line, column = source_location(node, position) | ||
|
|
||
| if filepath ∉ keys(inlay_hints) | ||
| inlay_hints[filepath] = InlayHint[] | ||
| end | ||
| push!(inlay_hints[filepath], InlayHint(line-1, column-1, message, kind)) | ||
| end | ||
|
|
||
| function clear_inlay_hints_vscode() | ||
| if inlay_hints_available_vscode() | ||
| display(Main.VSCodeServer.InlineDisplay(false), Dict{String, Vector{TypedSyntax.InlayHint}}()) | ||
| end | ||
| end | ||
|
|
||
| function display_inlay_hints_vscode(inlay_hints) | ||
| if inlay_hints_available_vscode() && !isnothing(inlay_hints) | ||
| # InlineDisplay(false) means we don't print to REPL | ||
| display(Main.VSCodeServer.InlineDisplay(false), inlay_hints) | ||
| end | ||
| end | ||
| display_inlay_hints_vscode(io::IO) = display_inlay_hints_vscode(get(io, :inlay_hints, nothing)) | ||
|
|
||
| function clear_all_vscode() | ||
| clear_diagnostics_vscode() | ||
| clear_inlay_hints_vscode() | ||
| end | ||
Uh oh!
There was an error while loading. Please reload this page.