From 84169ee52d56e3679e2176bc04eb198b4c5b4231 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 1 Jan 2023 20:36:21 -0800 Subject: [PATCH] Setup hookup --- lua/guid.lua | 49 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/lua/guid.lua b/lua/guid.lua index b905a8f..9095a25 100644 --- a/lua/guid.lua +++ b/lua/guid.lua @@ -1,3 +1,14 @@ +Config = { + default_style = 'd', + space_after_comma = true, +} + +local function setup(config) + for key, value in pairs(config) do + Config[key] = config[key] or value + end +end + local function get_cursor_pos() local _, row, col, _ = unpack(vim.fn.getpos('.')) return {row = row, col = col} @@ -50,7 +61,7 @@ end local function guid_print(guid, style) if style == '' then - style = 'd' + style = Config.default_style end -- Format specifier definition: @@ -73,6 +84,9 @@ local function guid_print(guid, style) elseif style_lower == 'x' then -- {0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}} format = '{0x%.2x%.2x%.2x%.2x,0x%.2x%.2x,0x%.2x%.2x,{0x%.2x,0x%.2x,0x%.2x,0x%.2x,0x%.2x,0x%.2x,0x%.2x,0x%.2x}}' + else + vim.api.nvim_notify('Unrecognized GUID format!', vim.log.levels.ERROR, {}) + return end local guid_printed = string.format(format, unpack(guid)) @@ -80,43 +94,48 @@ local function guid_print(guid, style) guid_printed = guid_printed:upper():gsub('X', 'x') end + if Config.space_after_comma then + guid_printed = guid_printed:gsub(',', ', ') + end + return guid_printed end local function guid_insert(style) - local pos = get_cursor_pos() - local guid = guid_generate() - local guid_printed = guid_print(guid, style) - insert_text_at_pos(guid_printed, pos) + local guid_printed = guid_print(guid_generate(), style) + if guid_printed then + insert_text_at_pos(guid_printed, get_cursor_pos()) + end end local function guid_format(style) - local pos = get_cursor_pos() local guid_patterns = { - '{0x[0-9a-fA-F]\\{8\\},\\s*0x[0-9a-fA-F]\\{4\\},\\s*0x[0-9a-fA-F]\\{4\\},\\s*{0x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_, guid_pattern in ipairs(guid_patterns) do - local match_pos = find_pattern_at_pos(guid_pattern, pos) + local match_pos = find_pattern_at_pos(guid_pattern, get_cursor_pos()) if match_pos then - local guid = guid_parse(match_pos.text) - local guid_printed = guid_print(guid, style) - local line = vim.fn.getline(pos.row) + local line = vim.fn.getline(match_pos.row) local line_prefix = line:sub(1, match_pos.col - 1) ---@diagnostic disable-line: undefined-field local line_suffix = line:sub(match_pos.col + #match_pos.text) ---@diagnostic disable-line: undefined-field - vim.fn.setline(match_pos.row, line_prefix .. guid_printed .. line_suffix) + local guid_printed = guid_print(guid_parse(match_pos.text), style) + if guid_printed then + vim.fn.setline(match_pos.row, line_prefix .. guid_printed .. line_suffix) + end return end end - vim.api.nvim_notify('GUID not found at cursor!', vim.log.levels.ERROR, {}) + vim.api.nvim_notify('No GUID found at cursor!', vim.log.levels.ERROR, {}) end return { + setup = setup, guid_format = guid_format, guid_insert = guid_insert, }