changeset 624:c0cef1aa4ccd

Replace didyoumean with lua implementation using vim.ui.select
author zegervdv <zegervdv@me.com>
date Mon, 01 Aug 2022 10:26:46 +0200
parents 5cceedc51955
children c8960ba7f019
files .chezmoitemplates/init.lua dot_config/nvim/plugin/filetypo.lua
diffstat 2 files changed, 24 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/.chezmoitemplates/init.lua	Mon Aug 01 10:24:05 2022 +0200
+++ b/.chezmoitemplates/init.lua	Mon Aug 01 10:26:46 2022 +0200
@@ -39,7 +39,6 @@
       }
     end,
   }
-  use { 'einfachtoll/didyoumean' }
 
   use {
     'tpope/vim-eunuch',
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dot_config/nvim/plugin/filetypo.lua	Mon Aug 01 10:26:46 2022 +0200
@@ -0,0 +1,24 @@
+-- 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 then matching_files = vim.fn.split(vim.fn.glob(filename .. '*', 1), '\n') 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 })