File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -80,11 +80,16 @@ function M.get_project_root(file_path)
8080 return vim .fn .fnamemodify (file_path , ' :p:h' )
8181end
8282
83- --- Get project name from root path
83+ --- Get project name from root path (last 2 folder layers)
8484--- @param project_root string Project root path
8585--- @return string Project name
8686function M .get_project_name (project_root )
87- return vim .fn .fnamemodify (project_root , ' :t' )
87+ local tail = vim .fn .fnamemodify (project_root , ' :t' )
88+ local parent = vim .fn .fnamemodify (project_root , ' :h:t' )
89+ if parent and parent ~= ' ' and parent ~= ' /' then
90+ return parent .. ' /' .. tail
91+ end
92+ return tail
8893end
8994
9095--- Generate UUID v4
Original file line number Diff line number Diff line change @@ -181,12 +181,12 @@ describe('shelltime.utils.system', function()
181181 end )
182182
183183 describe (' get_project_name' , function ()
184- it (' should extract directory name from path' , function ()
184+ it (' should extract last 2 folder layers from path' , function ()
185185 local name = system .get_project_name (' /home/user/projects/myapp' )
186- assert .equals (' myapp' , name )
186+ assert .equals (' projects/ myapp' , name )
187187 end )
188188
189- it (' should handle simple path' , function ()
189+ it (' should handle simple path with only 1 layer ' , function ()
190190 local name = system .get_project_name (' /myapp' )
191191 assert .equals (' myapp' , name )
192192 end )
@@ -196,6 +196,8 @@ describe('shelltime.utils.system', function()
196196 local name = system .get_project_name (project_dir )
197197 assert .is_string (name )
198198 assert .is_true (# name > 0 )
199+ -- Should contain a slash for 2 layers
200+ assert .is_true (name :find (' /' ) ~= nil )
199201 end )
200202 end )
201203end )
You can’t perform that action at this time.
0 commit comments