Created
September 29, 2013 01:45
-
-
Save houshuang/6748566 to your computer and use it in GitHub Desktop.
Automatically open function in Sublime Text (first Julia macro)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
macro whichs(ex) | |
ex = expand(ex) | |
exret = Expr(:call, :error, "expression is not a function call") | |
if !isa(ex, Expr) | |
# do nothing -> error | |
elseif ex.head == :call | |
exret = Expr(:call, :whichs, map(esc, ex.args)...) | |
elseif ex.head == :body | |
a1 = ex.args[1] | |
if isa(a1, Expr) && a1.head == :call | |
a11 = a1.args[1] | |
if a11 == :setindex! | |
exret = Expr(:call, :whichs, a11, map(esc, a1.args[2:end])...) | |
end | |
end | |
elseif ex.head == :thunk | |
exret = Expr(:call, :error, "expression is not a function call, or is too complex for @which to analyze; " | |
* "break it down to simpler parts if possible") | |
end | |
exret | |
end | |
function whichst(f, types) | |
for m in methods(f, types) | |
lsd = m.func.code::LambdaStaticData | |
file = string(lsd.file) | |
print(file) | |
print(typeof(file)) | |
print(ismatch(r"\/",file)) | |
if !ismatch(r"\/", file) | |
file = string("/Users/Stian/src/julia/base/", file) | |
print("hi") | |
end | |
print(file) | |
run(`subl $(file):$(lsd.line)`) | |
end | |
end | |
whichs(f, args...) = whichst(f, map(a->(isa(a,Type) ? Type{a} : typeof(a)), args)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This might be a cleaner implementation of the above functionality if anyone is interested.