annotate dot_config/nvim/plugin/filetypo.lua @ 628:1e6287df57f5

abort filetypo when there are no matches
author zegervdv <zegervdv@me.com>
date Thu, 04 Aug 2022 22:39:11 +0200
parents c0cef1aa4ccd
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
624
c0cef1aa4ccd Replace didyoumean with lua implementation using vim.ui.select
zegervdv <zegervdv@me.com>
parents:
diff changeset
1 -- Inspired by https://github.com/EinfachToll/DidYouMean/blob/master/plugin/DidYouMean.vim
c0cef1aa4ccd Replace didyoumean with lua implementation using vim.ui.select
zegervdv <zegervdv@me.com>
parents:
diff changeset
2
c0cef1aa4ccd Replace didyoumean with lua implementation using vim.ui.select
zegervdv <zegervdv@me.com>
parents:
diff changeset
3 local filetypo = function()
c0cef1aa4ccd Replace didyoumean with lua implementation using vim.ui.select
zegervdv <zegervdv@me.com>
parents:
diff changeset
4 if vim.fn.filereadable(vim.fn.expand '%') == 1 then return end
c0cef1aa4ccd Replace didyoumean with lua implementation using vim.ui.select
zegervdv <zegervdv@me.com>
parents:
diff changeset
5
c0cef1aa4ccd Replace didyoumean with lua implementation using vim.ui.select
zegervdv <zegervdv@me.com>
parents:
diff changeset
6 local filename = vim.fn.expand '%'
c0cef1aa4ccd Replace didyoumean with lua implementation using vim.ui.select
zegervdv <zegervdv@me.com>
parents:
diff changeset
7 local matching_files = vim.fn.split(vim.fn.glob(filename .. '*', 0), '\n')
628
1e6287df57f5 abort filetypo when there are no matches
zegervdv <zegervdv@me.com>
parents: 624
diff changeset
8 if matching_files == nil or vim.tbl_isempty(matching_files) then
1e6287df57f5 abort filetypo when there are no matches
zegervdv <zegervdv@me.com>
parents: 624
diff changeset
9 matching_files = vim.fn.split(vim.fn.glob(filename .. '*', 1), '\n')
1e6287df57f5 abort filetypo when there are no matches
zegervdv <zegervdv@me.com>
parents: 624
diff changeset
10 end
1e6287df57f5 abort filetypo when there are no matches
zegervdv <zegervdv@me.com>
parents: 624
diff changeset
11
1e6287df57f5 abort filetypo when there are no matches
zegervdv <zegervdv@me.com>
parents: 624
diff changeset
12 if matching_files == nil or vim.tbl_isempty(matching_files) then return end
624
c0cef1aa4ccd Replace didyoumean with lua implementation using vim.ui.select
zegervdv <zegervdv@me.com>
parents:
diff changeset
13
c0cef1aa4ccd Replace didyoumean with lua implementation using vim.ui.select
zegervdv <zegervdv@me.com>
parents:
diff changeset
14 local buf = vim.api.nvim_get_current_buf()
c0cef1aa4ccd Replace didyoumean with lua implementation using vim.ui.select
zegervdv <zegervdv@me.com>
parents:
diff changeset
15 vim.schedule(function()
c0cef1aa4ccd Replace didyoumean with lua implementation using vim.ui.select
zegervdv <zegervdv@me.com>
parents:
diff changeset
16 vim.ui.select(matching_files, { prompt = 'Select File:' }, function(choice)
c0cef1aa4ccd Replace didyoumean with lua implementation using vim.ui.select
zegervdv <zegervdv@me.com>
parents:
diff changeset
17 vim.cmd.edit(vim.fn.fnameescape(choice))
c0cef1aa4ccd Replace didyoumean with lua implementation using vim.ui.select
zegervdv <zegervdv@me.com>
parents:
diff changeset
18 vim.api.nvim_buf_delete(buf, { force = true })
c0cef1aa4ccd Replace didyoumean with lua implementation using vim.ui.select
zegervdv <zegervdv@me.com>
parents:
diff changeset
19
c0cef1aa4ccd Replace didyoumean with lua implementation using vim.ui.select
zegervdv <zegervdv@me.com>
parents:
diff changeset
20 vim.cmd.doautocmd { 'BufReadPre', mods = { silent = true } }
c0cef1aa4ccd Replace didyoumean with lua implementation using vim.ui.select
zegervdv <zegervdv@me.com>
parents:
diff changeset
21 vim.cmd.doautocmd { 'BufRead', mods = { silent = true } }
c0cef1aa4ccd Replace didyoumean with lua implementation using vim.ui.select
zegervdv <zegervdv@me.com>
parents:
diff changeset
22 vim.cmd.doautocmd { 'BufReadPost', mods = { silent = true } }
c0cef1aa4ccd Replace didyoumean with lua implementation using vim.ui.select
zegervdv <zegervdv@me.com>
parents:
diff changeset
23 vim.cmd.doautocmd { 'TextChanged', mods = { silent = true } }
c0cef1aa4ccd Replace didyoumean with lua implementation using vim.ui.select
zegervdv <zegervdv@me.com>
parents:
diff changeset
24 end)
c0cef1aa4ccd Replace didyoumean with lua implementation using vim.ui.select
zegervdv <zegervdv@me.com>
parents:
diff changeset
25 end)
c0cef1aa4ccd Replace didyoumean with lua implementation using vim.ui.select
zegervdv <zegervdv@me.com>
parents:
diff changeset
26 end
c0cef1aa4ccd Replace didyoumean with lua implementation using vim.ui.select
zegervdv <zegervdv@me.com>
parents:
diff changeset
27
c0cef1aa4ccd Replace didyoumean with lua implementation using vim.ui.select
zegervdv <zegervdv@me.com>
parents:
diff changeset
28 vim.api.nvim_create_autocmd('BufNewFile', { pattern = '*', callback = filetypo })