Skip to content

Commit

Permalink
feat: LSP inlay type hints on lambda parameters (#5639)
Browse files Browse the repository at this point in the history
# Description

## Problem

Resolves #5640

## Summary


![image](https://github.com/user-attachments/assets/e9caa443-3ffb-4a99-a5f0-ba79e1f1d106)


## Additional Context

None.

## Documentation

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** Documentation to be submitted in a
separate PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
  • Loading branch information
asterite authored Jul 30, 2024
1 parent daad75c commit 80128ff
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
38 changes: 37 additions & 1 deletion tooling/lsp/src/requests/inlay_hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,15 @@ impl<'a> InlayHintCollector<'a> {
self.collect_in_expression(expression);
}
}
ExpressionKind::Lambda(lambda) => self.collect_in_expression(&lambda.body),
ExpressionKind::Lambda(lambda) => {
for (pattern, typ) in &lambda.parameters {
if matches!(typ.typ, UnresolvedTypeData::Unspecified) {
self.collect_in_pattern(pattern);
}
}

self.collect_in_expression(&lambda.body);
}
ExpressionKind::Parenthesized(parenthesized) => {
self.collect_in_expression(parenthesized);
}
Expand Down Expand Up @@ -895,6 +903,34 @@ mod inlay_hints_tests {
);
}

#[test]
async fn test_type_inlay_hints_in_lambda() {
let inlay_hints = get_inlay_hints(102, 105, type_hints()).await;
assert_eq!(inlay_hints.len(), 1);

let position = Position { line: 104, character: 35 };

let inlay_hint = &inlay_hints[0];
assert_eq!(inlay_hint.position, position);

if let InlayHintLabel::LabelParts(labels) = &inlay_hint.label {
assert_eq!(labels.len(), 2);
assert_eq!(labels[0].value, ": ");
assert_eq!(labels[0].location, None);
assert_eq!(labels[1].value, "i32");
} else {
panic!("Expected InlayHintLabel::LabelParts, got {:?}", inlay_hint.label);
}

assert_eq!(
inlay_hint.text_edits,
Some(vec![TextEdit {
range: Range { start: position, end: position },
new_text: ": i32".to_string(),
}])
);
}

#[test]
async fn test_do_not_panic_when_given_line_is_too_big() {
let inlay_hints = get_inlay_hints(0, 100000, type_hints()).await;
Expand Down
11 changes: 10 additions & 1 deletion tooling/lsp/test_programs/inlay_hints/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,13 @@ fn call_yet_another_function() {

fn struct_member_hint() {
let SomeStruct { one } = SomeStruct { one: 1 };
}
}

fn some_map<T, U>(x: T, f: fn(T) -> U) -> U {
f(x)
}

fn hint_on_lambda_parameter() {
let value: i32 = 1;
let _: i32 = some_map(value, |x| x + 1);
}

0 comments on commit 80128ff

Please sign in to comment.