From bdd1d949de3b77657877a288afc608acf1cdd58f Mon Sep 17 00:00:00 2001
From: Jonathan Apodaca <jrapodaca@gmail.com>
Date: Fri, 11 Oct 2024 18:18:57 -0600
Subject: [PATCH] switch more APIs away from feedkeys

---
 lua/tt/pos.lua   | 21 +++------------------
 lua/tt/range.lua | 18 ++++++++++++------
 2 files changed, 15 insertions(+), 24 deletions(-)

diff --git a/lua/tt/pos.lua b/lua/tt/pos.lua
index 751e13c..d8c7a42 100644
--- a/lua/tt/pos.lua
+++ b/lua/tt/pos.lua
@@ -201,17 +201,8 @@ function Pos:find_match(max_chars, invocations)
   local is_closer = vim.tbl_contains(closers, c)
   if not is_opener and not is_closer then return nil end
 
-  ---@type number
-  local i
-  ---@type string
-  local c_match
-  if is_opener then
-    i, _ = vim.iter(openers):enumerate():find(function(_, c2) return c == c2 end)
-    c_match = closers[i]
-  else
-    i, _ = vim.iter(closers):enumerate():find(function(_, c2) return c == c2 end)
-    c_match = openers[i]
-  end
+  local i, _ = vim.iter(is_opener and openers or closers):enumerate():find(function(_, c2) return c == c2 end)
+  local c_match = (is_opener and closers or openers)[i]
 
   ---@type Pos|nil
   local cur = self
@@ -224,13 +215,7 @@ function Pos:find_match(max_chars, invocations)
       if max_chars < 0 then return nil end
     end
 
-    if is_opener then
-      -- scan forward
-      return cur:next()
-    else
-      -- scan backward
-      return cur:next(-1)
-    end
+    return cur:next(is_opener and 1 or -1)
   end
 
   -- scan until we find a match:
diff --git a/lua/tt/range.lua b/lua/tt/range.lua
index 7c50746..0e26369 100644
--- a/lua/tt/range.lua
+++ b/lua/tt/range.lua
@@ -116,10 +116,12 @@ function Range.from_text_object(text_obj, opts)
       null_pos:save_to_pos "'["
       null_pos:save_to_pos "']"
 
-      -- For some reason,
-      -- vim.cmd.normal { cmd = 'normal', args = { '""y' .. text_obj }, mods = { silent = true } }
-      -- does not work in all cases. We resort to using feedkeys instead:
-      utils.feedkeys('""y' .. text_obj, opts.user_defined and 'mx' or 'nx') -- 'm' = remap, 'n' = noremap
+      vim.cmd.normal {
+        cmd = 'normal',
+        bang = not opts.user_defined,
+        args = { '""y' .. text_obj },
+        mods = { silent = true },
+      }
 
       local start = Pos.from_pos "'["
       local stop = Pos.from_pos "']"
@@ -403,8 +405,12 @@ function Range:set_visual_selection()
     self.start:save_to_mark 'a'
     self.stop:save_to_mark 'b'
     local mode = self.mode
-    if vim.api.nvim_get_mode().mode == 'n' then utils.feedkeys(mode) end
-    utils.feedkeys '`ao`b'
+
+    local normal_cmd_args = ''
+    if vim.api.nvim_get_mode().mode == 'n' then normal_cmd_args = normal_cmd_args .. mode end
+    normal_cmd_args = normal_cmd_args .. '`ao`b'
+    vim.cmd { cmd = 'normal', args = { normal_cmd_args }, bang = true }
+
     return nil
   end)
 end