From 7605b6cf7e97580a443b599ce3025256ab863203 Mon Sep 17 00:00:00 2001 From: Mist Date: Sat, 4 May 2024 21:03:10 +0800 Subject: [PATCH] [Feat] use code lines instead of raw string --- lua/codesnap/highlight.lua | 3 ++- lua/codesnap/init.lua | 8 ++++++-- lua/codesnap/utils/visual.lua | 8 +++++--- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/lua/codesnap/highlight.lua b/lua/codesnap/highlight.lua index 6feac19..a5af06e 100644 --- a/lua/codesnap/highlight.lua +++ b/lua/codesnap/highlight.lua @@ -22,7 +22,8 @@ function highlight_module.create_highlight_selector_window(cb_name, code) local col = vim.fn.winwidth(0) / 2 - width / 2 local bufnr = vim.api.nvim_create_buf(false, true) - vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, string_utils.split(code, "\n")) + vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, code) + local window_id = vim.api.nvim_open_win(bufnr, false, { relative = "editor", width = width, diff --git a/lua/codesnap/init.lua b/lua/codesnap/init.lua index 559b129..2b3f2ea 100644 --- a/lua/codesnap/init.lua +++ b/lua/codesnap/init.lua @@ -1,4 +1,5 @@ local static = require("codesnap.static") +local visual_utils = require("codesnap.utils.visual") local table_utils = require("codesnap.utils.table") local string_utils = require("codesnap.utils.string") local config_module = require("codesnap.config") @@ -53,13 +54,16 @@ end function main.highlight_mode_copy_into_clipboard(extension) main.highlight_mode_config = config_module.get_config(extension) - highlight_module.create_highlight_selector_window("copy_into_clipboard_with_config", main.highlight_mode_config.code) + highlight_module.create_highlight_selector_window( + "copy_into_clipboard_with_config", + visual_utils.get_selected_lines() + ) end function main.highlight_mode_save_snapshot(extension) main.highlight_mode_config = config_module.get_config(extension) - highlight_module.create_highlight_selector_window("save_snapshot_with_config", main.highlight_mode_config.code) + highlight_module.create_highlight_selector_window("save_snapshot_with_config", visual_utils.get_selected_lines()) end return main diff --git a/lua/codesnap/utils/visual.lua b/lua/codesnap/utils/visual.lua index db7250e..49fff13 100644 --- a/lua/codesnap/utils/visual.lua +++ b/lua/codesnap/utils/visual.lua @@ -22,10 +22,12 @@ function visual_utils.get_end_line_number() return vim.fn.line("'>") end -function visual_utils.get_selected_text() - local selected_text = vim.fn.getline("'<", "'>") +function visual_utils.get_selected_lines() + return vim.fn.getline("'<", "'>") +end - return table.concat(selected_text, "\n") +function visual_utils.get_selected_text() + return table.concat(visual_utils.get_selected_lines(), "\n") end function visual_utils.get_selected_text_realtime()