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

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

Issue 774753004: gn format: fix not indenting multiline continued correctly (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@gn-format-dont-break-if-will-never-fit
Patch Set: rebase 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 453 matching lines...) Expand 10 before | Expand all | Expand 10 after
464 int indent_column = 464 int indent_column =
465 (is_assignment && 465 (is_assignment &&
466 (!right_as_list || (!right_as_list->prefer_multiline() && 466 (!right_as_list || (!right_as_list->prefer_multiline() &&
467 !ListWillBeMultiline(right_as_list->contents(), 467 !ListWillBeMultiline(right_as_list->contents(),
468 right_as_list->End())))) 468 right_as_list->End()))))
469 ? margin() + kIndentSize * 2 469 ? margin() + kIndentSize * 2
470 : start_column; 470 : start_column;
471 if (stack_.back().continuation_requires_indent) 471 if (stack_.back().continuation_requires_indent)
472 indent_column += kIndentSize * 2; 472 indent_column += kIndentSize * 2;
473 473
474 stack_.push_back( 474 stack_.push_back(IndentState(indent_column,
475 IndentState(indent_column, false, binop->op().value() == "||")); 475 stack_.back().continuation_requires_indent,
476 binop->op().value() == "||"));
476 Printer sub_left; 477 Printer sub_left;
477 InitializeSub(&sub_left); 478 InitializeSub(&sub_left);
478 sub_left.Expr(binop->left(), 479 sub_left.Expr(binop->left(),
479 prec_left, 480 prec_left,
480 std::string(" ") + binop->op().value().as_string()); 481 std::string(" ") + binop->op().value().as_string());
481 bool left_is_multiline = CountLines(sub_left.String()) > 1; 482 bool left_is_multiline = CountLines(sub_left.String()) > 1;
482 // Avoid walking the whole left redundantly times (see timing of Format.046) 483 // Avoid walking the whole left redundantly times (see timing of Format.046)
483 // so pull the output and comments from subprinter. 484 // so pull the output and comments from subprinter.
484 Print(sub_left.String().substr(start_column)); 485 Print(sub_left.String().substr(start_column));
485 std::copy(sub_left.comments_.begin(), 486 std::copy(sub_left.comments_.begin(),
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after
702 // 2. starting on same line, broken at each comma but paren aligned; 703 // 2. starting on same line, broken at each comma but paren aligned;
703 // 3. broken to next line + 4, broken at each comma. 704 // 3. broken to next line + 4, broken at each comma.
704 std::string terminator = ")"; 705 std::string terminator = ")";
705 if (have_block) 706 if (have_block)
706 terminator += " {"; 707 terminator += " {";
707 terminator += suffix; 708 terminator += suffix;
708 709
709 // Special case to make function calls of one arg taking a long list of 710 // Special case to make function calls of one arg taking a long list of
710 // boolean operators not indent. 711 // boolean operators not indent.
711 bool continuation_requires_indent = 712 bool continuation_requires_indent =
712 list.size() != 1 || !list[0]->AsBinaryOp() || 713 list.size() != 1 || !list[0]->AsBinaryOp();
713 (list[0]->AsBinaryOp()->op().value() != "||" &&
714 list[0]->AsBinaryOp()->op().value() != "&&");
715 714
716 // 1: Same line. 715 // 1: Same line.
717 Printer sub1; 716 Printer sub1;
718 InitializeSub(&sub1); 717 InitializeSub(&sub1);
719 sub1.stack_.push_back( 718 sub1.stack_.push_back(
720 IndentState(CurrentColumn(), continuation_requires_indent, false)); 719 IndentState(CurrentColumn(), continuation_requires_indent, false));
721 int penalty_one_line = 0; 720 int penalty_one_line = 0;
722 for (size_t i = 0; i < list.size(); ++i) { 721 for (size_t i = 0; i < list.size(); ++i) {
723 penalty_one_line += sub1.Expr(list[i], kPrecedenceLowest, 722 penalty_one_line += sub1.Expr(list[i], kPrecedenceLowest,
724 i < list.size() - 1 ? ", " : std::string()); 723 i < list.size() - 1 ? ", " : std::string());
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after
1005 } 1004 }
1006 } else { 1005 } else {
1007 printf("%s", output_string.c_str()); 1006 printf("%s", output_string.c_str());
1008 } 1007 }
1009 } 1008 }
1010 1009
1011 return 0; 1010 return 0;
1012 } 1011 }
1013 1012
1014 } // namespace commands 1013 } // 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