mirror of
https://github.com/nix-community/nixvim.git
synced 2025-06-21 08:35:43 +02:00
lib/lua: add isKeyword
and isIdentifier
Allow testing whether a string is a valid lua identifier or a reserved keyword.
This commit is contained in:
parent
b64ee08d6b
commit
01cf43dbaa
3 changed files with 118 additions and 3 deletions
|
@ -10,10 +10,10 @@ let
|
||||||
nixvimUtils = import ./utils.nix { inherit lib nixvimTypes _nixvimTests; };
|
nixvimUtils = import ./utils.nix { inherit lib nixvimTypes _nixvimTests; };
|
||||||
nixvimOptions = import ./options.nix { inherit lib nixvimTypes nixvimUtils; };
|
nixvimOptions = import ./options.nix { inherit lib nixvimTypes nixvimUtils; };
|
||||||
nixvimDeprecation = import ./deprecation.nix { inherit lib; };
|
nixvimDeprecation = import ./deprecation.nix { inherit lib; };
|
||||||
inherit (import ./to-lua.nix { inherit lib; }) toLuaObject;
|
|
||||||
in
|
in
|
||||||
{
|
rec {
|
||||||
maintainers = import ./maintainers.nix;
|
maintainers = import ./maintainers.nix;
|
||||||
|
lua = import ./to-lua.nix { inherit lib; };
|
||||||
keymaps = import ./keymap-helpers.nix { inherit lib nixvimOptions nixvimTypes; };
|
keymaps = import ./keymap-helpers.nix { inherit lib nixvimOptions nixvimTypes; };
|
||||||
autocmd = import ./autocmd-helpers.nix { inherit lib nixvimOptions nixvimTypes; };
|
autocmd = import ./autocmd-helpers.nix { inherit lib nixvimOptions nixvimTypes; };
|
||||||
neovim-plugin = import ./neovim-plugin.nix {
|
neovim-plugin = import ./neovim-plugin.nix {
|
||||||
|
@ -26,7 +26,7 @@ in
|
||||||
};
|
};
|
||||||
vim-plugin = import ./vim-plugin.nix { inherit lib nixvimOptions nixvimUtils; };
|
vim-plugin = import ./vim-plugin.nix { inherit lib nixvimOptions nixvimUtils; };
|
||||||
inherit nixvimTypes;
|
inherit nixvimTypes;
|
||||||
inherit toLuaObject;
|
inherit (lua) toLuaObject;
|
||||||
}
|
}
|
||||||
// nixvimUtils
|
// nixvimUtils
|
||||||
// nixvimOptions
|
// nixvimOptions
|
||||||
|
|
|
@ -1,6 +1,37 @@
|
||||||
{ lib }:
|
{ lib }:
|
||||||
with lib;
|
with lib;
|
||||||
rec {
|
rec {
|
||||||
|
# Whether the string is a reserved keyword in lua
|
||||||
|
isKeyword =
|
||||||
|
s:
|
||||||
|
elem s [
|
||||||
|
"and"
|
||||||
|
"break"
|
||||||
|
"do"
|
||||||
|
"else"
|
||||||
|
"elseif"
|
||||||
|
"end"
|
||||||
|
"false"
|
||||||
|
"for"
|
||||||
|
"function"
|
||||||
|
"if"
|
||||||
|
"in"
|
||||||
|
"local"
|
||||||
|
"nil"
|
||||||
|
"not"
|
||||||
|
"or"
|
||||||
|
"repeat"
|
||||||
|
"return"
|
||||||
|
"then"
|
||||||
|
"true"
|
||||||
|
"until"
|
||||||
|
"while"
|
||||||
|
];
|
||||||
|
|
||||||
|
# Valid lua identifiers are not reserved keywords, do not start with a digit,
|
||||||
|
# and contain only letters, digits, and underscores.
|
||||||
|
isIdentifier = s: !(isKeyword s) && (match "[A-Za-z_][0-9A-Za-z_]*" s) == [ ];
|
||||||
|
|
||||||
# Black functional magic that converts a bunch of different Nix types to their
|
# Black functional magic that converts a bunch of different Nix types to their
|
||||||
# lua equivalents!
|
# lua equivalents!
|
||||||
toLuaObject =
|
toLuaObject =
|
||||||
|
|
|
@ -6,6 +6,45 @@
|
||||||
helpers,
|
helpers,
|
||||||
}:
|
}:
|
||||||
let
|
let
|
||||||
|
luaNames = {
|
||||||
|
# Keywords in lua 5.1
|
||||||
|
keywords = [
|
||||||
|
"and"
|
||||||
|
"break"
|
||||||
|
"do"
|
||||||
|
"else"
|
||||||
|
"elseif"
|
||||||
|
"end"
|
||||||
|
"false"
|
||||||
|
"for"
|
||||||
|
"function"
|
||||||
|
"if"
|
||||||
|
"in"
|
||||||
|
"local"
|
||||||
|
"nil"
|
||||||
|
"not"
|
||||||
|
"or"
|
||||||
|
"repeat"
|
||||||
|
"return"
|
||||||
|
"then"
|
||||||
|
"true"
|
||||||
|
"until"
|
||||||
|
"while"
|
||||||
|
];
|
||||||
|
identifiers = [
|
||||||
|
"validIdentifier"
|
||||||
|
"valid_identifier"
|
||||||
|
"_also_valid_"
|
||||||
|
"_weirdNameFMT"
|
||||||
|
];
|
||||||
|
other = [
|
||||||
|
"1_starts_with_digit"
|
||||||
|
"01234"
|
||||||
|
"12340"
|
||||||
|
"kebab-case"
|
||||||
|
];
|
||||||
|
};
|
||||||
|
|
||||||
results = pkgs.lib.runTests {
|
results = pkgs.lib.runTests {
|
||||||
testToLuaObject = {
|
testToLuaObject = {
|
||||||
expr = helpers.toLuaObject {
|
expr = helpers.toLuaObject {
|
||||||
|
@ -114,6 +153,51 @@ let
|
||||||
expected = ''{["c"] = { },["d"] = {["g"] = { }}}'';
|
expected = ''{["c"] = { },["d"] = {["g"] = { }}}'';
|
||||||
};
|
};
|
||||||
|
|
||||||
|
testIsLuaKeyword = {
|
||||||
|
expr = builtins.mapAttrs (_: builtins.filter helpers.lua.isKeyword) luaNames;
|
||||||
|
expected = {
|
||||||
|
keywords = [
|
||||||
|
"and"
|
||||||
|
"break"
|
||||||
|
"do"
|
||||||
|
"else"
|
||||||
|
"elseif"
|
||||||
|
"end"
|
||||||
|
"false"
|
||||||
|
"for"
|
||||||
|
"function"
|
||||||
|
"if"
|
||||||
|
"in"
|
||||||
|
"local"
|
||||||
|
"nil"
|
||||||
|
"not"
|
||||||
|
"or"
|
||||||
|
"repeat"
|
||||||
|
"return"
|
||||||
|
"then"
|
||||||
|
"true"
|
||||||
|
"until"
|
||||||
|
"while"
|
||||||
|
];
|
||||||
|
identifiers = [ ];
|
||||||
|
other = [ ];
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
testIsLuaIdentifier = {
|
||||||
|
expr = builtins.mapAttrs (_: builtins.filter helpers.lua.isIdentifier) luaNames;
|
||||||
|
expected = {
|
||||||
|
keywords = [ ];
|
||||||
|
identifiers = [
|
||||||
|
"validIdentifier"
|
||||||
|
"valid_identifier"
|
||||||
|
"_also_valid_"
|
||||||
|
"_weirdNameFMT"
|
||||||
|
];
|
||||||
|
other = [ ];
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
testUpperFirstChar = {
|
testUpperFirstChar = {
|
||||||
expr = map helpers.upperFirstChar [
|
expr = map helpers.upperFirstChar [
|
||||||
"foo"
|
"foo"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue