Skip to content

Commit

Permalink
add: g<C-a> in normal mode
Browse files Browse the repository at this point in the history
  • Loading branch information
monaqa committed Apr 4, 2023
1 parent 0fb00f5 commit 1c76c44
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 9 deletions.
12 changes: 10 additions & 2 deletions autoload/dial/operator.vim
@@ -1,9 +1,17 @@
function dial#operator#increment_normal(type, ...)
lua require("dial.command").operator_normal("increment")
lua require("dial.command").operator_normal("increment", false)
endfunction

function dial#operator#decrement_normal(type, ...)
lua require("dial.command").operator_normal("decrement")
lua require("dial.command").operator_normal("decrement", false)
endfunction

function dial#operator#increment_gnormal(type, ...)
lua require("dial.command").operator_normal("increment", true)
endfunction

function dial#operator#decrement_gnormal(type, ...)
lua require("dial.command").operator_normal("decrement", true)
endfunction

function dial#operator#increment_visual(type, ...)
Expand Down
9 changes: 7 additions & 2 deletions lua/dial/command.lua
Expand Up @@ -34,6 +34,10 @@ function M.select_augend_normal(group_name)
handler:select_augend(line, col, augends)
end

function M.select_augend_gnormal(group_name)
return M.select_augend_normal(group_name)
end

---Select the most appropriate augend from given augend group (in VISUAL mode).
---@param group_name? string
function M.select_augend_visual(group_name)
Expand Down Expand Up @@ -102,12 +106,13 @@ end

---The process that runs when operator is called (in NORMAL mode).
---@param direction direction
function M.operator_normal(direction)
---@param additive? boolean
function M.operator_normal(direction, additive)
local col = vim.fn.col "."
local line_num = vim.fn.line "."
local line = vim.fn.getline "."

local result = handler:operate(line, col, direction)
local result = handler:operate(line, col, direction, additive)

if result.line ~= nil then
vim.fn.setline(".", result.line)
Expand Down
18 changes: 15 additions & 3 deletions lua/dial/handle.lua
Expand Up @@ -95,12 +95,13 @@ end

---@class Handler
---@field count integer
---@field cumsum integer
---@field range textrange?
---@field active_augend Augend?
local Handler = {}

function Handler.new()
return setmetatable({ count = 1, range = nil, active_augend = nil }, { __index = Handler })
return setmetatable({ count = 1, cumsum = 0, range = nil, active_augend = nil }, { __index = Handler })
end

---Get addend value.
Expand All @@ -125,6 +126,9 @@ end
---@param cursor? integer
---@param augends Augend[]
function Handler:select_augend(line, cursor, augends)
-- initialize
self.cumsum = 0

local interim_augend = nil
local interim_score = Score.new(3, 0, 0) -- score with the lowest priority

Expand Down Expand Up @@ -155,6 +159,9 @@ end
---@param cursor? integer
---@param augends Augend[]
function Handler:select_augend_visual(lines, cursor, augends)
-- initialize
self.cumsum = 0

local interim_augend = nil
local interim_score = Score.new(3, 0, 0) -- 最も優先度の低いスコア

Expand Down Expand Up @@ -189,15 +196,16 @@ end
---@param line string
---@param cursor integer
---@param direction direction
---@param additive? boolean
---@return {line?: string, cursor?: integer}
function Handler:operate(line, cursor, direction)
function Handler:operate(line, cursor, direction, additive)
if self.range == nil or self.active_augend == nil then
return {}
end

local text = line:sub(self.range.from, self.range.to)
local addend = self:get_addend(direction)
local add_result = self.active_augend:add(text, addend, cursor)
local add_result = self.active_augend:add(text, addend * (self.cumsum + 1), cursor)
local new_line = nil
local new_cursor = nil

Expand All @@ -208,6 +216,10 @@ function Handler:operate(line, cursor, direction)
new_cursor = self.range.from - 1 + add_result.cursor
end

if additive then
self.cumsum = self.cumsum + 1
end

return { line = new_line, cursor = new_cursor }
end

Expand Down
15 changes: 14 additions & 1 deletion lua/dial/map.lua
Expand Up @@ -26,7 +26,8 @@ local function _cmd_sequence(direction, mode, group_name, count)
end
-- command.select_augend_normal(vim.v.count, group_name)
local setopfunc = cmdcr([[let &opfunc="dial#operator#]] .. direction .. "_" .. mode .. [["]])
local textobj = util.if_expr(mode == "normal", cmdcr [[lua require("dial.command").textobj()]], "")
local textobj =
util.if_expr(mode == "normal" or mode == "gnormal", cmdcr [[lua require("dial.command").textobj()]], "")
if count ~= nil then
if type(count) ~= "number" then
error "count must be a integer."
Expand Down Expand Up @@ -60,6 +61,18 @@ function M.dec_normal(group_name)
return _cmd_sequence("decrement", "normal", group_name)
end

---@param group_name? string
---@return string
function M.inc_gnormal(group_name)
return _cmd_sequence("increment", "gnormal", group_name)
end

---@param group_name? string
---@return string
function M.dec_gnormal(group_name)
return _cmd_sequence("decrement", "gnormal", group_name)
end

---@param group_name? string
---@return string
function M.inc_visual(group_name)
Expand Down
2 changes: 1 addition & 1 deletion lua/dial/types.lua
Expand Up @@ -2,7 +2,7 @@
-- (See: https://github.com/sumneko/lua-language-server/wiki/Annotations)

---@alias direction '"increment"' | '"decrement"'
---@alias mode '"normal"' | '"visual"' | '"gvisual"'
---@alias mode '"normal"' | '"gnormal"' | '"visual"' | '"gvisual"'
---@alias textrange {from: integer, to: integer}
---@alias addresult {text?: string, cursor?: integer}

Expand Down
2 changes: 2 additions & 0 deletions plugin/dial.vim
Expand Up @@ -6,6 +6,8 @@ set cpo&vim " reset them to defaults
lua << EOF
vim.api.nvim_set_keymap("n", "<Plug>(dial-increment)", require("dial.map").inc_normal(), {noremap = true})
vim.api.nvim_set_keymap("n", "<Plug>(dial-decrement)", require("dial.map").dec_normal(), {noremap = true})
vim.api.nvim_set_keymap("n", "g<Plug>(dial-increment)", require("dial.map").inc_gnormal(), {noremap = true})
vim.api.nvim_set_keymap("n", "g<Plug>(dial-decrement)", require("dial.map").dec_gnormal(), {noremap = true})
vim.api.nvim_set_keymap("v", "<Plug>(dial-increment)", require("dial.map").inc_visual() .. "gv", {noremap = true})
vim.api.nvim_set_keymap("v", "<Plug>(dial-decrement)", require("dial.map").dec_visual() .. "gv", {noremap = true})
vim.api.nvim_set_keymap("v", "g<Plug>(dial-increment)", require("dial.map").inc_gvisual() .. "gv", {noremap = true})
Expand Down

0 comments on commit 1c76c44

Please sign in to comment.