Skip to content

Commit acce4e3

Browse files
like this maybe?
1 parent 96901d9 commit acce4e3

File tree

12 files changed

+137
-13
lines changed

12 files changed

+137
-13
lines changed

crates/pg_cli/src/execute/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::reporter::github::{GithubReporter, GithubReporterVisitor};
99
use crate::reporter::gitlab::{GitLabReporter, GitLabReporterVisitor};
1010
use crate::reporter::junit::{JunitReporter, JunitReporterVisitor};
1111
use crate::reporter::terminal::{ConsoleReporter, ConsoleReporterVisitor};
12+
use crate::reporter::UserHintsPayload;
1213
use crate::{CliDiagnostic, CliSession, DiagnosticsPayload, Reporter};
1314
use pg_diagnostics::{category, Category};
1415
use pg_fs::PgLspPath;
@@ -242,6 +243,7 @@ pub fn execute_mode(
242243
summary,
243244
evaluated_paths,
244245
diagnostics,
246+
user_hints,
245247
} = traverse(&execution, &mut session, cli_options, paths)?;
246248
let console = session.app.console;
247249
let errors = summary.errors;
@@ -260,6 +262,7 @@ pub fn execute_mode(
260262
},
261263
execution: execution.clone(),
262264
evaluated_paths,
265+
user_hints_payload: UserHintsPayload { hints: user_hints },
263266
};
264267
reporter.write(&mut ConsoleReporterVisitor(console))?;
265268
}
@@ -271,6 +274,7 @@ pub fn execute_mode(
271274
diagnostics,
272275
},
273276
execution: execution.clone(),
277+
user_hints: UserHintsPayload { hints: user_hints },
274278
};
275279
reporter.write(&mut GithubReporterVisitor(console))?;
276280
}
@@ -282,6 +286,7 @@ pub fn execute_mode(
282286
diagnostics,
283287
},
284288
execution: execution.clone(),
289+
user_hints: UserHintsPayload { hints: user_hints },
285290
};
286291
reporter.write(&mut GitLabReporterVisitor::new(
287292
console,
@@ -297,6 +302,7 @@ pub fn execute_mode(
297302
diagnostics,
298303
},
299304
execution: execution.clone(),
305+
user_hints: UserHintsPayload { hints: user_hints },
300306
};
301307
reporter.write(&mut JunitReporterVisitor::new(console))?;
302308
}

crates/pg_cli/src/execute/process_file.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ pub(crate) enum Message {
4646
diagnostics: Vec<Error>,
4747
skipped_diagnostics: u32,
4848
},
49+
Hint(String),
4950
}
5051

5152
impl Message {

crates/pg_cli/src/execute/process_file/check.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub(crate) fn check_with_guard<'ctx>(
2828
let (only, skip) = (Vec::new(), Vec::new());
2929

3030
let max_diagnostics = ctx.remaining_diagnostics.load(Ordering::Relaxed);
31+
3132
let pull_diagnostics_result = workspace_file
3233
.guard()
3334
.pull_diagnostics(
@@ -41,6 +42,10 @@ pub(crate) fn check_with_guard<'ctx>(
4142
category!("check"),
4243
)?;
4344

45+
if pull_diagnostics_result.skipped_db_checks {
46+
ctx.set_skipped_db_conn(true);
47+
}
48+
4449
let no_diagnostics = pull_diagnostics_result.diagnostics.is_empty()
4550
&& pull_diagnostics_result.skipped_diagnostics == 0;
4651

crates/pg_cli/src/execute/traverse.rs

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use pg_workspace::workspace::IsPathIgnoredParams;
1414
use pg_workspace::{Workspace, WorkspaceError};
1515
use rustc_hash::FxHashSet;
1616
use std::collections::BTreeSet;
17-
use std::sync::atomic::AtomicU32;
17+
use std::sync::atomic::{AtomicBool, AtomicU32};
1818
use std::sync::RwLock;
1919
use std::{
2020
env::current_dir,
@@ -33,6 +33,7 @@ pub(crate) struct TraverseResult {
3333
pub(crate) summary: TraversalSummary,
3434
pub(crate) evaluated_paths: BTreeSet<PgLspPath>,
3535
pub(crate) diagnostics: Vec<Error>,
36+
pub(crate) user_hints: Vec<String>,
3637
}
3738

3839
pub(crate) fn traverse(
@@ -72,6 +73,7 @@ pub(crate) fn traverse(
7273
let unchanged = AtomicUsize::new(0);
7374
let matches = AtomicUsize::new(0);
7475
let skipped = AtomicUsize::new(0);
76+
let skipped_db_conn = AtomicBool::new(false);
7577

7678
let fs = &*session.app.fs;
7779
let workspace = &*session.app.workspace;
@@ -84,7 +86,7 @@ pub(crate) fn traverse(
8486
.with_diagnostic_level(cli_options.diagnostic_level)
8587
.with_max_diagnostics(max_diagnostics);
8688

87-
let (duration, evaluated_paths, diagnostics) = thread::scope(|s| {
89+
let (duration, evaluated_paths, diagnostics, mut user_hints) = thread::scope(|s| {
8890
let handler = thread::Builder::new()
8991
.name(String::from("pglsp::console"))
9092
.spawn_scoped(s, || printer.run(receiver, recv_files))
@@ -104,15 +106,16 @@ pub(crate) fn traverse(
104106
changed: &changed,
105107
unchanged: &unchanged,
106108
skipped: &skipped,
109+
skipped_db_conn: &skipped_db_conn,
107110
messages: sender,
108111
remaining_diagnostics: &remaining_diagnostics,
109112
evaluated_paths: RwLock::default(),
110113
},
111114
);
112115
// wait for the main thread to finish
113-
let diagnostics = handler.join().unwrap();
116+
let (diagnostics, user_hints) = handler.join().unwrap();
114117

115-
(elapsed, evaluated_paths, diagnostics)
118+
(elapsed, evaluated_paths, diagnostics, user_hints)
116119
});
117120

118121
let errors = printer.errors();
@@ -123,6 +126,20 @@ pub(crate) fn traverse(
123126
let skipped = skipped.load(Ordering::Relaxed);
124127
let suggested_fixes_skipped = printer.skipped_fixes();
125128
let diagnostics_not_printed = printer.not_printed_diagnostics();
129+
130+
if duration.as_secs() >= 2 {
131+
user_hints.push(format!(
132+
"The traversal took longer than expected ({}s). Consider using the `--skip-db` option if your Postgres connection is slow.",
133+
duration.as_secs()
134+
));
135+
}
136+
137+
if skipped_db_conn.load(Ordering::Relaxed) {
138+
user_hints.push(format!(
139+
"Skipped all checks requiring database connections.",
140+
));
141+
}
142+
126143
Ok(TraverseResult {
127144
summary: TraversalSummary {
128145
changed,
@@ -137,6 +154,7 @@ pub(crate) fn traverse(
137154
},
138155
evaluated_paths,
139156
diagnostics,
157+
user_hints,
140158
})
141159
}
142160

@@ -288,10 +306,15 @@ impl<'ctx> DiagnosticsPrinter<'ctx> {
288306
should_print
289307
}
290308

291-
fn run(&self, receiver: Receiver<Message>, interner: Receiver<PathBuf>) -> Vec<Error> {
309+
fn run(
310+
&self,
311+
receiver: Receiver<Message>,
312+
interner: Receiver<PathBuf>,
313+
) -> (Vec<Error>, Vec<String>) {
292314
let mut paths: FxHashSet<String> = FxHashSet::default();
293315

294316
let mut diagnostics_to_print = vec![];
317+
let mut hints_to_print = vec![];
295318

296319
while let Ok(msg) = receiver.recv() {
297320
match msg {
@@ -306,6 +329,10 @@ impl<'ctx> DiagnosticsPrinter<'ctx> {
306329
self.errors.fetch_add(1, Ordering::Relaxed);
307330
}
308331

332+
Message::Hint(hint) => {
333+
hints_to_print.push(hint);
334+
}
335+
309336
Message::Error(mut err) => {
310337
let location = err.location();
311338
if self.should_skip_diagnostic(err.severity(), err.tags()) {
@@ -381,7 +408,8 @@ impl<'ctx> DiagnosticsPrinter<'ctx> {
381408
}
382409
}
383410
}
384-
diagnostics_to_print
411+
412+
(diagnostics_to_print, hints_to_print)
385413
}
386414
}
387415

@@ -403,6 +431,8 @@ pub(crate) struct TraversalOptions<'ctx, 'app> {
403431
matches: &'ctx AtomicUsize,
404432
/// Shared atomic counter storing the number of skipped files
405433
skipped: &'ctx AtomicUsize,
434+
/// Shared atomic bool tracking whether we used a DB connection
435+
skipped_db_conn: &'ctx AtomicBool,
406436
/// Channel sending messages to the display thread
407437
pub(crate) messages: Sender<Message>,
408438
/// The approximate number of diagnostics the console will print before
@@ -434,6 +464,10 @@ impl TraversalOptions<'_, '_> {
434464
self.messages.send(msg.into()).ok();
435465
}
436466

467+
pub(crate) fn set_skipped_db_conn(&self, has_skipped: bool) {
468+
self.skipped_db_conn.store(has_skipped, Ordering::Relaxed);
469+
}
470+
437471
pub(crate) fn protected_file(&self, pglsp_path: &PgLspPath) {
438472
self.push_diagnostic(
439473
WorkspaceError::protected_file(pglsp_path.display().to_string()).into(),

crates/pg_cli/src/reporter/github.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@ use pg_console::{markup, Console, ConsoleExt};
33
use pg_diagnostics::PrintGitHubDiagnostic;
44
use std::io;
55

6+
use super::UserHintsPayload;
7+
68
pub(crate) struct GithubReporter {
79
pub(crate) diagnostics_payload: DiagnosticsPayload,
810
pub(crate) execution: Execution,
11+
pub(crate) user_hints: UserHintsPayload,
912
}
1013

1114
impl Reporter for GithubReporter {
1215
fn write(self, visitor: &mut dyn ReporterVisitor) -> io::Result<()> {
1316
visitor.report_diagnostics(&self.execution, self.diagnostics_payload)?;
17+
visitor.report_user_hints(&self.execution, self.user_hints)?;
1418
Ok(())
1519
}
1620
}
@@ -42,4 +46,15 @@ impl ReporterVisitor for GithubReporterVisitor<'_> {
4246

4347
Ok(())
4448
}
49+
50+
fn report_user_hints(
51+
&mut self,
52+
_execution: &Execution,
53+
payload: super::UserHintsPayload,
54+
) -> io::Result<()> {
55+
for hint in payload.hints {
56+
self.0.log(markup! {{hint}});
57+
}
58+
Ok(())
59+
}
4560
}

crates/pg_cli/src/reporter/gitlab.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,18 @@ use std::{
1212
path::{Path, PathBuf},
1313
};
1414

15+
use super::UserHintsPayload;
16+
1517
pub struct GitLabReporter {
16-
pub execution: Execution,
17-
pub diagnostics: DiagnosticsPayload,
18+
pub(crate) execution: Execution,
19+
pub(crate) diagnostics: DiagnosticsPayload,
20+
pub(crate) user_hints: UserHintsPayload,
1821
}
1922

2023
impl Reporter for GitLabReporter {
2124
fn write(self, visitor: &mut dyn ReporterVisitor) -> std::io::Result<()> {
2225
visitor.report_diagnostics(&self.execution, self.diagnostics)?;
26+
visitor.report_user_hints(&self.execution, self.user_hints)?;
2327
Ok(())
2428
}
2529
}
@@ -72,6 +76,17 @@ impl ReporterVisitor for GitLabReporterVisitor<'_> {
7276
self.console.log(markup!({ diagnostics }));
7377
Ok(())
7478
}
79+
80+
fn report_user_hints(
81+
&mut self,
82+
_execution: &Execution,
83+
payload: super::UserHintsPayload,
84+
) -> std::io::Result<()> {
85+
for hint in payload.hints {
86+
self.console.log(markup! {{hint}});
87+
}
88+
Ok(())
89+
}
7590
}
7691

7792
struct GitLabDiagnostics<'a>(

crates/pg_cli/src/reporter/junit.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,20 @@ use quick_junit::{NonSuccessKind, Report, TestCase, TestCaseStatus, TestSuite};
66
use std::fmt::{Display, Formatter};
77
use std::io;
88

9+
use super::UserHintsPayload;
10+
911
pub(crate) struct JunitReporter {
1012
pub(crate) diagnostics_payload: DiagnosticsPayload,
1113
pub(crate) execution: Execution,
1214
pub(crate) summary: TraversalSummary,
15+
pub(crate) user_hints: UserHintsPayload,
1316
}
1417

1518
impl Reporter for JunitReporter {
1619
fn write(self, visitor: &mut dyn ReporterVisitor) -> io::Result<()> {
1720
visitor.report_summary(&self.execution, self.summary)?;
1821
visitor.report_diagnostics(&self.execution, self.diagnostics_payload)?;
22+
visitor.report_user_hints(&self.execution, self.user_hints)?;
1923
Ok(())
2024
}
2125
}
@@ -118,4 +122,17 @@ impl ReporterVisitor for JunitReporterVisitor<'_> {
118122

119123
Ok(())
120124
}
125+
126+
fn report_user_hints(
127+
&mut self,
128+
_execution: &Execution,
129+
payload: super::UserHintsPayload,
130+
) -> io::Result<()> {
131+
for hint in payload.hints {
132+
self.1.log(markup! {
133+
{hint}
134+
});
135+
}
136+
Ok(())
137+
}
121138
}

crates/pg_cli/src/reporter/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ pub struct DiagnosticsPayload {
1717
pub diagnostic_level: Severity,
1818
}
1919

20+
pub struct UserHintsPayload {
21+
pub hints: Vec<String>,
22+
}
23+
2024
/// A type that holds the result of the traversal
2125
#[derive(Debug, Default, Serialize, Copy, Clone)]
2226
pub struct TraversalSummary {
@@ -60,4 +64,11 @@ pub trait ReporterVisitor {
6064
execution: &Execution,
6165
payload: DiagnosticsPayload,
6266
) -> io::Result<()>;
67+
68+
/// Writes a diagnostics
69+
fn report_user_hints(
70+
&mut self,
71+
execution: &Execution,
72+
payload: UserHintsPayload,
73+
) -> io::Result<()>;
6374
}

crates/pg_cli/src/reporter/terminal.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,22 @@ use std::collections::BTreeSet;
1010
use std::io;
1111
use std::time::Duration;
1212

13+
use super::UserHintsPayload;
14+
1315
pub(crate) struct ConsoleReporter {
1416
pub(crate) summary: TraversalSummary,
1517
pub(crate) diagnostics_payload: DiagnosticsPayload,
1618
pub(crate) execution: Execution,
1719
pub(crate) evaluated_paths: BTreeSet<PgLspPath>,
20+
pub(crate) user_hints_payload: UserHintsPayload,
1821
}
1922

2023
impl Reporter for ConsoleReporter {
2124
fn write(self, visitor: &mut dyn ReporterVisitor) -> io::Result<()> {
2225
let verbose = self.diagnostics_payload.verbose;
2326
visitor.report_diagnostics(&self.execution, self.diagnostics_payload)?;
2427
visitor.report_summary(&self.execution, self.summary)?;
28+
visitor.report_user_hints(&self.execution, self.user_hints_payload)?;
2529
if verbose {
2630
visitor.report_handled_paths(self.evaluated_paths)?;
2731
}
@@ -115,6 +119,18 @@ impl ReporterVisitor for ConsoleReporterVisitor<'_> {
115119

116120
Ok(())
117121
}
122+
123+
fn report_user_hints(
124+
&mut self,
125+
_execution: &Execution,
126+
payload: UserHintsPayload,
127+
) -> io::Result<()> {
128+
for hint in payload.hints {
129+
self.0.log(markup! {{hint}});
130+
}
131+
self.0.log(markup! {{"\n"}});
132+
Ok(())
133+
}
118134
}
119135

120136
struct Files(usize);

0 commit comments

Comments
 (0)