Skip to content

Commit e7727d9

Browse files
committed
feat: Omit parameter hints when the argument name matches
1 parent 0b067fd commit e7727d9

File tree

3 files changed

+152
-1
lines changed

3 files changed

+152
-1
lines changed

script/core/hint.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ local function paramName(uri, results, start, finish)
145145
and (paramConfig ~= 'Literal' or guide.isLiteral(arg)) then
146146
mark[arg] = true
147147
local param = params[i]
148-
if param and param[1] then
148+
if param and param[1] and param[1] ~= arg[1] then
149149
results[#results+1] = {
150150
text = param[1] .. ':',
151151
offset = arg.start,

test.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ local function testAll()
7676
test 'diagnostics'
7777
test 'crossfile'
7878
test 'highlight'
79+
test 'inlay_hint'
7980
test 'rename'
8081
test 'signature'
8182
test 'command'

test/inlay_hint/init.lua

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
local core = require 'core.hint'
2+
local files = require 'files'
3+
local catch = require 'catch'
4+
local config = require 'config'
5+
local define = require 'proto.define'
6+
7+
rawset(_G, 'TEST', true)
8+
9+
---@diagnostic disable: await-in-sync
10+
local function TEST(script, expect, opts)
11+
opts = opts or {}
12+
local newScript, catched = catch(script, '!')
13+
14+
files.setText(TESTURI, newScript)
15+
files.compileState(TESTURI)
16+
17+
local results = core(TESTURI, 0, math.huge)
18+
table.sort(results, function (a, b)
19+
if a.offset ~= b.offset then
20+
return a.offset < b.offset
21+
end
22+
return a.text < b.text
23+
end)
24+
25+
if #expect ~= #results then
26+
print('Expect count:', #expect, 'Result count:', #results)
27+
for i, res in ipairs(results) do
28+
print((' %d: text=%s kind=%s where=%s offset=%s'):format(
29+
i,
30+
tostring(res.text),
31+
tostring(res.kind),
32+
tostring(res.where),
33+
tostring(res.offset)
34+
))
35+
end
36+
end
37+
38+
assert(#expect == #results)
39+
for i, res in ipairs(results) do
40+
local info = expect[i]
41+
local pos = catched['!'][info.pos]
42+
assert(pos)
43+
local offset = info.useFinish and pos[2] or pos[1]
44+
assert(res.text == info.text)
45+
assert(res.kind == info.kind)
46+
assert(res.where == info.where)
47+
assert(res.offset == offset)
48+
end
49+
50+
files.remove(TESTURI)
51+
end
52+
53+
config.set(nil, 'Lua.hint.enable', true)
54+
55+
config.set(nil, 'Lua.hint.setType', true)
56+
57+
TEST([[
58+
---@return integer
59+
local function returnsInt()
60+
return 1
61+
end
62+
63+
local <!val!> = returnsInt()
64+
]], {
65+
{
66+
pos = 1,
67+
text = ': integer',
68+
kind = define.InlayHintKind.Type,
69+
where = 'right',
70+
useFinish = true,
71+
},
72+
})
73+
74+
config.set(nil, 'Lua.hint.paramName', 'Literal')
75+
76+
TEST([[
77+
local function foo(first, second)
78+
end
79+
80+
foo(<!1!>, x)
81+
foo(<!1!>, <!2!>)
82+
]], {
83+
{
84+
pos = 1,
85+
text = 'first:',
86+
kind = define.InlayHintKind.Parameter,
87+
where = 'left',
88+
},
89+
{
90+
pos = 2,
91+
text = 'first:',
92+
kind = define.InlayHintKind.Parameter,
93+
where = 'left',
94+
},
95+
{
96+
pos = 3,
97+
text = 'second:',
98+
kind = define.InlayHintKind.Parameter,
99+
where = 'left',
100+
},
101+
})
102+
103+
config.set(nil, 'Lua.hint.paramName', 'All')
104+
105+
TEST([[
106+
local function foo(first, second)
107+
end
108+
109+
local first, second
110+
foo(<!first!>, <!second!>)
111+
]], {})
112+
113+
114+
config.set(nil, 'Lua.hint.arrayIndex', 'Enable')
115+
116+
TEST([[
117+
local t = {
118+
<!'first'!>,
119+
<!'second'!>,
120+
<!'third'!>,
121+
<!'fourth'!>,
122+
}
123+
]], {
124+
{
125+
pos = 1,
126+
text = '[1]',
127+
kind = define.InlayHintKind.Other,
128+
where = 'left',
129+
},
130+
{
131+
pos = 2,
132+
text = '[2]',
133+
kind = define.InlayHintKind.Other,
134+
where = 'left',
135+
},
136+
{
137+
pos = 3,
138+
text = '[3]',
139+
kind = define.InlayHintKind.Other,
140+
where = 'left',
141+
},
142+
{
143+
pos = 4,
144+
text = '[4]',
145+
kind = define.InlayHintKind.Other,
146+
where = 'left',
147+
},
148+
})
149+
150+
config.set(nil, 'Lua.hint.enable', false)

0 commit comments

Comments
 (0)