diff --git a/README.md b/README.md index 03c3d0e..baabed1 100644 --- a/README.md +++ b/README.md @@ -177,16 +177,16 @@ rest to use the defaults. ```lua require("text-transform").setup({ - -- Prints information about internals of the plugin. Very verbose, only useful for debugging. + --- Prints information about internals of the plugin. Very verbose, only useful for debugging. debug = false, - -- Keymap configurations + --- Keymap configurations keymap = { - -- Keymap to open the telescope popup. Set to `false` or `nil` to disable keymapping - -- You can always customize your own keymapping manually. + --- Keymap to open the telescope popup. Set to `false` or `nil` to disable keymapping + --- You can always customize your own keymapping manually. telescope_popup = { - -- Opens the popup in normal mode + --- Opens the popup in normal mode ["n"] = "~", - -- Opens the popup in visual/visual block modes + --- Opens the popup in visual/visual block modes ["v"] = "~", }, }, @@ -204,6 +204,10 @@ require("text-transform").setup({ snake_case = { enabled = true }, title_case = { enabled = true }, }, + + --- Sort the replacers in the popup. + --- Possible values: 'frequency', 'name' + sort_by = 'frequency', }) ``` diff --git a/doc/text-transform.txt b/doc/text-transform.txt index 185a732..fff52dd 100644 --- a/doc/text-transform.txt +++ b/doc/text-transform.txt @@ -15,16 +15,16 @@ Your plugin configuration with its default values. Default values: > TextTransform.options = { - -- Prints information about internals of the plugin. Very verbose, only useful for debugging. + --- Prints information about internals of the plugin. Very verbose, only useful for debugging. debug = false, - -- Keymap configurations + --- Keymap configurations keymap = { - -- Keymap to open the telescope popup. Set to `false` or `nil` to disable keymapping - -- You can always customize your own keymapping manually. + --- Keymap to open the telescope popup. Set to `false` or `nil` to disable keymapping + --- You can always customize your own keymapping manually. telescope_popup = { - -- Opens the popup in normal mode + --- Opens the popup in normal mode ["n"] = "~", - -- Opens the popup in visual/visual block modes + --- Opens the popup in visual/visual block modes ["v"] = "~", }, }, @@ -42,6 +42,10 @@ Default values: snake_case = { enabled = true }, title_case = { enabled = true }, }, + + --- Sort the replacers in the popup. + --- Possible values: 'frequency', 'name' + sort_by = "frequency", } local function init() diff --git a/lua/text-transform/config.lua b/lua/text-transform/config.lua index 65fdf32..97487a4 100644 --- a/lua/text-transform/config.lua +++ b/lua/text-transform/config.lua @@ -9,16 +9,16 @@ local TextTransform = {} --- Default values: ---@eval return MiniDoc.afterlines_to_code(MiniDoc.current.eval_section) TextTransform.options = { - -- Prints information about internals of the plugin. Very verbose, only useful for debugging. + --- Prints information about internals of the plugin. Very verbose, only useful for debugging. debug = false, - -- Keymap configurations + --- Keymap configurations keymap = { - -- Keymap to open the telescope popup. Set to `false` or `nil` to disable keymapping - -- You can always customize your own keymapping manually. + --- Keymap to open the telescope popup. Set to `false` or `nil` to disable keymapping + --- You can always customize your own keymapping manually. telescope_popup = { - -- Opens the popup in normal mode + --- Opens the popup in normal mode ["n"] = "~", - -- Opens the popup in visual/visual block modes + --- Opens the popup in visual/visual block modes ["v"] = "~", }, }, @@ -36,6 +36,10 @@ TextTransform.options = { snake_case = { enabled = true }, title_case = { enabled = true }, }, + + --- Sort the replacers in the popup. + --- Possible values: 'frequency', 'name' + sort_by = "frequency", } local function init() diff --git a/lua/text-transform/replacers.lua b/lua/text-transform/replacers.lua index 29d0e87..14d30a6 100644 --- a/lua/text-transform/replacers.lua +++ b/lua/text-transform/replacers.lua @@ -138,10 +138,14 @@ end --- This allows to treat all ranges equally and allows to work on each selection without knowing --- the full information around the selection logic. function TextTransform.get_visual_selection_details() + if not state.positions then + D.log("replacers", "No positions saved") + return {} + end D.log( "replacers", "Getting visual selection details - mode: %s, is_visual: %s, is_block: %s", - vim.inspect(state.positions), + state.positions.mode, utils.is_visual_mode(), utils.is_block_visual_mode() ) diff --git a/lua/text-transform/telescope.lua b/lua/text-transform/telescope.lua index f83ef63..31963bf 100644 --- a/lua/text-transform/telescope.lua +++ b/lua/text-transform/telescope.lua @@ -97,6 +97,12 @@ local frequency_sorter = Sorter:new({ return final_score end, }) + +local sorter_map = { + frequency = frequency_sorter, + name = generic_sorter, +} + ---@diagnostic disable-next-line: unused-local -- for _i, k in pairs(default_ordered_keys) do -- local v = map[k] @@ -114,11 +120,14 @@ local frequency_sorter = Sorter:new({ --- made. function TextTransform.popup() state.save_positions() - load_frequency() local filtered = {} - print(vim.inspect(_G.TextTransform.config)) local config = _G.TextTransform.config + local sorter = sorter_map[config.sort_by] or generic_sorter + + if config.sort_by == "frequency" then + load_frequency() + end for _, item in ipairs(items) do if not config.replacers[item.value] or not config.replacers[item.value].enabled then @@ -134,7 +143,7 @@ function TextTransform.popup() results = items, entry_maker = entry_maker, }), - sorter = frequency_sorter, + sorter = sorter, attach_mappings = function(prompt_bufnr) actions.select_default:replace(function() local selection = action_state.get_selected_entry()