Skip to content

Commit fdaddb9

Browse files
bors[bot]oxalica
andauthored
Merge #4210
4210: Include function qualifiers in signature r=matklad a=oxalica Fixes #2450 It seems there's no test for `ra_ide/display/{short_label,function_signature}`. I'm not sure how to setup it. Manually tested: <img width="428" alt="Screenshot_20200430_004434" src="https://user-images.githubusercontent.com/14816024/80622769-d6f1c200-8a7b-11ea-91f3-e94bfb2703c5.png"> Co-authored-by: oxalica <oxalicc@pm.me>
2 parents 95e8766 + b9b342f commit fdaddb9

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

crates/ra_ide/src/display/function_signature.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ pub struct FunctionSignature {
2626
pub kind: CallableKind,
2727
/// Optional visibility
2828
pub visibility: Option<String>,
29+
/// Qualifiers like `async`, `unsafe`, ...
30+
pub qualifier: FunctionQualifier,
2931
/// Name of the function
3032
pub name: Option<String>,
3133
/// Documentation for the function
@@ -46,6 +48,16 @@ pub struct FunctionSignature {
4648
pub has_self_param: bool,
4749
}
4850

51+
#[derive(Debug, Default)]
52+
pub struct FunctionQualifier {
53+
// `async` and `const` are mutually exclusive. Do we need to enforcing it here?
54+
pub is_async: bool,
55+
pub is_const: bool,
56+
pub is_unsafe: bool,
57+
/// The string `extern ".."`
58+
pub extern_abi: Option<String>,
59+
}
60+
4961
impl FunctionSignature {
5062
pub(crate) fn with_doc_opt(mut self, doc: Option<Documentation>) -> Self {
5163
self.doc = doc;
@@ -83,6 +95,8 @@ impl FunctionSignature {
8395
FunctionSignature {
8496
kind: CallableKind::StructConstructor,
8597
visibility: node.visibility().map(|n| n.syntax().text().to_string()),
98+
// Do we need `const`?
99+
qualifier: Default::default(),
86100
name: node.name().map(|n| n.text().to_string()),
87101
ret_type: node.name().map(|n| n.text().to_string()),
88102
parameters: params,
@@ -128,6 +142,8 @@ impl FunctionSignature {
128142
FunctionSignature {
129143
kind: CallableKind::VariantConstructor,
130144
visibility: None,
145+
// Do we need `const`?
146+
qualifier: Default::default(),
131147
name: Some(name),
132148
ret_type: None,
133149
parameters: params,
@@ -151,6 +167,7 @@ impl FunctionSignature {
151167
FunctionSignature {
152168
kind: CallableKind::Macro,
153169
visibility: None,
170+
qualifier: Default::default(),
154171
name: node.name().map(|n| n.text().to_string()),
155172
ret_type: None,
156173
parameters: params,
@@ -223,6 +240,12 @@ impl From<&'_ ast::FnDef> for FunctionSignature {
223240
FunctionSignature {
224241
kind: CallableKind::Function,
225242
visibility: node.visibility().map(|n| n.syntax().text().to_string()),
243+
qualifier: FunctionQualifier {
244+
is_async: node.async_token().is_some(),
245+
is_const: node.const_token().is_some(),
246+
is_unsafe: node.unsafe_token().is_some(),
247+
extern_abi: node.abi().map(|n| n.to_string()),
248+
},
226249
name: node.name().map(|n| n.text().to_string()),
227250
ret_type: node
228251
.ret_type()
@@ -246,6 +269,23 @@ impl Display for FunctionSignature {
246269
write!(f, "{} ", t)?;
247270
}
248271

272+
if self.qualifier.is_async {
273+
write!(f, "async ")?;
274+
}
275+
276+
if self.qualifier.is_const {
277+
write!(f, "const ")?;
278+
}
279+
280+
if self.qualifier.is_unsafe {
281+
write!(f, "unsafe ")?;
282+
}
283+
284+
if let Some(extern_abi) = &self.qualifier.extern_abi {
285+
// Keyword `extern` is included in the string.
286+
write!(f, "{} ", extern_abi)?;
287+
}
288+
249289
if let Some(name) = &self.name {
250290
match self.kind {
251291
CallableKind::Function => write!(f, "fn {}", name)?,

crates/ra_ide/src/hover.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,4 +844,29 @@ fn func(foo: i32) { if true { <|>foo; }; }
844844
&["fn foo()\n```\n\n<- `\u{3000}` here"],
845845
);
846846
}
847+
848+
#[test]
849+
fn test_hover_function_show_qualifiers() {
850+
check_hover_result(
851+
"
852+
//- /lib.rs
853+
async fn foo<|>() {}
854+
",
855+
&["async fn foo()"],
856+
);
857+
check_hover_result(
858+
"
859+
//- /lib.rs
860+
pub const unsafe fn foo<|>() {}
861+
",
862+
&["pub const unsafe fn foo()"],
863+
);
864+
check_hover_result(
865+
r#"
866+
//- /lib.rs
867+
pub(crate) async unsafe extern "C" fn foo<|>() {}
868+
"#,
869+
&[r#"pub(crate) async unsafe extern "C" fn foo()"#],
870+
);
871+
}
847872
}

0 commit comments

Comments
 (0)