Skip to content

Commit 79bd617

Browse files
committed
add more methods for get utilities
1 parent abbb84c commit 79bd617

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

src/utils.jl

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# path utilities
2+
# --------------
3+
14
include("path_matching.jl")
25

36
isuntitled(p) = occursin(r"^(\.\\|\./)?untitled-[\d\w]+(:\d+)?$", p)
@@ -99,6 +102,9 @@ function md_hlines(md)
99102
return MD(v)
100103
end
101104

105+
# string utilties
106+
# ---------------
107+
102108
function strlimit(str::AbstractString, limit::Int = 30, ellipsis::AbstractString = "")
103109
will_append = length(str) > limit
104110

@@ -122,33 +128,39 @@ shortstr(val) = strlimit(string(val), 20)
122128
struct Undefined end
123129

124130
# get utilities
131+
# -------------
132+
125133
using CodeTools
126134

127135
"""
128-
getfield′(mod::Module, name::String, default = Undefined())
136+
getfield′(mod::Module, name::AbstractString, default = Undefined())
129137
getfield′(mod::Module, name::Symbol, default = Undefined())
138+
getfield′(mod::AbstractString, name::Symbol, default = Undefined())
130139
getfield′(object, name::Symbol, default = Undefined())
140+
getfield′(object, name::AbstractString, default = Undefined())
131141
132142
Returns the specified field of a given `Module` or some arbitrary `object`,
133143
or `default` if no such a field is found.
134144
"""
135-
getfield′(mod::Module, name::String, default = Undefined()) = CodeTools.getthing(mod, name, default)
145+
getfield′(mod::Module, name::AbstractString, default = Undefined()) = CodeTools.getthing(mod, name, default)
136146
getfield′(mod::Module, name::Symbol, default = Undefined()) = getfield′(mod, string(name), default)
147+
getfield′(mod::AbstractString, name::Symbol, default = Undefined()) = getfield′(getmodule(mod), string(name), default)
137148
getfield′(@nospecialize(object), name::Symbol, default = Undefined()) = isdefined(object, name) ? getfield(object, name) : default
149+
getfield′(@nospecialize(object), name::AbstractString, default = Undefined()) = isdefined(object, name) ? getfield(object, Symbol(name)) : default
138150

139151
"""
140-
getmodule(mod::String)
141-
getmodule(parent::Union{Nothing, Module}, mod::String)
152+
getmodule(mod::AbstractString)
153+
getmodule(parent::Union{Nothing, Module}, mod::AbstractString)
142154
getmodule(code::AbstractString, pos; filemod)
143155
144156
Calls `CodeTools.getmodule(args...)`, but returns `Main` instead of `nothing` in a fallback case.
145157
"""
146158
getmodule(args...) = (m = CodeTools.getmodule(args...)) === nothing ? Main : m
147159

148-
getmethods(mod::Module, word::String) = methods(CodeTools.getthing(mod, word))
149-
getmethods(mod::String, word::String) = getmethods(getmodule(mod), word)
160+
getmethods(mod::Module, word::AbstractString) = methods(CodeTools.getthing(mod, word))
161+
getmethods(mod::AbstractString, word::AbstractString) = getmethods(getmodule(mod), word)
150162

151-
getdocs(mod::Module, word::String, fallbackmod::Module = Main) = begin
163+
getdocs(mod::Module, word::AbstractString, fallbackmod::Module = Main) = begin
152164
md = if Symbol(word) in keys(Docs.keywords)
153165
Core.eval(Main, :(@doc($(Symbol(word)))))
154166
else
@@ -164,13 +176,14 @@ getdocs(mod::Module, word::String, fallbackmod::Module = Main) = begin
164176
end
165177
md_hlines(md)
166178
end
167-
getdocs(mod::String, word::String, fallbackmod::Module = Main) =
179+
getdocs(mod::AbstractString, word::AbstractString, fallbackmod::Module = Main) =
168180
getdocs(getmodule(mod), word, fallbackmod)
169181

170182
cangetdocs(mod::Module, word::Symbol) =
171183
Base.isbindingresolved(mod, word) &&
172184
!Base.isdeprecated(mod, word)
173-
cangetdocs(mod::Module, word::String) = cangetdocs(mod, Symbol(word))
185+
cangetdocs(mod::Module, word::AbstractString) = cangetdocs(mod, Symbol(word))
186+
cangetdocs(mod::AbstractString, word::Union{Symbol, AbstractString}) = cangetdocs(getmodule(mod), word)
174187

175188
#=
176189
module file detections

0 commit comments

Comments
 (0)