Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: LSP inlay type hints on lambda parameters #5639

Merged
merged 2 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -41,14 +41,14 @@
args.files.get_file_id(&path).map(|file_id| {
let file = args.files.get_file(file_id).unwrap();
let source = file.source();
let (parsed_moduled, _errors) = noirc_frontend::parse_program(source);

Check warning on line 44 in tooling/lsp/src/requests/inlay_hint.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (moduled)

let span = range_to_byte_span(args.files, file_id, &params.range)
.map(|range| Span::from(range.start as u32..range.end as u32));

let mut collector =
InlayHintCollector::new(args.files, file_id, args.interner, span, options);
collector.collect_in_parsed_module(&parsed_moduled);

Check warning on line 51 in tooling/lsp/src/requests/inlay_hint.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (moduled)
collector.inlay_hints
})
});
Expand Down Expand Up @@ -251,7 +251,15 @@
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 @@
);
}

#[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);
}
Loading