From 6cd458f3d08030cddcd488ef28c2a8b581bd7928 Mon Sep 17 00:00:00 2001 From: Victor Song Date: Tue, 30 Jan 2024 01:13:17 -0600 Subject: [PATCH] Move `raw_delimiter_count` to `syntax` crate --- crates/proc-macro-srv/src/server.rs | 13 +++---------- crates/syntax/src/ast/token_ext.rs | 10 ++++++++++ 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/crates/proc-macro-srv/src/server.rs b/crates/proc-macro-srv/src/server.rs index 9339d226a2..9961c5910a 100644 --- a/crates/proc-macro-srv/src/server.rs +++ b/crates/proc-macro-srv/src/server.rs @@ -59,21 +59,21 @@ fn literal_to_external(literal: ast::LiteralKind) -> Option { if data.is_raw() { - bridge::LitKind::StrRaw(raw_delimiter_count(data)?) + bridge::LitKind::StrRaw(data.raw_delimiter_count()?) } else { bridge::LitKind::Str } } ast::LiteralKind::ByteString(data) => { if data.is_raw() { - bridge::LitKind::ByteStrRaw(raw_delimiter_count(data)?) + bridge::LitKind::ByteStrRaw(data.raw_delimiter_count()?) } else { bridge::LitKind::ByteStr } } ast::LiteralKind::CString(data) => { if data.is_raw() { - bridge::LitKind::CStrRaw(raw_delimiter_count(data)?) + bridge::LitKind::CStrRaw(data.raw_delimiter_count()?) } else { bridge::LitKind::CStr } @@ -86,13 +86,6 @@ fn literal_to_external(literal: ast::LiteralKind) -> Option(s: S) -> Option { - let text = s.text(); - let quote_range = s.text_range_between_quotes()?; - let range_start = s.syntax().text_range().start(); - text[TextRange::up_to((quote_range - range_start).start())].matches('#').count().try_into().ok() -} - fn str_to_lit_node(input: &str) -> Option { let input = input.trim(); let source_code = format!("fn f() {{ let _ = {input}; }}"); diff --git a/crates/syntax/src/ast/token_ext.rs b/crates/syntax/src/ast/token_ext.rs index 7cd1f1550b..c93391a979 100644 --- a/crates/syntax/src/ast/token_ext.rs +++ b/crates/syntax/src/ast/token_ext.rs @@ -204,6 +204,16 @@ pub trait IsString: AstToken { assert!(TextRange::up_to(contents_range.len()).contains_range(range)); Some(range + contents_range.start()) } + fn raw_delimiter_count(&self) -> Option { + let text = self.text(); + let quote_range = self.text_range_between_quotes()?; + let range_start = self.syntax().text_range().start(); + text[TextRange::up_to((quote_range - range_start).start())] + .matches('#') + .count() + .try_into() + .ok() + } } impl IsString for ast::String {