From 253906fb93f25481ae80af9043b58342858e156c Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 22 Feb 2021 16:00:21 -0700 Subject: [PATCH] zig fmt: 2nd arg multiline string --- lib/std/zig/parser_test.zig | 39 ++++++++++++++++++++++++++----------- lib/std/zig/render.zig | 25 ++++++++++++++++-------- 2 files changed, 45 insertions(+), 19 deletions(-) diff --git a/lib/std/zig/parser_test.zig b/lib/std/zig/parser_test.zig index 7bae2f3162..109d9e9620 100644 --- a/lib/std/zig/parser_test.zig +++ b/lib/std/zig/parser_test.zig @@ -1280,17 +1280,34 @@ test "zig fmt: async call in if condition" { ); } -//test "zig fmt: 2nd arg multiline string" { -// try testCanonical( -// \\comptime { -// \\ cases.addAsm("hello world linux x86_64", -// \\ \\.text -// \\ , "Hello, world!\n"); -// \\} -// \\ -// ); -//} -// +test "zig fmt: 2nd arg multiline string" { + try testCanonical( + \\comptime { + \\ cases.addAsm("hello world linux x86_64", + \\ \\.text + \\ , "Hello, world!\n"); + \\} + \\ + ); + try testTransform( + \\comptime { + \\ cases.addAsm("hello world linux x86_64", + \\ \\.text + \\ , "Hello, world!\n",); + \\} + , + \\comptime { + \\ cases.addAsm( + \\ "hello world linux x86_64", + \\ \\.text + \\ , + \\ "Hello, world!\n", + \\ ); + \\} + \\ + ); +} + //test "zig fmt: 2nd arg multiline string many args" { // try testCanonical( // \\comptime { diff --git a/lib/std/zig/render.zig b/lib/std/zig/render.zig index 67a7a9a514..105be0c9c4 100644 --- a/lib/std/zig/render.zig +++ b/lib/std/zig/render.zig @@ -1879,23 +1879,23 @@ fn renderCall( const after_last_param_tok = tree.lastToken(last_param) + 1; if (token_tags[after_last_param_tok] == .comma) { ais.pushIndentNextLine(); - try renderToken(ais, tree, lparen, Space.newline); // ( + try renderToken(ais, tree, lparen, .newline); // ( for (params) |param_node, i| { if (i + 1 < params.len) { - try renderExpression(ais, tree, param_node, Space.none); + try renderExpression(ais, tree, param_node, .none); - // Unindent the comma for multiline string literals + // Unindent the comma for multiline string literals. const is_multiline_string = node_tags[param_node] == .multiline_string_literal; if (is_multiline_string) ais.popIndent(); const comma = tree.lastToken(param_node) + 1; - try renderToken(ais, tree, comma, Space.newline); // , + try renderToken(ais, tree, comma, .newline); // , if (is_multiline_string) ais.pushIndent(); try renderExtraNewline(ais, tree, params[i + 1]); } else { - try renderExpression(ais, tree, param_node, Space.comma); + try renderExpression(ais, tree, param_node, .comma); } } ais.popIndent(); @@ -1903,14 +1903,23 @@ fn renderCall( } ais.pushIndentNextLine(); - try renderToken(ais, tree, lparen, Space.none); // ( + try renderToken(ais, tree, lparen, .none); // ( for (params) |param_node, i| { - try renderExpression(ais, tree, param_node, Space.none); + try renderExpression(ais, tree, param_node, .none); if (i + 1 < params.len) { const comma = tree.lastToken(param_node) + 1; - try renderToken(ais, tree, comma, Space.space); + const this_multiline_string = node_tags[param_node] == .multiline_string_literal; + const next_multiline_string = node_tags[params[i + 1]] == .multiline_string_literal; + const comma_space: Space = if (next_multiline_string) .none else .space; + if (this_multiline_string) { + ais.popIndent(); + try renderToken(ais, tree, comma, comma_space); + ais.pushIndent(); + } else { + try renderToken(ais, tree, comma, comma_space); + } } }