1- use std:: { backtrace, borrow :: Cow , cell:: Cell , num:: NonZeroU8 } ;
1+ use std:: { backtrace, cell:: Cell , num:: NonZeroU8 } ;
22
33use Tag :: {
44 EndAlign , EndConditionalContent , EndDedent , EndEntry , EndFill , EndGroup , EndIndent ,
@@ -302,6 +302,23 @@ pub fn text(text: &str) -> Text<'_> {
302302 Text { text, width : None }
303303}
304304
305+ /// Creates a text from a dynamic string and a known width, for example,
306+ /// identifiers or numbers that do not contain line breaks.
307+ pub fn text_with_width ( text : & str , width : TextWidth ) -> Text < ' _ > {
308+ if width. is_multiline ( ) {
309+ debug_assert ! (
310+ text. as_bytes( ) . iter( ) . any( |& b| matches!( b, b'\n' | b'\t' ) ) ,
311+ "Text with a known multiline width must contain at least one whitespace character. Found invalid content: '{text}'"
312+ ) ;
313+ } else {
314+ debug_assert ! (
315+ !text. as_bytes( ) . iter( ) . any( |& b| matches!( b, b'\n' | b'\t' ) ) ,
316+ "Text with a known width must not contain whitespace characters when the width is single line. Found invalid content: '{text}'"
317+ ) ;
318+ }
319+ Text { text, width : Some ( width) }
320+ }
321+
305322#[ derive( Eq , PartialEq ) ]
306323pub struct Text < ' a > {
307324 text : & ' a str ,
@@ -325,49 +342,6 @@ impl std::fmt::Debug for Text<'_> {
325342 }
326343}
327344
328- /// String that is the same as in the input source text if `text` is [`Cow::Borrowed`] or
329- /// some replaced content if `text` is [`Cow::Owned`].
330- pub fn syntax_token_cow_slice ( text : Cow < ' _ , str > , span : Span ) -> SyntaxTokenCowSlice < ' _ > {
331- debug_assert_no_newlines ( & text) ;
332- SyntaxTokenCowSlice { text, span }
333- }
334-
335- pub struct SyntaxTokenCowSlice < ' a > {
336- text : Cow < ' a , str > ,
337- span : Span ,
338- }
339-
340- impl < ' a > Format < ' a > for SyntaxTokenCowSlice < ' a > {
341- fn fmt ( & self , f : & mut Formatter < ' _ , ' a > ) -> FormatResult < ( ) > {
342- match & self . text {
343- Cow :: Borrowed ( content) => {
344- // let range = TextRange::at(self.start, text.text_len());
345- // debug_assert_eq!(
346- // *text,
347- // &self.token.token()[range - self.token.text_range().start()],
348- // "The borrowed string doesn't match the specified token substring. Does the borrowed string belong to this token and range?"
349- // );
350-
351- // let relative_range = range - self.token.text_range().start();
352- // let slice = self.token.token_text().slice(relative_range);
353-
354- text ( f. source_text ( ) . text_for ( & self . span ) ) . fmt ( f)
355- }
356- Cow :: Owned ( text) => f. write_element ( FormatElement :: Text {
357- // TODO: Should use arena String to replace Cow::Owned.
358- text : f. context ( ) . allocator ( ) . alloc_str ( text) ,
359- width : TextWidth :: from_text ( text, f. options ( ) . indent_width ) ,
360- } ) ,
361- }
362- }
363- }
364-
365- impl std:: fmt:: Debug for SyntaxTokenCowSlice < ' _ > {
366- fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
367- std:: write!( f, "SyntaxTokenCowSlice({})" , self . text)
368- }
369- }
370-
371345#[ track_caller]
372346fn debug_assert_no_newlines ( text : & str ) {
373347 debug_assert ! (
0 commit comments