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

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: Fix comments following suggestions 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
« 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 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 // Sort a list on the RHS if the LHS is 'sources', 'deps' or 'public_deps'.
132 void SortIfSources(const BinaryOpNode* binop); 132 // The 'sources' are sorted alphabetically while the 'deps' and 'public_deps'
133 // are sorted putting first the relative targets and then the global ones
134 // (both sorted alphabetically).
135 void SortIfSourcesOrDeps(const BinaryOpNode* binop);
133 136
134 // Heuristics to decide if there should be a blank line added between two 137 // 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 138 // items. For various "small" items, it doesn't look nice if there's too much
136 // vertical whitespace added. 139 // vertical whitespace added.
137 bool ShouldAddBlankLineInBetween(const ParseNode* a, const ParseNode* b); 140 bool ShouldAddBlankLineInBetween(const ParseNode* a, const ParseNode* b);
138 141
139 // Get the 0-based x position on the current line. 142 // Get the 0-based x position on the current line.
140 int CurrentColumn() const; 143 int CurrentColumn() const;
141 144
142 // Get the current line in the output; 145 // 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) { 303 if (binop->op().value() == "=" && ident && list) {
301 const base::StringPiece lhs = ident->value().value(); 304 const base::StringPiece lhs = ident->value().value();
302 if (lhs == "data" || lhs == "datadeps" || lhs == "data_deps" || 305 if (lhs == "data" || lhs == "datadeps" || lhs == "data_deps" ||
303 lhs == "deps" || lhs == "inputs" || lhs == "outputs" || 306 lhs == "deps" || lhs == "inputs" || lhs == "outputs" ||
304 lhs == "public" || lhs == "public_deps" || lhs == "sources") { 307 lhs == "public" || lhs == "public_deps" || lhs == "sources") {
305 const_cast<ListNode*>(list)->set_prefer_multiline(true); 308 const_cast<ListNode*>(list)->set_prefer_multiline(true);
306 } 309 }
307 } 310 }
308 } 311 }
309 312
310 void Printer::SortIfSources(const BinaryOpNode* binop) { 313 void Printer::SortIfSourcesOrDeps(const BinaryOpNode* binop) {
311 const IdentifierNode* ident = binop->left()->AsIdentifier(); 314 const IdentifierNode* ident = binop->left()->AsIdentifier();
312 const ListNode* list = binop->right()->AsList(); 315 const ListNode* list = binop->right()->AsList();
313 // TODO(scottmg): Sort more than 'sources'?
314 if ((binop->op().value() == "=" || binop->op().value() == "+=" || 316 if ((binop->op().value() == "=" || binop->op().value() == "+=" ||
315 binop->op().value() == "-=") && 317 binop->op().value() == "-=") &&
316 ident && list) { 318 ident && list) {
317 const base::StringPiece lhs = ident->value().value(); 319 const base::StringPiece lhs = ident->value().value();
318 if (lhs == "sources") 320 if (lhs == "sources")
319 const_cast<ListNode*>(list)->SortAsStringsList(); 321 const_cast<ListNode*>(list)->SortAsStringsList();
322 else if (lhs == "deps" || lhs == "public_deps")
323 const_cast<ListNode*>(list)->SortAsDepsList();
320 } 324 }
321 } 325 }
322 326
323
324 bool Printer::ShouldAddBlankLineInBetween(const ParseNode* a, 327 bool Printer::ShouldAddBlankLineInBetween(const ParseNode* a,
325 const ParseNode* b) { 328 const ParseNode* b) {
326 LocationRange a_range = a->GetRange(); 329 LocationRange a_range = a->GetRange();
327 LocationRange b_range = b->GetRange(); 330 LocationRange b_range = b->GetRange();
328 // If they're already separated by 1 or more lines, then we want to keep a 331 // If they're already separated by 1 or more lines, then we want to keep a
329 // blank line. 332 // blank line.
330 return (b_range.begin().line_number() > a_range.end().line_number() + 1) || 333 return (b_range.begin().line_number() > a_range.end().line_number() + 1) ||
331 // Always put a blank line before a block comment. 334 // Always put a blank line before a block comment.
332 b->AsBlockComment(); 335 b->AsBlockComment();
333 } 336 }
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
451 Expr(accessor->member(), kPrecedenceLowest, std::string()); 454 Expr(accessor->member(), kPrecedenceLowest, std::string());
452 } else { 455 } else {
453 CHECK(accessor->index()); 456 CHECK(accessor->index());
454 Print("["); 457 Print("[");
455 Expr(accessor->index(), kPrecedenceLowest, "]"); 458 Expr(accessor->index(), kPrecedenceLowest, "]");
456 } 459 }
457 } else if (const BinaryOpNode* binop = root->AsBinaryOp()) { 460 } else if (const BinaryOpNode* binop = root->AsBinaryOp()) {
458 CHECK(precedence_.find(binop->op().value()) != precedence_.end()); 461 CHECK(precedence_.find(binop->op().value()) != precedence_.end());
459 AnnotatePreferredMultilineAssignment(binop); 462 AnnotatePreferredMultilineAssignment(binop);
460 463
461 SortIfSources(binop); 464 SortIfSourcesOrDeps(binop);
462 465
463 Precedence prec = precedence_[binop->op().value()]; 466 Precedence prec = precedence_[binop->op().value()];
464 467
465 // Since binary operators format left-to-right, it is ok for the left side 468 // 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 469 // 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 + 470 // 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 471 // (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. 472 // the right expression as appearing one precedence level higher.
470 // However, because the source parens are not in the parse tree, as a 473 // 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- 474 // special case for && and || we insert strictly-redundant-but-helpful-for-
(...skipping 573 matching lines...) Expand 10 before | Expand all | Expand 10 after
1045 } 1048 }
1046 } else { 1049 } else {
1047 printf("%s", output_string.c_str()); 1050 printf("%s", output_string.c_str());
1048 } 1051 }
1049 } 1052 }
1050 1053
1051 return 0; 1054 return 0;
1052 } 1055 }
1053 1056
1054 } // namespace commands 1057 } // 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