Skip to content

Instantly share code, notes, and snippets.

@ivarne
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivarne/d232c1a815222fc5f896 to your computer and use it in GitHub Desktop.
Save ivarne/d232c1a815222fc5f896 to your computer and use it in GitHub Desktop.
function CUSTOM_AUTOCOMPLETION_HOOK(txt,pos)
m=match(r"(\w+)\[\"([^\"]*)$",txt[1:pos])
if !isa(m,Nothing)
#For String key
if !isdefined(Main,parse(m.captures[1]))
return UTF8String[], 0:-1, false
end
var=eval(parse(m.captures[1]))
if !applicable(keys,var)
return UTF8String[], 0:-1, false
end
ky=keys(var)
if length(ky)>500
return UTF8String[], 0:-1, false
end
ky=[filter(x->isa(x,String),ky)...]
lst=convert(Vector{UTF8String},[filter(x->beginswith(x,m.captures[2]),ky)...])
if !(length(txt)>=pos+1 && txt[pos+1]=='\"')
if length(txt)>=pos+1 && txt[pos+1]==']'
lst=map(x->x*"\"",lst)
else
lst=map(x->x*"\"]",lst)
end
end
return lst, (pos-length(m.captures[2])+1):pos, true
else
#For symbol key
m=match(r"(\w+)\[\:(\w*)$",txt[1:pos])
if isa(m,Nothing) || !isdefined(Main,parse(m.captures[1]))
return UTF8String[], 0:-1, false
end
var=eval(parse(m.captures[1]))
if !applicable(keys,var)
return UTF8String[], 0:-1, false
end
ky=keys(var)
if length(ky)>500
return UTF8String[], 0:-1, false
end
ky=map(string,[filter(x->isa(x,Symbol),ky)...])
lst=convert(Vector{UTF8String},[filter(x->beginswith(x,m.captures[2]),ky)...])
if !(length(txt)>=pos+1 && txt[pos+1]==']')
lst=map(x->x*"]",lst)
end
return lst, (pos-length(m.captures[2])+1):pos, true
end
end
diff --git a/base/REPLCompletions.jl b/base/REPLCompletions.jl
index df8c965..d9a8a2c 100644
--- a/base/REPLCompletions.jl
+++ b/base/REPLCompletions.jl
@@ -183,6 +183,17 @@ function latex_completions(string, pos)
end
function completions(string, pos)
+ try
+ if isdefined(Main, :CUSTOM_AUTOCOMPLETION_HOOK)
+ t=Main.CUSTOM_AUTOCOMPLETION_HOOK(string, pos)
+ if t[3]
+ return t
+ end
+ end
+ catch err
+ println("Error in CUSTOM_AUTOCOMPLETION_HOOK:")
+ println(err)
+ end
# First parse everything up to the current position
partial = string[1:pos]
inc_tag = Base.incomplete_tag(parse(partial , raise=false))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment