Skip to content

Commit 5375aad

Browse files
committed
progress
1 parent 3f1f8d4 commit 5375aad

File tree

3 files changed

+85
-3
lines changed

3 files changed

+85
-3
lines changed

crates/pgt_plpgsql_check/src/diagnostics.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,15 @@ pub fn create_diagnostics_from_check_result(
137137
}
138138

139139
fn resolve_span(issue: &PlpgSqlCheckIssue, fn_body: &str, offset: usize) -> Option<TextRange> {
140-
let stmt = issue.statement.as_ref()?;
140+
let stmt = match issue.statement.as_ref() {
141+
Some(s) => s,
142+
None => {
143+
return Some(TextRange::new(
144+
(offset as u32).into(),
145+
((offset + fn_body.len()) as u32).into(),
146+
));
147+
}
148+
};
141149

142150
let line_number = stmt
143151
.line_number

crates/pgt_workspace/src/workspace/server.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,8 @@ impl Workspace for WorkspaceServer {
511511
.await
512512
.unwrap_or_else(|_| vec![]);
513513

514+
println!("{:#?}", plpgsql_check_results);
515+
514516
for d in plpgsql_check_results {
515517
let r = d.span.map(|span| span + range.start());
516518
diagnostics.push(
@@ -571,6 +573,8 @@ impl Workspace for WorkspaceServer {
571573
analysable_stmts.push(node);
572574
}
573575
if let Some(diag) = diagnostic {
576+
println!("{:?}", diag);
577+
println!("{:?}", diagnostics);
574578
// ignore the syntax error if we already have more specialized diagnostics for the
575579
// same statement.
576580
// this is important for create function statements, where we might already have detailed
@@ -579,7 +583,7 @@ impl Workspace for WorkspaceServer {
579583
d.location().span.is_some_and(|async_loc| {
580584
diag.location()
581585
.span
582-
.is_some_and(|syntax_loc| syntax_loc.contains(async_loc))
586+
.is_some_and(|syntax_loc| syntax_loc.contains_range(async_loc))
583587
})
584588
}) {
585589
continue;

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

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use pgt_configuration::{
88
use pgt_diagnostics::Diagnostic;
99
use pgt_fs::PgTPath;
1010
use pgt_text_size::TextRange;
11-
use sqlx::PgPool;
11+
use sqlx::{Executor, PgPool};
1212

1313
use crate::{
1414
Workspace, WorkspaceError,
@@ -206,3 +206,73 @@ async fn correctly_ignores_files() {
206206

207207
assert!(execute_statement_result.is_ok_and(|res| res == ExecuteStatementResult::default()));
208208
}
209+
210+
#[sqlx::test(migrator = "pgt_test_utils::MIGRATIONS")]
211+
async fn test_dedupe_diagnostics(test_db: PgPool) {
212+
let mut conf = PartialConfiguration::init();
213+
conf.merge_with(PartialConfiguration {
214+
db: Some(PartialDatabaseConfiguration {
215+
database: Some(
216+
test_db
217+
.connect_options()
218+
.get_database()
219+
.unwrap()
220+
.to_string(),
221+
),
222+
..Default::default()
223+
}),
224+
..Default::default()
225+
});
226+
227+
let workspace = get_test_workspace(Some(conf)).expect("Unable to create test workspace");
228+
229+
let path = PgTPath::new("test.sql");
230+
231+
let setup_sql = "CREATE EXTENSION IF NOT EXISTS plpgsql_check;";
232+
test_db.execute(setup_sql).await.expect("setup sql failed");
233+
234+
let content = r#"
235+
CREATE OR REPLACE FUNCTION public.f1()
236+
RETURNS void
237+
LANGUAGE plpgsql
238+
AS $function$
239+
decare r text;
240+
BEGIN
241+
select '1' into into r;
242+
END;
243+
$function$;
244+
"#;
245+
246+
workspace
247+
.open_file(OpenFileParams {
248+
path: path.clone(),
249+
content: content.into(),
250+
version: 1,
251+
})
252+
.expect("Unable to open test file");
253+
254+
let diagnostics = workspace
255+
.pull_diagnostics(crate::workspace::PullDiagnosticsParams {
256+
path: path.clone(),
257+
categories: RuleCategories::all(),
258+
max_diagnostics: 100,
259+
only: vec![],
260+
skip: vec![],
261+
})
262+
.expect("Unable to pull diagnostics")
263+
.diagnostics;
264+
265+
assert_eq!(diagnostics.len(), 1, "Expected one diagnostic");
266+
267+
let diagnostic = &diagnostics[0];
268+
269+
assert_eq!(
270+
diagnostic.category().map(|c| c.name()),
271+
Some("plpgsql_check")
272+
);
273+
274+
assert_eq!(
275+
diagnostic.location().span,
276+
Some(TextRange::new(115.into(), 210.into()))
277+
);
278+
}

0 commit comments

Comments
 (0)