mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 12:29:21 +00:00
Merge commit 'ddf105b646
' into sync-from-ra
This commit is contained in:
parent
0816d49d83
commit
e41ab350d6
378 changed files with 14720 additions and 3111 deletions
|
@ -627,6 +627,8 @@ impl ast::Impl {
|
|||
}
|
||||
|
||||
impl ast::AssocItemList {
|
||||
/// Adds a new associated item after all of the existing associated items.
|
||||
///
|
||||
/// Attention! This function does align the first line of `item` with respect to `self`,
|
||||
/// but it does _not_ change indentation of other lines (if any).
|
||||
pub fn add_item(&self, item: ast::AssocItem) {
|
||||
|
@ -650,6 +652,46 @@ impl ast::AssocItemList {
|
|||
];
|
||||
ted::insert_all(position, elements);
|
||||
}
|
||||
|
||||
/// Adds a new associated item at the start of the associated item list.
|
||||
///
|
||||
/// Attention! This function does align the first line of `item` with respect to `self`,
|
||||
/// but it does _not_ change indentation of other lines (if any).
|
||||
pub fn add_item_at_start(&self, item: ast::AssocItem) {
|
||||
match self.assoc_items().next() {
|
||||
Some(first_item) => {
|
||||
let indent = IndentLevel::from_node(first_item.syntax());
|
||||
let before = Position::before(first_item.syntax());
|
||||
|
||||
ted::insert_all(
|
||||
before,
|
||||
vec![
|
||||
item.syntax().clone().into(),
|
||||
make::tokens::whitespace(&format!("\n\n{indent}")).into(),
|
||||
],
|
||||
)
|
||||
}
|
||||
None => {
|
||||
let (indent, position, whitespace) = match self.l_curly_token() {
|
||||
Some(l_curly) => {
|
||||
normalize_ws_between_braces(self.syntax());
|
||||
(IndentLevel::from_token(&l_curly) + 1, Position::after(&l_curly), "\n")
|
||||
}
|
||||
None => (IndentLevel::single(), Position::first_child_of(self.syntax()), ""),
|
||||
};
|
||||
|
||||
let mut elements = vec![];
|
||||
|
||||
// Avoid pushing an empty whitespace token
|
||||
if !indent.is_zero() || !whitespace.is_empty() {
|
||||
elements.push(make::tokens::whitespace(&format!("{whitespace}{indent}")).into())
|
||||
}
|
||||
elements.push(item.syntax().clone().into());
|
||||
|
||||
ted::insert_all(position, elements)
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl ast::Fn {
|
||||
|
|
|
@ -68,6 +68,9 @@ pub mod ext {
|
|||
pub fn expr_ty_new(ty: &ast::Type) -> ast::Expr {
|
||||
expr_from_text(&format!("{ty}::new()"))
|
||||
}
|
||||
pub fn expr_self() -> ast::Expr {
|
||||
expr_from_text("self")
|
||||
}
|
||||
|
||||
pub fn zero_number() -> ast::Expr {
|
||||
expr_from_text("0")
|
||||
|
@ -236,24 +239,21 @@ fn merge_where_clause(
|
|||
|
||||
pub fn impl_(
|
||||
generic_params: Option<ast::GenericParamList>,
|
||||
generic_args: Option<ast::GenericParamList>,
|
||||
generic_args: Option<ast::GenericArgList>,
|
||||
path_type: ast::Type,
|
||||
where_clause: Option<ast::WhereClause>,
|
||||
body: Option<Vec<either::Either<ast::Attr, ast::AssocItem>>>,
|
||||
) -> ast::Impl {
|
||||
let (gen_params, tr_gen_args) = match (generic_params, generic_args) {
|
||||
(None, None) => (String::new(), String::new()),
|
||||
(None, Some(args)) => (String::new(), args.to_generic_args().to_string()),
|
||||
(Some(params), None) => (params.to_string(), params.to_generic_args().to_string()),
|
||||
(Some(params), Some(args)) => match merge_gen_params(Some(params.clone()), Some(args)) {
|
||||
Some(merged) => (params.to_string(), merged.to_generic_args().to_string()),
|
||||
None => (params.to_string(), String::new()),
|
||||
},
|
||||
};
|
||||
let gen_args = generic_args.map_or_else(String::new, |it| it.to_string());
|
||||
|
||||
let gen_params = generic_params.map_or_else(String::new, |it| it.to_string());
|
||||
|
||||
let body_newline =
|
||||
if where_clause.is_some() && body.is_none() { "\n".to_owned() } else { String::new() };
|
||||
|
||||
let where_clause = match where_clause {
|
||||
Some(pr) => pr.to_string(),
|
||||
None => " ".to_string(),
|
||||
Some(pr) => format!("\n{pr}\n"),
|
||||
None => " ".to_owned(),
|
||||
};
|
||||
|
||||
let body = match body {
|
||||
|
@ -261,7 +261,9 @@ pub fn impl_(
|
|||
None => String::new(),
|
||||
};
|
||||
|
||||
ast_from_text(&format!("impl{gen_params} {path_type}{tr_gen_args}{where_clause}{{{}}}", body))
|
||||
ast_from_text(&format!(
|
||||
"impl{gen_params} {path_type}{gen_args}{where_clause}{{{body_newline}{body}}}"
|
||||
))
|
||||
}
|
||||
|
||||
pub fn impl_trait(
|
||||
|
@ -282,22 +284,27 @@ pub fn impl_trait(
|
|||
let trait_gen_args = trait_gen_args.map(|args| args.to_string()).unwrap_or_default();
|
||||
let type_gen_args = type_gen_args.map(|args| args.to_string()).unwrap_or_default();
|
||||
|
||||
let gen_params = match merge_gen_params(trait_gen_params, type_gen_params) {
|
||||
Some(pars) => pars.to_string(),
|
||||
None => String::new(),
|
||||
};
|
||||
let gen_params = merge_gen_params(trait_gen_params, type_gen_params)
|
||||
.map_or_else(String::new, |it| it.to_string());
|
||||
|
||||
let is_negative = if is_negative { "! " } else { "" };
|
||||
|
||||
let body_newline =
|
||||
if (ty_where_clause.is_some() || trait_where_clause.is_some()) && body.is_none() {
|
||||
"\n".to_owned()
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
|
||||
let where_clause = merge_where_clause(ty_where_clause, trait_where_clause)
|
||||
.map_or_else(|| " ".to_string(), |wc| format!("\n{}\n", wc));
|
||||
.map_or_else(|| " ".to_owned(), |wc| format!("\n{}\n", wc));
|
||||
|
||||
let body = match body {
|
||||
Some(bd) => bd.iter().map(|elem| elem.to_string()).join(""),
|
||||
None => String::new(),
|
||||
};
|
||||
|
||||
ast_from_text(&format!("{is_unsafe}impl{gen_params} {is_negative}{path_type}{trait_gen_args} for {ty}{type_gen_args}{where_clause}{{{}}}" , body))
|
||||
ast_from_text(&format!("{is_unsafe}impl{gen_params} {is_negative}{path_type}{trait_gen_args} for {ty}{type_gen_args}{where_clause}{{{body_newline}{body}}}"))
|
||||
}
|
||||
|
||||
pub fn impl_trait_type(bounds: ast::TypeBoundList) -> ast::ImplTraitType {
|
||||
|
@ -371,7 +378,7 @@ pub fn use_tree(
|
|||
alias: Option<ast::Rename>,
|
||||
add_star: bool,
|
||||
) -> ast::UseTree {
|
||||
let mut buf = "use ".to_string();
|
||||
let mut buf = "use ".to_owned();
|
||||
buf += &path.syntax().to_string();
|
||||
if let Some(use_tree_list) = use_tree_list {
|
||||
format_to!(buf, "::{use_tree_list}");
|
||||
|
@ -437,7 +444,7 @@ pub fn block_expr(
|
|||
stmts: impl IntoIterator<Item = ast::Stmt>,
|
||||
tail_expr: Option<ast::Expr>,
|
||||
) -> ast::BlockExpr {
|
||||
let mut buf = "{\n".to_string();
|
||||
let mut buf = "{\n".to_owned();
|
||||
for stmt in stmts.into_iter() {
|
||||
format_to!(buf, " {stmt}\n");
|
||||
}
|
||||
|
@ -452,7 +459,7 @@ pub fn async_move_block_expr(
|
|||
stmts: impl IntoIterator<Item = ast::Stmt>,
|
||||
tail_expr: Option<ast::Expr>,
|
||||
) -> ast::BlockExpr {
|
||||
let mut buf = "async move {\n".to_string();
|
||||
let mut buf = "async move {\n".to_owned();
|
||||
for stmt in stmts.into_iter() {
|
||||
format_to!(buf, " {stmt}\n");
|
||||
}
|
||||
|
@ -475,7 +482,7 @@ pub fn hacky_block_expr(
|
|||
elements: impl IntoIterator<Item = crate::SyntaxElement>,
|
||||
tail_expr: Option<ast::Expr>,
|
||||
) -> ast::BlockExpr {
|
||||
let mut buf = "{\n".to_string();
|
||||
let mut buf = "{\n".to_owned();
|
||||
for node_or_token in elements.into_iter() {
|
||||
match node_or_token {
|
||||
rowan::NodeOrToken::Node(n) => format_to!(buf, " {n}\n"),
|
||||
|
@ -903,7 +910,12 @@ pub fn trait_(
|
|||
ast_from_text(&text)
|
||||
}
|
||||
|
||||
pub fn type_bound(bound: &str) -> ast::TypeBound {
|
||||
// FIXME: remove when no one depends on `generate_impl_text_inner`
|
||||
pub fn type_bound_text(bound: &str) -> ast::TypeBound {
|
||||
ast_from_text(&format!("fn f<T: {bound}>() {{ }}"))
|
||||
}
|
||||
|
||||
pub fn type_bound(bound: ast::Type) -> ast::TypeBound {
|
||||
ast_from_text(&format!("fn f<T: {bound}>() {{ }}"))
|
||||
}
|
||||
|
||||
|
|
|
@ -34,7 +34,7 @@ impl CheckReparse {
|
|||
let mut lines = data.lines();
|
||||
let delete_start = usize::from_str(lines.next()?).ok()? + PREFIX.len();
|
||||
let delete_len = usize::from_str(lines.next()?).ok()?;
|
||||
let insert = lines.next()?.to_string();
|
||||
let insert = lines.next()?.to_owned();
|
||||
let text = lines.collect::<Vec<_>>().join("\n");
|
||||
let text = format!("{PREFIX}{text}{SUFFIX}");
|
||||
text.get(delete_start..delete_start.checked_add(delete_len)?)?; // make sure delete is a valid range
|
||||
|
@ -46,6 +46,7 @@ impl CheckReparse {
|
|||
Some(CheckReparse { text, edit, edited_text })
|
||||
}
|
||||
|
||||
#[allow(clippy::print_stderr)]
|
||||
pub fn run(&self) {
|
||||
let parse = SourceFile::parse(&self.text);
|
||||
let new_parse = parse.reparse(&self.edit);
|
||||
|
|
|
@ -432,7 +432,7 @@ fn api_walkthrough() {
|
|||
WalkEvent::Enter(node) => {
|
||||
let text = match &node {
|
||||
NodeOrToken::Node(it) => it.text().to_string(),
|
||||
NodeOrToken::Token(it) => it.text().to_string(),
|
||||
NodeOrToken::Token(it) => it.text().to_owned(),
|
||||
};
|
||||
format_to!(buf, "{:indent$}{:?} {:?}\n", " ", text, node.kind(), indent = indent);
|
||||
indent += 2;
|
||||
|
|
|
@ -28,7 +28,7 @@ pub(crate) fn build_tree(
|
|||
parser::StrStep::Enter { kind } => builder.start_node(kind),
|
||||
parser::StrStep::Exit => builder.finish_node(),
|
||||
parser::StrStep::Error { msg, pos } => {
|
||||
builder.error(msg.to_string(), pos.try_into().unwrap())
|
||||
builder.error(msg.to_owned(), pos.try_into().unwrap())
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
@ -105,7 +105,7 @@ fn get_text_after_edit(element: SyntaxElement, edit: &Indel) -> String {
|
|||
let edit = Indel::replace(edit.delete - element.text_range().start(), edit.insert.clone());
|
||||
|
||||
let mut text = match element {
|
||||
NodeOrToken::Token(token) => token.text().to_string(),
|
||||
NodeOrToken::Token(token) => token.text().to_owned(),
|
||||
NodeOrToken::Node(node) => node.text().to_string(),
|
||||
};
|
||||
edit.apply(&mut text);
|
||||
|
|
|
@ -36,7 +36,7 @@ impl<N: AstNode + std::fmt::Debug> std::fmt::Debug for AstPtr<N> {
|
|||
impl<N: AstNode> Copy for AstPtr<N> {}
|
||||
impl<N: AstNode> Clone for AstPtr<N> {
|
||||
fn clone(&self) -> AstPtr<N> {
|
||||
AstPtr { raw: self.raw, _ty: PhantomData }
|
||||
*self
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -3,14 +3,12 @@
|
|||
//! Specifically, it generates the `SyntaxKind` enum and a number of newtype
|
||||
//! wrappers around `SyntaxNode` which implement `syntax::AstNode`.
|
||||
|
||||
use std::{
|
||||
collections::{BTreeSet, HashSet},
|
||||
fmt::Write,
|
||||
};
|
||||
use std::{collections::BTreeSet, fmt::Write};
|
||||
|
||||
use itertools::Itertools;
|
||||
use proc_macro2::{Punct, Spacing};
|
||||
use quote::{format_ident, quote};
|
||||
use rustc_hash::FxHashSet;
|
||||
use ungrammar::{Grammar, Rule};
|
||||
|
||||
use crate::tests::ast_src::{
|
||||
|
@ -278,7 +276,7 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: &AstSrc) -> String {
|
|||
}
|
||||
});
|
||||
|
||||
let defined_nodes: HashSet<_> = node_names.collect();
|
||||
let defined_nodes: FxHashSet<_> = node_names.collect();
|
||||
|
||||
for node in kinds
|
||||
.nodes
|
||||
|
@ -575,7 +573,7 @@ fn lower(grammar: &Grammar) -> AstSrc {
|
|||
tokens:
|
||||
"Whitespace Comment String ByteString CString IntNumber FloatNumber Char Byte Ident"
|
||||
.split_ascii_whitespace()
|
||||
.map(|it| it.to_string())
|
||||
.map(|it| it.to_owned())
|
||||
.collect::<Vec<_>>(),
|
||||
..Default::default()
|
||||
};
|
||||
|
@ -818,7 +816,7 @@ fn extract_struct_trait(node: &mut AstNodeSrc, trait_name: &str, methods: &[&str
|
|||
}
|
||||
}
|
||||
if to_remove.len() == methods.len() {
|
||||
node.traits.push(trait_name.to_string());
|
||||
node.traits.push(trait_name.to_owned());
|
||||
node.remove_field(to_remove);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue