Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom expressions for replacements #44

Open
nkakouros opened this issue May 17, 2023 · 2 comments
Open

Support custom expressions for replacements #44

nkakouros opened this issue May 17, 2023 · 2 comments

Comments

@nkakouros
Copy link
Contributor

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
Copy link
Member

glepnir commented May 18, 2023

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

glepnir pushed a commit that referenced this issue Aug 24, 2024
…54)

* Refactor: Introduce a renderer with expressions & replacer funcs

And use it for rendering lines in the template. Also register
expressions at setup.

* Add the ability to register an expression replacer

* Documentation on how to register a new expression

* Removed an unnecessary closure after extracting the rendering logic
@mizuirorivi
Copy link

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:
    
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants