@@ -2,10 +2,9 @@ use std::collections::HashSet;
22
33use once_cell:: sync:: Lazy ;
44use prqlc_parser:: { Token , TokenKind , TokenVec } ;
5-
6- use crate :: ast:: * ;
75use regex:: Regex ;
86
7+ use crate :: ast:: * ;
98use crate :: codegen:: SeparatedExprs ;
109
1110use super :: { WriteOpt , WriteSource } ;
@@ -364,8 +363,13 @@ pub fn write_ident_part(s: &str) -> String {
364363 }
365364}
366365
367- /// Find a comment before a span. If there's exactly one newline, then the
368- /// comment is included; otherwise it's included after the current line.
366+ // impl WriteSource for ModuleDef {
367+ // fn write(&self, mut opt: WriteOpt) -> Option<String> {
368+ // codegen::WriteSource::write(&pl.stmts, codegen::WriteOpt::default()).unwrap()
369+ // }}
370+
371+ /// Find a comment before a span. If there's exactly one newline prior, then the
372+ /// comment is included here. Any furthur above are included with the prior token.
369373fn find_comment_before ( span : Span , tokens : & TokenVec ) -> Option < TokenKind > {
370374 // index of the span in the token vec
371375 let index = tokens
@@ -387,7 +391,7 @@ fn find_comment_before(span: Span, tokens: &TokenVec) -> Option<TokenKind> {
387391/// Find a comment after a span. If there's exactly one newline, then the
388392/// comment is included; otherwise it's included after the current line.
389393fn find_comments_after ( span : Span , tokens : & TokenVec ) -> Vec < Token > {
390- let mut out = vec ! [ ] ;
394+ let r : std :: ops :: Range < usize > = span . into ( ) ;
391395 // index of the span in the token vec
392396 let index = tokens
393397 . 0
@@ -396,9 +400,9 @@ fn find_comments_after(span: Span, tokens: &TokenVec) -> Vec<Token> {
396400 // .position(|t| t.1.start == span.start && t.1.end == span.end)
397401 . position ( |t| t. span . end == span. end )
398402 . unwrap_or_else ( || panic ! ( "{:?}, {:?}" , & tokens, & span) ) ;
399- // dbg!(index, span, &tokens);
403+
404+ let mut out = vec ! [ ] ;
400405 for token in tokens. 0 . iter ( ) . skip ( index + 1 ) {
401- // match dbg!(token).kind {
402406 match token. kind {
403407 TokenKind :: NewLine | TokenKind :: Comment ( _) => out. push ( token. clone ( ) ) ,
404408 _ => break ,
@@ -555,7 +559,8 @@ impl WriteSource for SwitchCase {
555559
556560#[ cfg( test) ]
557561mod test {
558- use insta:: assert_snapshot;
562+ use insta:: { assert_debug_snapshot, assert_snapshot} ;
563+ use prqlc_parser:: lex_source;
559564
560565 use super :: * ;
561566
@@ -577,29 +582,60 @@ mod test {
577582 }
578583
579584 #[ test]
580- fn test_find_comments_after ( ) {
581- use insta:: assert_debug_snapshot;
582- let tokens = prqlc_parser:: lex_source (
585+ fn test_find_comment_before ( ) {
586+ let tokens = lex_source (
583587 r#"
584- let a = 5 # comment
585- "# ,
588+ # comment
589+ let a = 5
590+ "# ,
586591 )
587592 . unwrap ( ) ;
593+ let span = tokens
594+ . clone ( )
595+ . 0
596+ . iter ( )
597+ . find ( |t| t. kind == TokenKind :: Keyword ( "let" . to_string ( ) ) )
598+ . unwrap ( )
599+ . span
600+ . clone ( ) ;
601+ let comment = find_comment_before ( span. into ( ) , & tokens) ;
602+ assert_debug_snapshot ! ( comment, @r###"
603+ Some(
604+ Comment(
605+ " comment",
606+ ),
607+ )
608+ "### ) ;
609+ }
588610
589- // This is the `5`
590- let span = Span {
591- start : 17 ,
592- end : 18 ,
593- source_id : 0 ,
594- } ;
595-
596- let comment = find_comments_after ( span, & tokens) ;
611+ #[ test]
612+ fn test_find_comments_after ( ) {
613+ let tokens = lex_source (
614+ r#"
615+ let a = 5 # on side
616+ # below
617+ # and another
618+ "# ,
619+ )
620+ . unwrap ( ) ;
621+ let span = dbg ! ( tokens. clone( ) )
622+ . 0
623+ . iter ( )
624+ . find ( |t| t. kind == TokenKind :: Literal ( Literal :: Integer ( 5 ) ) )
625+ . unwrap ( )
626+ . span
627+ . clone ( ) ;
628+ let comment = find_comments_after ( span. into ( ) , & tokens) ;
597629 assert_debug_snapshot ! ( comment, @r###"
598- [
599- 20..29: Comment(" comment"),
600- 29..30: NewLine,
601- ]
602- "### ) ;
630+ [
631+ 23..32: Comment(" on side"),
632+ 32..33: NewLine,
633+ 45..52: Comment(" below"),
634+ 52..53: NewLine,
635+ 65..78: Comment(" and another"),
636+ 78..79: NewLine,
637+ ]
638+ "### ) ;
603639 }
604640
605641 #[ test]
0 commit comments