11395: fix: Fix and re-enable format string completions r=Veykril a=Veykril

bors r+

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
bors[bot] 2022-02-02 11:20:38 +00:00 committed by GitHub
commit d20ff92747
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 75 additions and 56 deletions

View file

@ -62,6 +62,7 @@ pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext)
Some(()) Some(())
} }
// FIXME?: Move this functionality to (un)qualified_path, make this module work solely for builtin/known attributes for their inputs?
fn complete_new_attribute(acc: &mut Completions, ctx: &CompletionContext, attribute: &ast::Attr) { fn complete_new_attribute(acc: &mut Completions, ctx: &CompletionContext, attribute: &ast::Attr) {
let is_inner = attribute.kind() == ast::AttrKind::Inner; let is_inner = attribute.kind() == ast::AttrKind::Inner;
let attribute_annotated_item_kind = let attribute_annotated_item_kind =

View file

@ -8,15 +8,14 @@ use crate::{context::CompletionContext, CompletionItem, CompletionItemKind, Comp
/// Complete identifiers in format strings. /// Complete identifiers in format strings.
pub(crate) fn format_string(acc: &mut Completions, ctx: &CompletionContext) { pub(crate) fn format_string(acc: &mut Completions, ctx: &CompletionContext) {
if true { let string = match ast::String::cast(ctx.token.clone())
return; .zip(ast::String::cast(ctx.original_token.clone()))
} {
let string = match ast::String::cast(ctx.token.clone()) { Some((expanded, original)) if is_format_string(&expanded) => original,
Some(it) if is_format_string(&it) => it,
_ => return, _ => return,
}; };
let cursor = ctx.position.offset; let cursor = ctx.position.offset;
let lit_start = ctx.token.text_range().start(); let lit_start = ctx.original_token.text_range().start();
let cursor_in_lit = cursor - lit_start; let cursor_in_lit = cursor - lit_start;
let prefix = &string.text()[..cursor_in_lit.into()]; let prefix = &string.text()[..cursor_in_lit.into()];
@ -39,13 +38,32 @@ pub(crate) fn format_string(acc: &mut Completions, ctx: &CompletionContext) {
mod tests { mod tests {
use expect_test::{expect, Expect}; use expect_test::{expect, Expect};
use crate::tests::completion_list_no_kw; use crate::tests::{check_edit, completion_list_no_kw};
fn check(ra_fixture: &str, expect: Expect) { fn check(ra_fixture: &str, expect: Expect) {
let actual = completion_list_no_kw(ra_fixture); let actual = completion_list_no_kw(ra_fixture);
expect.assert_eq(&actual); expect.assert_eq(&actual);
} }
#[test]
fn works_when_wrapped() {
check(
r#"
macro_rules! format_args {
($lit:literal $(tt:tt)*) => { 0 },
}
macro_rules! print {
($($arg:tt)*) => (std::io::_print(format_args!($($arg)*)));
}
fn main() {
let foobar = 1;
print!("f$0");
}
"#,
expect![[]],
);
}
#[test] #[test]
fn no_completion_without_brace() { fn no_completion_without_brace() {
check( check(
@ -62,49 +80,49 @@ format_args!("f$0");
); );
} }
// #[test] #[test]
// fn completes_locals() { fn completes_locals() {
// check_edit( check_edit(
// "foobar", "foobar",
// r#" r#"
// macro_rules! format_args { macro_rules! format_args {
// ($lit:literal $(tt:tt)*) => { 0 }, ($lit:literal $(tt:tt)*) => { 0 },
// } }
// fn main() { fn main() {
// let foobar = 1; let foobar = 1;
// format_args!("{f$0"); format_args!("{f$0");
// } }
// "#, "#,
// r#" r#"
// macro_rules! format_args { macro_rules! format_args {
// ($lit:literal $(tt:tt)*) => { 0 }, ($lit:literal $(tt:tt)*) => { 0 },
// } }
// fn main() { fn main() {
// let foobar = 1; let foobar = 1;
// format_args!("{foobar"); format_args!("{foobar");
// } }
// "#, "#,
// ); );
// check_edit( check_edit(
// "foobar", "foobar",
// r#" r#"
// macro_rules! format_args { macro_rules! format_args {
// ($lit:literal $(tt:tt)*) => { 0 }, ($lit:literal $(tt:tt)*) => { 0 },
// } }
// fn main() { fn main() {
// let foobar = 1; let foobar = 1;
// format_args!("{$0"); format_args!("{$0");
// } }
// "#, "#,
// r#" r#"
// macro_rules! format_args { macro_rules! format_args {
// ($lit:literal $(tt:tt)*) => { 0 }, ($lit:literal $(tt:tt)*) => { 0 },
// } }
// fn main() { fn main() {
// let foobar = 1; let foobar = 1;
// format_args!("{foobar"); format_args!("{foobar");
// } }
// "#, "#,
// ); );
// } }
} }