Compare commits

..

No commits in common. "bb3861f436475f084e4fe117faed05777636de79" and "2290929381fad187a2d8b62f952f8ab06995d4cd" have entirely different histories.

4 changed files with 65 additions and 63 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.luarc.json

View File

@ -1,16 +1,55 @@
# guid.nvim # guid.nvim
This Neovim plugin simplifies common operations when working with [Globally Unique This Neovim-exclusive plugin simplifies common operations when working with [Globally Unique
Identifiers](https://en.wikipedia.org/wiki/Universally_unique_identifier) (GUIDs). The produced values fully conform to Identifiers](https://en.wikipedia.org/wiki/Universally_unique_identifier) (GUIDs). The produced values fully conform to
the [RFC 4122](https://www.rfc-editor.org/rfc/rfc4122) spec for pseudo-random GUIDs. Classic Vim is not supported. the [RFC 4122](https://www.rfc-editor.org/rfc/rfc4122) spec for for pseudo-random GUIDs.
![](img/guid.nvim.gif) ![](img/guid.nvim.gif)
## GUID Styles
There are several ways to represent GUIDs as text. The format specifier syntax outlined by
[Guid.ToString](https://learn.microsoft.com/en-us/dotnet/api/system.guid.tostring?view=net-7.0) is borrowed for styling
text output. This convention is expanded to allow the casing of hexadecimal characters to be specified. The casing of
the format specifier determines whether lowercase or uppercase will be used.
* `n` `00000000000000000000000000000000` \
32 digits.
* `d` `00000000-0000-0000-0000-000000000000` \
32 digits separated by hyphens.
* `b` `{00000000-0000-0000-0000-000000000000}` \
32 digits separated by hyphens, enclosed in braces.
* `p` `(00000000-0000-0000-0000-000000000000)` \
32 digits separated by hyphens, enclosed in parenthesis.
* `x` `{0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}` \
Four hexadecimal values enclosed in braces, where the fourth value is a subset of eight hexadecimal values that is also enclosed in braces.
## GUID Commands
* `GuidInsert [style]` \
Inserts a GUID at the cursor position using the default or provided style.
* `GuidFormat [style]` \
Formats the GUID at the cursor position using the default or provided style.
* `GuidObject` \
Selects the GUID at the cursor position (useful for text objects).
## GUID Text Object
A custom text object for GUIDs is provided. By default it is bound to the `g` key. For example to yank a GUID you would
input `yig` in normal mode (the functionally identical `yag` can also be used). The GUID text object supports all of the
GUID formatting styles described above. Make sure to call `setup` if you wish to use GUID text objects (details in the
next section).
## Configuration ## Configuration
The plugin should be configured by calling the `setup` function. Options may be provided to override the default The plugin should be configured by calling the `setup` function. Options can be optionally provided if you wish to
settings which are shown below: override the default settings, which are shown below:
```lua ```lua
require('guid').setup({ require('guid').setup({
@ -20,49 +59,13 @@ require('guid').setup({
}) })
``` ```
These options are: More specifically, these options are:
#### `comma_space` * `comma_space` \
Specifies if commas should be followed by spaces in GUIDs formatted with the `x` and `X` specifiers. Determines if commas should be followed by spaces in GUIDs formatted with the `x` specifier.
#### `default_style` * `default_style` \
Specifies which format to use if one is not provided for `GuidInsert` and `GuidFormat` commands. Determines what style to use if one is not provided for `GuidInsert` and `GuidFormat` commands.
#### `object_char` * `object-char` \
Specifies which character should be used for the GUID text object. Determines which character should be used for the GUID text object.
## GUID Formatting
The format specifier syntax outlined by
[Guid.ToString](https://learn.microsoft.com/en-us/dotnet/api/system.guid.tostring?view=net-7.0) is borrowed for styling
GUID text representation. The case of the format specifier determines whether lowercase or uppercase will be used.
| Format | Example |
| - | - |
| <kbd>n</kbd> | `cbb297c014a940bc8d911d0ef9b42df9` |
| <kbd>N</kbd> | `CBB297C014A940BC8D911D0EF9B42DF9` |
| <kbd>d</kbd> | `cbb297c0-14a9-40bc-8d91-1d0ef9b42df9` |
| <kbd>D</kbd> | `CBB297C0-14A9-40BC-8D91-1D0EF9B42DF9` |
| <kbd>b</kbd> | `{cbb297c0-14a9-40bc-8d91-1d0ef9b42df9}` |
| <kbd>B</kbd> | `{CBB297C0-14A9-40BC-8D91-1D0EF9B42DF9}` |
| <kbd>p</kbd> | `(cbb297c0-14a9-40bc-8d91-1d0ef9b42df9)` |
| <kbd>P</kbd> | `(CBB297C0-14A9-40BC-8D91-1D0EF9B42DF9)` |
| <kbd>x</kbd> | `{0xcbb297c0,0x14a9,0x40bc,{0x8d,0x91,0x1d,0x0e,0xf9,0xb4,0x2d,0xf9}}` |
| <kbd>X</kbd> | `{0xCBB297C0,0x14A9,0x40BC,{0x8D,0x91,0x1D,0x0E,0xF9,0xB4,0x2D,0xF9}}` |
## GUID Commands
#### `GuidInsert [format]`
Inserts a GUID at the cursor position using the default or provided format.
#### `GuidFormat [format]`
Formats the GUID at the cursor position using the default or provided format.
#### `GuidObject`
Selects the GUID at the cursor position (useful for text objects).
## GUID Text Object
A custom text object for GUIDs is provided. By default it is bound to the `g` key. For example to yank a GUID you would
input `yig` in normal mode. The functionally identical `yag` can also be used. The GUID text object supports all of the
GUID text representations described above. Be sure to call `setup` if you wish to use GUID text objects.

View File

@ -1,10 +1,10 @@
local GuidConfig = { Config = {
comma_space = false, comma_space = false,
default_style = 'd', default_style = 'd',
object_char = 'g', object_char = 'g',
} }
local GuidPatterns = { Guid_Patterns = {
'{\\s*0x[0-9a-fA-F]\\{8\\},\\s*0x[0-9a-fA-F]\\{4\\},\\s*0x[0-9a-fA-F]\\{4\\},\\s*{\\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\\}\\s*}\\s*}', -- x '{\\s*0x[0-9a-fA-F]\\{8\\},\\s*0x[0-9a-fA-F]\\{4\\},\\s*0x[0-9a-fA-F]\\{4\\},\\s*{\\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\\}\\s*}\\s*}', -- x
'(\\s*[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\\}\\s*)', -- p '(\\s*[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\\}\\s*)', -- p
'{\\s*[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\\}\\s*}', -- b '{\\s*[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\\}\\s*}', -- b
@ -15,14 +15,14 @@ local GuidPatterns = {
local function setup(config) local function setup(config)
if config then if config then
for key, value in pairs(config) do for key, value in pairs(config) do
GuidConfig[key] = config[key] or value Config[key] = config[key] or value
end end
end end
if GuidConfig.object_char then if Config.object_char then
for _, mode in ipairs({'x', 'o'}) do for _, mode in ipairs({'x', 'o'}) do
for _, prefix in ipairs({'i', 'a'}) do for _, prefix in ipairs({'i', 'a'}) do
vim.api.nvim_set_keymap(mode, prefix .. GuidConfig.object_char, ':<C-u>GuidObject<cr>', {noremap = true, silent = true}) vim.api.nvim_set_keymap(mode, prefix .. Config.object_char, ':<C-u>GuidObject<cr>', {noremap = true, silent = true})
end end
end end
end end
@ -35,8 +35,8 @@ end
local function insert_text_at_pos(text, pos) local function insert_text_at_pos(text, pos)
local line = vim.fn.getline(pos.row) local line = vim.fn.getline(pos.row)
local prefix = string.sub(line, 0, pos.col - 1) local prefix = string.sub(line, 0, pos.col - 1) ---@diagnostic disable-line: param-type-mismatch
local suffix = string.sub(line, pos.col) local suffix = string.sub(line, pos.col) ---@diagnostic disable-line: param-type-mismatch
vim.fn.setline(pos.row, prefix .. text .. suffix) vim.fn.setline(pos.row, prefix .. text .. suffix)
end end
@ -69,7 +69,7 @@ local function guid_generate()
-- Set the two most significant bits (bits 6 and 7) of the -- Set the two most significant bits (bits 6 and 7) of the
-- clock_seq_hi_and_reserved to zero and one, respectively. -- clock_seq_hi_and_reserved to zero and one, respectively.
bytes[9] = bit.band(bit.bor(bytes[9], 0x80), 0xbf) bytes[9] = bit.band(bit.bor(bytes[9], 0x80), 0x8f)
-- Set the four most significant bits (bits 12 through 15) of the -- Set the four most significant bits (bits 12 through 15) of the
-- time_hi_and_version field to the 4-bit version number. -- time_hi_and_version field to the 4-bit version number.
@ -93,7 +93,7 @@ end
local function guid_print(guid, style) local function guid_print(guid, style)
if style == '' then if style == '' then
style = GuidConfig.default_style style = Config.default_style
end end
-- Format specifier definition: -- Format specifier definition:
@ -126,7 +126,7 @@ local function guid_print(guid, style)
guid_printed = guid_printed:upper():gsub('X', 'x') guid_printed = guid_printed:upper():gsub('X', 'x')
end end
if GuidConfig.comma_space then if Config.comma_space then
guid_printed = guid_printed:gsub(',', ', ') guid_printed = guid_printed:gsub(',', ', ')
end end
@ -141,12 +141,12 @@ local function guid_insert(style)
end end
local function guid_format(style) local function guid_format(style)
for _, guid_pattern in ipairs(GuidPatterns) do for _, guid_pattern in ipairs(Guid_Patterns) do
local match_pos = find_pattern_at_pos(guid_pattern, get_cursor_pos(), true) local match_pos = find_pattern_at_pos(guid_pattern, get_cursor_pos(), true)
if match_pos then if match_pos then
local line = vim.fn.getline(match_pos.row) local line = vim.fn.getline(match_pos.row)
local line_prefix = line:sub(1, match_pos.col - 1) 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) local line_suffix = line:sub(match_pos.col + #match_pos.text) ---@diagnostic disable-line: undefined-field
local guid_printed = guid_print(guid_parse(match_pos.text), style) local guid_printed = guid_print(guid_parse(match_pos.text), style)
if guid_printed then if guid_printed then
vim.fn.setline(match_pos.row, line_prefix .. guid_printed .. line_suffix) vim.fn.setline(match_pos.row, line_prefix .. guid_printed .. line_suffix)
@ -159,7 +159,7 @@ local function guid_format(style)
end end
local function guid_object() local function guid_object()
for _, guid_pattern in ipairs(GuidPatterns) do for _, guid_pattern in ipairs(Guid_Patterns) do
local match_pos = find_pattern_at_pos(guid_pattern, get_cursor_pos(), false) local match_pos = find_pattern_at_pos(guid_pattern, get_cursor_pos(), false)
if match_pos then if match_pos then
vim.cmd(string.format(':normal! 0%dlv%dl', match_pos.col - 1, #match_pos.text - 1)) vim.cmd(string.format(':normal! 0%dlv%dl', match_pos.col - 1, #match_pos.text - 1))

View File

@ -18,8 +18,6 @@ if not vim.g.guid then
guid.guid_object() guid.guid_object()
end end
math.randomseed(os.time())
vim.api.nvim_create_user_command('GuidFormat', format, {nargs = '?'}) vim.api.nvim_create_user_command('GuidFormat', format, {nargs = '?'})
vim.api.nvim_create_user_command('GuidInsert', insert, {nargs = '?'}) vim.api.nvim_create_user_command('GuidInsert', insert, {nargs = '?'})
vim.api.nvim_create_user_command('GuidObject', object, {}) vim.api.nvim_create_user_command('GuidObject', object, {})