aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/ast/tokens.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax/src/ast/tokens.rs')
-rw-r--r--crates/ra_syntax/src/ast/tokens.rs52
1 files changed, 28 insertions, 24 deletions
diff --git a/crates/ra_syntax/src/ast/tokens.rs b/crates/ra_syntax/src/ast/tokens.rs
index 3cd6d99c3..2e72d4927 100644
--- a/crates/ra_syntax/src/ast/tokens.rs
+++ b/crates/ra_syntax/src/ast/tokens.rs
@@ -84,7 +84,7 @@ impl Whitespace {
84} 84}
85 85
86pub struct QuoteOffsets { 86pub struct QuoteOffsets {
87 pub quotes: [TextRange; 2], 87 pub quotes: (TextRange, TextRange),
88 pub contents: TextRange, 88 pub contents: TextRange,
89} 89}
90 90
@@ -103,7 +103,7 @@ impl QuoteOffsets {
103 let end = TextSize::of(literal); 103 let end = TextSize::of(literal);
104 104
105 let res = QuoteOffsets { 105 let res = QuoteOffsets {
106 quotes: [TextRange::new(start, left_quote), TextRange::new(right_quote, end)], 106 quotes: (TextRange::new(start, left_quote), TextRange::new(right_quote, end)),
107 contents: TextRange::new(left_quote, right_quote), 107 contents: TextRange::new(left_quote, right_quote),
108 }; 108 };
109 Some(res) 109 Some(res)
@@ -116,17 +116,17 @@ pub trait HasQuotes: AstToken {
116 let offsets = QuoteOffsets::new(text)?; 116 let offsets = QuoteOffsets::new(text)?;
117 let o = self.syntax().text_range().start(); 117 let o = self.syntax().text_range().start();
118 let offsets = QuoteOffsets { 118 let offsets = QuoteOffsets {
119 quotes: [offsets.quotes[0] + o, offsets.quotes[1] + o], 119 quotes: (offsets.quotes.0 + o, offsets.quotes.1 + o),
120 contents: offsets.contents + o, 120 contents: offsets.contents + o,
121 }; 121 };
122 Some(offsets) 122 Some(offsets)
123 } 123 }
124 fn open_quote_text_range(&self) -> Option<TextRange> { 124 fn open_quote_text_range(&self) -> Option<TextRange> {
125 self.quote_offsets().map(|it| it.quotes[0]) 125 self.quote_offsets().map(|it| it.quotes.0)
126 } 126 }
127 127
128 fn close_quote_text_range(&self) -> Option<TextRange> { 128 fn close_quote_text_range(&self) -> Option<TextRange> {
129 self.quote_offsets().map(|it| it.quotes[1]) 129 self.quote_offsets().map(|it| it.quotes.1)
130 } 130 }
131 131
132 fn text_range_between_quotes(&self) -> Option<TextRange> { 132 fn text_range_between_quotes(&self) -> Option<TextRange> {
@@ -335,16 +335,26 @@ pub trait HasFormatSpecifier: AstToken {
335 } 335 }
336 c if c == '_' || c.is_alphabetic() => { 336 c if c == '_' || c.is_alphabetic() => {
337 read_identifier(&mut chars, &mut callback); 337 read_identifier(&mut chars, &mut callback);
338 if chars.peek().and_then(|next| next.1.as_ref().ok()).copied() 338 // can be either width (indicated by dollar sign, or type in which case
339 != Some('$') 339 // the next sign has to be `}`)
340 { 340 let next =
341 continue; 341 chars.peek().and_then(|next| next.1.as_ref().ok()).copied();
342 } 342 match next {
343 skip_char_and_emit( 343 Some('$') => skip_char_and_emit(
344 &mut chars, 344 &mut chars,
345 FormatSpecifier::DollarSign, 345 FormatSpecifier::DollarSign,
346 &mut callback, 346 &mut callback,
347 ); 347 ),
348 Some('}') => {
349 skip_char_and_emit(
350 &mut chars,
351 FormatSpecifier::Close,
352 &mut callback,
353 );
354 continue;
355 }
356 _ => continue,
357 };
348 } 358 }
349 _ => {} 359 _ => {}
350 } 360 }
@@ -416,17 +426,11 @@ pub trait HasFormatSpecifier: AstToken {
416 } 426 }
417 } 427 }
418 428
419 let mut cloned = chars.clone().take(2); 429 if let Some((_, Ok('}'))) = chars.peek() {
420 let first = cloned.next().and_then(|next| next.1.as_ref().ok()).copied(); 430 skip_char_and_emit(&mut chars, FormatSpecifier::Close, &mut callback);
421 let second = cloned.next().and_then(|next| next.1.as_ref().ok()).copied(); 431 } else {
422 if first != Some('}') {
423 continue;
424 }
425 if second == Some('}') {
426 // Escaped format end specifier, `}}`
427 continue; 432 continue;
428 } 433 }
429 skip_char_and_emit(&mut chars, FormatSpecifier::Close, &mut callback);
430 } 434 }
431 _ => { 435 _ => {
432 while let Some((_, Ok(next_char))) = chars.peek() { 436 while let Some((_, Ok(next_char))) = chars.peek() {