Skip to content

Commit

Permalink
add new option
Browse files Browse the repository at this point in the history
  • Loading branch information
Futarimiti committed Oct 7, 2023
1 parent d8572fb commit 159657f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ require('spooky').setup { directory = vim.fn.stdpath('config') .. '/skeletons'
, prompt = 'Select template'
, previewer_prompt = 'Preview'
, preview_normalised = true
, highlight_preview = true
, no_template = '<No template>'
, telescope_opts = {}
}
Expand All @@ -75,6 +76,7 @@ Full description of the options:
| `ui.prompt` | string | Prompt for selecting templates |
| `ui.previewer_prompt` | string | Prompt for previewing templates (Telescope only) |
| `ui.preview_normalised` | boolean | Whether to show normalised template in preview (Telescope only) |
| `ui.highlight_preview` | boolean | Whether to highlight preview (Telescope only) |
| `ui.no_template` | string | String for `show_no_template` |
| `ui.telescope_opts` | table | Options to be passed to the picker, e.g. `require('telescope.themes').get_ivy {}` (Telescope only) |

Expand Down
2 changes: 2 additions & 0 deletions lua/spooky/config/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ M.defaults = { directory = vim.fn.stdpath('config') .. '/skeletons'
, prompt = 'Select template'
, previewer_prompt = 'Preview'
, preview_normalised = true
, highlight_preview = true
, no_template = '<No template>'
, telescope_opts = {}
}
Expand All @@ -28,6 +29,7 @@ M.typecheck = function (config)
, ['ui.prompt'] = { config.ui.prompt, 'string' }
, ['ui.previewer_prompt'] = { config.ui.previewer_prompt, 'string' }
, ['ui.preview_normalised'] = { config.ui.preview_normalised, 'boolean' }
, ['ui.highlight_preview'] = { config.ui.highlight_preview, 'boolean' }
, ['ui.no_template'] = { config.ui.no_template, 'string' }
, ['ui.telescope_opts'] = { config.ui.telescope_opts, 'table' }
}
Expand Down
27 changes: 16 additions & 11 deletions lua/spooky/templates/choose/telescope.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ M.choose_one = function (buf, fullpaths, user, insert, preview)
return ret, map
end)()

local highlight = function (previewer_buf, normalised)
if not normalised then
util.regex_highlighter(previewer_buf, 'spooky')
return
end

local ft, _ = vim.filetype.match { buf = previewer_buf }
if ft ~= nil then
util.highlighter(previewer_buf, ft)
end
end

local define_preview = function (self, entry, _)
local entry_name = entry[1]
local selected_no_template = entry_name == no_template
Expand All @@ -51,19 +63,12 @@ M.choose_one = function (buf, fullpaths, user, insert, preview)
end

local fullpath = mappings[entry_name]
local normalise = user.ui.preview_normalised
local highlight = normalise
local result = preview(fullpath, normalise)
local should_normalise = user.ui.preview_normalised
local should_highlight = user.ui.highlight_preview
local result = preview(fullpath, should_normalise)

vim.api.nvim_buf_set_lines(preview_buf, 0, -1, false, result)
if highlight then
local ft, _ = vim.filetype.match { buf = preview_buf, filename = vim.api.nvim_buf_get_name(buf) }
if ft ~= nil then
util.highlighter(preview_buf, ft)
else
util.regex_highlighter(preview_buf, 'spooky')
end
end
if should_highlight then highlight(buf, should_normalise) end
end

local previewer = previewers.new_buffer_previewer
Expand Down

0 comments on commit 159657f

Please sign in to comment.