Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ categories = ["text-processing", "development-tools"]

[dependencies]
tree-sitter = "0.26"
tree-sitter-postgres = "1.2"
tree-sitter-postgres = "1.2.2"

[dev-dependencies]
pretty_assertions = "1"
28 changes: 28 additions & 0 deletions examples/dump_plpgsql.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use tree_sitter::Parser;
use tree_sitter_postgres::LANGUAGE_PLPGSQL;

fn print_tree(node: tree_sitter::Node, source: &str, indent: usize) {
let kind = node.kind();
let text = &source[node.byte_range()];
let short = if text.len() > 60 { &text[..60] } else { text };

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

cd /tmp && find . -name "dump_plpgsql.rs" -type f 2>/dev/null | head -5

Repository: gmr/libpgfmt

Length of output: 38


🏁 Script executed:

git ls-files examples/dump_plpgsql.rs

Repository: gmr/libpgfmt

Length of output: 81


🏁 Script executed:

cat -n examples/dump_plpgsql.rs | head -20

Repository: gmr/libpgfmt

Length of output: 822


Use UTF-8-safe truncation for preview text.

Line 7 can panic on valid SQL containing multibyte characters because byte slicing ([..60]) may cut through a UTF-8 code point.

Proposed fix
-    let short = if text.len() > 60 { &text[..60] } else { text };
+    let short = if text.len() > 60 { text.chars().take(60).collect::<String>() } else { text.to_string() };
     let short = short.replace('\n', "\\n");
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
let short = if text.len() > 60 { &text[..60] } else { text };
let short = if text.len() > 60 { text.chars().take(60).collect::<String>() } else { text.to_string() };
let short = short.replace('\n', "\\n");
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@examples/dump_plpgsql.rs` at line 7, The short variable assignment on line 7
uses byte slicing with [..60] which can panic if it cuts through a multibyte
UTF-8 character. Replace this byte-level truncation with character-safe
truncation by iterating through the string's characters and truncating after the
60th character, or by finding the byte boundary that corresponds to the 60th
character to ensure the slice always ends at a valid UTF-8 boundary.

let short = short.replace('\n', "\\n");
let pad = " ".repeat(indent);
if node.is_named() {
println!("{pad}{kind}: {short:?}");
} else {
println!("{pad}[{kind}]: {short:?}");
}
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
print_tree(child, source, indent + 1);
}
}

fn main() {
let path = std::env::args().nth(1).expect("usage: dump_plpgsql <file>");
let sql = std::fs::read_to_string(&path).unwrap();
let mut parser = Parser::new();
parser.set_language(&LANGUAGE_PLPGSQL.into()).unwrap();
let tree = parser.parse(sql.trim(), None).unwrap();
print_tree(tree.root_node(), sql.trim(), 0);
}
16 changes: 16 additions & 0 deletions examples/format_plpgsql_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use libpgfmt::{format_plpgsql, style::Style};
fn main() {
let sql = std::fs::read_to_string(std::env::args().nth(1).unwrap()).unwrap();
let style: Style = std::env::args()
.nth(2)
.unwrap_or("aweber".to_string())
.parse()
.unwrap();
match format_plpgsql(sql.trim(), style) {
Ok(f) => println!("{f}"),
Err(e) => {
eprintln!("Error: {e}");
std::process::exit(1);
}
}
}
3 changes: 2 additions & 1 deletion src/formatter/plpgsql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,9 @@ impl<'a> Formatter<'a> {
self.kw("THEN")
));
i += 3; // skip cond and THEN
} else {
i += 1; // closing IF (END IF)
}
// Closing IF (END IF).
}
"sql_expression" => {
i += 1; // handled with IF/ELSIF
Expand Down
33 changes: 33 additions & 0 deletions tests/plpgsql_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use libpgfmt::{format_plpgsql, style::Style};

// Regression: a plain `IF ... THEN ... END IF` previously hung forever because
// format_stmt_if never advanced past the closing `kw_if` of `END IF`.
#[test]
fn if_then_end_if_terminates() {
let body = "BEGIN\n IF x = 1\n THEN\n v := y;\n END IF;\nEND";
let result = format_plpgsql(body, Style::Aweber).unwrap();
let expected = "\
BEGIN
IF x = 1 THEN
v := y;
END IF;
END;";
assert_eq!(result, expected, "\nGot:\n{result}");
}

#[test]
fn if_elsif_else_terminates() {
let body = "BEGIN\n IF a THEN\n v := 1;\n ELSIF b THEN\n v := 2;\n ELSE\n v := 3;\n END IF;\nEND";
let result = format_plpgsql(body, Style::Aweber).unwrap();
let expected = "\
BEGIN
IF a THEN
v := 1;
ELSIF b THEN
v := 2;
ELSE
v := 3;
END IF;
END;";
assert_eq!(result, expected, "\nGot:\n{result}");
}
Loading