Compare commits

..

10 Commits

Author SHA1 Message Date
bb3861f436 Update README 2023-02-20 18:12:46 -08:00
a67adc94a3 Remove diagnostics 2023-02-20 15:39:06 -08:00
5a30bd1670 Remove gitignore 2023-01-28 11:25:08 -08:00
d99a8f3e48 Fix naming 2023-01-25 22:29:36 -08:00
99316f9967 Make config and constants not be global 2023-01-25 18:03:33 -08:00
47bd68a21f Revert "Cleanup"
This reverts commit aa8c5504cb.
2023-01-24 22:35:13 -08:00
aa8c5504cb Cleanup 2023-01-24 22:33:24 -08:00
5211e9ed04 Update README 2023-01-24 19:48:55 -08:00
d1736dcb06 Fix bit math error, explicitly seed random number generator 2023-01-24 18:12:31 -08:00
202095edad Update README 2023-01-22 22:21:45 -08:00
4 changed files with 63 additions and 65 deletions

1
.gitignore vendored
View File

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

View File

@ -1,55 +1,16 @@
# guid.nvim # guid.nvim
This Neovim-exclusive plugin simplifies common operations when working with [Globally Unique This Neovim 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 for pseudo-random GUIDs. the [RFC 4122](https://www.rfc-editor.org/rfc/rfc4122) spec for pseudo-random GUIDs. Classic Vim is not supported.
![](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 can be optionally provided if you wish to The plugin should be configured by calling the `setup` function. Options may be provided to override the default
override the default settings, which are shown below: settings which are shown below:
```lua ```lua
require('guid').setup({ require('guid').setup({
@ -59,13 +20,49 @@ require('guid').setup({
}) })
``` ```
More specifically, these options are: These options are:
* `comma_space` \ #### `comma_space`
Determines if commas should be followed by spaces in GUIDs formatted with the `x` specifier. Specifies if commas should be followed by spaces in GUIDs formatted with the `x` and `X` specifiers.
* `default_style` \ #### `default_style`
Determines what style to use if one is not provided for `GuidInsert` and `GuidFormat` commands. Specifies which format to use if one is not provided for `GuidInsert` and `GuidFormat` commands.
* `object-char` \ #### `object_char`
Determines which character should be used for the GUID text object. Specifies 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 @@
Config = { local GuidConfig = {
comma_space = false, comma_space = false,
default_style = 'd', default_style = 'd',
object_char = 'g', object_char = 'g',
} }
Guid_Patterns = { local GuidPatterns = {
'{\\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 @@ Guid_Patterns = {
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
Config[key] = config[key] or value GuidConfig[key] = config[key] or value
end end
end end
if Config.object_char then if GuidConfig.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 .. Config.object_char, ':<C-u>GuidObject<cr>', {noremap = true, silent = true}) vim.api.nvim_set_keymap(mode, prefix .. GuidConfig.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) ---@diagnostic disable-line: param-type-mismatch local prefix = string.sub(line, 0, pos.col - 1)
local suffix = string.sub(line, pos.col) ---@diagnostic disable-line: param-type-mismatch local suffix = string.sub(line, pos.col)
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), 0x8f) bytes[9] = bit.band(bit.bor(bytes[9], 0x80), 0xbf)
-- 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 = Config.default_style style = GuidConfig.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 Config.comma_space then if GuidConfig.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(Guid_Patterns) do for _, guid_pattern in ipairs(GuidPatterns) 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) ---@diagnostic disable-line: undefined-field local line_prefix = line:sub(1, match_pos.col - 1)
local line_suffix = line:sub(match_pos.col + #match_pos.text) ---@diagnostic disable-line: undefined-field local line_suffix = line:sub(match_pos.col + #match_pos.text)
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(Guid_Patterns) do for _, guid_pattern in ipairs(GuidPatterns) 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,6 +18,8 @@ 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, {})