| OLD | NEW |
| 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 "tools/gn/commands.h" | 8 #include "tools/gn/commands.h" |
| 9 #include "tools/gn/input_file.h" | 9 #include "tools/gn/input_file.h" |
| 10 #include "tools/gn/parser.h" | 10 #include "tools/gn/parser.h" |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 void PrintMargin(); | 65 void PrintMargin(); |
| 66 | 66 |
| 67 void TrimAndPrintToken(const Token& token); | 67 void TrimAndPrintToken(const Token& token); |
| 68 | 68 |
| 69 // End the current line, flushing end of line comments. | 69 // End the current line, flushing end of line comments. |
| 70 void Newline(); | 70 void Newline(); |
| 71 | 71 |
| 72 // Remove trailing spaces from the current line. | 72 // Remove trailing spaces from the current line. |
| 73 void Trim(); | 73 void Trim(); |
| 74 | 74 |
| 75 // Whether there's a blank separator line at the current position. |
| 76 bool HaveBlankLine(); |
| 77 |
| 75 // Get the 0-based x position on the current line. | 78 // Get the 0-based x position on the current line. |
| 76 int CurrentColumn(); | 79 int CurrentColumn(); |
| 77 | 80 |
| 78 // Print the expression to the output buffer. Returns the type of element | 81 // Print the expression to the output buffer. Returns the type of element |
| 79 // added to the output. | 82 // added to the output. |
| 80 ExprStyle Expr(const ParseNode* root); | 83 ExprStyle Expr(const ParseNode* root); |
| 81 | 84 |
| 82 // Format a list of values using the given style. | 85 // Format a list of values using the given style. |
| 83 // |end| holds any trailing comments to be printed just before the closing | 86 // |end| holds any trailing comments to be printed just before the closing |
| 84 // bracket. | 87 // bracket. |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 PrintMargin(); | 137 PrintMargin(); |
| 135 } | 138 } |
| 136 | 139 |
| 137 void Printer::Trim() { | 140 void Printer::Trim() { |
| 138 size_t n = output_.size(); | 141 size_t n = output_.size(); |
| 139 while (n > 0 && output_[n - 1] == ' ') | 142 while (n > 0 && output_[n - 1] == ' ') |
| 140 --n; | 143 --n; |
| 141 output_.resize(n); | 144 output_.resize(n); |
| 142 } | 145 } |
| 143 | 146 |
| 147 bool Printer::HaveBlankLine() { |
| 148 size_t n = output_.size(); |
| 149 while (n > 0 && output_[n - 1] == ' ') |
| 150 --n; |
| 151 return n > 2 && output_[n - 1] == '\n' && output_[n - 2] == '\n'; |
| 152 } |
| 153 |
| 144 int Printer::CurrentColumn() { | 154 int Printer::CurrentColumn() { |
| 145 int n = 0; | 155 int n = 0; |
| 146 while (n < static_cast<int>(output_.size()) && | 156 while (n < static_cast<int>(output_.size()) && |
| 147 output_[output_.size() - 1 - n] != '\n') { | 157 output_[output_.size() - 1 - n] != '\n') { |
| 148 ++n; | 158 ++n; |
| 149 } | 159 } |
| 150 return n; | 160 return n; |
| 151 } | 161 } |
| 152 | 162 |
| 153 void Printer::Block(const ParseNode* root) { | 163 void Printer::Block(const ParseNode* root) { |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 308 Print(" "); | 318 Print(" "); |
| 309 Expr(list[0]); | 319 Expr(list[0]); |
| 310 CHECK(!list[0]->comments() || list[0]->comments()->after().empty()); | 320 CHECK(!list[0]->comments() || list[0]->comments()->after().empty()); |
| 311 if (style != kSequenceStyleFunctionCall) | 321 if (style != kSequenceStyleFunctionCall) |
| 312 Print(" "); | 322 Print(" "); |
| 313 } else { | 323 } else { |
| 314 margin_ += kIndentSize; | 324 margin_ += kIndentSize; |
| 315 size_t i = 0; | 325 size_t i = 0; |
| 316 for (const auto& x : list) { | 326 for (const auto& x : list) { |
| 317 Newline(); | 327 Newline(); |
| 328 // If: |
| 329 // - we're going to output some comments, and; |
| 330 // - we haven't just started this multiline list, and; |
| 331 // - there isn't already a blank line here; |
| 332 // Then: insert one. |
| 333 if (i != 0 && x->comments() && !x->comments()->before().empty() && |
| 334 !HaveBlankLine()) { |
| 335 Newline(); |
| 336 } |
| 318 ExprStyle expr_style = Expr(x); | 337 ExprStyle expr_style = Expr(x); |
| 319 CHECK(!x->comments() || x->comments()->after().empty()); | 338 CHECK(!x->comments() || x->comments()->after().empty()); |
| 320 if (i < list.size() - 1 || style == kSequenceStyleList) { | 339 if (i < list.size() - 1 || style == kSequenceStyleList) { |
| 321 if ((style == kSequenceStyleList || kSequenceStyleFunctionCall) && | 340 if ((style == kSequenceStyleList || kSequenceStyleFunctionCall) && |
| 322 expr_style == kExprStyleRegular) { | 341 expr_style == kExprStyleRegular) { |
| 323 Print(","); | 342 Print(","); |
| 324 } else { | 343 } else { |
| 325 Newline(); | 344 Newline(); |
| 326 } | 345 } |
| 327 } | 346 } |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 401 } | 420 } |
| 402 std::string output_string; | 421 std::string output_string; |
| 403 if (FormatFileToString(input_name, dump_tree, &output_string)) { | 422 if (FormatFileToString(input_name, dump_tree, &output_string)) { |
| 404 printf("%s", output_string.c_str()); | 423 printf("%s", output_string.c_str()); |
| 405 } | 424 } |
| 406 | 425 |
| 407 return 0; | 426 return 0; |
| 408 } | 427 } |
| 409 | 428 |
| 410 } // namespace commands | 429 } // namespace commands |
| OLD | NEW |