From: Daniel Jasper Date: Wed, 17 Jun 2015 09:44:07 +0000 (+0000) Subject: clang-format: [JS] Don't put top-level typescript enums on a single line. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e9b9cc12c5380d07188d7ed37b6764a864e73acc;p=clang clang-format: [JS] Don't put top-level typescript enums on a single line. This makes this consistent with non-typescript enums. Also shuffle the language-dependent stuff in mustBreakBefore to a single location. Patch initiated by Martin Probst. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@239894 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index a332b62ce8..8ba2eaf8d2 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -2010,6 +2010,40 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, if (Right.NewlinesBefore > 1) return true; + if (Style.Language == FormatStyle::LK_JavaScript) { + // FIXME: This might apply to other languages and token kinds. + if (Right.is(tok::char_constant) && Left.is(tok::plus) && Left.Previous && + Left.Previous->is(tok::char_constant)) + return true; + if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace) && + Line.Level == 0 && Left.Previous && + Left.Previous->is(tok::equal) && + Line.First->isOneOf(tok::identifier, Keywords.kw_import, + tok::kw_export, tok::kw_const) && + // kw_var is a pseudo-token that's a tok::identifier, so matches above. + !Line.startsWith(Keywords.kw_var)) + // Object literals on the top level of a file are treated as "enum-style". + // Each key/value pair is put on a separate line, instead of bin-packing. + return true; + if (Left.is(tok::l_brace) && Line.Level == 0 && + (Line.startsWith(tok::kw_enum) || + Line.startsWith(tok::kw_export, tok::kw_enum))) + // JavaScript top-level enum key/value pairs are put on separate lines + // instead of bin-packing. + return true; + if (Right.is(tok::r_brace) && Left.is(tok::l_brace) && + !Left.Children.empty()) + // Support AllowShortFunctionsOnASingleLine for JavaScript. + return Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_None || + (Left.NestingLevel == 0 && Line.Level == 0 && + Style.AllowShortFunctionsOnASingleLine == + FormatStyle::SFS_Inline); + } else if (Style.Language == FormatStyle::LK_Java) { + if (Right.is(tok::plus) && Left.is(tok::string_literal) && Right.Next && + Right.Next->is(tok::string_literal)) + return true; + } + // If the last token before a '}' is a comma or a trailing comment, the // intention is to insert a line break after it in order to make shuffling // around entries easier. @@ -2060,12 +2094,6 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, return true; if (Right.is(TT_InlineASMBrace)) return Right.HasUnescapedNewline; - if (Style.Language == FormatStyle::LK_JavaScript && Right.is(tok::r_brace) && - Left.is(tok::l_brace) && !Left.Children.empty()) - // Support AllowShortFunctionsOnASingleLine for JavaScript. - return Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_None || - (Left.NestingLevel == 0 && Line.Level == 0 && - Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_Inline); if (isAllmanBrace(Left) || isAllmanBrace(Right)) return Style.BreakBeforeBraces == FormatStyle::BS_Allman || Style.BreakBeforeBraces == FormatStyle::BS_GNU; @@ -2082,27 +2110,6 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, Line.Last->is(tok::l_brace)) return true; - if (Style.Language == FormatStyle::LK_JavaScript) { - // FIXME: This might apply to other languages and token kinds. - if (Right.is(tok::char_constant) && Left.is(tok::plus) && Left.Previous && - Left.Previous->is(tok::char_constant)) - return true; - if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace) && - Line.Level == 0 && Left.Previous && - Left.Previous->is(tok::equal) && - Line.First->isOneOf(tok::identifier, Keywords.kw_import, - tok::kw_export, tok::kw_const) && - // kw_var is a pseudo-token that's a tok::identifier, so matches above. - !Line.startsWith(Keywords.kw_var)) - // Object literals on the top level of a file are treated as "enum-style". - // Each key/value pair is put on a separate line, instead of bin-packing. - return true; - } else if (Style.Language == FormatStyle::LK_Java) { - if (Right.is(tok::plus) && Left.is(tok::string_literal) && Right.Next && - Right.Next->is(tok::string_literal)) - return true; - } - return false; } diff --git a/unittests/Format/FormatTestJS.cpp b/unittests/Format/FormatTestJS.cpp index 2911f0eb24..2c20ab058e 100644 --- a/unittests/Format/FormatTestJS.cpp +++ b/unittests/Format/FormatTestJS.cpp @@ -689,6 +689,14 @@ TEST_F(FormatTestJS, InterfaceDeclarations) { } TEST_F(FormatTestJS, EnumDeclarations) { + verifyFormat("enum Foo {\n" + " A = 1,\n" + " B\n" + "}"); + verifyFormat("export /* somecomment*/ enum Foo {\n" + " A = 1,\n" + " B\n" + "}"); verifyFormat("enum Foo {\n" " A = 1, // comment\n" " B\n"