This commit is contained in:
Alex Yatskov 2023-01-01 19:50:01 -08:00
parent 5ef3bbe4b7
commit 27cca89e7a
2 changed files with 14 additions and 15 deletions

View File

@ -92,22 +92,23 @@ end
local function guid_format(style)
local pos = get_cursor_pos()
local patterns = {
'{0x[0-9a-fA-F]\\{8\\},\\s*0x[0-9a-fA-F]\\{4\\},\\s*0x[0-9a-fA-F]\\{4\\},\\s*{0x[0-9a-fA-F]\\{2\\},\\s*0x[0-9a-fA-F]\\{2\\},\\s*0x[0-9a-fA-F]\\{2\\},\\s*0x[0-9a-fA-F]\\{2\\},\\s*0x[0-9a-fA-F]\\{2\\},\\s*0x[0-9a-fA-F]\\{2\\},\\s*0x[0-9a-fA-F]\\{2\\},\\s*0x[0-9a-fA-F]\\{2\\}}}',
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[0-9a-fA-F]\\{2\\},\\s*0x[0-9a-fA-F]\\{2\\},\\s*0x[0-9a-fA-F]\\{2\\},\\s*0x[0-9a-fA-F]\\{2\\},\\s*0x[0-9a-fA-F]\\{2\\},\\s*0x[0-9a-fA-F]\\{2\\},\\s*0x[0-9a-fA-F]\\{2\\},\\s*0x[0-9a-fA-F]\\{2\\}}}', -- x
'([0-9a-fA-F]\\{8\\}-[0-9a-fA-F]\\{4\\}-[0-9a-fA-F]\\{4\\}-[0-9a-fA-F]\\{4\\}-[0-9a-fA-F]\\{12\\})', -- p
'{[0-9a-fA-F]\\{8\\}-[0-9a-fA-F]\\{4\\}-[0-9a-fA-F]\\{4\\}-[0-9a-fA-F]\\{4\\}-[0-9a-fA-F]\\{12\\}}', -- b
'[0-9a-fA-F]\\{8\\}-[0-9a-fA-F]\\{4\\}-[0-9a-fA-F]\\{4\\}-[0-9a-fA-F]\\{4\\}-[0-9a-fA-F]\\{12\\}', -- d
'[0-9a-fA-F]\\{32\\}', -- n
'[0-9a-fA-F]\\{8\\}-[0-9a-fA-F]\\{4\\}-[0-9a-fA-F]\\{4\\}-[0-9a-fA-F]\\{4\\}-[0-9a-fA-F]\\{12\\}', -- d
'[0-9a-fA-F]\\{32\\}', -- n
}
for _, pattern in ipairs(patterns) do
local match_pos = find_pattern_at_pos(pattern, pos)
for _, guid_pattern in ipairs(guid_patterns) do
local match_pos = find_pattern_at_pos(guid_pattern, 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_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_print(guid, style) .. line_suffix)
vim.fn.setline(match_pos.row, line_prefix .. guid_printed .. line_suffix)
return
end
end

View File

@ -1,23 +1,21 @@
local guid = require('guid')
local function guid_reload()
local function reload()
package.loaded.guid = nil
guid = require('guid')
end
local function guid_insert(ctx)
guid_reload()
local function insert(ctx)
guid.guid_insert(ctx.args)
end
local function guid_format(ctx)
guid_reload()
local function format(ctx)
guid.guid_format(ctx.args)
end
if not vim.g.guid then
vim.api.nvim_create_user_command('GuidFormat', guid_format, {nargs = '?'})
vim.api.nvim_create_user_command('GuidInsert', guid_insert, {nargs = '?'})
vim.api.nvim_create_user_command('GuidReload', guid_reload, {})
vim.api.nvim_create_user_command('GuidFormat', format, {nargs = '?'})
vim.api.nvim_create_user_command('GuidInsert', insert, {nargs = '?'})
vim.api.nvim_create_user_command('GuidReload', reload, {})
vim.g.guid = true
end