forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunwrapped_line.cc
158 lines (143 loc) · 5.34 KB
/
unwrapped_line.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright 2017-2020 The Verible Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "common/formatting/unwrapped_line.h"
#include <algorithm>
#include <iostream>
#include <iterator>
#include <map>
#include <sstream>
#include <string>
#include <vector>
#include "absl/strings/str_cat.h"
#include "absl/strings/str_join.h"
#include "common/formatting/format_token.h"
#include "common/strings/display_utils.h"
#include "common/text/tree_utils.h"
#include "common/util/container_iterator_range.h"
#include "common/util/spacer.h"
namespace verible {
std::ostream& operator<<(std::ostream& stream, PartitionPolicyEnum p) {
switch (p) {
case PartitionPolicyEnum::kUninitialized:
return stream << "uninitialized";
case PartitionPolicyEnum::kAlwaysExpand:
return stream << "always-expand";
case PartitionPolicyEnum::kFitOnLineElseExpand:
return stream << "fit-else-expand";
case PartitionPolicyEnum::kTabularAlignment:
return stream << "tabular-alignment";
case PartitionPolicyEnum::kAlreadyFormatted:
return stream << "already-formatted";
case PartitionPolicyEnum::kInline:
return stream << "inline";
case PartitionPolicyEnum::kAppendFittingSubPartitions:
return stream << "append-fitting-sub-partitions";
case PartitionPolicyEnum::kJuxtaposition:
return stream << "juxtaposition";
case PartitionPolicyEnum::kStack:
return stream << "stack";
case PartitionPolicyEnum::kWrap:
return stream << "wrap";
case PartitionPolicyEnum::kJuxtapositionOrIndentedStack:
return stream << "juxtaposition-or-indented-stack";
}
LOG(FATAL) << "Unknown partition policy " << int(p);
}
static void TokenFormatter(std::string* out, const PreFormatToken& token,
bool verbose) {
if (verbose) {
std::ostringstream oss;
token.before.CompactNotation(oss);
absl::StrAppend(out, oss.str());
}
absl::StrAppend(out, token.Text());
}
void UnwrappedLine::SetIndentationSpaces(int spaces) {
CHECK_GE(spaces, 0);
indentation_spaces_ = spaces;
}
void UnwrappedLine::DefaultOriginPrinter(std::ostream& stream,
const verible::Symbol* symbol) {
static constexpr int kContextLimit = 25;
stream << '"' << AutoTruncate{StringSpanOfSymbol(*symbol), kContextLimit}
<< '"';
}
std::ostream* UnwrappedLine::AsCode(
std::ostream* stream, bool verbose,
const OriginPrinterFunction& origin_printer) const {
*stream << Spacer(indentation_spaces_, kIndentationMarker) << '['
<< absl::StrJoin(tokens_, " ",
[=](std::string* out, const PreFormatToken& token) {
TokenFormatter(out, token, verbose);
})
<< "], policy: " << partition_policy_;
if (origin_ != nullptr) {
*stream << ", (origin: ";
origin_printer(*stream, origin_);
*stream << ")";
}
return stream;
}
std::ostream& operator<<(std::ostream& stream, const UnwrappedLine& line) {
return *line.AsCode(&stream);
}
FormattedExcerpt::FormattedExcerpt(const UnwrappedLine& uwline)
: indentation_spaces_(uwline.IndentationSpaces()) {
tokens_.reserve(uwline.Size());
// Convert working PreFormatTokens (computed from wrap optimization) into
// decision-bound representation.
const auto range = uwline.TokensRange();
std::transform(range.begin(), range.end(), std::back_inserter(tokens_),
[](const PreFormatToken& t) { return FormattedToken(t); });
}
std::ostream& FormattedExcerpt::FormattedText(
std::ostream& stream, bool indent,
const std::function<bool(const TokenInfo&)>& include_token_p) const {
if (tokens_.empty()) return stream;
// Let caller print the preceding/trailing newline.
if (indent) {
if (tokens_.front().before.action != SpacingDecision::Preserve) {
stream << Spacer(IndentationSpaces());
}
}
// We do not want the indentation before the first token, if it was
// already handled separately.
const auto& front = tokens_.front();
if (include_token_p(*front.token)) {
VLOG(2) << "action: " << front.before.action;
switch (front.before.action) {
case SpacingDecision::Align:
// When aligning tokens, the first token might be further indented.
stream << Spacer(front.before.spaces) << front.token->text();
break;
default:
stream << front.token->text();
}
}
for (const auto& ftoken :
verible::make_range(tokens_.begin() + 1, tokens_.end())) {
if (include_token_p(*ftoken.token)) stream << ftoken;
}
return stream;
}
std::ostream& operator<<(std::ostream& stream,
const FormattedExcerpt& excerpt) {
return excerpt.FormattedText(stream, true);
}
std::string FormattedExcerpt::Render() const {
std::ostringstream stream;
FormattedText(stream, true);
return stream.str();
}
} // namespace verible