Skip to content

Commit

Permalink
Factor out DisableSyntaxBasedRanges() subroutine.
Browse files Browse the repository at this point in the history
Shortening main formatting function for readability.

PiperOrigin-RevId: 304297805
  • Loading branch information
fangism authored and hzeller committed Apr 2, 2020
1 parent 4b54673 commit 5d7fe52
Showing 1 changed file with 33 additions and 24 deletions.
57 changes: 33 additions & 24 deletions verilog/formatting/formatter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,37 @@ static verible::Interval<int> ByteOffsetRange(absl::string_view substring,
return {disable_begin + 1, disable_end};
}

// Given control flags and syntax tree, selectively disable some ranges
// of text from formatting.
static void DisableSyntaxBasedRanges(ByteOffsetSet* disabled_ranges,
const verible::Symbol& root,
const FormatStyle& style,
absl::string_view full_text) {
// Module-related sections:
for (const auto& match : FindAllModuleDeclarations(root)) {
if (!style.format_module_port_declarations) {
const auto* ports = GetModulePortDeclarationList(*match.match);
if (ports != nullptr) {
const auto ports_text = verible::StringSpanOfSymbol(*ports);
VLOG(4) << "disabled: " << ports_text;
disabled_ranges->Add(ByteOffsetRange(ports_text, full_text));
}
}
if (!style.format_module_instantiations) {
const auto& instantiations = FindAllDataDeclarations(*match.match);
for (const auto& inst : instantiations) {
const auto& module_instances = FindAllGateInstances(*inst.match);
// Only suppress formatting if instances contains a module or
// gate-like instance with ports in parentheses.
if (module_instances.empty()) continue;
const auto inst_text = verible::StringSpanOfSymbol(*inst.match);
VLOG(4) << "disabled: " << inst_text;
disabled_ranges->Add(ByteOffsetRange(inst_text, full_text));
}
}
}
}

Status Formatter::Format(const ExecutionControl& control) {
const absl::string_view full_text(text_structure_.Contents());
const auto& token_stream(text_structure_.TokenStream());
Expand All @@ -404,36 +435,14 @@ Status Formatter::Format(const ExecutionControl& control) {
unwrapper_data.preformatted_tokens.begin(),
unwrapper_data.preformatted_tokens.end());

// Determine ranges of disabling the formatter.
// Determine ranges of disabling the formatter, based on comment controls.
disabled_ranges_.Union(DisableFormattingRanges(full_text, token_stream));

// Find disabled formatting ranges for specific syntax tree node types.
// These are typically temporary workarounds for sections that users
// habitually prefer to format themselves.
if (const auto& root = text_structure_.SyntaxTree()) {
// Module-related sections:
for (const auto& match : FindAllModuleDeclarations(*root)) {
if (!style_.format_module_port_declarations) {
const auto* ports = GetModulePortDeclarationList(*match.match);
if (ports != nullptr) {
const auto ports_text = verible::StringSpanOfSymbol(*ports);
VLOG(4) << "disabled: " << ports_text;
disabled_ranges_.Add(ByteOffsetRange(ports_text, full_text));
}
}
if (!style_.format_module_instantiations) {
const auto& instantiations = FindAllDataDeclarations(*match.match);
for (const auto& inst : instantiations) {
const auto& module_instances = FindAllGateInstances(*inst.match);
// Only suppress formatting if instances contains a module or
// gate-like instance with ports in parentheses.
if (module_instances.empty()) continue;
const auto inst_text = verible::StringSpanOfSymbol(*inst.match);
VLOG(4) << "disabled: " << inst_text;
disabled_ranges_.Add(ByteOffsetRange(inst_text, full_text));
}
}
}
DisableSyntaxBasedRanges(&disabled_ranges_, *root, style_, full_text);
}

// Disable formatting ranges.
Expand Down

0 comments on commit 5d7fe52

Please sign in to comment.