Skip to content

Use completion.requireFunction for auto-require syntax and insert position - #3491

Open
svilenmarkov wants to merge 2 commits into
LuaLS:masterfrom
svilenmarkov:master
Open

svilenmarkov wants to merge 2 commits into
LuaLS:masterfrom
svilenmarkov:master

Conversation

@svilenmarkov

Copy link
Copy Markdown

The syntax and position for auto-require is currently hard-coded to look for require and doesn't respect the completion.requireFunction setting.

Example:

.luarc.json:

{
  "completion.requireFunction": "import"
}

main.lua:

local a = import("a")
local b = import("b")

When attempting to auto-require c, you get:

local c = import "c"
local a = import("a")
local b = import("b")

Instead of the expected:

local a = import("a")
local b = import("b")
local c = import("c")

Copilot AI balanced review requested due to automatic review settings November 8, 2026 18:55

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

中文:requireName 可能为 nil(当 Lua.completion.requireFunction 未配置时),此时 lnText:find(requireName, 1, true) 会抛出错误。请确认该配置项是否有默认值;如果没有,需要提供回退值(例如 requireName or 'require')。
English: requireName may be nil (when Lua.completion.requireFunction is not configured), in which case lnText:find(requireName, 1, true) will throw an error. Please confirm whether this config has a default value; if not, provide a fallback (e.g. requireName or 'require').

中文:测试中通过 config.set(nil, ...) 修改全局配置,但若测试断言失败(TEST 内部报错),恢复语句 config.set(nil, 'Lua.completion.requireFunction', originRequireFunction) 不会执行,会污染后续测试。建议使用 pcall/xpcall 或测试框架的清理机制确保恢复。
English: The test mutates global config via config.set(nil, ...), but if the assertion fails (error thrown inside TEST), the restore line config.set(nil, 'Lua.completion.requireFunction', originRequireFunction) won't run, polluting subsequent tests. Consider wrapping with pcall/xpcall or using the test framework's cleanup mechanism to guarantee restoration.

中文:findInsertRow 现在依赖配置的 requireFunction 名称来识别 require 行。若用户配置了自定义名称(如 import),但代码中同时存在 require 调用,插入位置判断可能不符合预期。建议确认该行为是否符合设计意图,并在文档/注释中说明。
English: findInsertRow now relies on the configured requireFunction name to detect require lines. If a user configures a custom name (e.g. import) while the code also contains require calls, the insertion position detection may not behave as expected. Please confirm this matches the intended design and document it in comments/docs.

中文:新增测试只覆盖了自定义 import 场景,建议补充一个 requireFunction 为默认值(或未设置)时的回归测试,以覆盖 nil 回退路径。
English: The new test only covers the custom import scenario. Consider adding a regression test for the default (or unset) requireFunction to cover the nil fallback path.

中文:find 使用纯文本匹配,requireName 若包含特殊字符无影响,但若配置为空字符串 '',find('', 1, true) 恒为真,会导致所有行都被视为 require 行,插入位置逻辑失效。建议对空字符串做校验。
English: find uses plain-text matching, so special characters in requireName are fine, but if the config is an empty string '', find('', 1, true) always returns true, causing every line to be treated as a require line and breaking insertion logic. Consider validating against empty strings.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the change to recognize imports using the configured function name.

No open issues 🎉

What changed in this PR

中文:此 PR 让自动导入根据 completion.requireFunction 识别已有导入语句,从而沿用其语法格式和插入位置。

English: This PR makes auto-require recognize existing imports using completion.requireFunction, preserving their syntax style and insertion position.

Changes:

  • 中文:使用配置的函数名替代硬编码的 require 检查。
    English: Replace the hard-coded require check with the configured function name.
  • 中文:新增 import(...) 回归测试,验证格式和插入位置,并在测试后恢复配置。
    English: Add a regression test for import(...) formatting and placement, restoring the configuration afterward.
File Description
test/​command/​auto-require.lua 中文:验证自定义导入函数的格式和插入位置。
English: Verify formatting and placement for a custom import function.
script/​core/​command/​autoRequire.lua 中文:根据配置的函数名识别已有导入语句。
English: Identify existing imports using the configured function name.

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 8, 2026 19:03

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the change to recognize configured import functions and preserve existing import style.

No open issues 🎉

Review effort: Balanced

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants