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

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

Issue 775793003: gn format: don't break+indent if it's going to be forced past 80col anyway (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 138
139 // Print the expression to the output buffer. Returns the type of element 139 // Print the expression to the output buffer. Returns the type of element
140 // added to the output. The value of outer_prec gives the precedence of the 140 // added to the output. The value of outer_prec gives the precedence of the
141 // operator outside this Expr. If that operator binds tighter than root's, 141 // operator outside this Expr. If that operator binds tighter than root's,
142 // Expr must introduce parentheses. 142 // Expr must introduce parentheses.
143 int Expr(const ParseNode* root, int outer_prec, const std::string& suffix); 143 int Expr(const ParseNode* root, int outer_prec, const std::string& suffix);
144 144
145 // Generic penalties for exceeding maximum width, adding more lines, etc. 145 // Generic penalties for exceeding maximum width, adding more lines, etc.
146 int AssessPenalty(const std::string& output); 146 int AssessPenalty(const std::string& output);
147 147
148 // Tests if any lines exceed the maximum width.
149 bool ExceedsMaximumWidth(const std::string& output);
150
148 // Format a list of values using the given style. 151 // Format a list of values using the given style.
149 // |end| holds any trailing comments to be printed just before the closing 152 // |end| holds any trailing comments to be printed just before the closing
150 // bracket. 153 // bracket.
151 template <class PARSENODE> // Just for const covariance. 154 template <class PARSENODE> // Just for const covariance.
152 void Sequence(SequenceStyle style, 155 void Sequence(SequenceStyle style,
153 const std::vector<PARSENODE*>& list, 156 const std::vector<PARSENODE*>& list,
154 const ParseNode* end, 157 const ParseNode* end,
155 bool force_multiline); 158 bool force_multiline);
156 159
157 // Returns the penalty. 160 // Returns the penalty.
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after
367 std::vector<std::string> lines; 370 std::vector<std::string> lines;
368 base::SplitStringDontTrim(output, '\n', &lines); 371 base::SplitStringDontTrim(output, '\n', &lines);
369 penalty += static_cast<int>(lines.size() - 1) * GetPenaltyForLineBreak(); 372 penalty += static_cast<int>(lines.size() - 1) * GetPenaltyForLineBreak();
370 for (const auto& line : lines) { 373 for (const auto& line : lines) {
371 if (line.size() > kMaximumWidth) 374 if (line.size() > kMaximumWidth)
372 penalty += static_cast<int>(line.size() - kMaximumWidth) * kPenaltyExcess; 375 penalty += static_cast<int>(line.size() - kMaximumWidth) * kPenaltyExcess;
373 } 376 }
374 return penalty; 377 return penalty;
375 } 378 }
376 379
380 bool Printer::ExceedsMaximumWidth(const std::string& output) {
381 std::vector<std::string> lines;
382 base::SplitStringDontTrim(output, '\n', &lines);
383 for (const auto& line : lines) {
384 if (line.size() > kMaximumWidth)
385 return true;
386 }
387 return false;
388 }
389
377 void Printer::AddParen(int prec, int outer_prec, bool* parenthesized) { 390 void Printer::AddParen(int prec, int outer_prec, bool* parenthesized) {
378 if (prec < outer_prec) { 391 if (prec < outer_prec) {
379 Print("("); 392 Print("(");
380 *parenthesized = true; 393 *parenthesized = true;
381 } 394 }
382 } 395 }
383 396
384 int Printer::Expr(const ParseNode* root, 397 int Printer::Expr(const ParseNode* root,
385 int outer_prec, 398 int outer_prec,
386 const std::string& suffix) { 399 const std::string& suffix) {
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
490 503
491 // Break after operator. 504 // Break after operator.
492 Printer sub2; 505 Printer sub2;
493 InitializeSub(&sub2); 506 InitializeSub(&sub2);
494 sub2.Newline(); 507 sub2.Newline();
495 int penalty_next_line = 508 int penalty_next_line =
496 sub2.Expr(binop->right(), prec_right, std::string()); 509 sub2.Expr(binop->right(), prec_right, std::string());
497 sub2.Print(suffix); 510 sub2.Print(suffix);
498 penalty_next_line += AssessPenalty(sub2.String()); 511 penalty_next_line += AssessPenalty(sub2.String());
499 512
500 if (penalty_current_line < penalty_next_line) { 513 // If in both cases it was forced past 80col, then we don't break to avoid
514 // breaking after '=' in the case of:
515 // variable = "... very long string ..."
516 // as breaking and indenting doesn't make things much more readable, even
517 // though there's less characters past the maximum width.
518 bool exceeds_maximum_either_way = ExceedsMaximumWidth(sub1.String()) &&
519 ExceedsMaximumWidth(sub2.String());
520
521 if (penalty_current_line < penalty_next_line ||
522 exceeds_maximum_either_way) {
501 Print(" "); 523 Print(" ");
502 Expr(binop->right(), prec_right, std::string()); 524 Expr(binop->right(), prec_right, std::string());
503 } else { 525 } else {
504 // Otherwise, put first argument and op, and indent next. 526 // Otherwise, put first argument and op, and indent next.
505 Newline(); 527 Newline();
506 penalty += std::abs(CurrentColumn() - start_column) * 528 penalty += std::abs(CurrentColumn() - start_column) *
507 kPenaltyHorizontalSeparation; 529 kPenaltyHorizontalSeparation;
508 Expr(binop->right(), prec_right, std::string()); 530 Expr(binop->right(), prec_right, std::string());
509 } 531 }
510 stack_.pop_back(); 532 stack_.pop_back();
(...skipping 472 matching lines...) Expand 10 before | Expand all | Expand 10 after
983 } 1005 }
984 } else { 1006 } else {
985 printf("%s", output_string.c_str()); 1007 printf("%s", output_string.c_str());
986 } 1008 }
987 } 1009 }
988 1010
989 return 0; 1011 return 0;
990 } 1012 }
991 1013
992 } // namespace commands 1014 } // 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