Skip to content

Commit 73c0504

Browse files
committed
.
1 parent 07eacad commit 73c0504

File tree

4 files changed

+88
-33
lines changed

4 files changed

+88
-33
lines changed

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: 61 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,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.
389393
fn 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)]
557560
mod 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]

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)