Skip to content

Commit 49193a0

Browse files
committed
.
1 parent 07eacad commit 49193a0

4 files changed

Lines changed: 89 additions & 33 deletions

File tree

prqlc/prqlc-ast/src/span.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ impl From<Span> for Range<usize> {
1717
a.start..a.end
1818
}
1919
}
20+
impl From<Range<usize>> for Span {
21+
fn from(range: Range<usize>) -> Self {
22+
Span {
23+
start: range.start,
24+
end: range.end,
25+
source_id: 0, // Default value as Range<usize> does not provide a source_id
26+
}
27+
}
28+
}
2029

2130
impl Debug for Span {
2231
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {

prqlc/prqlc-parser/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ use prqlc_ast::error::{Error, Reason, WithErrorInfo};
1414
use prqlc_ast::stmt::*;
1515
use prqlc_ast::Span;
1616

17-
use lexer::Token;
18-
pub use lexer::{TokenKind, TokenVec};
17+
pub use lexer::{Token, TokenKind, TokenVec};
1918
use span::ParserSpan;
2019

2120
/// Build PRQL AST from a PRQL query string.

prqlc/prqlc/src/codegen/ast.rs

Lines changed: 62 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ use std::collections::HashSet;
22

33
use once_cell::sync::Lazy;
44
use prqlc_parser::{Token, TokenKind, TokenVec};
5-
6-
use crate::ast::*;
75
use regex::Regex;
86

7+
use crate::ast::*;
98
use crate::codegen::SeparatedExprs;
109

1110
use 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.
369373
fn 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.
389393
fn 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)]
557561
mod 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]

prqlc/prqlc/src/lib.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -389,17 +389,29 @@ fn test_format_prql() {
389389
"###);
390390

391391
assert_snapshot!(format_prql( r#"
392+
from employees
392393
# test comment
393-
from db.employees # inline comment
394+
select {name}
395+
"#
396+
).unwrap(), @r###"
397+
from employees
398+
# test comment
399+
400+
# test comment
401+
select {name}
402+
"###);
403+
404+
assert_snapshot!(format_prql( r#"
405+
# test comment
406+
from employees # inline comment
394407
# another test comment
395-
select {name, age}"#
408+
select {name}"#
396409
).unwrap(), @r###"
397410
# test comment
398-
from db.employees # inline comment
399-
# another test comment
411+
from employees # inline comment
400412
401413
# another test comment
402-
select {name, age}
414+
select {name}
403415
"###);
404416
}
405417

0 commit comments

Comments
 (0)