Skip to content

Commit

Permalink
Duplicate code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Schottkyc137 committed Jul 23, 2024
1 parent 9170bb1 commit 734ef87
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 73 deletions.
46 changes: 15 additions & 31 deletions vhdl_lang/src/formatting/concurrent_statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,27 +162,12 @@ impl VHDLFormatter<'_> {
span: TokenSpan,
buffer: &mut Buffer,
) {
if self.tokens.get_token(span.start_token).kind == Kind::Postponed {
// postponed process
self.format_token_span(
TokenSpan::new(span.start_token, span.start_token + 1),
buffer,
);
} else {
// process
self.format_token_id(span.start_token, buffer);
}
self.token_with_opt_postponed(span, buffer);
if let Some(sensitivity_list) = &process.sensitivity_list {
match &sensitivity_list.item {
SensitivityList::Names(names) => {
self.format_token_id(sensitivity_list.span.start_token, buffer);
for (i, name) in names.iter().enumerate() {
self.format_name(name.as_ref(), buffer);
if i < names.len() - 1 {
self.format_token_id(name.span.end_token + 1, buffer);
buffer.push_whitespace();
}
}
self.format_name_list(buffer, names);
self.format_token_id(sensitivity_list.span.end_token, buffer);
}
SensitivityList::All => self.join_token_span(sensitivity_list.span, buffer),
Expand All @@ -205,22 +190,26 @@ impl VHDLFormatter<'_> {
self.format_token_id(process.span.end_token, buffer);
}

pub fn format_concurrent_assert_statement(
&self,
statement: &ConcurrentAssertStatement,
span: TokenSpan,
buffer: &mut Buffer,
) {
fn token_with_opt_postponed(&self, span: TokenSpan, buffer: &mut Buffer) {
if self.tokens.get_token(span.start_token).kind == Kind::Postponed {
// postponed assert
// postponed <x>
self.format_token_span(
TokenSpan::new(span.start_token, span.start_token + 1),
buffer,
);
} else {
// assert
// <x>
self.format_token_id(span.start_token, buffer);
}
}

pub fn format_concurrent_assert_statement(
&self,
statement: &ConcurrentAssertStatement,
span: TokenSpan,
buffer: &mut Buffer,
) {
self.token_with_opt_postponed(span, buffer);
buffer.push_whitespace();
self.format_assert_statement(&statement.statement, buffer);
// ;
Expand All @@ -235,12 +224,7 @@ impl VHDLFormatter<'_> {
buffer.push_whitespace();
self.format_expression(report.as_ref(), buffer);
}
if let Some(severity) = &assert_statement.severity {
buffer.push_whitespace();
self.format_token_id(severity.span.start_token - 1, buffer);
buffer.push_whitespace();
self.format_expression(severity.as_ref(), buffer);
}
self.format_opt_severity(assert_statement.severity.as_ref(), buffer);
}

pub fn format_assignment_statement(
Expand Down
24 changes: 12 additions & 12 deletions vhdl_lang/src/formatting/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ impl VHDLFormatter<'_> {

pub fn format_v_unit_binding_indications(
&self,
vunits: &[VUnitBindingIndication],
v_units: &[VUnitBindingIndication],
buffer: &mut Buffer,
) {
for vunit_bind_ind in vunits {
for v_unit_bind_ind in v_units {
buffer.line_break();
self.format_v_unit_indication(vunit_bind_ind, buffer);
self.format_v_unit_indication(v_unit_bind_ind, buffer);
}
}

Expand Down Expand Up @@ -186,23 +186,23 @@ impl VHDLFormatter<'_> {

pub fn format_v_unit_indication(
&self,
vunit_binding_indication: &VUnitBindingIndication,
v_unit_binding_indication: &VUnitBindingIndication,
buffer: &mut Buffer,
) {
// use
self.format_token_id(vunit_binding_indication.span.start_token, buffer);
self.format_token_id(v_unit_binding_indication.span.start_token, buffer);
buffer.push_whitespace();
// vunit
self.format_token_id(vunit_binding_indication.span.start_token + 1, buffer);
// v_unit
self.format_token_id(v_unit_binding_indication.span.start_token + 1, buffer);
buffer.push_whitespace();
for vunit in &vunit_binding_indication.vunit_list {
self.format_name(vunit.as_ref(), buffer);
if self.tokens.get_token(vunit.span.end_token + 1).kind == Kind::Comma {
self.format_token_id(vunit.span.end_token + 1, buffer);
for v_unit in &v_unit_binding_indication.vunit_list {
self.format_name(v_unit.as_ref(), buffer);
if self.tokens.get_token(v_unit.span.end_token + 1).kind == Kind::Comma {
self.format_token_id(v_unit.span.end_token + 1, buffer);
buffer.push_whitespace();
}
}
self.format_token_id(vunit_binding_indication.span.end_token, buffer);
self.format_token_id(v_unit_binding_indication.span.end_token, buffer);
}
}

Expand Down
1 change: 1 addition & 0 deletions vhdl_lang/src/formatting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mod expression;
mod interface;
mod name;
mod sequential_statement;
mod statement;
mod subprogram;
mod token;

Expand Down
10 changes: 10 additions & 0 deletions vhdl_lang/src/formatting/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ impl VHDLFormatter<'_> {
// >>
self.format_token_id(name.span.end_token, buffer);
}

pub fn format_name_list(&self, buffer: &mut Buffer, names: &[WithTokenSpan<Name>]) {
for name in names {
self.format_name(name.as_ref(), buffer);
if self.tokens.get_token(name.span.end_token + 1).kind == Kind::Comma {
self.format_token_id(name.span.end_token + 1, buffer);
buffer.push_whitespace();
}
}
}
}

#[cfg(test)]
Expand Down
42 changes: 20 additions & 22 deletions vhdl_lang/src/formatting/sequential_statement.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::ast::token_range::WithTokenSpan;
use crate::ast::{
AssignmentRightHand, CaseStatement, Choice, DelayMechanism, IterationScheme,
AssignmentRightHand, CaseStatement, Choice, DelayMechanism, Expression, Ident, IterationScheme,
LabeledSequentialStatement, LoopStatement, ReportStatement, SequentialStatement,
SignalAssignment, WaitStatement,
SignalAssignment, WaitStatement, WithRef,
};
use crate::formatting::buffer::Buffer;
use crate::{HasTokenSpan, TokenSpan};
Expand Down Expand Up @@ -146,12 +146,7 @@ impl VHDLFormatter<'_> {
self.format_token_id(span.start_token, buffer);
buffer.push_whitespace();
self.format_expression(report.report.as_ref(), buffer);
if let Some(severity) = &report.severity {
buffer.push_whitespace();
self.format_token_id(severity.span.start_token - 1, buffer);
buffer.push_whitespace();
self.format_expression(severity.as_ref(), buffer);
}
self.format_opt_severity(report.severity.as_ref(), buffer);
self.format_token_id(span.end_token, buffer);
}

Expand Down Expand Up @@ -421,17 +416,8 @@ impl VHDLFormatter<'_> {
) {
// next
self.format_token_id(span.start_token, buffer);
if let Some(label) = &statement.loop_label {
buffer.push_whitespace();
self.format_token_id(label.item.token, buffer);
}
if let Some(condition) = &statement.condition {
buffer.push_whitespace();
// when
self.format_token_id(condition.span.start_token - 1, buffer);
buffer.push_whitespace();
self.format_expression(condition.as_ref(), buffer);
}
self.format_opt_loop_label(statement.loop_label.as_ref(), buffer);
self.format_opt_condition(statement.condition.as_ref(), buffer);
self.format_token_id(span.end_token, buffer);
}

Expand All @@ -443,18 +429,30 @@ impl VHDLFormatter<'_> {
) {
// next
self.format_token_id(span.start_token, buffer);
if let Some(label) = &statement.loop_label {
self.format_opt_loop_label(statement.loop_label.as_ref(), buffer);
self.format_opt_condition(statement.condition.as_ref(), buffer);
self.format_token_id(span.end_token, buffer);
}

fn format_opt_loop_label(&self, loop_label: Option<&WithRef<Ident>>, buffer: &mut Buffer) {
if let Some(label) = loop_label {
buffer.push_whitespace();
self.format_token_id(label.item.token, buffer);
}
if let Some(condition) = &statement.condition {
}

fn format_opt_condition(
&self,
condition: Option<&WithTokenSpan<Expression>>,
buffer: &mut Buffer,
) {
if let Some(condition) = &condition {
buffer.push_whitespace();
// when
self.format_token_id(condition.span.start_token - 1, buffer);
buffer.push_whitespace();
self.format_expression(condition.as_ref(), buffer);
}
self.format_token_id(span.end_token, buffer);
}
}

Expand Down
19 changes: 19 additions & 0 deletions vhdl_lang/src/formatting/statement.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use crate::ast::token_range::WithTokenSpan;
use crate::ast::Expression;
use crate::formatting::buffer::Buffer;
use crate::VHDLFormatter;

impl VHDLFormatter<'_> {
pub(crate) fn format_opt_severity(
&self,
severity: Option<&WithTokenSpan<Expression>>,
buffer: &mut Buffer,
) {
if let Some(severity) = &severity {
buffer.push_whitespace();
self.format_token_id(severity.span.start_token - 1, buffer);
buffer.push_whitespace();
self.format_expression(severity.as_ref(), buffer);
}
}
}
9 changes: 1 addition & 8 deletions vhdl_lang/src/formatting/subprogram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,7 @@ impl VHDLFormatter<'_> {
self.format_name(return_type.as_ref(), buffer);
}
Signature::Procedure(procedures) => {
for (i, procedure) in procedures.iter().enumerate() {
self.format_name(procedure.as_ref(), buffer);
if i < procedures.len() - 1 {
// ,
self.format_token_id(procedure.span.end_token + 1, buffer);
buffer.push_whitespace();
}
}
self.format_name_list(buffer, procedures);
}
}
self.format_token_id(signature.span.end_token, buffer);
Expand Down

0 comments on commit 734ef87

Please sign in to comment.