Merge remote-tracking branch 'upstream/master' into sync-from-rust

This commit is contained in:
Laurențiu Nicola 2024-02-02 08:40:28 +02:00
commit 4f09335063
185 changed files with 2615 additions and 1781 deletions

View file

@ -120,7 +120,7 @@ pub struct TreeDiff {
impl TreeDiff {
pub fn into_text_edit(&self, builder: &mut TextEditBuilder) {
let _p = profile::span("into_text_edit");
let _p = tracing::span!(tracing::Level::INFO, "into_text_edit").entered();
for (anchor, to) in &self.insertions {
let offset = match anchor {
@ -149,7 +149,7 @@ impl TreeDiff {
///
/// This function tries to find a fine-grained diff.
pub fn diff(from: &SyntaxNode, to: &SyntaxNode) -> TreeDiff {
let _p = profile::span("diff");
let _p = tracing::span!(tracing::Level::INFO, "diff").entered();
let mut diff = TreeDiff {
replacements: FxHashMap::default(),

View file

@ -538,9 +538,13 @@ impl ast::UseTree {
/// `foo::bar` -> `{foo::bar}`
///
/// `{foo::bar}` -> `{foo::bar}`
pub fn wrap_in_tree_list(&self) {
if self.path().is_none() {
return;
pub fn wrap_in_tree_list(&self) -> Option<()> {
if self.use_tree_list().is_some()
&& self.path().is_none()
&& self.star_token().is_none()
&& self.rename().is_none()
{
return None;
}
let subtree = self.clone_subtree().clone_for_update();
ted::remove_all_iter(self.syntax().children_with_tokens());
@ -548,6 +552,7 @@ impl ast::UseTree {
self.syntax(),
make::use_tree_list(once(subtree)).clone_for_update().syntax(),
);
Some(())
}
}
@ -960,10 +965,10 @@ impl ast::IdentPat {
}
pub trait HasVisibilityEdit: ast::HasVisibility {
fn set_visibility(&self, visbility: ast::Visibility) {
fn set_visibility(&self, visibility: ast::Visibility) {
match self.visibility() {
Some(current_visibility) => {
ted::replace(current_visibility.syntax(), visbility.syntax())
ted::replace(current_visibility.syntax(), visibility.syntax())
}
None => {
let vis_before = self
@ -972,7 +977,7 @@ pub trait HasVisibilityEdit: ast::HasVisibility {
.find(|it| !matches!(it.kind(), WHITESPACE | COMMENT | ATTR))
.unwrap_or_else(|| self.syntax().first_child_or_token().unwrap());
ted::insert(ted::Position::before(vis_before), visbility.syntax());
ted::insert(ted::Position::before(vis_before), visibility.syntax());
}
}
}

View file

@ -9,10 +9,11 @@
//! API should require to assemble every node piecewise. The trick of
//! `parse(format!())` we use internally is an implementation detail -- long
//! term, it will be replaced with direct tree manipulation.
use itertools::Itertools;
use parser::T;
use rowan::NodeOrToken;
use stdx::{format_to, never};
use stdx::{format_to, format_to_acc, never};
use crate::{ast, utils::is_raw_identifier, AstNode, SourceFile, SyntaxKind, SyntaxToken};
@ -759,15 +760,12 @@ pub fn match_arm_with_guard(
}
pub fn match_arm_list(arms: impl IntoIterator<Item = ast::MatchArm>) -> ast::MatchArmList {
let arms_str = arms
.into_iter()
.map(|arm| {
let needs_comma = arm.expr().map_or(true, |it| !it.is_block_like());
let comma = if needs_comma { "," } else { "" };
let arm = arm.syntax();
format!(" {arm}{comma}\n")
})
.collect::<String>();
let arms_str = arms.into_iter().fold(String::new(), |mut acc, arm| {
let needs_comma = arm.expr().map_or(true, |it| !it.is_block_like());
let comma = if needs_comma { "," } else { "" };
let arm = arm.syntax();
format_to_acc!(acc, " {arm}{comma}\n")
});
return from_text(&arms_str);
fn from_text(text: &str) -> ast::MatchArmList {

View file

@ -384,7 +384,7 @@ impl ast::UseTreeList {
// the below remove the innermost {}, got `use crate::{{{A}}}`
remove_brace_in_use_tree_list(&self);
// the below remove othe unnecessary {}, got `use crate::A`
// the below remove other unnecessary {}, got `use crate::A`
while let Some(parent_use_tree_list) = self.parent_use_tree().parent_use_tree_list() {
remove_brace_in_use_tree_list(&parent_use_tree_list);
self = parent_use_tree_list;

View file

@ -11,6 +11,7 @@ use std::{
use ast::HasName;
use expect_test::expect_file;
use rayon::prelude::*;
use stdx::format_to_acc;
use test_utils::{bench, bench_fixture, project_root};
use crate::{ast, fuzz, AstNode, SourceFile, SyntaxError};
@ -104,10 +105,9 @@ fn self_hosting_parsing() {
.collect::<Vec<_>>();
if !errors.is_empty() {
let errors = errors
.into_iter()
.map(|(path, err)| format!("{}: {:?}\n", path.display(), err[0]))
.collect::<String>();
let errors = errors.into_iter().fold(String::new(), |mut acc, (path, err)| {
format_to_acc!(acc, "{}: {:?}\n", path.display(), err[0])
});
panic!("Parsing errors:\n{errors}\n");
}
}