diff --git a/README.md b/README.md index c680b7d..3ba276a 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,7 @@ MP command autocompletes available options for current state, so cycle if opts = { neo_tree = false, -- enable neo-tree integration timer_interval = 100, -- poll for codemp callbacks every __ ms + debug = false, -- print text operations as they happen } ``` diff --git a/lua/codemp/buffers.lua b/lua/codemp/buffers.lua index 2d41499..9f919b5 100644 --- a/lua/codemp/buffers.lua +++ b/lua/codemp/buffers.lua @@ -42,10 +42,10 @@ local function attach(name, buffer, content) on_bytes = function(_, buf, tick, start_row, start_col, start_offset, old_end_row, old_end_col, old_end_byte_len, new_end_row, new_end_col, new_byte_len) if tick <= ticks[buf] then return end if id_buffer_map[buf] == nil then return true end -- unregister callback handler - print(string.format( + if CODEMP.config.debug then print(string.format( "start(row:%s, col:%s) offset:%s end(row:%s, col:%s new(row:%s, col:%s)) len(old:%s, new:%s)", start_row, start_col, start_offset, old_end_row, old_end_col, new_end_row, new_end_col, old_end_byte_len, new_byte_len - )) + )) end local change_content if new_byte_len == 0 then change_content = "" @@ -55,7 +55,9 @@ local function attach(name, buffer, content) '\n' ) end - print(string.format("sending: %s %s %s %s -- '%s'", start_row, start_col, start_row + new_end_row, start_col + new_end_col, change_content)) + if CODEMP.config.debug then + print(string.format("sending: %s %s %s %s -- '%s'", start_row, start_col, start_row + new_end_row, start_col + new_end_col, change_content)) + end controller:send(start_offset, start_offset + old_end_byte_len, change_content):await() end, }) @@ -65,7 +67,9 @@ local function attach(name, buffer, content) local event = controller:try_recv():await() if event == nil then break end ticks[buffer] = vim.api.nvim_buf_get_changedtick(buffer) - print(" ~~ applying change ~~ " .. event.first .. ".." .. event.last .. "::[" .. event.content .. "]") + if CODEMP.config.debug then + print(" ~~ applying change ~~ " .. event.first .. ".." .. event.last .. "::[" .. event.content .. "]") + end utils.buffer.set_content(buffer, event.content, event.first, event.last) if event.hash ~= nil then if utils.hash(utils.buffer.get_content(buffer)) ~= event.hash then diff --git a/lua/codemp/init.lua b/lua/codemp/init.lua index e401a60..ad201d0 100644 --- a/lua/codemp/init.lua +++ b/lua/codemp/init.lua @@ -8,6 +8,7 @@ if CODEMP == nil then config = { neo_tree = false, timer_interval = 100, + debug = false, }, setup = function (opts) CODEMP.config = vim.tbl_extend('force', CODEMP.config, opts) diff --git a/lua/codemp/utils.lua b/lua/codemp/utils.lua index 9ac8059..ef40701 100644 --- a/lua/codemp/utils.lua +++ b/lua/codemp/utils.lua @@ -158,7 +158,9 @@ local function buffer_set_content(buf, content, first, last) else content_array = vim.split(content, "\n", {trimempty=false}) end - -- print(string.format("set [%s..%s::'%s'] -> start(row:%s col:%s) end(row:%s, col:%s)", first, last, content, first_row, first_col, last_row, last_col)) + if CODEMP.config.debug then + print(string.format("nvim_buf_set_text [%s..%s::'%s'] -> start(row:%s col:%s) end(row:%s, col:%s)", first, last, content, first_row, first_col, last_row, last_col)) + end vim.api.nvim_buf_set_text(buf, first_row, first_col, last_row, last_col, content_array) end end