From 992965cd82e71fc4dcd0584fe6814caf82b37e2c Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 16 Apr 2021 22:57:33 +0200 Subject: Return CallInfo for unclosed call expressions --- crates/ide_db/src/call_info.rs | 10 ++++++++-- crates/ide_db/src/call_info/tests.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) (limited to 'crates/ide_db') diff --git a/crates/ide_db/src/call_info.rs b/crates/ide_db/src/call_info.rs index e583a52f4..bad277a95 100644 --- a/crates/ide_db/src/call_info.rs +++ b/crates/ide_db/src/call_info.rs @@ -4,8 +4,9 @@ use either::Either; use hir::{HasAttrs, HirDisplay, Semantics, Type}; use stdx::format_to; use syntax::{ + algo, ast::{self, ArgListOwner, NameOwner}, - match_ast, AstNode, SyntaxNode, SyntaxToken, TextRange, TextSize, + match_ast, AstNode, Direction, SyntaxNode, SyntaxToken, TextRange, TextSize, }; use crate::RootDatabase; @@ -43,7 +44,12 @@ pub fn call_info(db: &RootDatabase, position: FilePosition) -> Option let sema = Semantics::new(db); let file = sema.parse(position.file_id); let file = file.syntax(); - let token = file.token_at_offset(position.offset).next()?; + let token = file + .token_at_offset(position.offset) + .left_biased() + // if the cursor is sandwiched between two space tokens and the call is unclosed + // this prevents us from leaving the CallExpression + .and_then(|tok| algo::skip_trivia_token(tok, Direction::Prev))?; let token = sema.descend_into_macros(token); let (callable, active_parameter) = call_info_impl(&sema, token)?; diff --git a/crates/ide_db/src/call_info/tests.rs b/crates/ide_db/src/call_info/tests.rs index 281a081a3..be1cc12de 100644 --- a/crates/ide_db/src/call_info/tests.rs +++ b/crates/ide_db/src/call_info/tests.rs @@ -522,3 +522,30 @@ fn main(f: fn(i32, f64) -> char) { "#]], ) } + +#[test] +fn call_info_for_unclosed_call() { + check( + r#" +fn foo(foo: u32, bar: u32) {} +fn main() { + foo($0 +}"#, + expect![[r#" + fn foo(foo: u32, bar: u32) + (, bar: u32) + "#]], + ); + // check with surrounding space + check( + r#" +fn foo(foo: u32, bar: u32) {} +fn main() { + foo( $0 +}"#, + expect![[r#" + fn foo(foo: u32, bar: u32) + (, bar: u32) + "#]], + ) +} -- cgit v1.2.3