Skip to content

Commit

Permalink
refactor: restructuring utf8 support for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisgrieser committed Dec 21, 2023
1 parent 2727ace commit 6e66f0a
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions lua/spider/init.lua
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
local M = {}
local patternVariants = require("spider.pattern-variants")
--------------------------------------------------------------------------------

-- original lua string functions
local stringFuncs = {
reverse = string.reverse,
find = string.find,
gmatch = string.gmatch,
len = string.len,
init_pos = function(_, col)
col = col + 1 -- from 0-based indexing to 1-based
local startCol = col
return col, startCol
end,
offset = function(_, pos) return pos end,
}

-- automatic remapping functions to utf8 supported functions
local ok, utf8 = pcall(require, "lua-utf8")
if ok then
--------------------------------------------------------------------------------
-- UTF-8 SUPPORT
local luaUtf8Installed, utf8 = pcall(require, "lua-utf8")
local stringFuncs

if not luaUtf8Installed then
-- use original lua string functions
stringFuncs = {
reverse = string.reverse,
find = string.find,
gmatch = string.gmatch,
len = string.len,
init_pos = function(_, col)
col = col + 1 -- from 0-based indexing to 1-based
local startCol = col
return col, startCol
end,
offset = function(_, pos) return pos end,
}
else
-- remapping functions to utf8 equivalents
for name, _ in pairs(stringFuncs) do
if utf8[name] then stringFuncs[name] = utf8[name] end
end
Expand Down Expand Up @@ -133,6 +136,8 @@ local function getNextPosition(line, offset, key, opts)
return nextPos
end

local function normal(keys) vim.cmd.normal{ keys, bang = true } end

--------------------------------------------------------------------------------

---@param key "w"|"e"|"b"|"ge" the motion to perform
Expand Down Expand Up @@ -180,7 +185,7 @@ function M.motion(key, motionOpts)

-- operator-pending specific considerations (see issues #3 and #5)
local mode = vim.api.nvim_get_mode().mode
local isOperatorPending = mode == "no" -- = [n]ormal & [o]perator, not the word "no"
local isOperatorPending = mode == "no" -- [n]ormal & [o]perator, not the word "no"
if isOperatorPending then
local lastCol = vim.fn.col("$")
if key == "e" then
Expand All @@ -189,17 +194,17 @@ function M.motion(key, motionOpts)
end

if lastCol - 1 == col then
-- HACK columns are end-exclusive, cannot actually target the last character
-- in the line otherwise without switching to visual mode?!
vim.cmd.normal { "v", bang = true }
-- HACK columns are end-exclusive, cannot actually target the last
-- character in the line without switching to visual mode
normal("v")
offset = offset - 1
col = stringFuncs.offset(line, offset) - 1 -- SIC indices in visual off-by-one compared to normal
end
end

-- respect `opt.foldopen = "hor"`
local shouldOpenFold = vim.tbl_contains(vim.opt_local.foldopen:get(), "hor")
if mode == "n" and shouldOpenFold then vim.cmd.normal { "zv", bang = true } end
if mode == "n" and shouldOpenFold then normal("zv") end

vim.api.nvim_win_set_cursor(0, { row, col })
end
Expand Down

0 comments on commit 6e66f0a

Please sign in to comment.