From 5c057c3b7c1a8233c8cf1fc07fd2be7513974697 Mon Sep 17 00:00:00 2001 From: Takeru Ohta Date: Mon, 26 Aug 2024 17:53:53 +0900 Subject: [PATCH] Fix a bug when formatting consecutive `-define` directives with single-quoted macro names. --- efmt_core/src/items/module.rs | 6 +++++- tests/testdata/aligned_macro.erl | 4 ++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/efmt_core/src/items/module.rs b/efmt_core/src/items/module.rs index 641a854..46826e6 100644 --- a/efmt_core/src/items/module.rs +++ b/efmt_core/src/items/module.rs @@ -121,7 +121,11 @@ impl<'a> FormatState<'a> { let indent = self .pending_constants .iter() - .map(|define| "-define(".len() + define.macro_name().len() + ", ".len()) + .map(|define| { + let macro_name_len = define.macro_name_token().end_position().offset() + - define.macro_name_token().start_position().offset(); + "-define(".len() + macro_name_len + ", ".len() + }) .max() .expect("unreachable"); diff --git a/tests/testdata/aligned_macro.erl b/tests/testdata/aligned_macro.erl index b9d7ec9..d160236 100644 --- a/tests/testdata/aligned_macro.erl +++ b/tests/testdata/aligned_macro.erl @@ -23,3 +23,7 @@ %% bar -define(ccc, 3). -define(dddd, 4). + +-define('meow-1', "Meow"). +-define('meow-2', "Meow Meow"). +-define('meow-3', "Meow Meow Meow").