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

fixbug: Add description to keymaps #8

Merged
merged 2 commits into from
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions lua/textcase/extensions/whichkey.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
local M = {}

function M.register(mode, mappings)
local opts = {
n = {
mode = "n",
buffer = nil,
silent = true,
noremap = true,
nowait = true,
},
v = {
mode = "v",
buffer = nil,
silent = true,
noremap = true,
nowait = true,
}
}

pcall(
require("which-key").register,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@johmsalas I think this is probably why you had issues, this function always requires which key even if it is inside a pcall because rather than wrapping the require you wrap the register.

This should be local ok, whichkey = pcall(require, 'which-key') then if ok is true then use register

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤯 I'm realizing you are a ninja.
If I could give you a review that you'd be "empowers others"

mappings,
opts[mode]
)
end

return M
28 changes: 17 additions & 11 deletions lua/textcase/plugin/plugin.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
local utils = require("textcase.shared.utils")
local constants = require("textcase.shared.constants")
local conversion = require("textcase.plugin.conversion")
local config = require("textcase.plugin.config")
local flag_incremental_preview = vim.fn.has("nvim-0.8-dev+374-ge13dcdf16") == 1

local M = {}
Expand All @@ -16,7 +15,7 @@ M.state = {
substitute = {},
}

function M.register_keybindings(method_table, keybindings, opts)
function M.register_keybindings(prefix, method_table, keybindings, opts)
-- TODO: validate method_table
M.state.methods_by_desc[method_table.desc] = method_table
M.state.methods_by_desc[method_table.desc].opts = opts
Expand All @@ -34,19 +33,30 @@ function M.register_keybindings(method_table, keybindings, opts)
if feature == 'visual' then
mode = 'v'
end
vim.api.nvim_set_keymap(
local desc = method_table.desc

if feature == 'current_word' then
desc = 'Convert ' .. desc
elseif feature == 'lsp_rename' then
desc = 'LSP rename ' .. desc
end

vim.keymap.set(
mode,
keybindings[feature],
prefix .. keybindings[feature],
"<cmd>lua require('" .. constants.namespace .. "')." .. feature .. "('" .. method_table.desc .. "')<cr>",
{ noremap = true }
{ desc = desc }
)

end
end

-- whichkey.register_batch(prefix)
end

function M.register_keys(method_table, keybindings)
function M.register_keys(prefix, method_table, keybindings)
-- Sugar syntax
M.register_keybindings(method_table, {
M.register_keybindings(prefix, method_table, {
line = keybindings[1],
eol = keybindings[2],
visual = keybindings[3],
Expand Down Expand Up @@ -113,10 +123,6 @@ function M.incremental_substitute(opts, preview_ns, preview_buf)
local source, dest = params[2], params[3]
local buf = (preview_ns ~= nil) and preview_buf or vim.api.nvim_get_current_buf()

if M.state.substitute.list == nil then

end

local cursor_pos = vim.fn.getpos(".")
vim.api.nvim_buf_clear_namespace(buf, 1, 0, -1)

Expand Down
92 changes: 57 additions & 35 deletions lua/textcase/plugin/presets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,67 @@ local M = {}

local plugin = require('textcase.plugin.plugin')
local api = require('textcase.plugin.api')
local whichkey = require("textcase.extensions.whichkey")

M.setup = function(opts)
local prefix = opts and opts.prefix or 'ga'

plugin.register_keybindings(api.to_constant_case, {
current_word = prefix .. 'n',
visual = prefix .. 'n',
operator = prefix .. 'on',
lsp_rename = prefix .. 'N',
})
plugin.register_keybindings(api.to_camel_case, {
current_word = prefix .. 'c',
visual = prefix .. 'c',
operator = prefix .. 'oc',
lsp_rename = prefix .. 'C',
})
plugin.register_keybindings(api.to_dash_case, {
current_word = prefix .. 'd',
visual = prefix .. 'd',
operator = prefix .. 'od',
lsp_rename = prefix .. 'D',
})
plugin.register_keybindings(api.to_pascal_case, {
current_word = prefix .. 'p',
visual = prefix .. 'p',
operator = prefix .. 'op',
lsp_rename = prefix .. 'P',
})
plugin.register_keybindings(api.to_upper_case, {
current_word = prefix .. 'u',
visual = prefix .. 'u',
operator = prefix .. 'ou',
lsp_rename = prefix .. 'U',
})
plugin.register_keybindings(api.to_lower_case, {
current_word = prefix .. 'l',
visual = prefix .. 'l',
operator = prefix .. 'ol',
lsp_rename = prefix .. 'L',
whichkey.register('v', {
[prefix] = {
name = 'text-case',
}
})

whichkey.register('n', {
[prefix] = {
name = 'text-case',
o = {
name = 'Pending mode operator'
}
}
})

plugin.register_keybindings(prefix, api.to_constant_case, {
prefix = prefix,
current_word = 'n',
visual = 'n',
operator = 'on',
lsp_rename = 'N',
})
plugin.register_keybindings(prefix, api.to_camel_case, {
prefix = prefix,
current_word = 'c',
visual = 'c',
operator = 'oc',
lsp_rename = 'C',
})
plugin.register_keybindings(prefix, api.to_dash_case, {
prefix = prefix,
current_word = 'd',
visual = 'd',
operator = 'od',
lsp_rename = 'D',
})
plugin.register_keybindings(prefix, api.to_pascal_case, {
prefix = prefix,
current_word = 'p',
visual = 'p',
operator = 'op',
lsp_rename = 'P',
})
plugin.register_keybindings(prefix, api.to_upper_case, {
prefix = prefix,
current_word = 'u',
visual = 'u',
operator = 'ou',
lsp_rename = 'U',
})
plugin.register_keybindings(prefix, api.to_lower_case, {
prefix = prefix,
current_word = 'l',
visual = 'l',
operator = 'ol',
lsp_rename = 'L',
})

plugin.register_replace_command('Subs', {
Expand Down