Skip to content

Commit 8f8c311

Browse files
committed
progress
1 parent d33d56a commit 8f8c311

File tree

5 files changed

+56
-73
lines changed

5 files changed

+56
-73
lines changed

crates/pgt_lexer_new/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ mod tests {
7474
fn test_empty_input() {
7575
let input = "";
7676
let lexed = lex(input);
77-
assert!(lexed.is_empty());
78-
assert_eq!(lexed.len(), 0);
77+
assert_eq!(lexed.len(), 1);
78+
assert_eq!(lexed.kind(0), SyntaxKind::EOF);
7979
}
8080

8181
#[test]
@@ -88,12 +88,13 @@ mod tests {
8888
for (idx, kind) in lexed.tokens().enumerate() {
8989
if !matches!(
9090
kind,
91-
SyntaxKind::SPACE | SyntaxKind::TAB | SyntaxKind::NEWLINE
91+
SyntaxKind::SPACE | SyntaxKind::TAB | SyntaxKind::LINE_ENDING | SyntaxKind::EOF
9292
) {
9393
non_whitespace.push(lexed.text(idx));
9494
}
9595
}
9696

97+
println!("{:?}", non_whitespace);
9798
assert_eq!(non_whitespace.len(), 2); // SELECT and id
9899
}
99100

crates/pgt_workspace/src/workspace/server/change.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -462,9 +462,7 @@ mod tests {
462462
}
463463

464464
fn assert_document_integrity(d: &Document) {
465-
let ranges = pgt_statement_splitter::split(&d.content)
466-
.expect("Unexpected scan error")
467-
.ranges;
465+
let ranges = pgt_statement_splitter::split(&d.content).ranges;
468466

469467
assert!(
470468
ranges.len() == d.positions.len(),

crates/pgt_workspace/src/workspace/server/document.rs

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -62,32 +62,21 @@ pub(crate) fn split_with_diagnostics(
6262
offset: Option<TextSize>,
6363
) -> (Vec<TextRange>, Vec<SDiagnostic>) {
6464
let o = offset.unwrap_or_else(|| 0.into());
65-
match pgt_statement_splitter::split(content) {
66-
Ok(parse) => (
67-
parse.ranges,
68-
parse
69-
.errors
70-
.into_iter()
71-
.map(|err| {
72-
SDiagnostic::new(
73-
err.clone()
74-
.with_file_span(err.location().span.map(|r| r + o)),
75-
)
76-
})
77-
.collect(),
78-
),
79-
Err(errs) => (
80-
vec![],
81-
errs.into_iter()
82-
.map(|err| {
83-
SDiagnostic::new(
84-
err.clone()
85-
.with_file_span(err.location().span.map(|r| r + o)),
86-
)
87-
})
88-
.collect(),
89-
),
90-
}
65+
let result = pgt_statement_splitter::split(content);
66+
67+
(
68+
result.ranges,
69+
result
70+
.errors
71+
.into_iter()
72+
.map(|err| {
73+
SDiagnostic::new(
74+
err.clone()
75+
.with_file_span(err.location().span.map(|r| r + o)),
76+
)
77+
})
78+
.collect(),
79+
)
9180
}
9281

9382
pub struct StatementIterator<'a> {

docs/codegen/src/rules_docs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ fn print_diagnostics(
442442
});
443443

444444
// split and parse each statement
445-
let stmts = pgt_statement_splitter::split(code).expect("unexpected parse error");
445+
let stmts = pgt_statement_splitter::split(code);
446446
for stmt in stmts.ranges {
447447
match pgt_query_ext::parse(&code[stmt]) {
448448
Ok(ast) => {

xtask/rules_check/src/lib.rs

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -126,52 +126,47 @@ fn assert_lint(
126126
filter,
127127
});
128128

129-
// split and parse each statement
130-
match pgt_statement_splitter::split(code) {
131-
Ok(stmts) => {
132-
for stmt in stmts.ranges {
133-
match pgt_query_ext::parse(&code[stmt]) {
134-
Ok(ast) => {
135-
for rule_diag in analyser.run(pgt_analyser::AnalyserContext { root: &ast })
136-
{
137-
let diag = pgt_diagnostics::serde::Diagnostic::new(rule_diag);
138-
139-
let category = diag.category().expect("linter diagnostic has no code");
140-
let severity = settings.get_severity_from_rule_code(category).expect(
129+
let result = pgt_statement_splitter::split(code);
130+
for stmt in result.ranges {
131+
match pgt_query_ext::parse(&code[stmt]) {
132+
Ok(ast) => {
133+
for rule_diag in analyser.run(pgt_analyser::AnalyserContext { root: &ast }) {
134+
let diag = pgt_diagnostics::serde::Diagnostic::new(rule_diag);
135+
136+
let category = diag.category().expect("linter diagnostic has no code");
137+
let severity = settings.get_severity_from_rule_code(category).expect(
141138
"If you see this error, it means you need to run cargo codegen-configuration",
142139
);
143140

144-
let error = diag
145-
.with_severity(severity)
146-
.with_file_path(&file_path)
147-
.with_file_source_code(code);
148-
149-
write_diagnostic(code, error)?;
150-
}
151-
}
152-
Err(e) => {
153-
let error = SyntaxDiagnostic::from(e)
154-
.with_file_path(&file_path)
155-
.with_file_source_code(code);
156-
write_diagnostic(code, error)?;
157-
}
158-
};
141+
let error = diag
142+
.with_severity(severity)
143+
.with_file_path(&file_path)
144+
.with_file_source_code(code);
145+
146+
write_diagnostic(code, error)?;
147+
}
159148
}
160-
}
161-
Err(errs) => {
162-
// Print all diagnostics to help the user
163-
let mut console = pgt_console::EnvConsole::default();
164-
for err in errs {
165-
console.println(
166-
pgt_console::LogLevel::Error,
167-
markup! {
168-
{PrintDiagnostic::verbose(&err)}
169-
},
170-
);
149+
Err(e) => {
150+
let error = SyntaxDiagnostic::from(e)
151+
.with_file_path(&file_path)
152+
.with_file_source_code(code);
153+
write_diagnostic(code, error)?;
171154
}
172-
bail!("Analysis of '{group}/{rule}' on the following code block returned a scan diagnostic.\n\n{code}");
155+
};
156+
}
157+
if !result.errors.is_empty() {
158+
// Print all diagnostics to help the user
159+
let mut console = pgt_console::EnvConsole::default();
160+
for err in result.errors {
161+
console.println(
162+
pgt_console::LogLevel::Error,
163+
markup! {
164+
{PrintDiagnostic::verbose(&err)}
165+
},
166+
);
173167
}
174-
};
168+
bail!("Analysis of '{group}/{rule}' on the following code block returned a scan diagnostic.\n\n{code}");
169+
}
175170

176171
Ok(())
177172
}

0 commit comments

Comments
 (0)