1- use proc_macro:: { TokenStream , TokenTree } ;
1+ use std:: ops:: Deref ;
2+
3+ use proc_macro:: TokenStream ;
4+ use proc_macro2:: TokenStream as TokenStream2 ;
5+ use quote:: ToTokens ;
26
37use crate :: token:: { Pos , Token , Tokens } ;
48
59#[ derive( Debug , Clone ) ]
6- pub ( crate ) struct Capture {
7- key : Token ,
8- rust : TokenTree ,
10+ pub ( crate ) struct Capture ( Token ) ;
11+
12+ impl Deref for Capture {
13+ type Target = Token ;
14+
15+ fn deref ( & self ) -> & Self :: Target {
16+ & self . 0
17+ }
918}
1019
1120impl Capture {
12- fn new ( key : Token , rust : TokenTree ) -> Self {
13- Self { key , rust }
21+ fn new ( token : & Token ) -> Self {
22+ Self ( token . clone ( ) )
1423 }
1524
16- /// Token string inside `chunk!`
17- pub ( crate ) fn key ( & self ) -> & Token {
18- & self . key
25+ pub ( crate ) fn name ( & self ) -> String {
26+ self . 0 . to_string ( )
1927 }
28+ }
2029
21- /// As rust variable, e.g. `x`
22- pub ( crate ) fn as_rust ( & self ) -> & TokenTree {
23- & self . rust
30+ impl ToTokens for Capture {
31+ fn to_tokens ( & self , tokens : & mut TokenStream2 ) {
32+ let ts: TokenStream = self . 0 . tree ( ) . clone ( ) . into ( ) ;
33+ tokens. extend ( TokenStream2 :: from ( ts) ) ;
2434 }
2535}
2636
@@ -33,11 +43,10 @@ impl Captures {
3343 }
3444
3545 pub ( crate ) fn add ( & mut self , token : & Token ) {
36- if self . 0 . iter ( ) . any ( |arg| arg. key ( ) == token) {
46+ if self . 0 . iter ( ) . any ( |arg| & * * arg == token) {
3747 return ;
3848 }
39- let arg = Capture :: new ( token. clone ( ) , token. tree ( ) . clone ( ) ) ;
40- self . 0 . push ( arg) ;
49+ self . 0 . push ( Capture :: new ( token) ) ;
4150 }
4251
4352 pub ( crate ) fn captures ( & self ) -> & [ Capture ] {
@@ -58,29 +67,26 @@ impl Chunk {
5867 let mut source = String :: new ( ) ;
5968 let mut caps = Captures :: new ( ) ;
6069
61- let mut pos : Option < Pos > = None ;
70+ let mut prev_end : Option < Pos > = None ;
6271 for t in tokens {
6372 if t. is_cap ( ) {
6473 caps. add ( & t) ;
6574 }
6675
6776 let ( line, col) = ( t. start ( ) . line , t. start ( ) . column ) ;
68- let ( prev_line, prev_col) = pos
69- . take ( )
70- . map ( |lc| ( lc. line , lc. column ) )
71- . unwrap_or_else ( || ( line, col) ) ;
72-
73- #[ allow( clippy:: comparison_chain) ]
74- if line > prev_line {
75- source. push ( '\n' ) ;
76- } else if line == prev_line {
77- for _ in 0 ..col. saturating_sub ( prev_col) {
78- source. push ( ' ' ) ;
77+ if let Some ( prev) = prev_end {
78+ if line > prev. line {
79+ source. push ( '\n' ) ;
80+ source. push_str ( & " " . repeat ( col. saturating_sub ( 1 ) ) ) ;
81+ } else if line == prev. line {
82+ source. push_str ( & " " . repeat ( col. saturating_sub ( prev. column ) ) ) ;
7983 }
84+ } else {
85+ source. push_str ( & " " . repeat ( col. saturating_sub ( 1 ) ) ) ;
8086 }
8187 source. push_str ( & t. to_string ( ) ) ;
8288
83- pos = Some ( t. end ( ) ) ;
89+ prev_end = Some ( t. end ( ) ) ;
8490 }
8591
8692 Self {
0 commit comments