filetype configs with named files. has telescope
telescope is baller
2 files changed, +66 -2
+1-2lua/ftconfig/init.lua
| @@ -1,7 +1,6 @@ | ||
| 1 | 1 | local M = {} |
| 2 | - | |
| 3 | -M.ftdir = vim.fn.stdpath("config") .. "/lua/ftconfig" | |
| 4 | 2 | M.filetypes = {} |
| 3 | +M.ftdir = vim.fn.stdpath("config") .. "/lua/ftconfig" | |
| 5 | 4 | |
| 6 | 5 | if vim.fn.isdirectory(M.ftdir) == 0 then |
| 7 | 6 | vim.fn.mkdir(M.ftdir, "p") |
+65-0lua/ftconfig/telescope/_extensions/ftconfig.lua
| @@ -0,0 +1,65 @@ | ||
| 1 | +local pickers = require("telescope.pickers") | |
| 2 | +local finders = require("telescope.finders") | |
| 3 | +local conf = require("telescope.config").values | |
| 4 | +local actions = require("telescope.actions") | |
| 5 | +local action_state = require("telescope.actions.state") | |
| 6 | + | |
| 7 | +local function prioritize_filetypes(fts, current) | |
| 8 | + local pri, mid, rest = {}, {}, {} | |
| 9 | + local m = {} | |
| 10 | + for _, filetype in ipairs(require("ftconfig").filetypes) do | |
| 11 | + m[filetype] = true | |
| 12 | + end | |
| 13 | + for _, ft in ipairs(fts) do | |
| 14 | + if ft == current then | |
| 15 | + table.insert(pri, { display = " " .. ft, value = ft }) | |
| 16 | + elseif m[ft] then | |
| 17 | + table.insert(mid, { display = " " .. ft, value = ft }) | |
| 18 | + else | |
| 19 | + table.insert(rest, { display = " " .. ft, value = ft }) | |
| 20 | + end | |
| 21 | + end | |
| 22 | + vim.list_extend(pri, mid) | |
| 23 | + vim.list_extend(pri, rest) | |
| 24 | + return pri | |
| 25 | +end | |
| 26 | + | |
| 27 | +local function filetype_picker(opts) | |
| 28 | + opts = opts or {} | |
| 29 | + local current = vim.bo.filetype | |
| 30 | + local filetypes = vim.fn.getcompletion("", "filetype") | |
| 31 | + filetypes = prioritize_filetypes(filetypes, current) | |
| 32 | + | |
| 33 | + pickers | |
| 34 | + .new(opts, { | |
| 35 | + prompt_title = "FTConfig", | |
| 36 | + finder = finders.new_table({ | |
| 37 | + results = filetypes, | |
| 38 | + entry_maker = function(entry) | |
| 39 | + return { | |
| 40 | + value = entry.value, | |
| 41 | + display = entry.display, | |
| 42 | + ordinal = entry.value, | |
| 43 | + } | |
| 44 | + end, | |
| 45 | + }), | |
| 46 | + sorter = conf.generic_sorter(opts), | |
| 47 | + attach_mappings = function(prompt_bufnr, _) | |
| 48 | + actions.select_default:replace(function() | |
| 49 | + actions.close(prompt_bufnr) | |
| 50 | + local selection = action_state.get_selected_entry() | |
| 51 | + if selection then | |
| 52 | + require("ftconfig").edit_filetype(selection[1]:sub(3, -1)) | |
| 53 | + end | |
| 54 | + end) | |
| 55 | + return true | |
| 56 | + end, | |
| 57 | + }) | |
| 58 | + :find() | |
| 59 | +end | |
| 60 | + | |
| 61 | +return require("telescope").register_extension({ | |
| 62 | + exports = { | |
| 63 | + ftconfig = filetype_picker, | |
| 64 | + }, | |
| 65 | +}) |