-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move shared logic into new path lib (#98)
- Loading branch information
1 parent
6d205fd
commit 0aaf524
Showing
4 changed files
with
45 additions
and
46 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
--- Helpers around filepaths. | ||
|
||
local M = {} | ||
|
||
--- Find a file upwards in the directory tree and return its path, if found. | ||
--- @param filename string | ||
--- @param start_path string | ||
--- @return string | nil | ||
function M.find_file_upwards(filename, start_path) | ||
local scan = require("plenary.scandir") | ||
local cwd = vim.fn.getcwd() | ||
local found_filepath = nil | ||
while start_path ~= cwd do | ||
local files = scan.scan_dir( | ||
start_path, | ||
{ search_pattern = filename, hidden = true, depth = 1 } | ||
) | ||
if #files > 0 then | ||
found_filepath = files[1] | ||
break | ||
end | ||
start_path = vim.fn.fnamemodify(start_path, ":h") -- go up one directory | ||
end | ||
|
||
if found_filepath == nil then | ||
-- check if filename exists in the current directory | ||
local files = scan.scan_dir( | ||
start_path, | ||
{ search_pattern = filename, hidden = true, depth = 1 } | ||
) | ||
if #files > 0 then | ||
found_filepath = files[1] | ||
end | ||
end | ||
|
||
return found_filepath | ||
end | ||
|
||
return M |
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
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