Make is_raw_identifier() public util function

This commit is contained in:
Ryo Yoshida 2023-02-13 18:43:59 +09:00
parent 646f973857
commit 92fdfb548e
No known key found for this signature in database
GPG Key ID: E25698A930586171
3 changed files with 9 additions and 10 deletions

View File

@ -2,7 +2,7 @@
use std::fmt; use std::fmt;
use syntax::{ast, SmolStr, SyntaxKind}; use syntax::{ast, utils::is_raw_identifier, SmolStr};
/// `Name` is a wrapper around string, which is used in hir for both references /// `Name` is a wrapper around string, which is used in hir for both references
/// and declarations. In theory, names should also carry hygiene info, but we are /// and declarations. In theory, names should also carry hygiene info, but we are
@ -33,11 +33,6 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
} }
} }
fn is_raw_identifier(name: &str) -> bool {
let is_keyword = SyntaxKind::from_keyword(name).is_some();
is_keyword && !matches!(name, "self" | "crate" | "super" | "Self")
}
impl<'a> fmt::Display for UnescapedName<'a> { impl<'a> fmt::Display for UnescapedName<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.0 .0 { match &self.0 .0 {

View File

@ -12,7 +12,7 @@
use itertools::Itertools; use itertools::Itertools;
use stdx::{format_to, never}; 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` /// While the parent module defines basic atomic "constructors", the `ext`
/// module defines shortcuts for common things. /// 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}; }}")) ast_from_text(&format!("fn f() {{ {raw_escape}{name_ref}; }}"))
} }
fn raw_ident_esc(ident: &str) -> &'static str { fn raw_ident_esc(ident: &str) -> &'static str {
let is_keyword = parser::SyntaxKind::from_keyword(ident).is_some(); if is_raw_identifier(ident) {
if is_keyword && !matches!(ident, "self" | "crate" | "super" | "Self") {
"r#" "r#"
} else { } else {
"" ""

View File

@ -2,7 +2,7 @@
use itertools::Itertools; use itertools::Itertools;
use crate::{ast, match_ast, AstNode}; use crate::{ast, match_ast, AstNode, SyntaxKind};
pub fn path_to_string_stripping_turbo_fish(path: &ast::Path) -> String { pub fn path_to_string_stripping_turbo_fish(path: &ast::Path) -> String {
path.syntax() path.syntax()
@ -23,6 +23,11 @@ pub fn path_to_string_stripping_turbo_fish(path: &ast::Path) -> String {
.join("::") .join("::")
} }
pub fn is_raw_identifier(name: &str) -> bool {
let is_keyword = SyntaxKind::from_keyword(name).is_some();
is_keyword && !matches!(name, "self" | "crate" | "super" | "Self")
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::path_to_string_stripping_turbo_fish; use super::path_to_string_stripping_turbo_fish;