@@ -154,37 +154,57 @@ call s:add_methods('app', ['path', 'src_path', 'config_path', 'migrations_path',
154154
155155" "
156156" Detect app's namespace
157- function ! s: app_detect_namespace () abort dict
157+ function ! s: app_namespaces () abort dict
158+ let namespaces = {}
159+
158160 try
159- let paths = composer#query ( ' autoload.psr-4 ' )
161+ let composer = composer#project ( )
160162
161- for [namespace, path ] in items (paths)
162- if path == # ' app/'
163- return namespace[0 :-2 ]
164- endif
163+ for [namespace, path ] in items (composer.query (' autoload.psr-4' , {}))
164+ let namespaces[path ] = namespace[0 :-2 ]
165+ endfor
166+
167+ for [namespace, path ] in items (composer.query (' autoload-dev.psr-4' , {}))
168+ let namespaces[path ] = namespace[0 :-2 ]
165169 endfor
166170 catch /^Vim\%((\a\+)\)\=:E117/
167- " Fall through when composer.vim isn't available
171+ " Fail gracefully when composer.vim isn't available.
172+ let namespaces = {
173+ \ ' app/' : ' App' ,
174+ \ ' database/factories/' : ' Database\Factories' ,
175+ \ ' database/seeders/' : ' Database\Seeders' ,
176+ \ ' tests/' : ' Tests' ,
177+ \ }
168178 endtry
169179
170- return ' App '
180+ return namespaces
171181endfunction
172182
173183" "
174184" Get app's namespace or namespace for file at [path]
175185function ! s: app_namespace (... ) abort dict
176- if self .cache.needs (' namespace ' )
177- call self .cache.set (' namespace ' , self .detect_namespace ())
186+ if self .cache.needs (' namespaces ' )
187+ call self .cache.set (' namespaces ' , self .namespaces ())
178188 endif
179189
180- let ns = [ self .cache.get (' namespace ' )]
190+ let namespaces = self .cache.get (' namespaces ' )
181191
182- if a: 0 == 1
183- let path = substitute (fnamemodify (a: 1 , ' :p' ), ' \V\^' . self .src_path (), ' ' , ' ' )
184- let ns += split (path , ' /' )[0 :-2 ]
192+ if a: 0 == 0
193+ return namespaces[' app/' ]
185194 endif
186195
187- return join (ns, ' \' )
196+ let path = substitute (fnamemodify (a: 1 , ' :p' ), ' \V\^' . self .path () . ' /' , ' ' , ' ' )
197+
198+ for [prefix, namespace] in items (namespaces)
199+ if path = ~# ' \V\^' . prefix
200+ let path = substitute (path , ' \V\^' . prefix, ' ' , ' ' )
201+ let ns = [namespace] + split (path , ' /' )[0 :-2 ]
202+
203+ return join (ns, ' \' )
204+ endif
205+ endfor
206+
207+ return ' '
188208endfunction
189209
190210" "
@@ -248,7 +268,7 @@ function! s:has_feature_by_path(app, feature)
248268 return ! empty (filter (split (path , ' |' ), ' a:app.has_path(v:val)' ))
249269endfunction
250270
251- call s: add_methods (' app' , [' detect_namespace ' , ' namespace' , ' has' ])
271+ call s: add_methods (' app' , [' namespaces ' , ' namespace' , ' has' ])
252272
253273" "
254274" Read first [n] lines of file at {path}.
0 commit comments