Skip to content

Commit 3e48f13

Browse files
Gerg-Lhorriblename
andauthored
lib: rewrite toLuaObject, deprecate everything else (#1178)
* lint: typos * wrapper/build: passthru mnw stuff * lib: rewrite toLuaObject, deprecate everything else * docs: deno fmt * Emac --------- Co-authored-by: Ching Pei Yang <[email protected]>
1 parent 3ace078 commit 3e48f13

File tree

8 files changed

+57
-95
lines changed

8 files changed

+57
-95
lines changed

.github/typos.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ default.extend-ignore-words-re = [
99
"edn",
1010
"esy",
1111
"BA", # somehow "BANanaD3V" is valid, but BA is not...
12+
"Emac"
1213
]
1314

docs/release-notes/rl-0.8.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -569,8 +569,7 @@
569569
[typst-concealer]: https://github.com/PartyWumpus/typst-concealer
570570

571571
- Add inline typst concealing support under `vim.languages.typst` using
572-
[typst-concealer].
573-
[simon-wg](https://github.com/simon-wg):
572+
[typst-concealer]. [simon-wg](https://github.com/simon-wg):
574573

575574
- Update `python` language module to use correct lsp binary.
576575
- Fix `python` pyright and basedpyright language servers not using default on

lib/lua.nix

Lines changed: 45 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,52 @@
11
# Helpers for converting values to lua
22
{lib}: let
3-
inherit (builtins) hasAttr head throw typeOf isList isAttrs isBool isInt isString isPath isFloat toJSON;
4-
inherit (lib.attrsets) mapAttrsToList filterAttrs;
5-
inherit (lib.strings) concatStringsSep concatMapStringsSep stringToCharacters;
6-
inherit (lib.trivial) boolToString warn;
7-
in rec {
8-
# Convert a null value to lua's nil
9-
nullString = value:
10-
if value == null
11-
then "nil"
12-
else "'${value}'";
3+
isLuaInline = object: (object._type or null) == "lua-inline";
134

14-
# convert an expression to lua
15-
expToLua = exp:
16-
if isList exp
17-
then listToLuaTable exp # if list, convert to lua table
18-
else if isAttrs exp
19-
then attrsetToLuaTable exp # if attrs, convert to table
20-
else if isBool exp
21-
then boolToString exp # if bool, convert to string
22-
else if isInt exp
23-
then toString exp # if int, convert to string
24-
else if exp == null
25-
then "nil"
26-
else (toJSON exp); # otherwise jsonify the value and print as is
5+
toLuaObject = args:
6+
{
7+
int = toString args;
8+
float = toString args;
279

28-
# convert list to a lua table
29-
listToLuaTable = list:
30-
"{ " + (concatStringsSep ", " (map expToLua list)) + " }";
10+
# escapes \ and quotes
11+
string = builtins.toJSON args;
12+
path = builtins.toJSON args;
3113

32-
# convert attrset to a lua table
33-
attrsetToLuaTable = attrset:
34-
"{ "
35-
+ (
36-
concatStringsSep ", "
37-
(
38-
mapAttrsToList (
39-
name: value:
40-
name
41-
+ " = "
42-
+ (expToLua value)
43-
)
44-
attrset
45-
)
46-
)
47-
+ " }";
48-
# Convert a list of lua expressions to a lua table. The difference to listToLuaTable is that the elements here are expected to be lua expressions already, whereas listToLuaTable converts from nix types to lua first
49-
luaTable = items: ''{${concatStringsSep "," items}}'';
14+
bool = lib.boolToString args;
15+
null = "nil";
5016

51-
isLuaInline = object: (object._type or null) == "lua-inline";
17+
list = "{${lib.concatMapStringsSep ",\n" toLuaObject args}}";
5218

53-
toLuaObject = args:
54-
if isAttrs args
55-
then
56-
if isLuaInline args
57-
then args.expr
58-
else if hasAttr "__empty" args
59-
then
60-
warn ''
61-
Using `__empty` to define an empty lua table is deprecated. Use an empty attrset instead.
62-
'' "{ }"
63-
else
64-
"{"
65-
+ (concatStringsSep ","
66-
(mapAttrsToList
67-
(n: v:
68-
if head (stringToCharacters n) == "@"
69-
then toLuaObject v
70-
else "[${toLuaObject n}] = " + (toLuaObject v))
71-
(filterAttrs
72-
(_: v: v != null)
73-
args)))
74-
+ "}"
75-
else if isList args
76-
then "{" + concatMapStringsSep "," toLuaObject args + "}"
77-
else if isString args
78-
then
79-
# This should be enough!
80-
toJSON args
81-
else if isPath args
82-
then toJSON (toString args)
83-
else if isBool args
84-
then "${boolToString args}"
85-
else if isFloat args
86-
then "${toString args}"
87-
else if isInt args
88-
then "${toString args}"
89-
else if (args == null)
90-
then "nil"
91-
else throw "could not convert object of type `${typeOf args}` to lua object";
92-
}
19+
set =
20+
if lib.isDerivation args
21+
then ''"${args}"''
22+
else if isLuaInline args
23+
then args.expr
24+
else "{${
25+
lib.pipe args [
26+
(lib.filterAttrs (_: v: v != null))
27+
(builtins.mapAttrs (
28+
n: v:
29+
if lib.hasPrefix "@" n
30+
then toLuaObject v
31+
else "[${toLuaObject n}] = ${toLuaObject v}"
32+
))
33+
builtins.attrValues
34+
(lib.concatStringsSep ",\n")
35+
]
36+
}}";
37+
}
38+
.${
39+
builtins.typeOf args
40+
}
41+
or (builtins.throw "Could not convert object of type `${builtins.typeOf args}` to lua object");
42+
in
43+
{
44+
inherit isLuaInline toLuaObject;
45+
luaTable = x: (toLuaObject (map lib.mkLuaInline x));
46+
}
47+
// lib.genAttrs [
48+
"nullString"
49+
"expToLua"
50+
"listToLuaTable"
51+
"attrsetToLuaTable"
52+
] (name: lib.warn "${name} is deprecated use toLuaObject instead" toLuaObject)

modules/neovim/init/spellcheck.nix

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
inherit (lib.strings) concatLines concatStringsSep optionalString;
1010
inherit (lib.attrsets) mapAttrsToList;
1111
inherit (lib.types) listOf str attrsOf;
12-
inherit (lib.nvim.lua) listToLuaTable;
12+
inherit (lib.nvim.lua) toLuaObject;
1313
inherit (lib.nvim.dag) entryAfter;
1414

1515
cfg = config.vim.spellcheck;
@@ -152,7 +152,7 @@ in {
152152
vim.api.nvim_create_augroup("nvf_autocmds", {clear = false})
153153
vim.api.nvim_create_autocmd({ "FileType" }, {
154154
group = "nvf_autocmds",
155-
pattern = ${listToLuaTable cfg.ignoredFiletypes},
155+
pattern = ${toLuaObject cfg.ignoredFiletypes},
156156
callback = function()
157157
vim.opt_local.spell = false
158158
end,

modules/plugins/assistant/copilot/copilot.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ in {
9393
accept = mkOption {
9494
type = nullOr str;
9595
default = "<M-l>";
96-
description = "Accept suggetion";
96+
description = "Accept suggestion";
9797
};
9898

9999
acceptWord = mkOption {

modules/plugins/languages/haskell.nix

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@
1111
inherit (lib.modules) mkIf mkMerge;
1212
inherit (lib.nvim.types) mkGrammarOption;
1313
inherit (lib.nvim.dag) entryAfter;
14-
inherit (lib.nvim.lua) expToLua;
14+
inherit (lib.nvim.lua) toLuaObject;
1515
inherit (lib.meta) getExe';
1616
inherit (lib.generators) mkLuaInline;
1717
inherit (pkgs) haskellPackages;
18-
inherit (lib.nvim.lua) toLuaObject;
1918

2019
cfg = config.vim.languages.haskell;
2120

@@ -120,7 +119,7 @@ in {
120119
dap = {
121120
cmd = ${
122121
if isList cfg.dap.package
123-
then expToLua cfg.dap.package
122+
then toLuaObject cfg.dap.package
124123
else ''{"${cfg.dap.package}/bin/haskell-debug-adapter"}''
125124
},
126125
},

modules/plugins/languages/rust.nix

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
inherit (lib.lists) isList;
1414
inherit (lib.types) bool package str listOf either enum;
1515
inherit (lib.nvim.types) mkGrammarOption;
16-
inherit (lib.nvim.lua) expToLua;
16+
inherit (lib.nvim.lua) toLuaObject;
1717
inherit (lib.nvim.dag) entryAfter entryAnywhere;
1818

1919
cfg = config.vim.languages.rust;
@@ -153,7 +153,7 @@ in {
153153
server = {
154154
cmd = ${
155155
if isList cfg.lsp.package
156-
then expToLua cfg.lsp.package
156+
then toLuaObject cfg.lsp.package
157157
else ''{"${cfg.lsp.package}/bin/rust-analyzer"}''
158158
},
159159
default_settings = {

modules/wrapper/build/config.nix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@
130130
# In systems where we only have a package and no module, this can be used
131131
# to access the built init.lua
132132
initLua = dummyInit;
133+
134+
mnwConfig = neovim-wrapped.passthru.config;
135+
mnwConfigDir = neovim-wrapped.passthru.configDir;
133136
};
134137

135138
meta =

0 commit comments

Comments
 (0)