Vonr / align.nvim

A minimal plugin for NeoVim for aligning lines
GNU General Public License v3.0
147 stars 4 forks source link

Some error #1

Closed kaiphat closed 2 years ago

kaiphat commented 2 years ago

image Then i press <Space> image

Chromosore commented 2 years ago

This patch seems to fix the issue. I don't want to make it into a PR however, because it may be better to create a local function that does this and I cannot take decisions for projects that aren't mine.

diff --git a/lua/align/init.lua b/lua/align/init.lua
index 6b5ef82..3ab2dce 100644
--- a/lua/align/init.lua
+++ b/lua/align/init.lua
@@ -127,7 +127,7 @@ local function align(str, reverse, preview, marks)
         local r, c = unpack(pos)
         local curr = lines[r - sr + 1]
         if c <= target then
-            vim.api.nvim_buf_set_lines(0, r - 1, r, true, {string.insert(curr, c, (' '):rep(target - c + (reverse and 1 or 0)))})
+            vim.api.nvim_buf_set_lines(0, r - 1, r, true, {curr:sub(1, c-1) .. (' '):rep(target - c + (reverse and 1 or 0)) .. curr:sub(c)})
         else
             vim.api.nvim_buf_set_lines(0, r - 1, r, true, {string.sub(curr, 1, target) .. string.sub(curr, c)})
         end
With a local function ```diff diff --git a/lua/align/init.lua b/lua/align/init.lua index 6b5ef82..95c20fb 100644 --- a/lua/align/init.lua +++ b/lua/align/init.lua @@ -36,6 +36,10 @@ local function escape(str, is_pattern) return str end +local function str_insert(dst, pos, src) + return dst:sub(1, pos-1) .. src .. dst:sub(pos) +end + local function align(str, reverse, preview, marks) local sr, sc, er, ec if marks then @@ -127,7 +131,7 @@ local function align(str, reverse, preview, marks) local r, c = unpack(pos) local curr = lines[r - sr + 1] if c <= target then - vim.api.nvim_buf_set_lines(0, r - 1, r, true, {string.insert(curr, c, (' '):rep(target - c + (reverse and 1 or 0)))}) + vim.api.nvim_buf_set_lines(0, r - 1, r, true, {str_insert(curr, c, (' '):rep(target - c + (reverse and 1 or 0)))}) else vim.api.nvim_buf_set_lines(0, r - 1, r, true, {string.sub(curr, 1, target) .. string.sub(curr, c)}) end ```
Vonr commented 2 years ago

The patch looks good to me, I'm not sure why the current implementatuon did not work, but I am willing to either accept a pull request with the changes or I can change it myself.

Chromosore commented 2 years ago

Honestly I was surprised that it worked for you because according to the lua reference, there is no string.insert method, so I don't know how you got one. Maybe you have a plugin that is monkey patching the string object.

Anyway, go ahead and change it yourself.