@@ -144,37 +144,45 @@ local viewNodeSwitch = util.switch()
144144 local argView = ' '
145145 local regView = ' '
146146 for i , arg in ipairs (source .args ) do
147+ local argNode = vm .compileNode (arg )
148+ local isOptional = argNode :isOptional ()
149+ if isOptional then
150+ argNode = argNode :copy ()
151+ argNode :removeOptional ()
152+ end
147153 args [i ] = string.format (' %s%s: %s'
148154 , arg .name [1 ]
149- , arg . optional and ' ?' or ' '
150- , m .getInfer (arg ):view ()
155+ , isOptional and ' ?' or ' '
156+ , m .getInfer (argNode ):view ()
151157 )
152158 end
153159 if # args > 0 then
154160 argView = table.concat (args , ' , ' )
155161 end
156162 for i , ret in ipairs (source .returns ) do
157- rets [i ] = string.format (' %s%s'
158- , m .getInfer (ret ):view ()
159- , ret .optional and ' ?' or ' '
160- )
163+ rets [i ] = m .getInfer (ret ):view ()
161164 end
162165 if # rets > 0 then
163166 regView = ' :' .. table.concat (rets , ' , ' )
164167 end
165168 return (' fun(%s)%s' ):format (argView , regView )
166169 end )
167170
168- --- @param source parser.object
171+ --- @param source parser.object | vm.node
169172--- @return vm.infer
170173function m .getInfer (source )
171- local node = vm .compileNode (source )
174+ local node
175+ if source .type == ' vm.node' then
176+ node = source
177+ else
178+ node = vm .compileNode (source )
179+ end
172180 if node .lastInfer then
173181 return node .lastInfer
174182 end
175183 local infer = setmetatable ({
176184 node = node ,
177- uri = guide .getUri (source ),
185+ uri = source . type ~= ' vm.node ' and guide .getUri (source ),
178186 }, mt )
179187 node .lastInfer = infer
180188
@@ -298,22 +306,26 @@ function mt:view(default, uri)
298306 local max = # array
299307 local limit = config .get (uri or self .uri , ' Lua.hover.enumsLimit' )
300308
309+ local view
301310 if max > limit then
302- local view = string.format (' %s...(+%d)'
311+ view = string.format (' %s...(+%d)'
303312 , table.concat (array , ' |' , 1 , limit )
304313 , max - limit
305314 )
306-
307- self .cachedView = view
308-
309- return view
310315 else
311- local view = table.concat (array , ' |' )
312-
313- self .cachedView = view
316+ view = table.concat (array , ' |' )
317+ end
314318
315- return view
319+ if self .node :isOptional () then
320+ if max > 1 then
321+ view = ' (' .. view .. ' )?'
322+ else
323+ view = view .. ' ?'
324+ end
316325 end
326+ self .cachedView = view
327+
328+ return view
317329end
318330
319331function mt :eachView ()
0 commit comments