diff --git a/lua/vern-nvim/actions/init.lua b/lua/vern-nvim/actions/init.lua index 5965b1d..aaa811a 100644 --- a/lua/vern-nvim/actions/init.lua +++ b/lua/vern-nvim/actions/init.lua @@ -1,5 +1,8 @@ local M = {} +---TODO +---Split filetype only and just change a filetype variable and vairables both share and then run the duplicate code + local utils = require("vern-nvim.utils") local templates = require("vern-nvim.templates") @@ -84,22 +87,18 @@ end M.create_file = function(opts) opts = opts or {} - vim.ui.input({ - prompt = "File language (e.g. python): ", - completion = "filetype", - }, function(ft) - ft = ft and vim.trim(ft) - if not ft or vim.trim(ft) == "" then - return - end - local ok, actions = pcall(require, "vern-nvim.actions." .. ft) + local filetype = vim.bo.filetype + + local function continue_filetype(ft) + filetype = ft + local ok, actions = pcall(require, "vern-nvim.actions." .. filetype) if not ok then - vim.notify("Unknown Filetype: " .. ft, vim.log.levels.WARN, { + vim.notify("Unknown Filetype: " .. filetype, vim.log.levels.WARN, { title = "Vern File", }) return end - local configs = require("vern-nvim.configs." .. ft) + local configs = require("vern-nvim.configs." .. filetype) local args = vim.tbl_deep_extend("force", {}, configs, opts or {}) vim.ui.input({ prompt = "Enter file name: " }, function(fname) @@ -110,7 +109,9 @@ M.create_file = function(opts) local mod if ok and actions and type(actions.create_file) == "function" then - mod = actions.create_file(args[ft]) or args[ft].templates.file.source or {} + mod = actions.create_file(args[filetype]) + or args[filetype].templates.file.source + or {} end local raw_path = vim.fs.joinpath(mod.path, mod.name) local context = { @@ -118,7 +119,7 @@ M.create_file = function(opts) company_name = opts.env.author, file_name = fname, } - local path = utils.resolve_path(raw_path, args[ft].dirs or {}, context) + local path = utils.resolve_path(raw_path, args[filetype].dirs or {}, context) local root = utils.proj_root() utils.dump( vim.fs.joinpath(root, path), @@ -127,7 +128,23 @@ M.create_file = function(opts) vim.cmd.edit(vim.fn.fnameescape(path)) vim.notify(fname .. " has been created!", vim.log.levels.INFO, { title = "Vern File" }) end) - end) + end + + local ft_ok, ft_config = pcall(require, "vern-nvim.configs." .. filetype) + if not filetype or filetype == "" or not ft_ok or type(ft_config) ~= "table" then + vim.ui.input({ + prompt = "File language (e.g. python): ", + completion = "filetype", + }, function(ft) + ft = ft and vim.trim(ft) + if not ft or vim.trim(ft) == "" then + return + end + continue_filetype(ft) + end) + else + continue_filetype(filetype) + end end return M