Skip to content

Instantly share code, notes, and snippets.

@nickrobinson251
Last active January 22, 2021 12:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickrobinson251/b9806ae49cb67e3624a4b0fe0f49586b to your computer and use it in GitHub Desktop.
Save nickrobinson251/b9806ae49cb67e3624a4b0fe0f49586b to your computer and use it in GitHub Desktop.
A Julia startup.jl config file that creates REPL modes which help you find functionality
# Functionality for the `names>` repl mode
using REPL: moduleusings
# search through all accessible names for functionality with a name like `str`
names_like(str) = foreach(println, _corrections(str, _accessible_names()))
function _accessible_names(mod=Main) # Based on code in stdlib
_names(m) = names(m; all=true, imported=true) # include unexported `Base` + `mod` names
mod_names = filter!(s -> !Base.isdeprecated(mod, s), _names(mod))
base_names = filter!(s -> !Base.isdeprecated(Base, s), _names(Base))
other_names = map(names, moduleusings(mod))
all_names = map(string, unique(vcat(mod_names, base_names, other_names...)))
return filter!(s -> !startswith(s, '#') && !startswith(s, '_'), all_names)
end
# `RatcliffObershelp` picked by experimenting with options til something gave results I liked.
_corrections(str, candidates) = _corrections(RatcliffObershelp(), str, candidates)
function _corrections(distance, str, candidates)
partial_matches = filter(contains(str), candidates)
scores = map(c -> distance(c, str), candidates)
low_scorers = partialsortperm(scores, 1:10)
is_close = scores[low_scorers] .< _search_limit(distance)
close_matches = candidates[low_scorers][is_close]
return unique(vcat(partial_matches, close_matches))
end
_search_limit(::Union{Levenshtein,DamerauLevenshtein}) = 8 # nothing good comes after 8
_search_limit(metric) = 0.5
atreplinit() do repl
try
# repl mode for searching through all documentation for a string, using `apropos`
@eval using ReplMaker
@async initrepl(
apropos;
repl=repl,
mode_name="search_mode",
prompt_text="search> ",
prompt_color=:magenta,
start_key='`',
startup_text=false,
)
# repl mode for searching through all accessible names to find similarly named functionality
@eval using StringDistances
include(joinpath(@__DIR__, "name_search.jl"))
@async initrepl(
names_like;
repl=repl,
mode_name="names_mode",
prompt_text="names> ",
prompt_color=:magenta,
start_key='~',
startup_text=false,
)
catch
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment