Skip to content

Commit

Permalink
cache some data for the project prompt printer (#431)
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferC committed Jun 27, 2018
1 parent 18b703b commit 0db3840
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 42 deletions.
38 changes: 28 additions & 10 deletions stdlib/Pkg/src/REPLMode.jl
Original file line number Diff line number Diff line change
Expand Up @@ -936,29 +936,47 @@ function completions(full, index)
end
end

prev_project_file = nothing
prev_project_timestamp = nothing
prev_prefix = ""

function promptf()
env = try
EnvCache()
global prev_project_timestamp, prev_prefix, prev_project_file
project_file = try
Types.find_project_file()
catch
nothing
end
prefix = ""
if env !== nothing
proj_dir = dirname(env.project_file)
if startswith(pwd(), proj_dir) && env.pkg != nothing && !isempty(env.pkg.name)
name = env.pkg.name
if project_file !== nothing
if prev_project_file == project_file && prev_project_timestamp == mtime(project_file)
prefix = prev_prefix
else
name = basename(proj_dir)
project = try
Types.read_project(project_file)
catch
nothing
end
if project !== nothing
proj_dir = dirname(project_file)
projname = get(project, "name", nothing)
if startswith(pwd(), proj_dir) && projname !== nothing
name = projname
else
name = basename(proj_dir)
end
prefix = string("(", name, ") ")
prev_prefix = prefix
prev_project_timestamp = mtime(project_file)
prev_project_file = project_file
end
end
prefix = string("(", name, ") ")
end
return prefix * "pkg> "
end

# Set up the repl Pkg REPLMode
function create_mode(repl, main)


pkg_mode = LineEdit.Prompt(promptf;
prompt_prefix = Base.text_colors[:blue],
prompt_suffix = "",
Expand Down
70 changes: 38 additions & 32 deletions stdlib/Pkg/src/Types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,43 @@ const default_envs = [
"default",
]

function find_project_file(env::Union{Nothing,String}=nothing)
project_file = nothing
if env isa Nothing
for entry in LOAD_PATH
project_file = Base.load_path_expand(entry)
project_file isa String && !isdir(project_file) && break
project_file = nothing
end
if project_file == nothing
project_dir = nothing
for entry in LOAD_PATH
project_dir = Base.load_path_expand(entry)
project_dir isa String && isdir(project_dir) && break
project_dir = nothing
end
project_dir == nothing && error("No Pkg environment found in LOAD_PATH")
project_file = joinpath(project_dir, Base.project_names[end])
end
elseif startswith(env, '@')
project_file = Base.load_path_expand(env)
project_file === nothing && error("package environment does not exist: $env")
elseif env isa String
if isdir(env)
isempty(readdir(env)) || error("environment is a package directory: $env")
project_file = joinpath(env, Base.project_names[end])
else
project_file = endswith(env, ".toml") ? abspath(env) :
abspath(env, Base.project_names[end])
end
end
@assert project_file isa String &&
(isfile(project_file) || !ispath(project_file) ||
isdir(project_file) && isempty(readdir(project_file)))
return project_file
end


mutable struct EnvCache
# environment info:
env::Union{Nothing,String}
Expand All @@ -226,38 +263,7 @@ mutable struct EnvCache
paths::Dict{UUID,Vector{String}}

function EnvCache(env::Union{Nothing,String}=nothing)
if env isa Nothing
project_file = nothing
for entry in LOAD_PATH
project_file = Base.load_path_expand(entry)
project_file isa String && !isdir(project_file) && break
project_file = nothing
end
if project_file == nothing
project_dir = nothing
for entry in LOAD_PATH
project_dir = Base.load_path_expand(entry)
project_dir isa String && isdir(project_dir) && break
project_dir = nothing
end
project_dir == nothing && error("No Pkg environment found in LOAD_PATH")
project_file = joinpath(project_dir, Base.project_names[end])
end
elseif startswith(env, '@')
project_file = Base.load_path_expand(env)
project_file === nothing && error("package environment does not exist: $env")
elseif env isa String
if isdir(env)
isempty(readdir(env)) || error("environment is a package directory: $env")
project_file = joinpath(env, Base.project_names[end])
else
project_file = endswith(env, ".toml") ? abspath(env) :
abspath(env, Base.project_names[end])
end
end
@assert project_file isa String &&
(isfile(project_file) || !ispath(project_file) ||
isdir(project_file) && isempty(readdir(project_file)))
project_file = find_project_file(env)
project_dir = dirname(project_file)
git = ispath(joinpath(project_dir, ".git")) ? LibGit2.GitRepo(project_dir) : nothing

Expand Down

0 comments on commit 0db3840

Please sign in to comment.