From ca31f4f10541d2d26ae75f126d1e87db6f384d8e Mon Sep 17 00:00:00 2001 From: roife Date: Tue, 9 Dec 2025 14:12:37 +0800 Subject: [PATCH] feat: Omit parameter hints when the argument name matches --- changelog.md | 2 +- script/core/hint.lua | 2 +- test.lua | 1 + test/inlay_hint/init.lua | 150 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 153 insertions(+), 2 deletions(-) create mode 100644 test/inlay_hint/init.lua diff --git a/changelog.md b/changelog.md index cdbe218dc..775b3c4e7 100644 --- a/changelog.md +++ b/changelog.md @@ -1,7 +1,7 @@ # changelog ## Unreleased - +* `New` Omit parameter hints when the argument name matches ## 3.16.1 `2025-12-8` diff --git a/script/core/hint.lua b/script/core/hint.lua index 8ce73e56d..2c7494df2 100644 --- a/script/core/hint.lua +++ b/script/core/hint.lua @@ -145,7 +145,7 @@ local function paramName(uri, results, start, finish) and (paramConfig ~= 'Literal' or guide.isLiteral(arg)) then mark[arg] = true local param = params[i] - if param and param[1] then + if param and param[1] and param[1] ~= arg[1] then results[#results+1] = { text = param[1] .. ':', offset = arg.start, diff --git a/test.lua b/test.lua index 5b45a5ba2..691882138 100644 --- a/test.lua +++ b/test.lua @@ -76,6 +76,7 @@ local function testAll() test 'diagnostics' test 'crossfile' test 'highlight' + test 'inlay_hint' test 'rename' test 'signature' test 'command' diff --git a/test/inlay_hint/init.lua b/test/inlay_hint/init.lua new file mode 100644 index 000000000..644fbff25 --- /dev/null +++ b/test/inlay_hint/init.lua @@ -0,0 +1,150 @@ +local core = require 'core.hint' +local files = require 'files' +local catch = require 'catch' +local config = require 'config' +local define = require 'proto.define' + +rawset(_G, 'TEST', true) + +---@diagnostic disable: await-in-sync +local function TEST(script, expect, opts) + opts = opts or {} + local newScript, catched = catch(script, '!') + + files.setText(TESTURI, newScript) + files.compileState(TESTURI) + + local results = core(TESTURI, 0, math.huge) + table.sort(results, function (a, b) + if a.offset ~= b.offset then + return a.offset < b.offset + end + return a.text < b.text + end) + + if #expect ~= #results then + print('Expect count:', #expect, 'Result count:', #results) + for i, res in ipairs(results) do + print((' %d: text=%s kind=%s where=%s offset=%s'):format( + i, + tostring(res.text), + tostring(res.kind), + tostring(res.where), + tostring(res.offset) + )) + end + end + + assert(#expect == #results) + for i, res in ipairs(results) do + local info = expect[i] + local pos = catched['!'][info.pos] + assert(pos) + local offset = info.useFinish and pos[2] or pos[1] + assert(res.text == info.text) + assert(res.kind == info.kind) + assert(res.where == info.where) + assert(res.offset == offset) + end + + files.remove(TESTURI) +end + +config.set(nil, 'Lua.hint.enable', true) + +config.set(nil, 'Lua.hint.setType', true) + +TEST([[ +---@return integer +local function returnsInt() + return 1 +end + +local = returnsInt() +]], { + { + pos = 1, + text = ': integer', + kind = define.InlayHintKind.Type, + where = 'right', + useFinish = true, + }, +}) + +config.set(nil, 'Lua.hint.paramName', 'Literal') + +TEST([[ +local function foo(first, second) +end + +foo(, x) +foo(, ) +]], { + { + pos = 1, + text = 'first:', + kind = define.InlayHintKind.Parameter, + where = 'left', + }, + { + pos = 2, + text = 'first:', + kind = define.InlayHintKind.Parameter, + where = 'left', + }, + { + pos = 3, + text = 'second:', + kind = define.InlayHintKind.Parameter, + where = 'left', + }, +}) + +config.set(nil, 'Lua.hint.paramName', 'All') + +TEST([[ +local function foo(first, second) +end + +local first, second +foo(, ) +]], {}) + + +config.set(nil, 'Lua.hint.arrayIndex', 'Enable') + +TEST([[ +local t = { + , + , + , + , +} +]], { + { + pos = 1, + text = '[1]', + kind = define.InlayHintKind.Other, + where = 'left', + }, + { + pos = 2, + text = '[2]', + kind = define.InlayHintKind.Other, + where = 'left', + }, + { + pos = 3, + text = '[3]', + kind = define.InlayHintKind.Other, + where = 'left', + }, + { + pos = 4, + text = '[4]', + kind = define.InlayHintKind.Other, + where = 'left', + }, +}) + +config.set(nil, 'Lua.hint.enable', false)