4406: Update cargo-metadata r=matklad a=edwin0cheng

This PR update `cargo-metadata` to  0.10.0 and it also relax the` serde-derive` deps to 1.0 for tests in `proc-macro-srv`.

cc @robojumper 

r= @matklad  , I think you would have something to say related to https://github.com/serde-rs/json/issues/647#issue-593788429 ?





4410: Improve wording in comment r=matklad a=edwin0cheng



4411: do not remove then block when you unwrap else block #4361 r=matklad a=bnjjj

close #4361 

4417: Omit default types in HirDisplay SourceCode mode r=matklad a=TimoFreiberg

Closes #4390

Co-authored-by: Edwin Cheng <edwin0cheng@gmail.com>
Co-authored-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com>
Co-authored-by: Timo Freiberg <timo.freiberg@gmail.com>
This commit is contained in:
bors[bot] 2020-05-10 17:37:07 +00:00 committed by GitHub
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 285 additions and 97 deletions

12
Cargo.lock generated
View file

@ -89,9 +89,9 @@ dependencies = [
[[package]]
name = "cargo_metadata"
version = "0.9.1"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "46e3374c604fb39d1a2f35ed5e4a4e30e60d01fab49446e08f1b3e9a90aef202"
checksum = "b8de60b887edf6d74370fc8eb177040da4847d971d6234c7b13a6da324ef0caf"
dependencies = [
"semver",
"serde",
@ -1514,18 +1514,18 @@ checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
[[package]]
name = "serde"
version = "1.0.106"
version = "1.0.107"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "36df6ac6412072f67cf767ebbde4133a5b2e88e76dc6187fa7104cd16f783399"
checksum = "eba7550f2cdf88ffc23ab0f1607133486c390a8c0f89b57e589b9654ee15e04d"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.106"
version = "1.0.107"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e549e3abf4fb8621bd1609f11dfc9f5e50320802273b12f3811a67e6716ea6c"
checksum = "10be45e22e5597d4b88afcc71f9d7bfadcd604bf0c78a3ab4582b8d2b37f39f3"
dependencies = [
"proc-macro2",
"quote",

View file

@ -209,7 +209,7 @@ struct Test<K, T = u8> {
}
fn main() {
let test<|>: Test<i32, u8> = Test { t: 23, k: 33 };
let test<|>: Test<i32> = Test { t: 23, k: 33 };
}"#,
);
}

View file

@ -1,6 +1,6 @@
use crate::{AssistContext, AssistId, Assists};
use ast::LoopBodyOwner;
use ast::{ElseBranch, Expr, LoopBodyOwner};
use ra_fmt::unwrap_trivial_block;
use ra_syntax::{ast, match_ast, AstNode, TextRange, T};
@ -25,19 +25,11 @@ pub(crate) fn unwrap_block(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
let l_curly_token = ctx.find_token_at_offset(T!['{'])?;
let block = ast::BlockExpr::cast(l_curly_token.parent())?;
let parent = block.syntax().parent()?;
let assist_id = AssistId("unwrap_block");
let assist_label = "Unwrap block";
let (expr, expr_to_unwrap) = match_ast! {
match parent {
ast::IfExpr(if_expr) => {
let expr_to_unwrap = if_expr.blocks().find_map(|expr| extract_expr(ctx.frange.range, expr));
let expr_to_unwrap = expr_to_unwrap?;
// Find if we are in a else if block
let ancestor = if_expr.syntax().parent().and_then(ast::IfExpr::cast);
match ancestor {
None => (ast::Expr::IfExpr(if_expr), expr_to_unwrap),
Some(ancestor) => (ast::Expr::IfExpr(ancestor), expr_to_unwrap),
}
},
ast::ForExpr(for_expr) => {
let block_expr = for_expr.loop_body()?;
let expr_to_unwrap = extract_expr(ctx.frange.range, block_expr)?;
@ -53,27 +45,62 @@ pub(crate) fn unwrap_block(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
let expr_to_unwrap = extract_expr(ctx.frange.range, block_expr)?;
(ast::Expr::LoopExpr(loop_expr), expr_to_unwrap)
},
ast::IfExpr(if_expr) => {
let mut resp = None;
let then_branch = if_expr.then_branch()?;
if then_branch.l_curly_token()?.text_range().contains_range(ctx.frange.range) {
if let Some(ancestor) = if_expr.syntax().parent().and_then(ast::IfExpr::cast) {
// For `else if` blocks
let ancestor_then_branch = ancestor.then_branch()?;
let l_curly_token = then_branch.l_curly_token()?;
let target = then_branch.syntax().text_range();
return acc.add(assist_id, assist_label, target, |edit| {
let range_to_del_else_if = TextRange::new(ancestor_then_branch.syntax().text_range().end(), l_curly_token.text_range().start());
let range_to_del_rest = TextRange::new(then_branch.syntax().text_range().end(), if_expr.syntax().text_range().end());
edit.set_cursor(ancestor_then_branch.syntax().text_range().end());
edit.delete(range_to_del_rest);
edit.delete(range_to_del_else_if);
edit.replace(target, update_expr_string(then_branch.to_string(), &[' ', '{']));
});
} else {
resp = Some((ast::Expr::IfExpr(if_expr.clone()), Expr::BlockExpr(then_branch)));
}
} else if let Some(else_branch) = if_expr.else_branch() {
match else_branch {
ElseBranch::Block(else_block) => {
let l_curly_token = else_block.l_curly_token()?;
if l_curly_token.text_range().contains_range(ctx.frange.range) {
let target = else_block.syntax().text_range();
return acc.add(assist_id, assist_label, target, |edit| {
let range_to_del = TextRange::new(then_branch.syntax().text_range().end(), l_curly_token.text_range().start());
edit.set_cursor(then_branch.syntax().text_range().end());
edit.delete(range_to_del);
edit.replace(target, update_expr_string(else_block.to_string(), &[' ', '{']));
});
}
},
ElseBranch::IfExpr(_) => {},
}
}
resp?
},
_ => return None,
}
};
let target = expr_to_unwrap.syntax().text_range();
acc.add(AssistId("unwrap_block"), "Unwrap block", target, |edit| {
acc.add(assist_id, assist_label, target, |edit| {
edit.set_cursor(expr.syntax().text_range().start());
let pat_start: &[_] = &[' ', '{', '\n'];
let expr_to_unwrap = expr_to_unwrap.to_string();
let expr_string = expr_to_unwrap.trim_start_matches(pat_start);
let mut expr_string_lines: Vec<&str> = expr_string.lines().collect();
expr_string_lines.pop(); // Delete last line
let expr_string = expr_string_lines
.into_iter()
.map(|line| line.replacen(" ", "", 1)) // Delete indentation
.collect::<Vec<String>>()
.join("\n");
edit.replace(expr.syntax().text_range(), expr_string);
edit.replace(
expr.syntax().text_range(),
update_expr_string(expr_to_unwrap.to_string(), &[' ', '{', '\n']),
);
})
}
@ -87,6 +114,18 @@ fn extract_expr(cursor_range: TextRange, block: ast::BlockExpr) -> Option<ast::E
}
}
fn update_expr_string(expr_str: String, trim_start_pat: &[char]) -> String {
let expr_string = expr_str.trim_start_matches(trim_start_pat);
let mut expr_string_lines: Vec<&str> = expr_string.lines().collect();
expr_string_lines.pop(); // Delete last line
expr_string_lines
.into_iter()
.map(|line| line.replacen(" ", "", 1)) // Delete indentation
.collect::<Vec<String>>()
.join("\n")
}
#[cfg(test)]
mod tests {
use crate::tests::{check_assist, check_assist_not_applicable};
@ -142,7 +181,13 @@ mod tests {
r#"
fn main() {
bar();
<|>println!("bar");
if true {
foo();
//comment
bar();
}<|>
println!("bar");
}
"#,
);
@ -170,7 +215,127 @@ mod tests {
r#"
fn main() {
//bar();
<|>println!("bar");
if true {
println!("true");
//comment
//bar();
}<|>
println!("bar");
}
"#,
);
}
#[test]
fn simple_if_else_if_nested() {
check_assist(
unwrap_block,
r#"
fn main() {
//bar();
if true {
println!("true");
//comment
//bar();
} else if false {
println!("bar");
} else if true {<|>
println!("foo");
}
}
"#,
r#"
fn main() {
//bar();
if true {
println!("true");
//comment
//bar();
} else if false {
println!("bar");
}<|>
println!("foo");
}
"#,
);
}
#[test]
fn simple_if_else_if_nested_else() {
check_assist(
unwrap_block,
r#"
fn main() {
//bar();
if true {
println!("true");
//comment
//bar();
} else if false {
println!("bar");
} else if true {
println!("foo");
} else {<|>
println!("else");
}
}
"#,
r#"
fn main() {
//bar();
if true {
println!("true");
//comment
//bar();
} else if false {
println!("bar");
} else if true {
println!("foo");
}<|>
println!("else");
}
"#,
);
}
#[test]
fn simple_if_else_if_nested_middle() {
check_assist(
unwrap_block,
r#"
fn main() {
//bar();
if true {
println!("true");
//comment
//bar();
} else if false {
println!("bar");
} else if true {<|>
println!("foo");
} else {
println!("else");
}
}
"#,
r#"
fn main() {
//bar();
if true {
println!("true");
//comment
//bar();
} else if false {
println!("bar");
}<|>
println!("foo");
}
"#,
);

View file

@ -11,7 +11,7 @@ doctest = false
crossbeam-channel = "0.4.0"
lsp-types = { version = "0.74.0", features = ["proposed"] }
log = "0.4.8"
cargo_metadata = "0.9.1"
cargo_metadata = "0.10.0"
serde_json = "1.0.48"
jod-thread = "0.1.1"
ra_toolchain = { path = "../ra_toolchain" }

View file

@ -204,6 +204,8 @@ impl FlycheckThread {
}
CheckEvent::Msg(Message::BuildScriptExecuted(_msg)) => {}
CheckEvent::Msg(Message::BuildFinished(_)) => {}
CheckEvent::Msg(Message::TextLine(_)) => {}
CheckEvent::Msg(Message::Unknown) => {}
}
}

View file

@ -136,6 +136,12 @@ enum DisplayTarget {
SourceCode { module_id: ModuleId },
}
impl DisplayTarget {
fn is_source_code(&self) -> bool {
matches!(self, Self::SourceCode {..})
}
}
#[derive(Debug)]
pub enum DisplaySourceCodeError {
PathNotFound,
@ -303,37 +309,40 @@ impl HirDisplay for ApplicationTy {
if self.parameters.len() > 0 {
let mut non_default_parameters = Vec::with_capacity(self.parameters.len());
let parameters_to_write = if f.omit_verbose_types() {
match self
.ctor
.as_generic_def()
.map(|generic_def_id| f.db.generic_defaults(generic_def_id))
.filter(|defaults| !defaults.is_empty())
{
None => self.parameters.0.as_ref(),
Some(default_parameters) => {
for (i, parameter) in self.parameters.iter().enumerate() {
match (parameter, default_parameters.get(i)) {
(&Ty::Unknown, _) | (_, None) => {
non_default_parameters.push(parameter.clone())
let parameters_to_write =
if f.display_target.is_source_code() || f.omit_verbose_types() {
match self
.ctor
.as_generic_def()
.map(|generic_def_id| f.db.generic_defaults(generic_def_id))
.filter(|defaults| !defaults.is_empty())
{
None => self.parameters.0.as_ref(),
Some(default_parameters) => {
for (i, parameter) in self.parameters.iter().enumerate() {
match (parameter, default_parameters.get(i)) {
(&Ty::Unknown, _) | (_, None) => {
non_default_parameters.push(parameter.clone())
}
(_, Some(default_parameter))
if parameter != default_parameter =>
{
non_default_parameters.push(parameter.clone())
}
_ => (),
}
(_, Some(default_parameter))
if parameter != default_parameter =>
{
non_default_parameters.push(parameter.clone())
}
_ => (),
}
&non_default_parameters
}
&non_default_parameters
}
}
} else {
self.parameters.0.as_ref()
};
write!(f, "<")?;
f.write_joined(parameters_to_write, ", ")?;
write!(f, ">")?;
} else {
self.parameters.0.as_ref()
};
if !parameters_to_write.is_empty() {
write!(f, "<")?;
f.write_joined(parameters_to_write, ", ")?;
write!(f, ">")?;
}
}
}
TypeCtor::AssociatedType(type_alias) => {

View file

@ -21,3 +21,30 @@ fn bar() {
);
assert_eq!("foo::Foo", displayed_source_at_pos(&db, pos));
}
#[test]
fn omit_default_type_parameters() {
let (db, pos) = TestDB::with_position(
r"
//- /main.rs
struct Foo<T = u8> { t: T }
fn main() {
let foo = Foo { t: 5 };
foo<|>;
}
",
);
assert_eq!("Foo", displayed_source_at_pos(&db, pos));
let (db, pos) = TestDB::with_position(
r"
//- /main.rs
struct Foo<K, T = u8> { k: K, t: T }
fn main() {
let foo = Foo { k: 400, t: 5 };
foo<|>;
}
",
);
assert_eq!("Foo<i32>", displayed_source_at_pos(&db, pos));
}

View file

@ -18,7 +18,7 @@ memmap = "0.7"
test_utils = { path = "../test_utils" }
[dev-dependencies]
cargo_metadata = "0.9.1"
cargo_metadata = "0.10.0"
difference = "2.0.0"
# used as proc macro test target
serde_derive = "=1.0.106"
serde_derive = "1.0.106"

View file

@ -20,26 +20,15 @@ SUBTREE $
PUNCH = [alone] 4294967295
SUBTREE {} 4294967295
PUNCH # [alone] 4294967295
SUBTREE [] 4294967295
IDENT allow 4294967295
SUBTREE () 4294967295
IDENT unknown_lints 4294967295
PUNCH # [alone] 4294967295
SUBTREE [] 4294967295
IDENT cfg_attr 4294967295
SUBTREE () 4294967295
IDENT feature 4294967295
PUNCH = [alone] 4294967295
LITERAL "cargo-clippy" 0
PUNCH , [alone] 4294967295
IDENT allow 4294967295
SUBTREE () 4294967295
IDENT useless_attribute 4294967295
PUNCH # [alone] 4294967295
SUBTREE [] 4294967295
IDENT allow 4294967295
SUBTREE () 4294967295
IDENT rust_2018_idioms 4294967295
PUNCH , [alone] 4294967295
IDENT clippy 4294967295
PUNCH : [joint] 4294967295
PUNCH : [alone] 4294967295
IDENT useless_attribute 4294967295
IDENT extern 4294967295
IDENT crate 4294967295
IDENT serde 4294967295

View file

@ -10,7 +10,7 @@ fn test_derive_serialize_proc_macro() {
assert_expand(
"serde_derive",
"Serialize",
"1.0.106",
"1.0",
r##"struct Foo {}"##,
include_str!("fixtures/test_serialize_proc_macro.txt"),
);
@ -21,7 +21,7 @@ fn test_derive_serialize_proc_macro_failed() {
assert_expand(
"serde_derive",
"Serialize",
"1.0.106",
"1.0",
r##"
struct {}
"##,
@ -37,7 +37,7 @@ SUBTREE $
#[test]
fn test_derive_proc_macro_list() {
let res = list("serde_derive", "1.0.106").join("\n");
let res = list("serde_derive", "1.0").join("\n");
assert_eq_text!(
&res,

View file

@ -8,7 +8,7 @@ use std::str::FromStr;
use test_utils::assert_eq_text;
mod fixtures {
use cargo_metadata::{parse_messages, Message};
use cargo_metadata::Message;
use std::process::Command;
// Use current project metadata to get the proc-macro dylib path
@ -19,7 +19,7 @@ mod fixtures {
.unwrap()
.stdout;
for message in parse_messages(command.as_slice()) {
for message in Message::parse_stream(command.as_slice()) {
match message.unwrap() {
Message::CompilerArtifact(artifact) => {
if artifact.target.kind.contains(&"proc-macro".to_string()) {

View file

@ -11,7 +11,7 @@ doctest = false
log = "0.4.8"
rustc-hash = "1.1.0"
cargo_metadata = "0.9.1"
cargo_metadata = "0.10.0"
ra_arena = { path = "../ra_arena" }
ra_cfg = { path = "../ra_cfg" }

View file

@ -161,7 +161,7 @@ impl CargoWorkspace {
meta.current_dir(parent);
}
if let Some(target) = cargo_features.target.as_ref() {
meta.other_options(&[String::from("--filter-platform"), target.clone()]);
meta.other_options(vec![String::from("--filter-platform"), target.clone()]);
}
let meta = meta.exec().with_context(|| {
format!("Failed to run `cargo metadata --manifest-path {}`", cargo_toml.display())
@ -304,19 +304,13 @@ pub fn load_extern_resources(
let mut res = ExternResources::default();
for message in cargo_metadata::parse_messages(output.stdout.as_slice()) {
for message in cargo_metadata::Message::parse_stream(output.stdout.as_slice()) {
if let Ok(message) = message {
match message {
Message::BuildScriptExecuted(BuildScript { package_id, out_dir, cfgs, .. }) => {
res.out_dirs.insert(package_id.clone(), out_dir);
res.cfgs.insert(
package_id,
// FIXME: Current `cargo_metadata` uses `PathBuf` instead of `String`,
// change when https://github.com/oli-obk/cargo_metadata/pulls/112 reaches crates.io
cfgs.iter().filter_map(|c| c.to_str().map(|s| s.to_owned())).collect(),
);
res.cfgs.insert(package_id, cfgs);
}
Message::CompilerArtifact(message) => {
if message.target.kind.contains(&"proc-macro".to_string()) {
let package_id = message.package_id;
@ -329,6 +323,8 @@ pub fn load_extern_resources(
}
Message::CompilerMessage(_) => (),
Message::Unknown => (),
Message::BuildFinished(_) => {}
Message::TextLine(_) => {}
}
}
}

View file

@ -225,7 +225,7 @@ class AstInspector implements vscode.HoverProvider, vscode.DefinitionProvider, D
return doc.positionAt(targetOffset);
}
// Shitty workaround for crlf line endings
// Dirty workaround for crlf line endings
// We are still in this prehistoric era of carriage returns here...
let line = 0;