Skip to content

Commit 6e9ed63

Browse files
committed
fix: nil couldn't delete the table. Use vim.NIL
1 parent 4f04c62 commit 6e9ed63

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ Most will use the current word to find the import statement, but the treesitter
112112
opts = {
113113
-- Example 1:
114114
-- Default behaviour for `tqdm` is `from tqdm.auto import tqdm`.
115-
-- If you want to change it to `import tqdm`, you can set `import = {"tqdm"}` and `import_from = {tqdm = nil}` here.
115+
-- If you want to change it to `import tqdm`, you can set `import = {"tqdm"}` and `import_from = {tqdm = vim.NIL}` here.
116116
-- If you want to change it to `from tqdm import tqdm`, you can set `import_from = {tqdm = "tqdm"}` here.
117117

118118
-- Example 2:
@@ -125,20 +125,20 @@ Most will use the current word to find the import statement, but the treesitter
125125
-- "tqdm",
126126
},
127127

128-
---@type table<string, string>
128+
---@type table<string, string|vim.NIL>
129129
import_as = {
130130
-- These are the default values. Here for demonstration.
131131
-- np = "numpy",
132132
-- pd = "pandas",
133133
},
134134

135-
---@type table<string, string>
135+
---@type table<string, string|vim.NIL>
136136
import_from = {
137-
-- tqdm = nil,
137+
-- tqdm = vim.NIL,
138138
-- tqdm = "tqdm",
139139
},
140140

141-
---@type table<string, string[]>
141+
---@type table<string, string[]|vim.NIL>
142142
statement_after_imports = {
143143
-- logger = { "import my_custom_logger", "", "logger = my_custom_logger.get_logger()" },
144144
},

lua/python_import.lua

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,33 @@
11
local config = require "python_import.config"
22
local lookup_table = require "python_import.lookup_table"
33

4+
---In Lua, if a value in a table is set to `nil`, it is treated as if the key does not exist.
5+
---Thus, we use `vim.NIL` in the PythonImport.UserConfig to indicate that the value should be removed from the table.
6+
---@param t table
7+
local function remove_vim_nil(t)
8+
for k, v in pairs(t) do
9+
if v == vim.NIL then
10+
t[k] = nil
11+
end
12+
end
13+
end
14+
415
local M = {}
516

17+
---@class PythonImport.UserExtendLookupTable
18+
---@field import string[]?
19+
---@field import_as table<string, string|vim.NIL>?
20+
---@field import_from table<string, string|vim.NIL>?
21+
---@field statement_after_imports table<string, string[]|vim.NIL>?
22+
23+
---@class PythonImport.UserConfig
24+
---@field extend_lookup_table PythonImport.UserExtendLookupTable?
25+
---
26+
---Return nil to indicate no match is found and continue with the default lookup
27+
---Return a table to stop the lookup and use the returned table as the result
28+
---Return an empty table to stop the lookup. This is useful when you want to add to wherever you need to.
29+
---@field custom_function fun(winnr: integer, word: string, ts_node: TSNode?): string[]? | nil
30+
631
---@param opts PythonImport.UserConfig
732
function M.setup(opts)
833
-- NOTE: This may be called twice if you lazy load the plugin
@@ -25,6 +50,10 @@ function M.setup(opts)
2550
config.opts.extend_lookup_table.statement_after_imports
2651
)
2752

53+
remove_vim_nil(lookup_table.import_as)
54+
remove_vim_nil(lookup_table.import_from)
55+
remove_vim_nil(lookup_table.statement_after_imports)
56+
2857
-- make lookup table for faster lookup
2958
for _, v in ipairs(lookup_table.import) do
3059
lookup_table.is_import[v] = true

lua/python_import/config.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ M.default_opts = {
88
-- "pickle",
99
},
1010

11-
---@type table<string, string>
11+
---@type table<string, string|vim.NIL>
1212
import_as = {
1313
-- np = "numpy",
1414
-- pd = "pandas",
1515
},
1616

17-
---@type table<string, string>
17+
---@type table<string, string|vim.NIL>
1818
import_from = {
1919
-- tqdm = "tqdm.auto",
2020
-- nn = "torch",
2121
},
2222

23-
---@type table<string, string[]>
23+
---@type table<string, string[]|vim.NIL>
2424
statement_after_imports = {
2525
-- logger = { "import my_custom_logger", "", "logger = my_custom_logger.get_logger()" },
2626
},

0 commit comments

Comments
 (0)