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

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

Issue 1442023002: Sort "deps" and "public_deps" when running the "gn format" command. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 1 month 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
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 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 121
122 // Remove trailing spaces from the current line. 122 // Remove trailing spaces from the current line.
123 void Trim(); 123 void Trim();
124 124
125 // Whether there's a blank separator line at the current position. 125 // Whether there's a blank separator line at the current position.
126 bool HaveBlankLine(); 126 bool HaveBlankLine();
127 127
128 // Flag assignments to sources, deps, etc. to make their RHSs multiline. 128 // Flag assignments to sources, deps, etc. to make their RHSs multiline.
129 void AnnotatePreferredMultilineAssignment(const BinaryOpNode* binop); 129 void AnnotatePreferredMultilineAssignment(const BinaryOpNode* binop);
130 130
131 // Alphabetically a list on the RHS if the LHS is 'sources'. 131 // Alphabetically a list on the RHS if the LHS is 'sources' or 'deps'.
132 void SortIfSources(const BinaryOpNode* binop); 132 void SortIfSourcesOrDeps(const BinaryOpNode* binop);
133 133
134 // Heuristics to decide if there should be a blank line added between two 134 // Heuristics to decide if there should be a blank line added between two
135 // items. For various "small" items, it doesn't look nice if there's too much 135 // items. For various "small" items, it doesn't look nice if there's too much
136 // vertical whitespace added. 136 // vertical whitespace added.
137 bool ShouldAddBlankLineInBetween(const ParseNode* a, const ParseNode* b); 137 bool ShouldAddBlankLineInBetween(const ParseNode* a, const ParseNode* b);
138 138
139 // Get the 0-based x position on the current line. 139 // Get the 0-based x position on the current line.
140 int CurrentColumn() const; 140 int CurrentColumn() const;
141 141
142 // Get the current line in the output; 142 // Get the current line in the output;
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
300 if (binop->op().value() == "=" && ident && list) { 300 if (binop->op().value() == "=" && ident && list) {
301 const base::StringPiece lhs = ident->value().value(); 301 const base::StringPiece lhs = ident->value().value();
302 if (lhs == "data" || lhs == "datadeps" || lhs == "data_deps" || 302 if (lhs == "data" || lhs == "datadeps" || lhs == "data_deps" ||
303 lhs == "deps" || lhs == "inputs" || lhs == "outputs" || 303 lhs == "deps" || lhs == "inputs" || lhs == "outputs" ||
304 lhs == "public" || lhs == "public_deps" || lhs == "sources") { 304 lhs == "public" || lhs == "public_deps" || lhs == "sources") {
305 const_cast<ListNode*>(list)->set_prefer_multiline(true); 305 const_cast<ListNode*>(list)->set_prefer_multiline(true);
306 } 306 }
307 } 307 }
308 } 308 }
309 309
310 void Printer::SortIfSources(const BinaryOpNode* binop) { 310 void Printer::SortIfSourcesOrDeps(const BinaryOpNode* binop) {
311 const IdentifierNode* ident = binop->left()->AsIdentifier(); 311 const IdentifierNode* ident = binop->left()->AsIdentifier();
312 const ListNode* list = binop->right()->AsList(); 312 const ListNode* list = binop->right()->AsList();
313 // TODO(scottmg): Sort more than 'sources'? 313 // TODO(scottmg): Sort more than 'sources' and 'deps'?
314 if ((binop->op().value() == "=" || binop->op().value() == "+=" || 314 if ((binop->op().value() == "=" || binop->op().value() == "+=" ||
315 binop->op().value() == "-=") && 315 binop->op().value() == "-=") &&
316 ident && list) { 316 ident && list) {
317 const base::StringPiece lhs = ident->value().value(); 317 const base::StringPiece lhs = ident->value().value();
318 if (lhs == "sources") 318 if (lhs == "sources" || lhs == "deps")
319 const_cast<ListNode*>(list)->SortAsStringsList(); 319 const_cast<ListNode*>(list)->SortAsStringsList();
320 } 320 }
321 } 321 }
322 322
323 323
324 bool Printer::ShouldAddBlankLineInBetween(const ParseNode* a, 324 bool Printer::ShouldAddBlankLineInBetween(const ParseNode* a,
325 const ParseNode* b) { 325 const ParseNode* b) {
326 LocationRange a_range = a->GetRange(); 326 LocationRange a_range = a->GetRange();
327 LocationRange b_range = b->GetRange(); 327 LocationRange b_range = b->GetRange();
328 // If they're already separated by 1 or more lines, then we want to keep a 328 // If they're already separated by 1 or more lines, then we want to keep a
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
451 Expr(accessor->member(), kPrecedenceLowest, std::string()); 451 Expr(accessor->member(), kPrecedenceLowest, std::string());
452 } else { 452 } else {
453 CHECK(accessor->index()); 453 CHECK(accessor->index());
454 Print("["); 454 Print("[");
455 Expr(accessor->index(), kPrecedenceLowest, "]"); 455 Expr(accessor->index(), kPrecedenceLowest, "]");
456 } 456 }
457 } else if (const BinaryOpNode* binop = root->AsBinaryOp()) { 457 } else if (const BinaryOpNode* binop = root->AsBinaryOp()) {
458 CHECK(precedence_.find(binop->op().value()) != precedence_.end()); 458 CHECK(precedence_.find(binop->op().value()) != precedence_.end());
459 AnnotatePreferredMultilineAssignment(binop); 459 AnnotatePreferredMultilineAssignment(binop);
460 460
461 SortIfSources(binop); 461 SortIfSourcesOrDeps(binop);
462 462
463 Precedence prec = precedence_[binop->op().value()]; 463 Precedence prec = precedence_[binop->op().value()];
464 464
465 // Since binary operators format left-to-right, it is ok for the left side 465 // Since binary operators format left-to-right, it is ok for the left side
466 // use the same operator without parentheses, so the left uses prec. For the 466 // use the same operator without parentheses, so the left uses prec. For the
467 // same reason, the right side cannot reuse the same operator, or else "x + 467 // same reason, the right side cannot reuse the same operator, or else "x +
468 // (y + z)" would format as "x + y + z" which means "(x + y) + z". So, treat 468 // (y + z)" would format as "x + y + z" which means "(x + y) + z". So, treat
469 // the right expression as appearing one precedence level higher. 469 // the right expression as appearing one precedence level higher.
470 // However, because the source parens are not in the parse tree, as a 470 // However, because the source parens are not in the parse tree, as a
471 // special case for && and || we insert strictly-redundant-but-helpful-for- 471 // special case for && and || we insert strictly-redundant-but-helpful-for-
(...skipping 573 matching lines...) Expand 10 before | Expand all | Expand 10 after
1045 } 1045 }
1046 } else { 1046 } else {
1047 printf("%s", output_string.c_str()); 1047 printf("%s", output_string.c_str());
1048 } 1048 }
1049 } 1049 }
1050 1050
1051 return 0; 1051 return 0;
1052 } 1052 }
1053 1053
1054 } // namespace commands 1054 } // namespace commands
OLDNEW
« no previous file with comments | « no previous file | tools/gn/command_format_unittest.cc » ('j') | tools/gn/format_test_data/063.golden » ('J')

Powered by Google App Engine
This is Rietveld 408576698