1821: Macro completion tweaks r=matklad a=SomeoneToIgnore

Thanks @uHOOCCOOHu for making the macro completion happen :)

I've added a few tweaks to the current completion to make it a bit more convenient:

* Automatically add braces and put the editor cursor inside of them:
<img width="159" alt="image" src="https://user-images.githubusercontent.com/2690773/64737220-022b9f00-d4f5-11e9-8088-76d4678921ab.png">

Currently I have to add the braces manually which is a bit cumbersome.
One further improvement can be to detect if macro accepts no parameters and place the cursor differently for this case.

* Add an exclamation mark to the macro completion label

This helps to distinguish macros from other completion items and also allows to show only macros in completion if you type `!`:
<img width="722" alt="image" src="https://user-images.githubusercontent.com/2690773/64736987-8b8ea180-d4f4-11e9-8355-2ce4f83b7aa8.png">

<img width="732" alt="image" src="https://user-images.githubusercontent.com/2690773/64737214-ffc94500-d4f4-11e9-946e-1ba2db1c7fb1.png">


Additionally, automatic formatting hooks had adjusted two `help.rs` files, I've added them as a last commit to the PR even though they are not really related.

Co-authored-by: Kirill Bulatov <mail4score@gmail.com>
This commit is contained in:
bors[bot] 2019-09-12 10:45:47 +00:00 committed by GitHub
commit 561e7aea5b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 110 additions and 73 deletions

View file

@ -37,13 +37,41 @@ mod tests {
), ),
@r##"[ @r##"[
CompletionItem { CompletionItem {
label: "foo", label: "foo!",
source_range: [46; 46), source_range: [46; 46),
delete: [46; 46), delete: [46; 46),
insert: "foo!", insert: "foo!($0)",
kind: Macro, kind: Macro,
detail: "macro_rules! foo", detail: "macro_rules! foo",
}, },
]"##
);
}
#[test]
fn completes_vec_macros_with_square_brackets() {
assert_debug_snapshot!(
do_reference_completion(
"
//- /main.rs
macro_rules! vec {
() => {}
}
fn foo() {}
<|>
"
),
@r##"[
CompletionItem {
label: "vec!",
source_range: [46; 46),
delete: [46; 46),
insert: "vec![$0]",
kind: Macro,
detail: "macro_rules! vec",
},
]"## ]"##
); );
} }

View file

@ -605,10 +605,10 @@ mod tests {
), ),
@r###"[ @r###"[
CompletionItem { CompletionItem {
label: "foo", label: "foo!",
source_range: [179; 179), source_range: [179; 179),
delete: [179; 179), delete: [179; 179),
insert: "foo!", insert: "foo!($0)",
kind: Macro, kind: Macro,
detail: "#[macro_export]\nmacro_rules! foo", detail: "#[macro_export]\nmacro_rules! foo",
}, },

View file

@ -568,26 +568,26 @@ mod tests {
), ),
@r##"[ @r##"[
CompletionItem { CompletionItem {
label: "bar", label: "bar!",
source_range: [252; 252), source_range: [252; 252),
delete: [252; 252), delete: [252; 252),
insert: "bar!", insert: "bar!($0)",
kind: Macro, kind: Macro,
detail: "macro_rules! bar", detail: "macro_rules! bar",
}, },
CompletionItem { CompletionItem {
label: "baz", label: "baz!",
source_range: [252; 252), source_range: [252; 252),
delete: [252; 252), delete: [252; 252),
insert: "baz!", insert: "baz!($0)",
kind: Macro, kind: Macro,
detail: "#[macro_export]\nmacro_rules! baz", detail: "#[macro_export]\nmacro_rules! baz",
}, },
CompletionItem { CompletionItem {
label: "foo", label: "foo!",
source_range: [252; 252), source_range: [252; 252),
delete: [252; 252), delete: [252; 252),
insert: "foo!", insert: "foo!($0)",
kind: Macro, kind: Macro,
detail: "macro_rules! foo", detail: "macro_rules! foo",
}, },
@ -633,14 +633,6 @@ mod tests {
" "
), ),
@r##"[ @r##"[
CompletionItem {
label: "foo",
source_range: [49; 49),
delete: [49; 49),
insert: "foo!",
kind: Macro,
detail: "macro_rules! foo",
},
CompletionItem { CompletionItem {
label: "foo", label: "foo",
source_range: [49; 49), source_range: [49; 49),
@ -649,6 +641,14 @@ mod tests {
kind: Function, kind: Function,
detail: "fn foo()", detail: "fn foo()",
}, },
CompletionItem {
label: "foo!",
source_range: [49; 49),
delete: [49; 49),
insert: "foo!($0)",
kind: Macro,
detail: "macro_rules! foo",
},
]"## ]"##
); );
} }
@ -670,10 +670,10 @@ mod tests {
), ),
@r##"[ @r##"[
CompletionItem { CompletionItem {
label: "foo", label: "foo!",
source_range: [57; 57), source_range: [57; 57),
delete: [57; 57), delete: [57; 57),
insert: "foo!", insert: "foo!($0)",
kind: Macro, kind: Macro,
detail: "macro_rules! foo", detail: "macro_rules! foo",
}, },
@ -706,10 +706,10 @@ mod tests {
), ),
@r##"[ @r##"[
CompletionItem { CompletionItem {
label: "foo", label: "foo!",
source_range: [50; 50), source_range: [50; 50),
delete: [50; 50), delete: [50; 50),
insert: "foo!", insert: "foo!($0)",
kind: Macro, kind: Macro,
detail: "macro_rules! foo", detail: "macro_rules! foo",
}, },

View file

@ -109,12 +109,21 @@ impl Completions {
if let Some(name) = name { if let Some(name) = name {
let detail = macro_label(&ast_node); let detail = macro_label(&ast_node);
let builder = let macro_braces_to_insert = match name.as_str() {
CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.clone()) "vec" => "[$0]",
.kind(CompletionItemKind::Macro) _ => "($0)",
.set_documentation(macro_.docs(ctx.db)) };
.detail(detail) let macro_declaration = name + "!";
.insert_snippet(format!("{}!", name));
let builder = CompletionItem::new(
CompletionKind::Reference,
ctx.source_range(),
&macro_declaration,
)
.kind(CompletionItemKind::Macro)
.set_documentation(macro_.docs(ctx.db))
.detail(detail)
.insert_snippet(macro_declaration + macro_braces_to_insert);
self.add(builder); self.add(builder);
} }

View file

@ -1,45 +1,45 @@
pub const GLOBAL_HELP: &str = "tasks pub const GLOBAL_HELP: &str = "tasks
USAGE: USAGE:
ra_tools <SUBCOMMAND> ra_tools <SUBCOMMAND>
FLAGS: FLAGS:
-h, --help Prints help information -h, --help Prints help information
SUBCOMMANDS: SUBCOMMANDS:
format format
format-hook format-hook
fuzz-tests fuzz-tests
gen-syntax gen-syntax
gen-tests gen-tests
install-ra install-ra
lint"; lint";
pub const INSTALL_RA_HELP: &str = "ra_tools-install-ra pub const INSTALL_RA_HELP: &str = "ra_tools-install-ra
USAGE: USAGE:
ra_tools.exe install-ra [FLAGS] ra_tools.exe install-ra [FLAGS]
FLAGS: FLAGS:
--client-code --client-code
-h, --help Prints help information -h, --help Prints help information
--jemalloc --jemalloc
--server"; --server";
pub fn print_no_param_subcommand_help(subcommand: &str) { pub fn print_no_param_subcommand_help(subcommand: &str) {
eprintln!( eprintln!(
"ra_tools-{} "ra_tools-{}
USAGE: USAGE:
ra_tools {} ra_tools {}
FLAGS: FLAGS:
-h, --help Prints help information", -h, --help Prints help information",
subcommand, subcommand subcommand, subcommand
); );
} }
pub const INSTALL_RA_CONFLICT: &str = pub const INSTALL_RA_CONFLICT: &str =
"error: The argument `--server` cannot be used with `--client-code` "error: The argument `--server` cannot be used with `--client-code`
For more information try --help"; For more information try --help";