Skip to content

Commit

Permalink
🚧 Feat: Add source to find template telescope
Browse files Browse the repository at this point in the history
  • Loading branch information
Nguyen-Hoang-Nam committed Aug 7, 2022
1 parent 5a4ac98 commit a79de66
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 37 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,6 @@ You can check test tools [here](https://github.com/vim-test/vim-test)
- [ ] Handle lsp_extension
- [ ] Support [nvim-covarage](https://github.com/andythigpen/nvim-coverage)
- [x] Support [nvim-ufo](https://github.com/kevinhwang91/nvim-ufo)
- [ ] Support [template.nvim](https://github.com/glepnir/template.nvim)
- [x] Support [nvim-notify](https://github.com/rcarriga/nvim-notify)
- [ ] Support [Graphql](https://github.com/graphql/graphiql/tree/main/packages/graphql-language-service-cli)

Expand Down
2 changes: 1 addition & 1 deletion lua/setup/telescope/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ telescope.setup({
telescope.load_extension("fzf") -- Better fuzzy search result
telescope.load_extension("ui-select") -- Use for lsp action
telescope.load_extension("aerial")
telescope.load_extension("template")
telescope.load_extension("find_template")
48 changes: 48 additions & 0 deletions lua/telescope/_extensions/find_template.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
-- Credit https://github.com/glepnir/template.nvim/blob/main/lua/telescope/_extensions/find_template.lua
local telescope = require("telescope")
local finders = require("telescope.finders")
local pickers = require("telescope.pickers")
local conf = require("telescope.config").values
local entry_display = require("telescope.pickers.entry_display")

local templates = require("templates.templates")

local find_template = function(opts)
opts = opts or {}

-- Credit https://github.com/xiyaowong/telescope-emoji.nvim/blob/master/lua/telescope/_extensions/emoji.lua
local displayer = entry_display.create({
separator = " ",
items = {
{ width = 40 },
{ width = 18 },
{ remaining = true },
},
})

local make_display = function(entry)
return displayer({
entry.value .. " " .. entry.name,
})
end

pickers.new(opts, {
prompt_title = "Templates",
results_title = "Templates",
sorter = conf.generic_sorter(opts),
finder = finders.new_table({
results = templates,
entry_maker = function(template)
return {
ordinal = template.name,
display = make_display,

name = template.name,
value = template.value,
}
end,
}),
}):find()
end

return telescope.register_extension({ exports = { find_template = find_template } })
28 changes: 0 additions & 28 deletions lua/telescope/_extensions/template.lua

This file was deleted.

22 changes: 22 additions & 0 deletions lua/templates/templates.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
local M = {}

M.templates = {
{
name = "java/class",
value = require("templates.java.class").template,
},
{
name = "java/interface",
value = require("templates.java.interface").template,
},
{
name = "java/enum",
value = require("templates.java.enum").template,
},
{
name = "java/mongo repository",
value = require("templates.java.mongo_repository").template,
},
}

return M
30 changes: 23 additions & 7 deletions lua/utils/templates.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ function M.file_supported(file_extension)
return file_extension == "java" or file_extension == "md"
end

local function java_name(filename, file_extension)
return string.sub(filename, 1, #filename - #file_extension - 1)
local function java_name(filename)
return string.sub(filename, 1, #filename - 5)
end

local function slice(tbl, s, e)
Expand Down Expand Up @@ -41,12 +41,32 @@ local function java_package_name(path, filename)
return t[#t]
end

local expr = {
"{{_java_package_name_}}",
"{{_cursor_}}",
"{{_java_name}}",
}

local expand = {
[expr[1]] = function(ctx)
local package_name = java_package_name(ctx.path, ctx.filename)
return ctx.line:gsub(expr[1], package_name)
end,
[expr[2]] = function(ctx)
return ctx.line:gsub(expr[2], "")
end,
[expr[3]] = function(ctx)
local package_name = java_name(ctx.filename)
return ctx.line:gsub(expr[1], package_name)
end,
}

function M.generate(file_extension, filename, path, file_type)
if file_extension == "md" and filename == "README.md" then
return require("languages.markdown").template[file_type]
elseif file_extension == "java" then
local template = require("languages.java").template[file_type]
local name = java_name(filename, file_extension)
local name = java_name(filename)
local group = java_package_name(path, filename)

return string.format(template, group, name)
Expand All @@ -55,8 +75,4 @@ function M.generate(file_extension, filename, path, file_type)
end
end

function M.get_all_templates()
return {}
end

return M

0 comments on commit a79de66

Please sign in to comment.