Skip to content

Profile: Improve generation of clickable terminal links #55857

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

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
13 changes: 5 additions & 8 deletions base/path.jl
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,11 @@ for f in (:isdirpath, :splitdir, :splitdrive, :splitext, :normpath, :abspath)
@eval $f(path::AbstractString) = $f(String(path))
end

# RFC3986 Section 2.1
percent_escape(s) = '%' * join(map(b -> uppercase(string(b, base=16)), codeunits(s)), '%')
# RFC3986 Section 2.3
encode_uri_component(s) = replace(s, r"[^A-Za-z0-9\-_.~/]+" => percent_escape)

"""
uripath(path::AbstractString)

Expand All @@ -636,10 +641,6 @@ function uripath end

@static if Sys.iswindows()
function uripath(path::String)
percent_escape(s) = # RFC3986 Section 2.1
'%' * join(map(b -> uppercase(string(b, base=16)), codeunits(s)), '%')
encode_uri_component(s) = # RFC3986 Section 2.3
replace(s, r"[^A-Za-z0-9\-_.~/]+" => percent_escape)
path = abspath(path)
if startswith(path, "\\\\") # UNC path, RFC8089 Appendix E.3
unixpath = join(eachsplit(path, path_separator_re, keepempty=false), '/')
Expand All @@ -653,10 +654,6 @@ function uripath end
end
else
function uripath(path::String)
percent_escape(s) = # RFC3986 Section 2.1
'%' * join(map(b -> uppercase(string(b, base=16)), codeunits(s)), '%')
encode_uri_component(s) = # RFC3986 Section 2.3
replace(s, r"[^A-Za-z0-9\-_.~/]+" => percent_escape)
localpath = join(eachsplit(abspath(path), path_separator_re, keepempty=false), '/')
host = if ispath("/proc/sys/fs/binfmt_misc/WSLInterop") # WSL sigil
distro = get(ENV, "WSL_DISTRO_NAME", "") # See <https://patrickwu.space/wslconf/>
Expand Down
53 changes: 32 additions & 21 deletions stdlib/Profile/src/Profile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Profiling support.

## CPU profiling
- `@profile foo()` to profile a specific call.
- `Profile.print()` to print the report.
- `Profile.print()` to print the report. Paths are clickable links in supported terminals and specialized for JULIA_EDITOR etc.
- `Profile.clear()` to clear the buffer.
- Send a $(Sys.isbsd() ? "SIGINFO (ctrl-t)" : "SIGUSR1") signal to the process to automatically trigger a profile and print.

Expand Down Expand Up @@ -198,7 +198,9 @@ const META_OFFSET_THREADID = 5

Prints profiling results to `io` (by default, `stdout`). If you do not
supply a `data` vector, the internal buffer of accumulated backtraces
will be used.
will be used. Paths are clickable links in supported terminals and
specialized for [`JULIA_EDITOR`](@ref) with line numbers, or just file
links if no editor is set.

The keyword arguments can be any combination of:

Expand Down Expand Up @@ -807,26 +809,35 @@ end
# make a terminal-clickable link to the file and linenum.
# Similar to `define_default_editors` in `Base.Filesystem` but for creating URIs not commands
function editor_link(path::String, linenum::Int)
editor = get(ENV, "JULIA_EDITOR", "")

if editor == "code"
return "vscode://file/$path:$linenum"
elseif editor == "subl" || editor == "sublime_text"
return "subl://$path:$linenum"
elseif editor == "idea" || occursin("idea", editor)
return "idea://open?file=$path&line=$linenum"
elseif editor == "pycharm"
return "pycharm://open?file=$path&line=$linenum"
elseif editor == "atom"
return "atom://core/open/file?filename=$path&line=$linenum"
elseif editor == "emacsclient"
return "emacs://open?file=$path&line=$linenum"
elseif editor == "vim" || editor == "nvim"
return "vim://open?file=$path&line=$linenum"
else
# TODO: convert the path to a generic URI (line numbers are not supported by generic URI)
return path
# Note: the editor path can include spaces (if escaped) and flags.
editor = nothing
for var in ["JULIA_EDITOR", "VISUAL", "EDITOR"]
str = get(ENV, var, nothing)
str isa String || continue
isempty(str) && break
editor = str
end
path_encoded = Base.Filesystem.encode_uri_component(path)
if editor !== nothing
if editor == "code"
return "vscode://file/$path_encoded:$linenum"
elseif editor == "subl" || editor == "sublime_text"
return "subl://open?url=file://$path_encoded&line=$linenum"
elseif editor == "idea" || occursin("idea", editor)
return "idea://open?file=$path_encoded&line=$linenum"
elseif editor == "pycharm"
return "pycharm://open?file=$path_encoded&line=$linenum"
elseif editor == "atom"
return "atom://core/open/file?filename=$path_encoded&line=$linenum"
elseif editor == "emacsclient" || editor == "emacs"
return "emacs://open?file=$path_encoded&line=$linenum"
elseif editor == "vim" || editor == "nvim"
# Note: Vim/Nvim may not support standard URI schemes without specific plugins
return "vim://open?file=$path_encoded&line=$linenum"
end
end
# fallback to generic URI, but line numbers are not supported by generic URI
return Base.Filesystem.uripath(path)
Copy link
Member Author

@IanButterworth IanButterworth Sep 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's really a shame that there isn't a general way to express a uri with a line number.. These file-only links open vscode for me, but if you add:123 or other variants, that generic form doesn't register as a clickable link. It's surprising that the standard hasn't added support.

end

function print_flat(io::IO, lilist::Vector{StackFrame},
Expand Down