aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/completion/complete_macro_in_item_position.rs
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2019-09-11 15:49:57 +0100
committerGitHub <[email protected]>2019-09-11 15:49:57 +0100
commit6ce6744e18f25ebcde387178125d820686692df5 (patch)
treee1861982356f9905980537f962dff9560423f901 /crates/ra_ide_api/src/completion/complete_macro_in_item_position.rs
parent7bbb039fbdd124cb6549eb67bbe7316f03ff40e8 (diff)
parent6353b1621f44e1b0db65ebbe414aa7c5f1864b9d (diff)
Merge #1796
1796: Support completion for macros r=matklad a=uHOOCCOOHu This is based on #1795 , and fixes #1727 Also prettify hover text of macros. Some screenshorts below: Completion in item place. <img width="416" alt="Screenshot_20190910_134056" src="https://user-images.githubusercontent.com/14816024/64587159-fa72da00-d3d0-11e9-86bb-c98f169ec08d.png"> After pressing `tab`. <img width="313" alt="Screenshot_20190910_134111" src="https://user-images.githubusercontent.com/14816024/64587160-fa72da00-d3d0-11e9-9464-21e3f6957bd7.png"> Complete macros from `std`. <img width="588" alt="Screenshot_20190910_134147" src="https://user-images.githubusercontent.com/14816024/64587161-fb0b7080-d3d0-11e9-866e-5161f0d1b546.png"> Hover text. <img width="521" alt="Screenshot_20190910_134242" src="https://user-images.githubusercontent.com/14816024/64587162-fb0b7080-d3d0-11e9-8f09-ad17e3f6702a.png"> Co-authored-by: uHOOCCOOHu <[email protected]>
Diffstat (limited to 'crates/ra_ide_api/src/completion/complete_macro_in_item_position.rs')
-rw-r--r--crates/ra_ide_api/src/completion/complete_macro_in_item_position.rs50
1 files changed, 50 insertions, 0 deletions
diff --git a/crates/ra_ide_api/src/completion/complete_macro_in_item_position.rs b/crates/ra_ide_api/src/completion/complete_macro_in_item_position.rs
new file mode 100644
index 000000000..708dc9777
--- /dev/null
+++ b/crates/ra_ide_api/src/completion/complete_macro_in_item_position.rs
@@ -0,0 +1,50 @@
1use crate::completion::{CompletionContext, Completions};
2
3pub(super) fn complete_macro_in_item_position(acc: &mut Completions, ctx: &CompletionContext) {
4 // Show only macros in top level.
5 if ctx.is_new_item {
6 for (name, res) in ctx.analyzer.all_names(ctx.db) {
7 if res.get_macros().is_some() {
8 acc.add_resolution(ctx, name.to_string(), &res.only_macros());
9 }
10 }
11 }
12}
13
14#[cfg(test)]
15mod tests {
16 use crate::completion::{do_completion, CompletionItem, CompletionKind};
17 use insta::assert_debug_snapshot;
18
19 fn do_reference_completion(code: &str) -> Vec<CompletionItem> {
20 do_completion(code, CompletionKind::Reference)
21 }
22
23 #[test]
24 fn completes_macros_as_item() {
25 assert_debug_snapshot!(
26 do_reference_completion(
27 "
28 //- /main.rs
29 macro_rules! foo {
30 () => {}
31 }
32
33 fn foo() {}
34
35 <|>
36 "
37 ),
38 @r##"[
39 CompletionItem {
40 label: "foo",
41 source_range: [46; 46),
42 delete: [46; 46),
43 insert: "foo!",
44 kind: Macro,
45 detail: "macro_rules! foo",
46 },
47]"##
48 );
49 }
50}