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

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

Issue 778833002: gn format: cosmetic changes to AnnotatePreferredMultilineAssignment (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 | no next file » | 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 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 // End the current line, flushing end of line comments. 111 // End the current line, flushing end of line comments.
112 void Newline(); 112 void Newline();
113 113
114 // Remove trailing spaces from the current line. 114 // Remove trailing spaces from the current line.
115 void Trim(); 115 void Trim();
116 116
117 // Whether there's a blank separator line at the current position. 117 // Whether there's a blank separator line at the current position.
118 bool HaveBlankLine(); 118 bool HaveBlankLine();
119 119
120 // Flag assignments to sources, deps, etc. to make their RHSs multiline. 120 // Flag assignments to sources, deps, etc. to make their RHSs multiline.
121 void AnnotatePreferedMultilineAssignment(const BinaryOpNode* binop); 121 void AnnotatePreferredMultilineAssignment(const BinaryOpNode* binop);
122 122
123 // Heuristics to decide if there should be a blank line added between two 123 // Heuristics to decide if there should be a blank line added between two
124 // items. For various "small" items, it doesn't look nice if there's too much 124 // items. For various "small" items, it doesn't look nice if there's too much
125 // vertical whitespace added. 125 // vertical whitespace added.
126 bool ShouldAddBlankLineInBetween(const ParseNode* a, const ParseNode* b); 126 bool ShouldAddBlankLineInBetween(const ParseNode* a, const ParseNode* b);
127 127
128 // Get the 0-based x position on the current line. 128 // Get the 0-based x position on the current line.
129 int CurrentColumn() const; 129 int CurrentColumn() const;
130 130
131 // Get the current line in the output; 131 // Get the current line in the output;
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
274 output_.resize(n); 274 output_.resize(n);
275 } 275 }
276 276
277 bool Printer::HaveBlankLine() { 277 bool Printer::HaveBlankLine() {
278 size_t n = output_.size(); 278 size_t n = output_.size();
279 while (n > 0 && output_[n - 1] == ' ') 279 while (n > 0 && output_[n - 1] == ' ')
280 --n; 280 --n;
281 return n > 2 && output_[n - 1] == '\n' && output_[n - 2] == '\n'; 281 return n > 2 && output_[n - 1] == '\n' && output_[n - 2] == '\n';
282 } 282 }
283 283
284 void Printer::AnnotatePreferedMultilineAssignment(const BinaryOpNode* binop) { 284 void Printer::AnnotatePreferredMultilineAssignment(const BinaryOpNode* binop) {
285 const IdentifierNode* ident = binop->left()->AsIdentifier(); 285 const IdentifierNode* ident = binop->left()->AsIdentifier();
286 const ListNode* list = binop->right()->AsList(); 286 const ListNode* list = binop->right()->AsList();
287 // This is somewhat arbitrary, but we include the 'deps'- and 'sources'-like 287 // This is somewhat arbitrary, but we include the 'deps'- and 'sources'-like
288 // things, but not flags things. 288 // things, but not flags things.
289 if (binop->op().value() == "=" && ident && list && 289 if (binop->op().value() == "=" && ident && list) {
290 (ident->value().value() == "data" || 290 const base::StringPiece lhs = ident->value().value();
291 ident->value().value() == "datadeps" || 291 if (lhs == "data" || lhs == "datadeps" || lhs == "deps" ||
292 ident->value().value() == "deps" || ident->value().value() == "inputs" || 292 lhs == "inputs" || lhs == "outputs" || lhs == "public" ||
293 ident->value().value() == "outputs" || 293 lhs == "public_deps" || lhs == "sources") {
294 ident->value().value() == "public" || 294 const_cast<ListNode*>(list)->set_prefer_multiline(true);
295 ident->value().value() == "public_deps" || 295 }
296 ident->value().value() == "sources")) {
297 const_cast<ListNode*>(list)->set_prefer_multiline(true);
298 } 296 }
299 } 297 }
300 298
301 bool Printer::ShouldAddBlankLineInBetween(const ParseNode* a, 299 bool Printer::ShouldAddBlankLineInBetween(const ParseNode* a,
302 const ParseNode* b) { 300 const ParseNode* b) {
303 LocationRange a_range = a->GetRange(); 301 LocationRange a_range = a->GetRange();
304 LocationRange b_range = b->GetRange(); 302 LocationRange b_range = b->GetRange();
305 // If they're already separated by 1 or more lines, then we want to keep a 303 // If they're already separated by 1 or more lines, then we want to keep a
306 // blank line. 304 // blank line.
307 return b_range.begin().line_number() > a_range.end().line_number() + 1; 305 return b_range.begin().line_number() > a_range.end().line_number() + 1;
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
425 if (accessor->member()) { 423 if (accessor->member()) {
426 Print("."); 424 Print(".");
427 Expr(accessor->member(), kPrecedenceLowest, std::string()); 425 Expr(accessor->member(), kPrecedenceLowest, std::string());
428 } else { 426 } else {
429 CHECK(accessor->index()); 427 CHECK(accessor->index());
430 Print("["); 428 Print("[");
431 Expr(accessor->index(), kPrecedenceLowest, "]"); 429 Expr(accessor->index(), kPrecedenceLowest, "]");
432 } 430 }
433 } else if (const BinaryOpNode* binop = root->AsBinaryOp()) { 431 } else if (const BinaryOpNode* binop = root->AsBinaryOp()) {
434 CHECK(precedence_.find(binop->op().value()) != precedence_.end()); 432 CHECK(precedence_.find(binop->op().value()) != precedence_.end());
435 AnnotatePreferedMultilineAssignment(binop); 433 AnnotatePreferredMultilineAssignment(binop);
436 434
437 Precedence prec = precedence_[binop->op().value()]; 435 Precedence prec = precedence_[binop->op().value()];
438 436
439 // Since binary operators format left-to-right, it is ok for the left side 437 // Since binary operators format left-to-right, it is ok for the left side
440 // use the same operator without parentheses, so the left uses prec. For the 438 // use the same operator without parentheses, so the left uses prec. For the
441 // same reason, the right side cannot reuse the same operator, or else "x + 439 // same reason, the right side cannot reuse the same operator, or else "x +
442 // (y + z)" would format as "x + y + z" which means "(x + y) + z". So, treat 440 // (y + z)" would format as "x + y + z" which means "(x + y) + z". So, treat
443 // the right expression as appearing one precedence level higher. 441 // the right expression as appearing one precedence level higher.
444 // However, because the source parens are not in the parse tree, as a 442 // However, because the source parens are not in the parse tree, as a
445 // special case for && and || we insert strictly-redundant-but-helpful-for- 443 // special case for && and || we insert strictly-redundant-but-helpful-for-
(...skipping 559 matching lines...) Expand 10 before | Expand all | Expand 10 after
1005 } 1003 }
1006 } else { 1004 } else {
1007 printf("%s", output_string.c_str()); 1005 printf("%s", output_string.c_str());
1008 } 1006 }
1009 } 1007 }
1010 1008
1011 return 0; 1009 return 0;
1012 } 1010 }
1013 1011
1014 } // namespace commands 1012 } // namespace commands
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698