Skip to content
Draft
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
2 changes: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[alias]
xtask = "run -p xtask --"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ desktop.ini
node_modules/

**/dist/
.claude-session-id
76 changes: 76 additions & 0 deletions Cargo.lock

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

14 changes: 13 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
[workspace]
members = ["crates/*", "lib/*", "xtask/codegen", "xtask/rules_check", "docs/codegen"]
members = [
"crates/*",
"lib/*",
"xtask/codegen",
"xtask/rules_check",
"xtask/agentic",
"xtask/download_regression_tests",
"docs/codegen",
]
resolver = "2"

[workspace.package]
Expand All @@ -23,7 +31,9 @@ biome_js_syntax = "0.5.7"
biome_rowan = "0.5.7"
biome_string_case = "0.5.8"
bpaf = { version = "0.9.15", features = ["derive"] }
camino = "1.1.9"
crossbeam = "0.8.4"
dir-test = "0.4.1"
enumflags2 = "0.7.11"
ignore = "0.4.23"
indexmap = { version = "2.6.0", features = ["serde"] }
Expand Down Expand Up @@ -78,6 +88,8 @@ pgt_lexer_codegen = { path = "./crates/pgt_lexer_codegen", version = "0
pgt_lsp = { path = "./crates/pgt_lsp", version = "0.0.0" }
pgt_markup = { path = "./crates/pgt_markup", version = "0.0.0" }
pgt_plpgsql_check = { path = "./crates/pgt_plpgsql_check", version = "0.0.0" }
pgt_pretty_print = { path = "./crates/pgt_pretty_print", version = "0.0.0" }
pgt_pretty_print_codegen = { path = "./crates/pgt_pretty_print_codegen", version = "0.0.0" }
pgt_query = { path = "./crates/pgt_query", version = "0.0.0" }
pgt_query_ext = { path = "./crates/pgt_query_ext", version = "0.0.0" }
pgt_query_macros = { path = "./crates/pgt_query_macros", version = "0.0.0" }
Expand Down
21 changes: 21 additions & 0 deletions crates/pgt_pretty_print/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
authors.workspace = true
categories.workspace = true
description = "<DESCRIPTION>"
edition.workspace = true
homepage.workspace = true
keywords.workspace = true
license.workspace = true
name = "pgt_pretty_print"
repository.workspace = true
version = "0.0.0"


[dependencies]
pgt_pretty_print_codegen.workspace = true
pgt_query.workspace = true

[dev-dependencies]
camino.workspace = true
dir-test.workspace = true
insta.workspace = true
1 change: 1 addition & 0 deletions crates/pgt_pretty_print/src/codegen/group_kind.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pgt_pretty_print_codegen::group_kind_codegen!();
2 changes: 2 additions & 0 deletions crates/pgt_pretty_print/src/codegen/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod group_kind;
pub mod token_kind;
1 change: 1 addition & 0 deletions crates/pgt_pretty_print/src/codegen/token_kind.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pgt_pretty_print_codegen::token_kind_codegen!();
123 changes: 123 additions & 0 deletions crates/pgt_pretty_print/src/emitter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
pub use crate::codegen::group_kind::GroupKind;
pub use crate::codegen::token_kind::TokenKind;

#[derive(Debug, Clone, PartialEq)]
pub enum LineType {
/// Must break (semicolon, etc.)
Hard,
/// Break if group doesn't fit
Soft,
/// Break if group doesn't fit, but collapse to space if it does
SoftOrSpace,
}

#[derive(Debug, Clone, PartialEq)]
pub enum LayoutEvent {
Token(TokenKind),
Space,
Line(LineType),
GroupStart {
id: Option<String>,
break_parent: bool,
kind: GroupKind,
},
GroupEnd,
IndentStart,
IndentEnd,
}

pub struct EventEmitter {
pub events: Vec<LayoutEvent>,
}

impl EventEmitter {
pub fn new() -> Self {
Self { events: Vec::new() }
}

pub fn token(&mut self, token: TokenKind) {
self.events.push(LayoutEvent::Token(token));
}

pub fn space(&mut self) {
self.events.push(LayoutEvent::Space);
}

pub fn line(&mut self, line_type: LineType) {
self.events.push(LayoutEvent::Line(line_type));
}

pub fn group_start(&mut self, kind: GroupKind, id: Option<String>, break_parent: bool) {
self.events.push(LayoutEvent::GroupStart {
id,
break_parent,
kind,
});
}

pub fn is_within_group(&self, target_kind: GroupKind) -> bool {
let mut depth = 0;
for event in self.events.iter().rev() {
match event {
LayoutEvent::GroupEnd => depth += 1,
LayoutEvent::GroupStart { kind, .. } => {
if depth == 0 && *kind == target_kind {
return true;
}
if depth > 0 {
depth -= 1;
}
}
_ => {}
}
}
false
}

pub fn parent_group(&self) -> Option<GroupKind> {
let mut depth = 0;
for event in self.events.iter().rev() {
match event {
LayoutEvent::GroupEnd => depth += 1,
LayoutEvent::GroupStart { kind, .. } => {
if depth == 1 {
return Some(kind.clone());
}
if depth > 0 {
depth -= 1;
}
}
_ => {}
}
}
None
}

pub fn is_top_level(&self) -> bool {
let mut depth = 0;
for event in &self.events {
match event {
LayoutEvent::GroupStart { .. } => depth += 1,
LayoutEvent::GroupEnd => depth -= 1,
_ => {}
}
}
depth == 1
}

pub fn group_end(&mut self) {
self.events.push(LayoutEvent::GroupEnd);
}

pub fn indent_start(&mut self) {
self.events.push(LayoutEvent::IndentStart);
}

pub fn indent_end(&mut self) {
self.events.push(LayoutEvent::IndentEnd);
}
}

pub trait ToTokens {
fn to_tokens(&self, emitter: &mut EventEmitter);
}
6 changes: 6 additions & 0 deletions crates/pgt_pretty_print/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
mod codegen;
pub mod emitter;
mod nodes;
pub mod renderer;

pub use crate::codegen::token_kind::TokenKind;
Loading
Loading