MCPcopy Create free account
hub / github.com/PHPantom-dev/phpantom_lsp / emit_closure_hints

Method emit_closure_hints

src/inlay_hints.rs:259–383  ·  view source on GitHub ↗

Emit a return-type inlay hint for a function / method / closure / arrow function that lacks an explicit return type declaration. Emit parameter-type and return-type inlay hints for closures and arrow functions whose types can be inferred from the callable context.

(
        &self,
        content: &str,
        sites: &[UntypedClosureSite],
        call_sites: &[CallSite],
        range: (u32, u32),
        ctx: &FileContext,
        hints: &mut Vec<InlayHint>,

Source from the content-addressed store, hash-verified

257 /// Emit parameter-type and return-type inlay hints for closures and
258 /// arrow functions whose types can be inferred from the callable context.
259 fn emit_closure_hints(
260 &self,
261 content: &str,
262 sites: &[UntypedClosureSite],
263 call_sites: &[CallSite],
264 range: (u32, u32),
265 ctx: &FileContext,
266 hints: &mut Vec<InlayHint>,
267 ) {
268 let (range_start, range_end) = range;
269 for site in sites {
270 // Quick range check: use close_paren_offset if available,
271 // otherwise the first untyped param offset.
272 let representative_offset = site
273 .close_paren_offset
274 .or_else(|| site.untyped_params.first().map(|&(_, off)| off));
275 if let Some(off) = representative_offset {
276 if off < range_start || off > range_end {
277 continue;
278 }
279 } else {
280 continue;
281 }
282
283 // Find the matching CallSite so we can extract the full
284 // argument text for template substitution. We match by
285 // call expression string and verify that any of the
286 // closure site's offsets fall within the call site's
287 // argument range. We check ALL untyped-param offsets
288 // and the close-paren offset since the representative
289 // offset alone may not be inside the parent call's range
290 // for all AST shapes.
291 let call_args_text: Option<&str> = {
292 let closure_offsets: Vec<u32> = site
293 .untyped_params
294 .iter()
295 .map(|&(_, off)| off)
296 .chain(site.close_paren_offset)
297 .collect();
298 call_sites
299 .iter()
300 .find(|cs| {
301 cs.call_expression == site.parent_call_expression
302 && closure_offsets
303 .iter()
304 .any(|&off| off >= cs.args_start && off <= cs.args_end)
305 })
306 .and_then(|cs| content.get(cs.args_start as usize..cs.args_end as usize))
307 };
308
309 // Resolve the callable to get the parameter's type signature.
310 // Pass the call-site argument text so that function/method-level
311 // @template parameters are inferred from the sibling arguments
312 // and substituted into parameter type hints (e.g. turning
313 // `callable(T): T` into `callable(int): int`).
314 let position = offset_to_position(content, representative_offset.unwrap_or(0) as usize);
315 let resolved = match self.resolve_callable_target_with_args(
316 &site.parent_call_expression,

Callers 1

handle_inlay_hintsMethod · 0.80

Calls 13

offset_to_positionFunction · 0.85
iterMethod · 0.80
callable_param_typesMethod · 0.80
shortenMethod · 0.80
is_mixedMethod · 0.80
pushMethod · 0.80
callable_return_typeMethod · 0.80
mapMethod · 0.45
firstMethod · 0.45
findMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected