Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(92)

Side by Side Diff: tools/gn/command_format.cc

Issue 770053002: gn format: Special case for single arg function calls containing boolean ops (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@gn-unnecessary-parens
Patch Set: if too Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | tools/gn/command_format_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <sstream> 5 #include <sstream>
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/files/file_util.h" 8 #include "base/files/file_util.h"
9 #include "base/strings/string_split.h" 9 #include "base/strings/string_split.h"
10 #include "tools/gn/commands.h" 10 #include "tools/gn/commands.h"
(...skipping 668 matching lines...) Expand 10 before | Expand all | Expand 10 after
679 679
680 // Calculate the penalties for 3 possible layouts: 680 // Calculate the penalties for 3 possible layouts:
681 // 1. all on same line; 681 // 1. all on same line;
682 // 2. starting on same line, broken at each comma but paren aligned; 682 // 2. starting on same line, broken at each comma but paren aligned;
683 // 3. broken to next line + 4, broken at each comma. 683 // 3. broken to next line + 4, broken at each comma.
684 std::string terminator = ")"; 684 std::string terminator = ")";
685 if (have_block) 685 if (have_block)
686 terminator += " {"; 686 terminator += " {";
687 terminator += suffix; 687 terminator += suffix;
688 688
689 // Special case to make function calls of one arg taking a long list of
690 // boolean operators not indent.
691 bool continuation_requires_indent =
692 list.size() != 1 || !list[0]->AsBinaryOp() ||
693 (list[0]->AsBinaryOp()->op().value() != "||" &&
694 list[0]->AsBinaryOp()->op().value() != "&&");
695
689 // 1: Same line. 696 // 1: Same line.
690 Printer sub1; 697 Printer sub1;
691 InitializeSub(&sub1); 698 InitializeSub(&sub1);
692 sub1.stack_.push_back(IndentState(CurrentColumn(), true, false)); 699 sub1.stack_.push_back(
700 IndentState(CurrentColumn(), continuation_requires_indent, false));
693 int penalty_one_line = 0; 701 int penalty_one_line = 0;
694 for (size_t i = 0; i < list.size(); ++i) { 702 for (size_t i = 0; i < list.size(); ++i) {
695 penalty_one_line += sub1.Expr(list[i], kPrecedenceLowest, 703 penalty_one_line += sub1.Expr(list[i], kPrecedenceLowest,
696 i < list.size() - 1 ? ", " : std::string()); 704 i < list.size() - 1 ? ", " : std::string());
697 } 705 }
698 sub1.Print(terminator); 706 sub1.Print(terminator);
699 penalty_one_line += AssessPenalty(sub1.String()); 707 penalty_one_line += AssessPenalty(sub1.String());
700 // This extra penalty prevents a short second argument from being squeezed in 708 // This extra penalty prevents a short second argument from being squeezed in
701 // after a first argument that went multiline (and instead preferring a 709 // after a first argument that went multiline (and instead preferring a
702 // variant below). 710 // variant below).
703 penalty_one_line += 711 penalty_one_line +=
704 (CountLines(sub1.String()) - 1) * kPenaltyBrokenLineOnOneLiner; 712 (CountLines(sub1.String()) - 1) * kPenaltyBrokenLineOnOneLiner;
705 713
706 // 2: Starting on same line, broken at commas. 714 // 2: Starting on same line, broken at commas.
707 Printer sub2; 715 Printer sub2;
708 InitializeSub(&sub2); 716 InitializeSub(&sub2);
709 sub2.stack_.push_back(IndentState(CurrentColumn(), true, false)); 717 sub2.stack_.push_back(
718 IndentState(CurrentColumn(), continuation_requires_indent, false));
710 int penalty_multiline_start_same_line = 0; 719 int penalty_multiline_start_same_line = 0;
711 for (size_t i = 0; i < list.size(); ++i) { 720 for (size_t i = 0; i < list.size(); ++i) {
712 penalty_multiline_start_same_line += sub2.Expr( 721 penalty_multiline_start_same_line += sub2.Expr(
713 list[i], kPrecedenceLowest, i < list.size() - 1 ? "," : std::string()); 722 list[i], kPrecedenceLowest, i < list.size() - 1 ? "," : std::string());
714 if (i < list.size() - 1) { 723 if (i < list.size() - 1) {
715 sub2.Newline(); 724 sub2.Newline();
716 } 725 }
717 } 726 }
718 sub2.Print(terminator); 727 sub2.Print(terminator);
719 penalty_multiline_start_same_line += AssessPenalty(sub2.String()); 728 penalty_multiline_start_same_line += AssessPenalty(sub2.String());
720 729
721 // 3: Starting on next line, broken at commas. 730 // 3: Starting on next line, broken at commas.
722 Printer sub3; 731 Printer sub3;
723 InitializeSub(&sub3); 732 InitializeSub(&sub3);
724 sub3.stack_.push_back(IndentState(margin() + kIndentSize * 2, true, false)); 733 sub3.stack_.push_back(IndentState(margin() + kIndentSize * 2,
734 continuation_requires_indent, false));
725 sub3.Newline(); 735 sub3.Newline();
726 int penalty_multiline_start_next_line = 0; 736 int penalty_multiline_start_next_line = 0;
727 for (size_t i = 0; i < list.size(); ++i) { 737 for (size_t i = 0; i < list.size(); ++i) {
728 if (i == 0) { 738 if (i == 0) {
729 penalty_multiline_start_next_line += 739 penalty_multiline_start_next_line +=
730 std::abs(sub3.CurrentColumn() - start_column) * 740 std::abs(sub3.CurrentColumn() - start_column) *
731 kPenaltyHorizontalSeparation; 741 kPenaltyHorizontalSeparation;
732 } 742 }
733 penalty_multiline_start_next_line += sub3.Expr( 743 penalty_multiline_start_next_line += sub3.Expr(
734 list[i], kPrecedenceLowest, i < list.size() - 1 ? "," : std::string()); 744 list[i], kPrecedenceLowest, i < list.size() - 1 ? "," : std::string());
(...skipping 15 matching lines...) Expand all
750 force_multiline = true; 760 force_multiline = true;
751 } 761 }
752 } else { 762 } else {
753 force_multiline = true; 763 force_multiline = true;
754 } 764 }
755 765
756 if (list.size() == 0 && !force_multiline) { 766 if (list.size() == 0 && !force_multiline) {
757 // No elements, and not forcing newlines, print nothing. 767 // No elements, and not forcing newlines, print nothing.
758 } else { 768 } else {
759 if (penalty_multiline_start_next_line < penalty_multiline_start_same_line) { 769 if (penalty_multiline_start_next_line < penalty_multiline_start_same_line) {
760 stack_.push_back(IndentState(margin() + kIndentSize * 2, true, false)); 770 stack_.push_back(IndentState(margin() + kIndentSize * 2,
771 continuation_requires_indent,
772 false));
761 Newline(); 773 Newline();
762 } else { 774 } else {
763 stack_.push_back(IndentState(CurrentColumn(), true, false)); 775 stack_.push_back(
776 IndentState(CurrentColumn(), continuation_requires_indent, false));
764 } 777 }
765 778
766 for (size_t i = 0; i < list.size(); ++i) { 779 for (size_t i = 0; i < list.size(); ++i) {
767 const auto& x = list[i]; 780 const auto& x = list[i];
768 if (i > 0) { 781 if (i > 0) {
769 if (fits_on_current_line && !force_multiline) 782 if (fits_on_current_line && !force_multiline)
770 Print(" "); 783 Print(" ");
771 else 784 else
772 Newline(); 785 Newline();
773 } 786 }
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
964 printf("Wrote formatted to '%s'.\n", to_write.AsUTF8Unsafe().c_str()); 977 printf("Wrote formatted to '%s'.\n", to_write.AsUTF8Unsafe().c_str());
965 } else { 978 } else {
966 printf("%s", output_string.c_str()); 979 printf("%s", output_string.c_str());
967 } 980 }
968 } 981 }
969 982
970 return 0; 983 return 0;
971 } 984 }
972 985
973 } // namespace commands 986 } // namespace commands
OLDNEW
« no previous file with comments | « no previous file | tools/gn/command_format_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698