Skip to content

refactor file_exists function#131

Merged
NTBBloodbath merged 2 commits intorest-nvim:mainfrom
Yagua:fix-file-exist-func
Aug 11, 2022
Merged

refactor file_exists function#131
NTBBloodbath merged 2 commits intorest-nvim:mainfrom
Yagua:fix-file-exist-func

Conversation

@Yagua
Copy link
Copy Markdown
Contributor

@Yagua Yagua commented Aug 10, 2022

Since this implementation of the file_exists function has the ability to give an answer as to whether or not a file exists, it is not completely accurate since giving the open function a directory path as an argument will still return true indicating that the file exists even though it is not actually a file.

M.file_exists = function(file)
  file = io.open(file, "rb")
  if file then
    file:close()
  end
  return file ~= nil
end

so I refactored the function, making use of the built-in function'vim.fn.filereadable, to ensure that first we are verifying that we are actually reading a file and not a directory, and in turn showing if this file exists or not.

M.file_exists = function(file)
  if vim.fn.filereadable(file) ~= 1 then return false end
  return true
end

In this way when running the read_env_file function and there is a virtual environment (directory) with the name .env in the current directory, only the .env files will be taken into account and not the directory, preventing io.lines from trying to scan a directory.

Copy link
Copy Markdown
Member

@NTBBloodbath NTBBloodbath left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey,

This is indeed a great catch, thanks for the refactoring!

I would like the change I suggested to be applied before merging tho, I feel like it's worth it to simplify codebase and improve its readability.

Cheers

Comment thread lua/rest-nvim/utils/init.lua Outdated
@NTBBloodbath NTBBloodbath added the enhancement New feature or request label Aug 10, 2022
Co-authored-by: Alejandro <bloodbathalchemist@protonmail.com>
@Yagua
Copy link
Copy Markdown
Contributor Author

Yagua commented Aug 10, 2022

Of course, it looks even better!

@NTBBloodbath NTBBloodbath merged commit 8c719ed into rest-nvim:main Aug 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants