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
34 changes: 32 additions & 2 deletions src/parser/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,40 @@ fn extract_name_value_nodes<'a>(node: &'a Node<'a>, context: &str) -> Result<(No
Ok((name_node, value_node))
}

/// Parse a string node, removing quotes
/// Parse a string node, removing quotes and processing escape sequences
fn parse_string_node(node: &Node, source: &SourceTree) -> String {
let text = source.get_text(node);
text.trim_matches(|c| c == '\'' || c == '"').to_string()
let unquoted = text.trim_matches(|c| c == '\'' || c == '"');
process_escape_sequences(unquoted)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This has bothered me a few times as well (but not enough to file an issue apparently)

}

/// Process escape sequences in a string (e.g., \n, \t, \\, \')
fn process_escape_sequences(s: &str) -> String {
let mut result = String::with_capacity(s.len());
let mut chars = s.chars().peekable();

while let Some(c) = chars.next() {
if c == '\\' {
match chars.next() {
Some('n') => result.push('\n'),
Some('t') => result.push('\t'),
Some('r') => result.push('\r'),
Some('\\') => result.push('\\'),
Some('\'') => result.push('\''),
Some('"') => result.push('"'),
Some(other) => {
// Unknown escape sequence - keep as-is
result.push('\\');
result.push(other);
}
None => result.push('\\'), // Trailing backslash
}
} else {
result.push(c);
}
}

result
}

/// Parse a number node into f64
Expand Down
64 changes: 63 additions & 1 deletion src/writer/vegalite/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,65 @@ impl VegaLiteWriter {
schema: "https://vega.github.io/schema/vega-lite/v6.json".to_string(),
}
}

/// Build default Vega-Lite config matching ggplot2's theme_gray()
///
/// Font sizes converted from ggplot2 points to pixels (1 pt ≈ 1.33 px at 96 DPI):
/// - axis.text: 8.8 pts (rel(0.8) × 11) → 12 px
/// - axis.title: 11 pts → 15 px
/// - legend.text: 8.8 pts → 12 px
/// - legend.title: 11 pts → 15 px
/// - plot.title: 13.2 pts (rel(1.2) × 11) → 18 px
/// - tick size: ~2.75 pts → 4 px
fn default_theme_config(&self) -> Value {
json!({
"view": {
"stroke": null,
"fill": "#EBEBEB"
},
"axis": {
"domain": false,
"grid": true,
"gridColor": "#FFFFFF",
"gridWidth": 1,
"tickColor": "#333333",
"tickSize": 4,
"labelColor": "#4D4D4D",
"labelFontSize": 12,
"titleColor": "#000000",
"titleFontSize": 15,
"titleFontWeight": "normal",
"titlePadding": 10
},
"legend": {
"labelColor": "#4D4D4D",
"labelFontSize": 12,
"titleColor": "#000000",
"titleFontSize": 15,
"titleFontWeight": "normal",
"titlePadding": 8,
"rowPadding": 6
},
"title": {
"color": "#000000",
"fontSize": 18,
"fontWeight": "normal",
"subtitleColor": "#4D4D4D",
"subtitleFontSize": 15,
"subtitleFontWeight": "normal",
"anchor": "start",
"frame": "group",
"offset": 10
},
"header": {
"labelColor": "#000000",
"labelFontSize": 15,
"labelFontWeight": "normal",
"labelPadding": 5,
"title": null
}
})
}
}

impl Default for VegaLiteWriter {
Expand Down Expand Up @@ -439,7 +498,10 @@ impl Writer for VegaLiteWriter {
apply_faceting(&mut vl_spec, facet, facet_df);
}

// 11. Serialize
// 11. Add default theme config (ggplot2-like gray theme)
vl_spec["config"] = self.default_theme_config();

// 12. Serialize
serde_json::to_string_pretty(&vl_spec).map_err(|e| {
GgsqlError::WriterError(format!("Failed to serialize Vega-Lite JSON: {}", e))
})
Expand Down
2 changes: 1 addition & 1 deletion tree-sitter-ggsql/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,7 @@ module.exports = grammar({
)
)),

string: $ => seq("'", repeat(choice(/[^'\\]/, seq('\\', /.*/))), "'"),
string: $ => seq("'", repeat(choice(/[^'\\]/, /\\./)), "'"),

boolean: $ => choice('true', 'false'),

Expand Down