Skip to content

Commit

Permalink
Merge pull request #7384 from gamebox/fix-snake-case-formatting
Browse files Browse the repository at this point in the history
Fix snake case formatting
  • Loading branch information
Anton-4 authored Dec 17, 2024
2 parents 5178c2a + 310acf0 commit 2c929c2
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 14 deletions.
26 changes: 21 additions & 5 deletions crates/compiler/fmt/src/annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
collection::{fmt_collection, Braces},
expr::{format_spaces, merge_spaces_conservative},
node::{Node, NodeSequenceBuilder, Nodify, Sp},
pattern::pattern_lift_spaces_after,
pattern::{pattern_lift_spaces_after, snakify_camel_ident},
spaces::{fmt_comments_only, fmt_spaces, NewlineAt, INDENT},
Buf,
};
Expand Down Expand Up @@ -775,7 +775,11 @@ fn format_assigned_field_help<T>(
}

buf.indent(indent);
buf.push_str(name.value);
if buf.flags().snakify {
snakify_camel_ident(buf, name.value);
} else {
buf.push_str(name.value);
}

if !spaces.is_empty() {
fmt_spaces(buf, spaces.iter(), indent);
Expand All @@ -793,7 +797,11 @@ fn format_assigned_field_help<T>(
}

buf.indent(indent);
buf.push_str(name.value);
if buf.flags().snakify {
snakify_camel_ident(buf, name.value);
} else {
buf.push_str(name.value);
}

if !spaces.is_empty() {
fmt_spaces(buf, spaces.iter(), indent);
Expand All @@ -812,7 +820,11 @@ fn format_assigned_field_help<T>(

buf.indent(indent);
buf.push('_');
buf.push_str(name.value);
if buf.flags().snakify {
snakify_camel_ident(buf, name.value);
} else {
buf.push_str(name.value);
}

if !spaces.is_empty() {
fmt_spaces(buf, spaces.iter(), indent);
Expand All @@ -830,7 +842,11 @@ fn format_assigned_field_help<T>(
}

buf.indent(indent);
buf.push_str(name.value);
if buf.flags().snakify {
snakify_camel_ident(buf, name.value);
} else {
buf.push_str(name.value);
}
}
AssignedField::SpaceBefore(sub_field, spaces) => {
fmt_comments_only(buf, spaces.iter(), NewlineAt::Bottom, indent);
Expand Down
36 changes: 29 additions & 7 deletions crates/compiler/fmt/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use crate::annotation::{except_last, is_collection_multiline, Formattable, Newli
use crate::collection::{fmt_collection, Braces};
use crate::def::{fmt_defs, valdef_lift_spaces_before};
use crate::pattern::{
fmt_pattern, pattern_lift_spaces, pattern_lift_spaces_before, starts_with_inline_comment,
fmt_pattern, pattern_lift_spaces, pattern_lift_spaces_before, snakify_camel_ident,
starts_with_inline_comment,
};
use crate::spaces::{
count_leading_newlines, fmt_comments_only, fmt_spaces, fmt_spaces_no_blank_lines,
Expand Down Expand Up @@ -72,8 +73,11 @@ fn format_expr_only(
buf.push_str(module_name);
buf.push('.');
}

buf.push_str(ident);
if buf.flags().snakify {
snakify_camel_ident(buf, ident);
} else {
buf.push_str(ident);
}
}
Expr::Underscore(name) => {
buf.indent(indent);
Expand Down Expand Up @@ -303,19 +307,33 @@ fn format_expr_only(
buf.indent(indent);
buf.push('.');
match key {
Accessor::RecordField(key) => buf.push_str(key),
Accessor::RecordField(key) => {
if buf.flags().snakify {
snakify_camel_ident(buf, key);
} else {
buf.push_str(key);
}
}
Accessor::TupleIndex(key) => buf.push_str(key),
}
}
Expr::RecordUpdater(key) => {
buf.indent(indent);
buf.push('&');
buf.push_str(key);
if buf.flags().snakify {
snakify_camel_ident(buf, key);
} else {
buf.push_str(key);
}
}
Expr::RecordAccess(expr, key) => {
expr.format_with_options(buf, Parens::InApply, Newlines::Yes, indent);
buf.push('.');
buf.push_str(key);
if buf.flags().snakify {
snakify_camel_ident(buf, key);
} else {
buf.push_str(key);
}
}
Expr::TupleAccess(expr, key) => {
expr.format_with_options(buf, Parens::InApply, Newlines::Yes, indent);
Expand All @@ -331,7 +349,11 @@ fn format_expr_only(
}
Expr::MalformedIdent(str, _) => {
buf.indent(indent);
buf.push_str(str)
if buf.flags().snakify {
snakify_camel_ident(buf, str);
} else {
buf.push_str(str);
}
}
Expr::MalformedSuffixed(loc_expr) => {
buf.indent(indent);
Expand Down
13 changes: 12 additions & 1 deletion crates/compiler/fmt/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::cmp::max;
use crate::annotation::{is_collection_multiline, Formattable, Newlines, Parens};
use crate::collection::{fmt_collection, Braces};
use crate::expr::fmt_str_literal;
use crate::pattern::snakify_camel_ident;
use crate::spaces::{fmt_comments_only, fmt_default_spaces, fmt_spaces, NewlineAt, INDENT};
use crate::Buf;
use roc_parse::ast::{Collection, CommentOrNewline, Header, Spaced, Spaces, SpacesBefore};
Expand Down Expand Up @@ -410,7 +411,17 @@ impl<'a> Formattable for ExposedName<'a> {
indent: u16,
) {
buf.indent(indent);
buf.push_str(self.as_str());
if buf.flags().snakify
&& self
.as_str()
.chars()
.next()
.is_some_and(|c| c.is_ascii_lowercase())
{
snakify_camel_ident(buf, self.as_str());
} else {
buf.push_str(self.as_str());
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/compiler/fmt/src/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ pub fn pattern_lift_spaces_after<'a, 'b: 'a>(
}

/// Convert camelCase identifier to snake case
fn snakify_camel_ident(buf: &mut Buf, string: &str) {
pub fn snakify_camel_ident(buf: &mut Buf, string: &str) {
let chars: Vec<char> = string.chars().collect();
if !buf.flags().snakify || (string.contains('_') && !string.ends_with('_')) {
buf.push_str(string);
Expand Down

0 comments on commit 2c929c2

Please sign in to comment.