|
| 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