nvimdev / template.nvim

Quickly insert templates into file
MIT License
197 stars 20 forks source link

Support custom expressions for replacements #44

Open nkakouros opened 1 year ago

nkakouros commented 1 year ago

Following up on #38, it would be nice to allow the user to define custom expressions that will get replaced in a template. I would submit a PR but I am afraid my Lua skills are limited for sth like that.

glepnir commented 1 year ago

don't worry go for it or wait for me when i have time recently

mizuirorivi commented 3 weeks ago

this is example one please refer this by the way, forgive me about what the script is sloppy

-- template.lua
require('template').setup({
  temp_dir = "~/.config/nvim/templates",       -- template directory
  author = "mizuiro_rivi",                     -- your name
  email = "onodaha@gmail.com",                 -- email address
})
_G.template_persistent_vars = {}

function get_or_set_persistent_var(var_name)
    if _G.template_persistent_vars[var_name] then
        return _G.template_persistent_vars[var_name]
    else
        local value = vim.fn.input(var_name .. ": ")
        _G.template_persistent_vars[var_name] = value
        return value
    end
end

require('template').register('{{_variableA_}}', function()
    return get_or_set_persistent_var('variableA')
end)
-- whichkey.lua

local wk = require("which-key")

vim.g.mapleader = "\\"

wk.setup({
  triggers = {"<space>"}
})

require('telescope').load_extension('vim_bookmarks')

wk.register({
  f = {
    name = "find by using Telescope",
    f = { "<cmd>Telescope find_files<cr>", "Find Files" },
    r = { "<cmd>Telescope oldfiles<cr>", "Recent Files" },
    b = { "<cmd>Telescope buffers<cr>", "Buffers" },
    n = { "<cmd> Telescope notify<cr>","Show Notifications"},
    ma = { "<cmd>Telescope vim_bookmarks all<cr>","Show all bookmarks"},
    mc = { "<cmd>Telescope vim_bookmarks current_file<cr>","Show bookmarks in current file"},
  },
  s = {
    name = "Terminal",
    s = { "<cmd>ToggleTerm<CR>", "Toggle Terminal" }
  },
  ["?"] = { function() require("which-key").show({}) end, "Show Keymaps (which-key)" },
},{prefix="<space>"})

vim.keymap.set('n', '<leader>k', "<cmd>lua require('fzf-lua').keymaps()<CR>", { desc = "Search Keymaps" })
-- template.tpl
struct {{_variableA_}} {
  public:
    {{_variableA_}}();
    ~{{_variableA_}}();
  private:

};