view dot_config/nvim/plugin/filetypo.lua @ 801:16d86895ac4b

[pre-commit.ci] pre-commit autoupdate updates: - [github.com/pre-commit/pre-commit-hooks: v4.4.0 ? v4.5.0](https://github.com/pre-commit/pre-commit-hooks/compare/v4.4.0...v4.5.0) - [github.com/JohnnyMorganz/StyLua: v0.18.1 ? v0.19.0](https://github.com/JohnnyMorganz/StyLua/compare/v0.18.1...v0.19.0) - [github.com/pre-commit/mirrors-prettier: v3.0.2 ? v3.1.0](https://github.com/pre-commit/mirrors-prettier/compare/v3.0.2...v3.1.0)
author pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
date Mon, 13 Nov 2023 18:18:57 +0000
parents 1e6287df57f5
children
line wrap: on
line source

-- Inspired by https://github.com/EinfachToll/DidYouMean/blob/master/plugin/DidYouMean.vim

local filetypo = function()
  if vim.fn.filereadable(vim.fn.expand '%') == 1 then return end

  local filename = vim.fn.expand '%'
  local matching_files = vim.fn.split(vim.fn.glob(filename .. '*', 0), '\n')
  if matching_files == nil or vim.tbl_isempty(matching_files) then
    matching_files = vim.fn.split(vim.fn.glob(filename .. '*', 1), '\n')
  end

  if matching_files == nil or vim.tbl_isempty(matching_files) then return end

  local buf = vim.api.nvim_get_current_buf()
  vim.schedule(function()
    vim.ui.select(matching_files, { prompt = 'Select File:' }, function(choice)
      vim.cmd.edit(vim.fn.fnameescape(choice))
      vim.api.nvim_buf_delete(buf, { force = true })

      vim.cmd.doautocmd { 'BufReadPre', mods = { silent = true } }
      vim.cmd.doautocmd { 'BufRead', mods = { silent = true } }
      vim.cmd.doautocmd { 'BufReadPost', mods = { silent = true } }
      vim.cmd.doautocmd { 'TextChanged', mods = { silent = true } }
    end)
  end)
end

vim.api.nvim_create_autocmd('BufNewFile', { pattern = '*', callback = filetypo })