Skip to content

Commit 2bad714

Browse files
committed
refactor: pull diagnostics
1 parent 1ff2ee1 commit 2bad714

File tree

12 files changed

+158
-77
lines changed

12 files changed

+158
-77
lines changed
Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
use std::time::Instant;
2+
13
use crate::cli_options::CliOptions;
24
use crate::reporter::Report;
35
use crate::{CliDiagnostic, CliSession, VcsIntegration};
46
use pgls_configuration::PartialConfiguration;
7+
use pgls_diagnostics::Error;
8+
use pgls_workspace::features::diagnostics::{PullDatabaseDiagnosticsParams, PullDiagnosticsResult};
59

610
pub fn dblint(
711
mut session: CliSession,
@@ -10,9 +14,27 @@ pub fn dblint(
1014
) -> Result<(), CliDiagnostic> {
1115
let configuration = session.prepare_with_config(cli_options, cli_configuration)?;
1216
session.setup_workspace(configuration, VcsIntegration::Disabled)?;
17+
let workspace = session.workspace();
18+
19+
let max_diagnostics = if cli_options.reporter.is_default() {
20+
cli_options.max_diagnostics.into()
21+
} else {
22+
u32::MAX
23+
};
24+
25+
let start = Instant::now();
26+
27+
let PullDiagnosticsResult {
28+
diagnostics,
29+
skipped_diagnostics,
30+
} = workspace.pull_db_diagnostics(PullDatabaseDiagnosticsParams { max_diagnostics })?;
1331

14-
// TODO: Implement actual dblint logic here
15-
let report = Report::new(vec![], std::time::Duration::new(0, 0), 0, None);
32+
let report = Report::new(
33+
diagnostics.into_iter().map(Error::from).collect(),
34+
start.elapsed(),
35+
skipped_diagnostics,
36+
None,
37+
);
1638

1739
session.report("dblint", cli_options, &report)
1840
}

crates/pgls_lsp/src/server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ impl ServerFactory {
457457
workspace_method!(builder, open_file);
458458
workspace_method!(builder, change_file);
459459
workspace_method!(builder, close_file);
460-
workspace_method!(builder, pull_diagnostics);
460+
workspace_method!(builder, pull_file_diagnostics);
461461
workspace_method!(builder, get_completions);
462462
workspace_method!(builder, register_project_folder);
463463
workspace_method!(builder, unregister_project_folder);

crates/pgls_lsp/src/session.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -270,15 +270,15 @@ impl Session {
270270
let categories = RuleCategoriesBuilder::default().all();
271271

272272
let diagnostics: Vec<lsp_types::Diagnostic> = {
273-
let result =
274-
self.workspace
275-
.pull_diagnostics(features::diagnostics::PullDiagnosticsParams {
276-
path: pgls_path.clone(),
277-
max_diagnostics: u64::MAX,
278-
categories: categories.build(),
279-
only: Vec::new(),
280-
skip: Vec::new(),
281-
})?;
273+
let result = self.workspace.pull_file_diagnostics(
274+
features::diagnostics::PullFileDiagnosticsParams {
275+
path: pgls_path.clone(),
276+
max_diagnostics: u32::MAX,
277+
categories: categories.build(),
278+
only: Vec::new(),
279+
skip: Vec::new(),
280+
},
281+
)?;
282282

283283
result
284284
.diagnostics

crates/pgls_workspace/src/features/diagnostics.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ use pgls_fs::PgTPath;
44

55
#[derive(Debug, serde::Serialize, serde::Deserialize)]
66
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
7-
pub struct PullDiagnosticsParams {
7+
pub struct PullFileDiagnosticsParams {
88
pub path: PgTPath,
99
pub categories: RuleCategories,
10-
pub max_diagnostics: u64,
10+
pub max_diagnostics: u32,
1111
pub only: Vec<RuleSelector>,
1212
pub skip: Vec<RuleSelector>,
1313
}
@@ -16,6 +16,11 @@ pub struct PullDiagnosticsParams {
1616
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
1717
pub struct PullDiagnosticsResult {
1818
pub diagnostics: Vec<pgls_diagnostics::serde::Diagnostic>,
19-
pub errors: usize,
20-
pub skipped_diagnostics: u64,
19+
pub skipped_diagnostics: u32,
20+
}
21+
22+
#[derive(Debug, serde::Serialize, serde::Deserialize)]
23+
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
24+
pub struct PullDatabaseDiagnosticsParams {
25+
pub max_diagnostics: u32,
2126
}

crates/pgls_workspace/src/workspace.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ use crate::{
1616
CodeActionsParams, CodeActionsResult, ExecuteStatementParams, ExecuteStatementResult,
1717
},
1818
completions::{CompletionsResult, GetCompletionsParams},
19-
diagnostics::{PullDiagnosticsParams, PullDiagnosticsResult},
19+
diagnostics::{
20+
PullDatabaseDiagnosticsParams, PullDiagnosticsResult, PullFileDiagnosticsParams,
21+
},
2022
on_hover::{OnHoverParams, OnHoverResult},
2123
},
2224
};
@@ -98,9 +100,15 @@ pub struct UnregisterProjectFolderParams {
98100

99101
pub trait Workspace: Send + Sync + RefUnwindSafe {
100102
/// Retrieves the list of diagnostics associated to a file
101-
fn pull_diagnostics(
103+
fn pull_file_diagnostics(
102104
&self,
103-
params: PullDiagnosticsParams,
105+
params: PullFileDiagnosticsParams,
106+
) -> Result<PullDiagnosticsResult, WorkspaceError>;
107+
108+
/// Retrieves the list of diagnostics associated to a database schema
109+
fn pull_db_diagnostics(
110+
&self,
111+
params: PullDatabaseDiagnosticsParams,
104112
) -> Result<PullDiagnosticsResult, WorkspaceError>;
105113

106114
/// Retrieves a list of available code_actions for a file/cursor_position
@@ -214,13 +222,14 @@ impl<'app, W: Workspace + ?Sized> FileGuard<'app, W> {
214222
only: Vec<RuleSelector>,
215223
skip: Vec<RuleSelector>,
216224
) -> Result<PullDiagnosticsResult, WorkspaceError> {
217-
self.workspace.pull_diagnostics(PullDiagnosticsParams {
218-
path: self.path.clone(),
219-
categories,
220-
max_diagnostics: max_diagnostics.into(),
221-
only,
222-
skip,
223-
})
225+
self.workspace
226+
.pull_file_diagnostics(PullFileDiagnosticsParams {
227+
path: self.path.clone(),
228+
categories,
229+
max_diagnostics: max_diagnostics.into(),
230+
only,
231+
skip,
232+
})
224233
}
225234
}
226235

crates/pgls_workspace/src/workspace/client.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,20 @@ where
148148
self.request("pgt/get_file_content", params)
149149
}
150150

151-
fn pull_diagnostics(
151+
fn pull_file_diagnostics(
152152
&self,
153-
params: crate::features::diagnostics::PullDiagnosticsParams,
153+
params: crate::features::diagnostics::PullFileDiagnosticsParams,
154154
) -> Result<crate::features::diagnostics::PullDiagnosticsResult, WorkspaceError> {
155155
self.request("pgt/pull_diagnostics", params)
156156
}
157157

158+
fn pull_db_diagnostics(
159+
&self,
160+
params: crate::features::diagnostics::PullDatabaseDiagnosticsParams,
161+
) -> Result<crate::features::diagnostics::PullDiagnosticsResult, WorkspaceError> {
162+
self.request("pgt/pull_db_diagnostics", params)
163+
}
164+
158165
fn get_completions(
159166
&self,
160167
params: super::GetCompletionsParams,

crates/pgls_workspace/src/workspace/server.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use crate::{
3636
CommandActionCategory, ExecuteStatementParams, ExecuteStatementResult,
3737
},
3838
completions::{CompletionsResult, GetCompletionsParams, get_statement_for_completions},
39-
diagnostics::{PullDiagnosticsParams, PullDiagnosticsResult},
39+
diagnostics::{PullDiagnosticsResult, PullFileDiagnosticsParams},
4040
on_hover::{OnHoverParams, OnHoverResult},
4141
},
4242
settings::{WorkspaceSettings, WorkspaceSettingsHandle, WorkspaceSettingsHandleMut},
@@ -425,9 +425,9 @@ impl Workspace for WorkspaceServer {
425425
}
426426

427427
#[ignored_path(path=&params.path)]
428-
fn pull_diagnostics(
428+
fn pull_file_diagnostics(
429429
&self,
430-
params: PullDiagnosticsParams,
430+
params: PullFileDiagnosticsParams,
431431
) -> Result<PullDiagnosticsResult, WorkspaceError> {
432432
let settings = self.workspaces();
433433

@@ -438,7 +438,6 @@ impl Workspace for WorkspaceServer {
438438
// we might want to return an error here in the future
439439
return Ok(PullDiagnosticsResult {
440440
diagnostics: Vec::new(),
441-
errors: 0,
442441
skipped_diagnostics: 0,
443442
});
444443
}
@@ -670,19 +669,20 @@ impl Workspace for WorkspaceServer {
670669
diagnostics.retain(|d| !suppressions.is_suppressed(d));
671670
diagnostics.extend(suppression_errors.into_iter().map(SDiagnostic::new));
672671

673-
let errors = diagnostics
674-
.iter()
675-
.filter(|d| d.severity() == Severity::Error || d.severity() == Severity::Fatal)
676-
.count();
677-
678672
info!("Pulled {:?} diagnostic(s)", diagnostics.len());
679673
Ok(PullDiagnosticsResult {
680674
diagnostics,
681-
errors,
682675
skipped_diagnostics: 0,
683676
})
684677
}
685678

679+
fn pull_db_diagnostics(
680+
&self,
681+
_params: crate::features::diagnostics::PullDatabaseDiagnosticsParams,
682+
) -> Result<PullDiagnosticsResult, WorkspaceError> {
683+
Ok(PullDiagnosticsResult::default())
684+
}
685+
686686
#[ignored_path(path=&params.path)]
687687
#[tracing::instrument(level = "debug", skip_all, fields(
688688
path = params.path.as_os_str().to_str(),

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

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ async fn test_diagnostics(test_db: PgPool) {
8383
.expect("Unable to open test file");
8484

8585
let diagnostics = workspace
86-
.pull_diagnostics(crate::workspace::PullDiagnosticsParams {
86+
.pull_file_diagnostics(crate::workspace::PullFileDiagnosticsParams {
8787
path: path.clone(),
8888
categories: RuleCategories::all(),
8989
max_diagnostics: 100,
@@ -141,7 +141,7 @@ async fn test_syntax_error(test_db: PgPool) {
141141
.expect("Unable to open test file");
142142

143143
let diagnostics = workspace
144-
.pull_diagnostics(crate::workspace::PullDiagnosticsParams {
144+
.pull_file_diagnostics(crate::workspace::PullFileDiagnosticsParams {
145145
path: path.clone(),
146146
categories: RuleCategories::all(),
147147
max_diagnostics: 100,
@@ -181,13 +181,14 @@ async fn correctly_ignores_files() {
181181
seect 1;
182182
"#;
183183

184-
let diagnostics_result = workspace.pull_diagnostics(crate::workspace::PullDiagnosticsParams {
185-
path: path.clone(),
186-
categories: RuleCategories::all(),
187-
max_diagnostics: 100,
188-
only: vec![],
189-
skip: vec![],
190-
});
184+
let diagnostics_result =
185+
workspace.pull_file_diagnostics(crate::workspace::PullFileDiagnosticsParams {
186+
path: path.clone(),
187+
categories: RuleCategories::all(),
188+
max_diagnostics: 100,
189+
only: vec![],
190+
skip: vec![],
191+
});
191192

192193
assert!(
193194
diagnostics_result.is_ok_and(|res| res.diagnostics.is_empty()
@@ -257,7 +258,7 @@ async fn test_dedupe_diagnostics(test_db: PgPool) {
257258
.expect("Unable to open test file");
258259

259260
let diagnostics = workspace
260-
.pull_diagnostics(crate::workspace::PullDiagnosticsParams {
261+
.pull_file_diagnostics(crate::workspace::PullFileDiagnosticsParams {
261262
path: path.clone(),
262263
categories: RuleCategories::all(),
263264
max_diagnostics: 100,
@@ -322,7 +323,7 @@ async fn test_plpgsql_assign_composite_types(test_db: PgPool) {
322323
.expect("Unable to open test file");
323324

324325
let diagnostics = workspace
325-
.pull_diagnostics(crate::workspace::PullDiagnosticsParams {
326+
.pull_file_diagnostics(crate::workspace::PullFileDiagnosticsParams {
326327
path: path.clone(),
327328
categories: RuleCategories::all(),
328329
max_diagnostics: 100,
@@ -376,7 +377,7 @@ async fn test_positional_params(test_db: PgPool) {
376377
.expect("Unable to open test file");
377378

378379
let diagnostics = workspace
379-
.pull_diagnostics(crate::workspace::PullDiagnosticsParams {
380+
.pull_file_diagnostics(crate::workspace::PullFileDiagnosticsParams {
380381
path: path.clone(),
381382
categories: RuleCategories::all(),
382383
max_diagnostics: 100,
@@ -437,7 +438,7 @@ async fn test_disable_plpgsql_check(test_db: PgPool) {
437438
.expect("Unable to open test file");
438439

439440
let diagnostics = workspace
440-
.pull_diagnostics(crate::workspace::PullDiagnosticsParams {
441+
.pull_file_diagnostics(crate::workspace::PullFileDiagnosticsParams {
441442
path: path.clone(),
442443
categories: RuleCategories::all(),
443444
max_diagnostics: 100,
@@ -469,7 +470,7 @@ async fn test_disable_plpgsql_check(test_db: PgPool) {
469470
});
470471

471472
let diagnostics = workspace
472-
.pull_diagnostics(crate::workspace::PullDiagnosticsParams {
473+
.pull_file_diagnostics(crate::workspace::PullFileDiagnosticsParams {
473474
path: path.clone(),
474475
categories: RuleCategories::all(),
475476
max_diagnostics: 100,
@@ -529,7 +530,7 @@ async fn test_disable_typecheck(test_db: PgPool) {
529530
.expect("Unable to open test file");
530531

531532
let diagnostics = workspace
532-
.pull_diagnostics(crate::workspace::PullDiagnosticsParams {
533+
.pull_file_diagnostics(crate::workspace::PullFileDiagnosticsParams {
533534
path: path.clone(),
534535
categories: RuleCategories::all(),
535536
max_diagnostics: 100,
@@ -562,7 +563,7 @@ async fn test_disable_typecheck(test_db: PgPool) {
562563
});
563564

564565
let diagnostics = workspace
565-
.pull_diagnostics(crate::workspace::PullDiagnosticsParams {
566+
.pull_file_diagnostics(crate::workspace::PullFileDiagnosticsParams {
566567
path: path.clone(),
567568
categories: RuleCategories::all(),
568569
max_diagnostics: 100,
@@ -609,7 +610,7 @@ FOR NO KEY UPDATE;
609610
.expect("Unable to open test file");
610611

611612
let diagnostics = workspace
612-
.pull_diagnostics(crate::workspace::PullDiagnosticsParams {
613+
.pull_file_diagnostics(crate::workspace::PullFileDiagnosticsParams {
613614
path: path.clone(),
614615
categories: RuleCategories::all(),
615616
max_diagnostics: 100,
@@ -670,7 +671,7 @@ async fn test_cstyle_comments(test_db: PgPool) {
670671
.expect("Unable to open test file");
671672

672673
let diagnostics = workspace
673-
.pull_diagnostics(crate::workspace::PullDiagnosticsParams {
674+
.pull_file_diagnostics(crate::workspace::PullFileDiagnosticsParams {
674675
path: path.clone(),
675676
categories: RuleCategories::all(),
676677
max_diagnostics: 100,
@@ -730,7 +731,7 @@ async fn test_search_path_configuration(test_db: PgPool) {
730731
.expect("Unable to open test file");
731732

732733
let diagnostics_glob = workspace
733-
.pull_diagnostics(crate::workspace::PullDiagnosticsParams {
734+
.pull_file_diagnostics(crate::workspace::PullFileDiagnosticsParams {
734735
path: path_glob.clone(),
735736
categories: RuleCategories::all(),
736737
max_diagnostics: 100,
@@ -776,7 +777,7 @@ async fn test_search_path_configuration(test_db: PgPool) {
776777
.expect("Unable to open test file");
777778

778779
let diagnostics_glob = workspace
779-
.pull_diagnostics(crate::workspace::PullDiagnosticsParams {
780+
.pull_file_diagnostics(crate::workspace::PullFileDiagnosticsParams {
780781
path: path_glob.clone(),
781782
categories: RuleCategories::all(),
782783
max_diagnostics: 100,

crates/pgls_workspace/src/workspace_types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ pub fn methods() -> [WorkspaceMethod; 9] {
462462
workspace_method!(is_path_ignored),
463463
workspace_method!(register_project_folder),
464464
workspace_method!(get_file_content),
465-
workspace_method!(pull_diagnostics),
465+
workspace_method!(pull_file_diagnostics),
466466
workspace_method!(get_completions),
467467
workspace_method!(update_settings),
468468
workspace_method!(open_file),

0 commit comments

Comments
 (0)