Skip to content
This repository has been archived by the owner on May 6, 2024. It is now read-only.

Detect file type based on file contents #69

Closed
ElSamhaa opened this issue Feb 2, 2022 · 5 comments
Closed

Detect file type based on file contents #69

ElSamhaa opened this issue Feb 2, 2022 · 5 comments

Comments

@ElSamhaa
Copy link

ElSamhaa commented Feb 2, 2022

The easiest way to enable this -- until implemented -- is to pass the file path/name to the functions under function_extensions for instance.

This will allow for custom logic provided by the user, based on which he can set the filetype eventually before the function returns.

Currently, this is how I do it:

local filename = vim.api.nvim_buf_get_name(0)

require("filetype").setup {
  overrides = {
    function_extensions = {
      ["yaml"] = function()
        file = io.open(filename, "r")
        filecontent = file:read("*all")
        if string.find(filecontent, "{{") then
          vim.bo.filetype = "gotexttmpl"
        end
        file.close()
      end,
    }
  },
}

But the above fails to run when opening a buffer after starting a neovim session.

@ObserverOfTime
Copy link

It should work if you place the call to nvim_buf_get_name inside the function.

@ElSamhaa
Copy link
Author

ElSamhaa commented Feb 4, 2022

@ObserverOfTime You're right 😅 . You can close this issue.

Do you know where the print output goes to?, It doesn't appear under with :messages.

@ElSamhaa ElSamhaa closed this as completed Feb 4, 2022
@ObserverOfTime
Copy link

Use vim.notify instead.

@ObserverOfTime
Copy link

Also, you can use vim.api.nvim_buf_get_lines instead of manually opening the file.

@ElSamhaa
Copy link
Author

ElSamhaa commented Feb 5, 2022

Thanks

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants