diff options
| author | Lukas Wirth <lukastw97@gmail.com> | 2021-11-22 13:19:46 +0100 |
|---|---|---|
| committer | Lukas Wirth <lukastw97@gmail.com> | 2021-11-22 13:23:34 +0100 |
| commit | 77f08d0fc3387beb63543510f0fbe47a516857b2 (patch) | |
| tree | 84b23e085a838bcbf0ef54b2debe4e699b3699dd | |
| parent | 54b2de45e1bb1453dbaabee3e970f39b6f019c9e (diff) | |
| download | rust-77f08d0fc3387beb63543510f0fbe47a516857b2.tar.gz rust-77f08d0fc3387beb63543510f0fbe47a516857b2.zip | |
Split parts of `ide_db::call_info` off into `ide`
| -rw-r--r-- | crates/ide/src/call_info.rs | 679 | ||||
| -rw-r--r-- | crates/ide/src/lib.rs | 5 | ||||
| -rw-r--r-- | crates/ide/src/static_index.rs | 18 | ||||
| -rw-r--r-- | crates/ide/src/syntax_highlighting/inject.rs | 3 | ||||
| -rw-r--r-- | crates/ide_completion/src/context.rs | 2 | ||||
| -rw-r--r-- | crates/ide_db/src/active_parameter.rs | 70 | ||||
| -rw-r--r-- | crates/ide_db/src/call_info.rs | 174 | ||||
| -rw-r--r-- | crates/ide_db/src/call_info/tests.rs | 564 | ||||
| -rw-r--r-- | crates/ide_db/src/lib.rs | 2 |
9 files changed, 765 insertions, 752 deletions
diff --git a/crates/ide/src/call_info.rs b/crates/ide/src/call_info.rs new file mode 100644 index 00000000000..7568faa6bd5 --- /dev/null +++ b/crates/ide/src/call_info.rs @@ -0,0 +1,679 @@ +//! This module provides primitives for tracking the information about a call site. + +use either::Either; +use hir::{HasAttrs, HirDisplay, Semantics}; +use ide_db::{active_parameter::callable_for_token, base_db::FilePosition}; +use stdx::format_to; +use syntax::{algo, AstNode, Direction, TextRange, TextSize}; + +use crate::RootDatabase; + +/// Contains information about a call site. Specifically the +/// `FunctionSignature`and current parameter. +#[derive(Debug)] +pub struct CallInfo { + pub doc: Option<String>, + pub signature: String, + pub active_parameter: Option<usize>, + parameters: Vec<TextRange>, +} + +impl CallInfo { + pub fn parameter_labels(&self) -> impl Iterator<Item = &str> + '_ { + self.parameters.iter().map(move |&it| &self.signature[it]) + } + + pub fn parameter_ranges(&self) -> &[TextRange] { + &self.parameters + } + + fn push_param(&mut self, param: &str) { + if !self.signature.ends_with('(') { + self.signature.push_str(", "); + } + let start = TextSize::of(&self.signature); + self.signature.push_str(param); + let end = TextSize::of(&self.signature); + self.parameters.push(TextRange::new(start, end)) + } +} + +/// Computes parameter information for the given call expression. +pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option<CallInfo> { + let sema = Semantics::new(db); + let file = sema.parse(position.file_id); + let file = file.syntax(); + 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_single(token); + + let (callable, active_parameter) = callable_for_token(&sema, token)?; + + let mut res = + CallInfo { doc: None, signature: String::new(), parameters: vec![], active_parameter }; + + match callable.kind() { + hir::CallableKind::Function(func) => { + res.doc = func.docs(db).map(|it| it.into()); + format_to!(res.signature, "fn {}", func.name(db)); + } + hir::CallableKind::TupleStruct(strukt) => { + res.doc = strukt.docs(db).map(|it| it.into()); + format_to!(res.signature, "struct {}", strukt.name(db)); + } + hir::CallableKind::TupleEnumVariant(variant) => { + res.doc = variant.docs(db).map(|it| it.into()); + format_to!( + res.signature, + "enum {}::{}", + variant.parent_enum(db).name(db), + variant.name(db) + ); + } + hir::CallableKind::Closure => (), + } + + res.signature.push('('); + { + if let Some(self_param) = callable.receiver_param(db) { + format_to!(res.signature, "{}", self_param) + } + let mut buf = String::new(); + for (pat, ty) in callable.params(db) { + buf.clear(); + if let Some(pat) = pat { + match pat { + Either::Left(_self) => format_to!(buf, "self: "), + Either::Right(pat) => format_to!(buf, "{}: ", pat), + } + } + format_to!(buf, "{}", ty.display(db)); + res.push_param(&buf); + } + } + res.signature.push(')'); + + match callable.kind() { + hir::CallableKind::Function(_) | hir::CallableKind::Closure => { + let ret_type = callable.return_type(); + if !ret_type.is_unit() { + format_to!(res.signature, " -> {}", ret_type.display(db)); + } + } + hir::CallableKind::TupleStruct(_) | hir::CallableKind::TupleEnumVariant(_) => {} + } + Some(res) +} + +#[cfg(test)] +mod tests { + use expect_test::{expect, Expect}; + use ide_db::base_db::{fixture::ChangeFixture, FilePosition}; + + use crate::RootDatabase; + + /// Creates analysis from a multi-file fixture, returns positions marked with $0. + pub(crate) fn position(ra_fixture: &str) -> (RootDatabase, FilePosition) { + let change_fixture = ChangeFixture::parse(ra_fixture); + let mut database = RootDatabase::default(); + database.apply_change(change_fixture.change); + let (file_id, range_or_offset) = + change_fixture.file_position.expect("expected a marker ($0)"); + let offset = range_or_offset.expect_offset(); + (database, FilePosition { file_id, offset }) + } + + fn check(ra_fixture: &str, expect: Expect) { + let (db, position) = position(ra_fixture); + let call_info = crate::call_info::call_info(&db, position); + let actual = match call_info { + Some(call_info) => { + let docs = match &call_info.doc { + None => "".to_string(), + Some(docs) => format!("{}\n------\n", docs.as_str()), + }; + let params = call_info + .parameter_labels() + .enumerate() + .map(|(i, param)| { + if Some(i) == call_info.active_parameter { + format!("<{}>", param) + } else { + param.to_string() + } + }) + .collect::<Vec<_>>() + .join(", "); + format!("{}{}\n({})\n", docs, call_info.signature, params) + } + None => String::new(), + }; + expect.assert_eq(&actual); + } + + #[test] + fn test_fn_signature_two_args() { + check( + r#" +fn foo(x: u32, y: u32) -> u32 {x + y} +fn bar() { foo($03, ); } +"#, + expect![[r#" + fn foo(x: u32, y: u32) -> u32 + (<x: u32>, y: u32) + "#]], + ); + check( + r#" +fn foo(x: u32, y: u32) -> u32 {x + y} +fn bar() { foo(3$0, ); } +"#, + expect![[r#" + fn foo(x: u32, y: u32) -> u32 + (<x: u32>, y: u32) + "#]], + ); + check( + r#" +fn foo(x: u32, y: u32) -> u32 {x + y} +fn bar() { foo(3,$0 ); } +"#, + expect![[r#" + fn foo(x: u32, y: u32) -> u32 + (x: u32, <y: u32>) + "#]], + ); + check( + r#" +fn foo(x: u32, y: u32) -> u32 {x + y} +fn bar() { foo(3, $0); } +"#, + expect![[r#" + fn foo(x: u32, y: u32) -> u32 + (x: u32, <y: u32>) + "#]], + ); + } + + #[test] + fn test_fn_signature_two_args_empty() { + check( + r#" +fn foo(x: u32, y: u32) -> u32 {x + y} +fn bar() { foo($0); } +"#, + expect![[r#" + fn foo(x: u32, y: u32) -> u32 + (<x: u32>, y: u32) + "#]], + ); + } + + #[test] + fn test_fn_signature_two_args_first_generics() { + check( + r#" +fn foo<T, U: Copy + Display>(x: T, y: U) -> u32 + where T: Copy + Display, U: Debug +{ x + y } + +fn bar() { foo($03, ); } +"#, + expect![[r#" + fn foo(x: i32, y: {unknown}) -> u32 + (<x: i32>, y: {unknown}) + "#]], + ); + } + + #[test] + fn test_fn_signature_no_params() { + check( + r#" +fn foo<T>() -> T where T: Copy + Display {} +fn bar() { foo($0); } +"#, + expect![[r#" + fn foo() -> {unknown} + () + "#]], + ); + } + + #[test] + fn test_fn_signature_for_impl() { + check( + r#" +struct F; +impl F { pub fn new() { } } +fn bar() { + let _ : F = F::new($0); +} +"#, + expect![[r#" + fn new() + () + "#]], + ); + } + + #[test] + fn test_fn_signature_for_method_self() { + check( + r#" +struct S; +impl S { pub fn do_it(&self) {} } + +fn bar() { + let s: S = S; + s.do_it($0); +} +"#, + expect![[r#" + fn do_it(&self) + () + "#]], + ); + } + + #[test] + fn test_fn_signature_for_method_with_arg() { + check( + r#" +struct S; +impl S { + fn foo(&self, x: i32) {} +} + +fn main() { S.foo($0); } +"#, + expect![[r#" + fn foo(&self, x: i32) + (<x: i32>) + "#]], + ); + } + + #[test] + fn test_fn_signature_for_generic_method() { + check( + r#" +struct S<T>(T); +impl<T> S<T> { + fn foo(&self, x: T) {} +} + +fn main() { S(1u32).foo($0); } +"#, + expect![[r#" + fn foo(&self, x: u32) + (<x: u32>) + "#]], + ); + } + + #[test] + fn test_fn_signature_for_method_with_arg_as_assoc_fn() { + check( + r#" +struct S; +impl S { + fn foo(&self, x: i32) {} +} + +fn main() { S::foo($0); } +"#, + expect![[r#" + fn foo(self: &S, x: i32) + (<self: &S>, x: i32) + "#]], + ); + } + + #[test] + fn test_fn_signature_with_docs_simple() { + check( + r#" +/// test +// non-doc-comment +fn foo(j: u32) -> u32 { + j +} + +fn bar() { + let _ = foo($0); +} +"#, + expect![[r#" + test + ------ + fn foo(j: u32) -> u32 + (<j: u32>) + "#]], + ); + } + + #[test] + fn test_fn_signature_with_docs() { + check( + r#" +/// Adds one to the number given. +/// +/// # Examples +/// +/// ``` +/// let five = 5; +/// +/// assert_eq!(6, my_crate::add_one(5)); +/// ``` +pub fn add_one(x: i32) -> i32 { + x + 1 +} + +pub fn do() { + add_one($0 +}"#, + expect![[r##" + Adds one to the number given. + + # Examples + + ``` + let five = 5; + + assert_eq!(6, my_crate::add_one(5)); + ``` + ------ + fn add_one(x: i32) -> i32 + (<x: i32>) + "##]], + ); + } + + #[test] + fn test_fn_signature_with_docs_impl() { + check( + r#" +struct addr; +impl addr { + /// Adds one to the number given. + /// + /// # Examples + /// + /// ``` + /// let five = 5; + /// + /// assert_eq!(6, my_crate::add_one(5)); + /// ``` + pub fn add_one(x: i32) -> i32 { + x + 1 + } +} + +pub fn do_it() { + addr {}; + addr::add_one($0); +} +"#, + expect![[r##" + Adds one to the number given. + + # Examples + + ``` + let five = 5; + + assert_eq!(6, my_crate::add_one(5)); + ``` + ------ + fn add_one(x: i32) -> i32 + (<x: i32>) + "##]], + ); + } + + #[test] + fn test_fn_signature_with_docs_from_actix() { + check( + r#" +struct WriteHandler<E>; + +impl<E> WriteHandler<E> { + /// Method is called when writer emits error. + /// + /// If this method returns `ErrorAction::Continue` writer processing + /// continues otherwise stream processing stops. + fn error(&mut self, err: E, ctx: &mut Self::Context) -> Running { + Running::Stop + } + + /// Method is called when writer finishes. + /// + /// By default this method stops actor's `Context`. + fn finished(&mut self, ctx: &mut Self::Context) { + ctx.stop() + } +} + +pub fn foo(mut r: WriteHandler<()>) { + r.finished($0); +} +"#, + expect![[r#" + Method is called when writer finishes. + + By default this method stops actor's `Context`. + ------ + fn finished(&mut self, ctx: &mut {unknown}) + (<ctx: &mut {unknown}>) + "#]], + ); + } + + #[test] + fn call_info_bad_offset() { + check( + r#" +fn foo(x: u32, y: u32) -> u32 {x + y} +fn bar() { foo $0 (3, ); } +"#, + expect![[""]], + ); + } + + #[test] + fn test_nested_method_in_lambda() { + check( + r#" +struct Foo; +impl Foo { fn bar(&self, _: u32) { } } + +fn bar(_: u32) { } + +fn main() { + let foo = Foo; + std::thread::spawn(move || foo.bar($0)); +} +"#, + expect![[r#" + fn bar(&self, _: u32) + (<_: u32>) + "#]], + ); + } + + #[test] + fn works_for_tuple_structs() { + check( + r#" +/// A cool tuple struct +struct S(u32, i32); +fn main() { + let s = S(0, $0); +} +"#, + expect![[r#" + A cool tuple struct + ------ + struct S(u32, i32) + (u32, <i32>) + "#]], + ); + } + + #[test] + fn generic_struct() { + check( + r#" +struct S<T>(T); +fn main() { + let s = S($0); +} +"#, + expect![[r#" + struct S({unknown}) + (<{unknown}>) + "#]], + ); + } + + #[test] + fn works_for_enum_variants() { + check( + r#" +enum E { + /// A Variant + A(i32), + /// Another + B, + /// And C + C { a: i32, b: i32 } +} + +fn main() { + let a = E::A($0); +} +"#, + expect![[r#" + A Variant + ------ + enum E::A(i32) + (<i32>) + "#]], + ); + } + + #[test] + fn cant_call_struct_record() { + check( + r#" +struct S { x: u32, y: i32 } +fn main() { + let s = S($0); +} +"#, + expect![[""]], + ); + } + + #[test] + fn cant_call_enum_record() { + check( + r#" +enum E { + /// A Variant + A(i32), + /// Another + B, + /// And C + C { a: i32, b: i32 } +} + +fn main() { + let a = E::C($0); +} +"#, + expect![[""]], + ); + } + + #[test] + fn fn_signature_for_call_in_macro() { + check( + r#" +macro_rules! id { ($($tt:tt)*) => { $($tt)* } } +fn foo() { } +id! { + fn bar() { foo($0); } +} +"#, + expect![[r#" + fn foo() + () + "#]], + ); + } + + #[test] + fn call_info_for_lambdas() { + check( + r#" +struct S; +fn foo(s: S) -> i32 { 92 } +fn main() { + (|s| foo(s))($0) +} + "#, + expect![[r#" + (S) -> i32 + (<S>) + "#]], + ) + } + + #[test] + fn call_info_for_fn_ptr() { + check( + r#" +fn main(f: fn(i32, f64) -> char) { + f(0, $0) +} + "#, + expect![[r#" + (i32, f64) -> char + (i32, <f64>) + "#]], + ) + } + + #[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) + (<foo: 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) + (<foo: u32>, bar: u32) + "#]], + ) + } +} diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs index df26869dba6..034a5117933 100644 --- a/crates/ide/src/lib.rs +++ b/crates/ide/src/lib.rs @@ -24,6 +24,7 @@ mod navigation_target; mod annotations; mod call_hierarchy; +mod call_info; mod doc_links; mod highlight_related; mod expand_macro; @@ -73,6 +74,7 @@ use crate::navigation_target::{ToNav, TryToNav}; pub use crate::{ annotations::{Annotation, AnnotationConfig, AnnotationKind}, call_hierarchy::CallItem, + call_info::CallInfo, expand_macro::ExpandedMacro, file_structure::{StructureNode, StructureNodeKind}, folding_ranges::{Fold, FoldKind}, @@ -106,7 +108,6 @@ pub use ide_db::{ Cancelled, Change, CrateGraph, CrateId, Edition, FileId, FilePosition, FileRange, SourceRoot, SourceRootId, }, - call_info::CallInfo, label::Label, line_index::{LineCol, LineColUtf16, LineIndex}, search::{ReferenceCategory, SearchScope}, @@ -434,7 +435,7 @@ impl Analysis { /// Computes parameter information for the given call expression. pub fn call_info(&self, position: FilePosition) -> Cancellable<Option<CallInfo>> { - self.with_db(|db| ide_db::call_info::call_info(db, position)) + self.with_db(|db| call_info::call_info(db, position)) } /// Computes call hierarchy candidates for the given file position. diff --git a/crates/ide/src/static_index.rs b/crates/ide/src/static_index.rs index e09683f6890..29eef546cef 100644 --- a/crates/ide/src/static_index.rs +++ b/crates/ide/src/static_index.rs @@ -3,18 +3,18 @@ use std::collections::HashMap; -use hir::Semantics; -use hir::{db::HirDatabase, Crate, Module}; -use ide_db::base_db::{FileId, FileRange, SourceDatabaseExt}; -use ide_db::defs::Definition; -use ide_db::RootDatabase; +use hir::{db::HirDatabase, Crate, Module, Semantics}; +use ide_db::{ + base_db::{FileId, FileRange, SourceDatabaseExt}, + defs::Definition, + RootDatabase, +}; use rustc_hash::FxHashSet; -use syntax::{AstNode, SyntaxKind::*, T}; -use syntax::{SyntaxToken, TextRange}; +use syntax::{AstNode, SyntaxKind::*, SyntaxToken, TextRange, T}; -use crate::hover::hover_for_definition; use crate::{ - Analysis, Fold, HoverConfig, HoverDocFormat, HoverResult, InlayHint, InlayHintsConfig, TryToNav, + hover::hover_for_definition, Analysis, Fold, HoverConfig, HoverDocFormat, HoverResult, + InlayHint, InlayHintsConfig, TryToNav, }; /// A static representation of fully analyzed source code. diff --git a/crates/ide/src/syntax_highlighting/inject.rs b/crates/ide/src/syntax_highlighting/inject.rs index b7a3fb62cb4..543a7884825 100644 --- a/crates/ide/src/syntax_highlighting/inject.rs +++ b/crates/ide/src/syntax_highlighting/inject.rs @@ -5,7 +5,8 @@ use std::mem; use either::Either; use hir::{InFile, Semantics}; use ide_db::{ - call_info::ActiveParameter, defs::Definition, helpers::rust_doc::is_rust_fence, SymbolKind, + active_parameter::ActiveParameter, defs::Definition, helpers::rust_doc::is_rust_fence, + SymbolKind, }; use syntax::{ ast::{self, AstNode, IsString}, diff --git a/crates/ide_completion/src/context.rs b/crates/ide_completion/src/context.rs index 8759e2d108c..d9ddd9f2aeb 100644 --- a/crates/ide_completion/src/context.rs +++ b/crates/ide_completion/src/context.rs @@ -3,8 +3,8 @@ use base_db::SourceDatabaseExt; use hir::{Local, Name, ScopeDef, Semantics, SemanticsScope, Type, TypeInfo}; use ide_db::{ + active_parameter::ActiveParameter, base_db::{FilePosition, SourceDatabase}, - call_info::ActiveParameter, RootDatabase, }; use syntax::{ diff --git a/crates/ide_db/src/active_parameter.rs b/crates/ide_db/src/active_parameter.rs new file mode 100644 index 00000000000..47bd7aa9798 --- /dev/null +++ b/crates/ide_db/src/active_parameter.rs @@ -0,0 +1,70 @@ +//! This module provides functionality for querying callable information about a token. + +use either::Either; +use hir::{Semantics, Type}; +use syntax::{ + ast::{self, HasArgList, HasName}, + AstNode, SyntaxToken, +}; + +use crate::RootDatabase; + +#[derive(Debug)] +pub struct ActiveParameter { + pub ty: Type, + pub pat: Either<ast::SelfParam, ast::Pat>, +} + +impl ActiveParameter { + /// Returns information about the call argument this token is part of. + pub fn at_token(sema: &Semantics<RootDatabase>, token: SyntaxToken) -> Option<Self> { + let (signature, active_parameter) = callable_for_token(sema, token)?; + + let idx = active_parameter?; + let mut params = signature.params(sema.db); + if !(idx < params.len()) { + cov_mark::hit!(too_many_arguments); + return None; + } + let (pat, ty) = params.swap_remove(idx); + pat.map(|pat| ActiveParameter { ty, pat }) + } + + pub fn ident(&self) -> Option<ast::Name> { + self.pat.as_ref().right().and_then(|param| match param { + ast::Pat::IdentPat(ident) => ident.name(), + _ => None, + }) + } +} + +/// Returns a [`hir::Callable`] this token is a part of and its argument index of said callable. +pub fn callable_for_token( + sema: &Semantics<RootDatabase>, + token: SyntaxToken, +) -> Option<(hir::Callable, Option<usize>)> { + // Find the calling expression and it's NameRef + let parent = token.parent()?; + let calling_node = parent.ancestors().filter_map(ast::CallableExpr::cast).find(|it| { + it.arg_list() + .map_or(false, |it| it.syntax().text_range().contains(token.text_range().start())) + })?; + + let callable = match &calling_node { + ast::CallableExpr::Call(call) => { + let expr = call.expr()?; + sema.type_of_expr(&expr)?.adjusted().as_callable(sema.db) + } + ast::CallableExpr::MethodCall(call) => sema.resolve_method_call_as_callable(call), + }?; + let active_param = if let Some(arg_list) = calling_node.arg_list() { + let param = arg_list + .args() + .take_while(|arg| arg.syntax().text_range().end() <= token.text_range().start()) + .count(); + Some(param) + } else { + None + }; + Some((callable, active_param)) +} diff --git a/crates/ide_db/src/call_info.rs b/crates/ide_db/src/call_info.rs deleted file mode 100644 index 9e15702c5fb..00000000000 --- a/crates/ide_db/src/call_info.rs +++ /dev/null @@ -1,174 +0,0 @@ -//! This crate provides primitives for tracking the information about a call site. -use base_db::FilePosition; -use either::Either; -use hir::{HasAttrs, HirDisplay, Semantics, Type}; -use stdx::format_to; -use syntax::{ - algo, - ast::{self, HasArgList, HasName}, - AstNode, Direction, SyntaxToken, TextRange, TextSize, -}; - -use crate::RootDatabase; - -/// Contains information about a call site. Specifically the -/// `FunctionSignature`and current parameter. -#[derive(Debug)] -pub struct CallInfo { - pub doc: Option<String>, - pub signature: String, - pub active_parameter: Option<usize>, - parameters: Vec<TextRange>, -} - -impl CallInfo { - pub fn parameter_labels(&self) -> impl Iterator<Item = &str> + '_ { - self.parameters.iter().map(move |&it| &self.signature[it]) - } - - pub fn parameter_ranges(&self) -> &[TextRange] { - &self.parameters - } - - fn push_param(&mut self, param: &str) { - if !self.signature.ends_with('(') { - self.signature.push_str(", "); - } - let start = TextSize::of(&self.signature); - self.signature.push_str(param); - let end = TextSize::of(&self.signature); - self.parameters.push(TextRange::new(start, end)) - } -} - -/// Computes parameter information for the given call expression. -pub fn call_info(db: &RootDatabase, position: FilePosition) -> Option<CallInfo> { - let sema = Semantics::new(db); - let file = sema.parse(position.file_id); - let file = file.syntax(); - 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_single(token); - - let (callable, active_parameter) = call_info_impl(&sema, token)?; - - let mut res = - CallInfo { doc: None, signature: String::new(), parameters: vec![], active_parameter }; - - match callable.kind() { - hir::CallableKind::Function(func) => { - res.doc = func.docs(db).map(|it| it.into()); - format_to!(res.signature, "fn {}", func.name(db)); - } - hir::CallableKind::TupleStruct(strukt) => { - res.doc = strukt.docs(db).map(|it| it.into()); - format_to!(res.signature, "struct {}", strukt.name(db)); - } - hir::CallableKind::TupleEnumVariant(variant) => { - res.doc = variant.docs(db).map(|it| it.into()); - format_to!( - res.signature, - "enum {}::{}", - variant.parent_enum(db).name(db), - variant.name(db) - ); - } - hir::CallableKind::Closure => (), - } - - res.signature.push('('); - { - if let Some(self_param) = callable.receiver_param(db) { - format_to!(res.signature, "{}", self_param) - } - let mut buf = String::new(); - for (pat, ty) in callable.params(db) { - buf.clear(); - if let Some(pat) = pat { - match pat { - Either::Left(_self) => format_to!(buf, "self: "), - Either::Right(pat) => format_to!(buf, "{}: ", pat), - } - } - format_to!(buf, "{}", ty.display(db)); - res.push_param(&buf); - } - } - res.signature.push(')'); - - match callable.kind() { - hir::CallableKind::Function(_) | hir::CallableKind::Closure => { - let ret_type = callable.return_type(); - if !ret_type.is_unit() { - format_to!(res.signature, " -> {}", ret_type.display(db)); - } - } - hir::CallableKind::TupleStruct(_) | hir::CallableKind::TupleEnumVariant(_) => {} - } - Some(res) -} - -fn call_info_impl( - sema: &Semantics<RootDatabase>, - token: SyntaxToken, -) -> Option<(hir::Callable, Option<usize>)> { - // Find the calling expression and it's NameRef - let parent = token.parent()?; - let calling_node = parent.ancestors().filter_map(ast::CallableExpr::cast).find(|it| { - it.arg_list() - .map_or(false, |it| it.syntax().text_range().contains(token.text_range().start())) - })?; - - let callable = match &calling_node { - ast::CallableExpr::Call(call) => { - let expr = call.expr()?; - sema.type_of_expr(&expr)?.adjusted().as_callable(sema.db) - } - ast::CallableExpr::MethodCall(call) => sema.resolve_method_call_as_callable(call), - }?; - let active_param = if let Some(arg_list) = calling_node.arg_list() { - let param = arg_list - .args() - .take_while(|arg| arg.syntax().text_range().end() <= token.text_range().start()) - .count(); - Some(param) - } else { - None - }; - Some((callable, active_param)) -} - -#[derive(Debug)] -pub struct ActiveParameter { - pub ty: Type, - pub pat: Either<ast::SelfParam, ast::Pat>, -} - -impl ActiveParameter { - pub fn at_token(sema: &Semantics<RootDatabase>, token: SyntaxToken) -> Option<Self> { - let (signature, active_parameter) = call_info_impl(sema, token)?; - - let idx = active_parameter?; - let mut params = signature.params(sema.db); - if !(idx < params.len()) { - cov_mark::hit!(too_many_arguments); - return None; - } - let (pat, ty) = params.swap_remove(idx); - pat.map(|pat| ActiveParameter { ty, pat }) - } - - pub fn ident(&self) -> Option<ast::Name> { - self.pat.as_ref().right().and_then(|param| match param { - ast::Pat::IdentPat(ident) => ident.name(), - _ => None, - }) - } -} - -#[cfg(test)] -mod tests; diff --git a/crates/ide_db/src/call_info/tests.rs b/crates/ide_db/src/call_info/tests.rs deleted file mode 100644 index 8cdfcd027a3..00000000000 --- a/crates/ide_db/src/call_info/tests.rs +++ /dev/null @@ -1,564 +0,0 @@ -use base_db::{fixture::ChangeFixture, FilePosition}; -use expect_test::{expect, Expect}; - -use crate::RootDatabase; - -/// Creates analysis from a multi-file fixture, returns positions marked with $0. -pub(crate) fn position(ra_fixture: &str) -> (RootDatabase, FilePosition) { - let change_fixture = ChangeFixture::parse(ra_fixture); - let mut database = RootDatabase::default(); - database.apply_change(change_fixture.change); - let (file_id, range_or_offset) = change_fixture.file_position.expect("expected a marker ($0)"); - let offset = range_or_offset.expect_offset(); - (database, FilePosition { file_id, offset }) -} - -fn check(ra_fixture: &str, expect: Expect) { - let (db, position) = position(ra_fixture); - let call_info = crate::call_info::call_info(&db, position); - let actual = match call_info { - Some(call_info) => { - let docs = match &call_info.doc { - None => "".to_string(), - Some(docs) => format!("{}\n------\n", docs.as_str()), - }; - let params = call_info - .parameter_labels() - .enumerate() - .map(|(i, param)| { - if Some(i) == call_info.active_parameter { - format!("<{}>", param) - } else { - param.to_string() - } - }) - .collect::<Vec<_>>() - .join(", "); - format!("{}{}\n({})\n", docs, call_info.signature, params) - } - None => String::new(), - }; - expect.assert_eq(&actual); -} - -#[test] -fn test_fn_signature_two_args() { - check( - r#" -fn foo(x: u32, y: u32) -> u32 {x + y} -fn bar() { foo($03, ); } -"#, - expect![[r#" - fn foo(x: u32, y: u32) -> u32 - (<x: u32>, y: u32) - "#]], - ); - check( - r#" -fn foo(x: u32, y: u32) -> u32 {x + y} -fn bar() { foo(3$0, ); } -"#, - expect![[r#" - fn foo(x: u32, y: u32) -> u32 - (<x: u32>, y: u32) - "#]], - ); - check( - r#" -fn foo(x: u32, y: u32) -> u32 {x + y} -fn bar() { foo(3,$0 ); } -"#, - expect![[r#" - fn foo(x: u32, y: u32) -> u32 - (x: u32, <y: u32>) - "#]], - ); - check( - r#" -fn foo(x: u32, y: u32) -> u32 {x + y} -fn bar() { foo(3, $0); } -"#, - expect![[r#" - fn foo(x: u32, y: u32) -> u32 - (x: u32, <y: u32>) - "#]], - ); -} - -#[test] -fn test_fn_signature_two_args_empty() { - check( - r#" -fn foo(x: u32, y: u32) -> u32 {x + y} -fn bar() { foo($0); } -"#, - expect![[r#" - fn foo(x: u32, y: u32) -> u32 - (<x: u32>, y: u32) - "#]], - ); -} - -#[test] -fn test_fn_signature_two_args_first_generics() { - check( - r#" -fn foo<T, U: Copy + Display>(x: T, y: U) -> u32 - where T: Copy + Display, U: Debug -{ x + y } - -fn bar() { foo($03, ); } -"#, - expect![[r#" - fn foo(x: i32, y: {unknown}) -> u32 - (<x: i32>, y: {unknown}) - "#]], - ); -} - -#[test] -fn test_fn_signature_no_params() { - check( - r#" -fn foo<T>() -> T where T: Copy + Display {} -fn bar() { foo($0); } -"#, - expect![[r#" - fn foo() -> {unknown} - () - "#]], - ); -} - -#[test] -fn test_fn_signature_for_impl() { - check( - r#" -struct F; -impl F { pub fn new() { } } -fn bar() { - let _ : F = F::new($0); -} -"#, - expect![[r#" - fn new() - () - "#]], - ); -} - -#[test] -fn test_fn_signature_for_method_self() { - check( - r#" -struct S; -impl S { pub fn do_it(&self) {} } - -fn bar() { - let s: S = S; - s.do_it($0); -} -"#, - expect![[r#" - fn do_it(&self) - () - "#]], - ); -} - -#[test] -fn test_fn_signature_for_method_with_arg() { - check( - r#" -struct S; -impl S { - fn foo(&self, x: i32) {} -} - -fn main() { S.foo($0); } -"#, - expect![[r#" - fn foo(&self, x: i32) - (<x: i32>) - "#]], - ); -} - -#[test] -fn test_fn_signature_for_generic_method() { - check( - r#" -struct S<T>(T); -impl<T> S<T> { - fn foo(&self, x: T) {} -} - -fn main() { S(1u32).foo($0); } -"#, - expect![[r#" - fn foo(&self, x: u32) - (<x: u32>) - "#]], - ); -} - -#[test] -fn test_fn_signature_for_method_with_arg_as_assoc_fn() { - check( - r#" -struct S; -impl S { - fn foo(&self, x: i32) {} -} - -fn main() { S::foo($0); } -"#, - expect![[r#" - fn foo(self: &S, x: i32) - (<self: &S>, x: i32) - "#]], - ); -} - -#[test] -fn test_fn_signature_with_docs_simple() { - check( - r#" -/// test -// non-doc-comment -fn foo(j: u32) -> u32 { - j -} - -fn bar() { - let _ = foo($0); -} -"#, - expect![[r#" - test - ------ - fn foo(j: u32) -> u32 - (<j: u32>) - "#]], - ); -} - -#[test] -fn test_fn_signature_with_docs() { - check( - r#" -/// Adds one to the number given. -/// -/// # Examples -/// -/// ``` -/// let five = 5; -/// -/// assert_eq!(6, my_crate::add_one(5)); -/// ``` -pub fn add_one(x: i32) -> i32 { - x + 1 -} - -pub fn do() { - add_one($0 -}"#, - expect![[r##" - Adds one to the number given. - - # Examples - - ``` - let five = 5; - - assert_eq!(6, my_crate::add_one(5)); - ``` - ------ - fn add_one(x: i32) -> i32 - (<x: i32>) - "##]], - ); -} - -#[test] -fn test_fn_signature_with_docs_impl() { - check( - r#" -struct addr; -impl addr { - /// Adds one to the number given. - /// - /// # Examples - /// - /// ``` - /// let five = 5; - /// - /// assert_eq!(6, my_crate::add_one(5)); - /// ``` - pub fn add_one(x: i32) -> i32 { - x + 1 - } -} - -pub fn do_it() { - addr {}; - addr::add_one($0); -} -"#, - expect![[r##" - Adds one to the number given. - - # Examples - - ``` - let five = 5; - - assert_eq!(6, my_crate::add_one(5)); - ``` - ------ - fn add_one(x: i32) -> i32 - (<x: i32>) - "##]], - ); -} - -#[test] -fn test_fn_signature_with_docs_from_actix() { - check( - r#" -struct WriteHandler<E>; - -impl<E> WriteHandler<E> { - /// Method is called when writer emits error. - /// - /// If this method returns `ErrorAction::Continue` writer processing - /// continues otherwise stream processing stops. - fn error(&mut self, err: E, ctx: &mut Self::Context) -> Running { - Running::Stop - } - - /// Method is called when writer finishes. - /// - /// By default this method stops actor's `Context`. - fn finished(&mut self, ctx: &mut Self::Context) { - ctx.stop() - } -} - -pub fn foo(mut r: WriteHandler<()>) { - r.finished($0); -} -"#, - expect![[r#" - Method is called when writer finishes. - - By default this method stops actor's `Context`. - ------ - fn finished(&mut self, ctx: &mut {unknown}) - (<ctx: &mut {unknown}>) - "#]], - ); -} - -#[test] -fn call_info_bad_offset() { - check( - r#" -fn foo(x: u32, y: u32) -> u32 {x + y} -fn bar() { foo $0 (3, ); } -"#, - expect![[""]], - ); -} - -#[test] -fn test_nested_method_in_lambda() { - check( - r#" -struct Foo; -impl Foo { fn bar(&self, _: u32) { } } - -fn bar(_: u32) { } - -fn main() { - let foo = Foo; - std::thread::spawn(move || foo.bar($0)); -} -"#, - expect![[r#" - fn bar(&self, _: u32) - (<_: u32>) - "#]], - ); -} - -#[test] -fn works_for_tuple_structs() { - check( - r#" -/// A cool tuple struct -struct S(u32, i32); -fn main() { - let s = S(0, $0); -} -"#, - expect![[r#" - A cool tuple struct - ------ - struct S(u32, i32) - (u32, <i32>) - "#]], - ); -} - -#[test] -fn generic_struct() { - check( - r#" -struct S<T>(T); -fn main() { - let s = S($0); -} -"#, - expect![[r#" - struct S({unknown}) - (<{unknown}>) - "#]], - ); -} - -#[test] -fn works_for_enum_variants() { - check( - r#" -enum E { - /// A Variant - A(i32), - /// Another - B, - /// And C - C { a: i32, b: i32 } -} - -fn main() { - let a = E::A($0); -} -"#, - expect![[r#" - A Variant - ------ - enum E::A(i32) - (<i32>) - "#]], - ); -} - -#[test] -fn cant_call_struct_record() { - check( - r#" -struct S { x: u32, y: i32 } -fn main() { - let s = S($0); -} -"#, - expect![[""]], - ); -} - -#[test] -fn cant_call_enum_record() { - check( - r#" -enum E { - /// A Variant - A(i32), - /// Another - B, - /// And C - C { a: i32, b: i32 } -} - -fn main() { - let a = E::C($0); -} -"#, - expect![[""]], - ); -} - -#[test] -fn fn_signature_for_call_in_macro() { - check( - r#" -macro_rules! id { ($($tt:tt)*) => { $($tt)* } } -fn foo() { } -id! { - fn bar() { foo($0); } -} -"#, - expect![[r#" - fn foo() - () - "#]], - ); -} - -#[test] -fn call_info_for_lambdas() { - check( - r#" -struct S; -fn foo(s: S) -> i32 { 92 } -fn main() { - (|s| foo(s))($0) -} - "#, - expect![[r#" - (S) -> i32 - (<S>) - "#]], - ) -} - -#[test] -fn call_info_for_fn_ptr() { - check( - r#" -fn main(f: fn(i32, f64) -> char) { - f(0, $0) -} - "#, - expect![[r#" - (i32, f64) -> char - (i32, <f64>) - "#]], - ) -} - -#[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) - (<foo: 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) - (<foo: u32>, bar: u32) - "#]], - ) -} diff --git a/crates/ide_db/src/lib.rs b/crates/ide_db/src/lib.rs index 77db09315d9..1250008984e 100644 --- a/crates/ide_db/src/lib.rs +++ b/crates/ide_db/src/lib.rs @@ -13,12 +13,12 @@ pub mod items_locator; pub mod source_change; pub mod ty_filter; pub mod traits; -pub mod call_info; pub mod helpers; pub mod path_transform; pub mod search; pub mod rename; +pub mod active_parameter; use std::{fmt, mem::ManuallyDrop, sync::Arc}; |
