@@ -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,6 @@ 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 ! [ ] ;
391394 // index of the span in the token vec
392395 let index = tokens
393396 . 0
@@ -396,9 +399,9 @@ fn find_comments_after(span: Span, tokens: &TokenVec) -> Vec<Token> {
396399 // .position(|t| t.1.start == span.start && t.1.end == span.end)
397400 . position ( |t| t. span . end == span. end )
398401 . unwrap_or_else ( || panic ! ( "{:?}, {:?}" , & tokens, & span) ) ;
399- // dbg!(index, span, &tokens);
402+
403+ let mut out = vec ! [ ] ;
400404 for token in tokens. 0 . iter ( ) . skip ( index + 1 ) {
401- // match dbg!(token).kind {
402405 match token. kind {
403406 TokenKind :: NewLine | TokenKind :: Comment ( _) => out. push ( token. clone ( ) ) ,
404407 _ => break ,
@@ -555,7 +558,8 @@ impl WriteSource for SwitchCase {
555558
556559#[ cfg( test) ]
557560mod test {
558- use insta:: assert_snapshot;
561+ use insta:: { assert_debug_snapshot, assert_snapshot} ;
562+ use prqlc_parser:: lex_source;
559563
560564 use super :: * ;
561565
@@ -577,29 +581,60 @@ mod test {
577581 }
578582
579583 #[ test]
580- fn test_find_comments_after ( ) {
581- use insta:: assert_debug_snapshot;
582- let tokens = prqlc_parser:: lex_source (
584+ fn test_find_comment_before ( ) {
585+ let tokens = lex_source (
583586 r#"
584- let a = 5 # comment
585- "# ,
587+ # comment
588+ let a = 5
589+ "# ,
586590 )
587591 . unwrap ( ) ;
592+ let span = tokens
593+ . clone ( )
594+ . 0
595+ . iter ( )
596+ . find ( |t| t. kind == TokenKind :: Keyword ( "let" . to_string ( ) ) )
597+ . unwrap ( )
598+ . span
599+ . clone ( ) ;
600+ let comment = find_comment_before ( span. into ( ) , & tokens) ;
601+ assert_debug_snapshot ! ( comment, @r###"
602+ Some(
603+ Comment(
604+ " comment",
605+ ),
606+ )
607+ "### ) ;
608+ }
588609
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) ;
610+ #[ test]
611+ fn test_find_comments_after ( ) {
612+ let tokens = lex_source (
613+ r#"
614+ let a = 5 # on side
615+ # below
616+ # and another
617+ "# ,
618+ )
619+ . unwrap ( ) ;
620+ let span = dbg ! ( tokens. clone( ) )
621+ . 0
622+ . iter ( )
623+ . find ( |t| t. kind == TokenKind :: Literal ( Literal :: Integer ( 5 ) ) )
624+ . unwrap ( )
625+ . span
626+ . clone ( ) ;
627+ let comment = find_comments_after ( span. into ( ) , & tokens) ;
597628 assert_debug_snapshot ! ( comment, @r###"
598- [
599- 20..29: Comment(" comment"),
600- 29..30: NewLine,
601- ]
602- "### ) ;
629+ [
630+ 23..32: Comment(" on side"),
631+ 32..33: NewLine,
632+ 45..52: Comment(" below"),
633+ 52..53: NewLine,
634+ 65..78: Comment(" and another"),
635+ 78..79: NewLine,
636+ ]
637+ "### ) ;
603638 }
604639
605640 #[ test]
0 commit comments