⬆️ rust-analyzer

This commit is contained in:
Laurențiu Nicola 2023-02-13 13:55:14 +02:00
parent 3e0e51c108
commit bc45c7659a
321 changed files with 11210 additions and 9720 deletions

View file

@ -12,7 +12,7 @@
use itertools::Itertools;
use stdx::{format_to, never};
use crate::{ast, AstNode, SourceFile, SyntaxKind, SyntaxToken};
use crate::{ast, utils::is_raw_identifier, AstNode, SourceFile, SyntaxKind, SyntaxToken};
/// While the parent module defines basic atomic "constructors", the `ext`
/// module defines shortcuts for common things.
@ -111,8 +111,7 @@ pub fn name_ref(name_ref: &str) -> ast::NameRef {
ast_from_text(&format!("fn f() {{ {raw_escape}{name_ref}; }}"))
}
fn raw_ident_esc(ident: &str) -> &'static str {
let is_keyword = parser::SyntaxKind::from_keyword(ident).is_some();
if is_keyword && !matches!(ident, "self" | "crate" | "super" | "Self") {
if is_raw_identifier(ident) {
"r#"
} else {
""
@ -520,6 +519,15 @@ pub fn literal_pat(lit: &str) -> ast::LiteralPat {
}
}
pub fn slice_pat(pats: impl IntoIterator<Item = ast::Pat>) -> ast::SlicePat {
let pats_str = pats.into_iter().join(", ");
return from_text(&format!("[{pats_str}]"));
fn from_text(text: &str) -> ast::SlicePat {
ast_from_text(&format!("fn f() {{ match () {{{text} => ()}} }}"))
}
}
/// Creates a tuple of patterns from an iterator of patterns.
///
/// Invariant: `pats` must be length > 0
@ -814,6 +822,7 @@ pub fn fn_(
visibility: Option<ast::Visibility>,
fn_name: ast::Name,
type_params: Option<ast::GenericParamList>,
where_clause: Option<ast::WhereClause>,
params: ast::ParamList,
body: ast::BlockExpr,
ret_type: Option<ast::RetType>,
@ -823,6 +832,10 @@ pub fn fn_(
Some(type_params) => format!("{type_params}"),
None => "".into(),
};
let where_clause = match where_clause {
Some(it) => format!("{it} "),
None => "".into(),
};
let ret_type = match ret_type {
Some(ret_type) => format!("{ret_type} "),
None => "".into(),
@ -835,7 +848,7 @@ pub fn fn_(
let async_literal = if is_async { "async " } else { "" };
ast_from_text(&format!(
"{visibility}{async_literal}fn {fn_name}{type_params}{params} {ret_type}{body}",
"{visibility}{async_literal}fn {fn_name}{type_params}{params} {ret_type}{where_clause}{body}",
))
}