Skip to content

Commit 5fcd052

Browse files
committed
fix: diagnostics in docs
1 parent b7a9009 commit 5fcd052

File tree

3 files changed

+50
-17
lines changed

3 files changed

+50
-17
lines changed

docs/codegen/src/rules_docs.rs

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use anyhow::{bail, Result};
22
use biome_string_case::Case;
33
use pglt_analyse::{AnalyserOptions, AnalysisFilter, RuleFilter, RuleMetadata};
44
use pglt_analyser::{Analyser, AnalyserConfig};
5-
use pglt_console::fmt::{Formatter, HTML};
6-
use pglt_console::markup;
5+
use pglt_console::fmt::{Display, Formatter, Termcolor};
6+
use pglt_diagnostics::termcolor::NoColor;
77
use pglt_diagnostics::{Diagnostic, DiagnosticExt, PrintDiagnostic};
88
use pglt_query_ext::diagnostics::SyntaxDiagnostic;
99
use pglt_workspace::settings::Settings;
@@ -17,6 +17,28 @@ use std::{
1717
str::{self, FromStr},
1818
};
1919

20+
/// TODO: get this from jules pr
21+
/// Adapter type providing a std::fmt::Display implementation for any type that
22+
/// implements pglt_console::fmt::Display.
23+
pub struct StdDisplay<T: Display>(pub T);
24+
25+
impl<T> std::fmt::Display for StdDisplay<T>
26+
where
27+
T: Display,
28+
{
29+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
30+
let mut buffer: Vec<u8> = Vec::new();
31+
let mut termcolor = Termcolor(NoColor::new(&mut buffer));
32+
let mut formatter = Formatter::new(&mut termcolor);
33+
34+
self.0.fmt(&mut formatter).map_err(|_| std::fmt::Error)?;
35+
36+
let content = String::from_utf8(buffer).map_err(|_| std::fmt::Error)?;
37+
38+
f.write_str(content.as_str())
39+
}
40+
}
41+
2042
/// Generates the documentation page for each lint rule.
2143
///
2244
/// * `docs_dir`: Path to the docs directory.
@@ -75,7 +97,7 @@ fn generate_rule_doc(
7597
writeln!(content, "> [!NOTE]")?;
7698
writeln!(
7799
content,
78-
"> - This rule is recommended. A diagnostic error will appear when linting your code."
100+
"> This rule is recommended. A diagnostic error will appear when linting your code."
79101
)?;
80102
}
81103

@@ -165,16 +187,13 @@ fn write_documentation(
165187

166188
if let Some((test, block)) = language.take() {
167189
if test.expect_diagnostic {
168-
write!(
169-
content,
170-
"<pre class=\"language-text\"><code class=\"language-text\">"
171-
)?;
190+
writeln!(content, "```sh")?;
172191
}
173192

174193
print_diagnostics(group, rule, &test, &block, content)?;
175194

176195
if test.expect_diagnostic {
177-
writeln!(content, "</code></pre>")?;
196+
writeln!(content, "```")?;
178197
writeln!(content)?;
179198
}
180199
}
@@ -411,12 +430,10 @@ fn print_diagnostics(
411430
) -> Result<()> {
412431
let file_path = format!("code-block.{}", test.tag);
413432

414-
let mut write = HTML::new(content).with_mdx();
415-
416433
let mut write_diagnostic = |_: &str, diag: pglt_diagnostics::Error| -> Result<()> {
417-
Formatter::new(&mut write).write_markup(markup! {
418-
{PrintDiagnostic::verbose(&diag)}
419-
})?;
434+
let printer = PrintDiagnostic::simple(&diag);
435+
writeln!(content, "{}", StdDisplay(printer)).unwrap();
436+
420437
Ok(())
421438
};
422439
if test.ignore {

docs/rules/ban-drop-column.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
**Since**: `vnext`
55
> [!NOTE]
6-
> - This rule is recommended. A diagnostic error will appear when linting your code.
6+
> This rule is recommended. A diagnostic error will appear when linting your code.
77
88
Sources:
99
- Inspired from: <a href="https://squawkhq.com/docs/ban-drop-column" target="_blank"><code>squawk/ban-drop-column</code></a>
@@ -23,7 +23,15 @@ You can leave the column as nullable or delete the column once queries no longer
2323
alter table test drop column id;
2424
```
2525

26-
<pre class="language-text"><code class="language-text">code-block.sql <a href="https://pglt.dev/linter/rules/ban-drop-column">lint/safety/banDropColumn</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━<br /><br /> <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">Dropping a column may break existing clients.</span><br /> <br /> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">You can leave the column as nullable or delete the column once queries no longer select or modify the column.</span><br /> <br /></code></pre>
26+
```sh
27+
code-block.sql lint/safety/banDropColumn ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
28+
29+
× Dropping a column may break existing clients.
30+
31+
i You can leave the column as nullable or delete the column once queries no longer select or modify the column.
32+
33+
34+
```
2735
2836
## How to configure
2937
```toml title="pglt.toml"

docs/rules/ban-drop-not-null.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
**Since**: `vnext`
55
> [!NOTE]
6-
> - This rule is recommended. A diagnostic error will appear when linting your code.
6+
> This rule is recommended. A diagnostic error will appear when linting your code.
77
88
Sources:
99
- Inspired from: <a href="https://squawkhq.com/docs/ban-drop-not-null" target="_blank"><code>squawk/ban-drop-not-null</code></a>
@@ -23,7 +23,15 @@ You can consider using a marker value that represents NULL. Alternatively, creat
2323
alter table users alter column email drop not null;
2424
```
2525

26-
<pre class="language-text"><code class="language-text">code-block.sql <a href="https://pglt.dev/linter/rules/ban-drop-not-null">lint/safety/banDropNotNull</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━<br /><br /> <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">Dropping a NOT NULL constraint may break existing clients.</span><br /> <br /> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Consider using a marker value that represents NULL. Alternatively, create a new table allowing NULL values, copy the data from the old table, and create a view that filters NULL values.</span><br /> <br /></code></pre>
26+
```sh
27+
code-block.sql lint/safety/banDropNotNull ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
28+
29+
× Dropping a NOT NULL constraint may break existing clients.
30+
31+
i Consider using a marker value that represents NULL. Alternatively, create a new table allowing NULL values, copy the data from the old table, and create a view that filters NULL values.
32+
33+
34+
```
2735

2836
## How to configure
2937
```toml title="pglt.toml"

0 commit comments

Comments
 (0)