Skip to content

Commit

Permalink
Merge pull request #994 from veryl-lang/fix_case_indent
Browse files Browse the repository at this point in the history
Fix case item misindent
  • Loading branch information
dalance authored Oct 7, 2024
2 parents 7d5cdac + dbe8f7d commit 8b29202
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 42 deletions.
40 changes: 21 additions & 19 deletions crates/emitter/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,17 @@ impl Emitter {
}

fn unindent(&mut self) {
if self
.string
.ends_with(&" ".repeat(self.indent * self.format_opt.indent_width))
{
self.truncate(self.string.len() - self.indent * self.format_opt.indent_width);
let indent_width =
self.indent * self.format_opt.indent_width + self.case_item_indent.unwrap_or(0);
if self.string.ends_with(&" ".repeat(indent_width)) {
self.truncate(self.string.len() - indent_width);
}
}

fn indent(&mut self) {
self.str(&" ".repeat(
self.indent * self.format_opt.indent_width + self.case_item_indent.unwrap_or(0),
));
let indent_width =
self.indent * self.format_opt.indent_width + self.case_item_indent.unwrap_or(0);
self.str(&" ".repeat(indent_width));
}

fn newline_push(&mut self) {
Expand Down Expand Up @@ -338,9 +337,14 @@ impl Emitter {
}
self.colon(&arg.colon);
self.space(1);
self.case_item_indent = Some((self.dst_column - start) as usize);
self.case_item_statement(&arg.case_item_group0);
self.case_item_indent = None;
match arg.case_item_group0.as_ref() {
CaseItemGroup0::Statement(x) => self.statement(&x.statement),
CaseItemGroup0::StatementBlock(x) => {
self.case_item_indent = Some((self.dst_column - start) as usize);
self.statement_block(&x.statement_block);
self.case_item_indent = None;
}
}
}

fn case_expaneded_statement(&mut self, arg: &CaseStatement) {
Expand Down Expand Up @@ -372,15 +376,13 @@ impl Emitter {
}
self.colon(&item.colon);
self.space(1);
self.case_item_indent = Some((self.dst_column - start) as usize);
self.case_item_statement(&item.case_item_group0);
self.case_item_indent = None;
}

fn case_item_statement(&mut self, arg: &CaseItemGroup0) {
match arg {
match item.case_item_group0.as_ref() {
CaseItemGroup0::Statement(x) => self.statement(&x.statement),
CaseItemGroup0::StatementBlock(x) => self.statement_block(&x.statement_block),
CaseItemGroup0::StatementBlock(x) => {
self.case_item_indent = Some((self.dst_column - start) as usize);
self.statement_block(&x.statement_block);
self.case_item_indent = None;
}
}
}

Expand Down
32 changes: 17 additions & 15 deletions crates/formatter/src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,17 @@ impl Formatter {
}

fn unindent(&mut self) {
if self
.string
.ends_with(&" ".repeat(self.indent * self.format_opt.indent_width))
{
self.string
.truncate(self.string.len() - self.indent * self.format_opt.indent_width);
let indent_width =
self.indent * self.format_opt.indent_width + self.case_item_indent.last().unwrap_or(&0);
if self.string.ends_with(&" ".repeat(indent_width)) {
self.string.truncate(self.string.len() - indent_width);
}
}

fn indent(&mut self) {
self.str(&" ".repeat(
self.indent * self.format_opt.indent_width + self.case_item_indent.last().unwrap_or(&0),
));
let indent_width =
self.indent * self.format_opt.indent_width + self.case_item_indent.last().unwrap_or(&0);
self.str(&" ".repeat(indent_width));
}

fn newline_push(&mut self) {
Expand Down Expand Up @@ -787,12 +785,14 @@ impl VerylWalker for Formatter {
}
self.colon(&arg.colon);
self.space(1);
self.case_item_indent.push(self.column() - start);
match &*arg.case_item_group0 {
CaseItemGroup0::Statement(x) => self.statement(&x.statement),
CaseItemGroup0::StatementBlock(x) => self.statement_block(&x.statement_block),
CaseItemGroup0::StatementBlock(x) => {
self.case_item_indent.push(self.column() - start);
self.statement_block(&x.statement_block);
self.case_item_indent.pop();
}
}
self.case_item_indent.pop();
}

/// Semantic action for non-terminal 'CaseCondition'
Expand Down Expand Up @@ -827,12 +827,14 @@ impl VerylWalker for Formatter {
}
self.colon(&arg.colon);
self.space(1);
self.case_item_indent.push(self.column() - start);
match &*arg.switch_item_group0 {
SwitchItemGroup0::Statement(x) => self.statement(&x.statement),
SwitchItemGroup0::StatementBlock(x) => self.statement_block(&x.statement_block),
SwitchItemGroup0::StatementBlock(x) => {
self.case_item_indent.push(self.column() - start);
self.statement_block(&x.statement_block);
self.case_item_indent.pop();
}
}
self.case_item_indent.pop();
}

/// Semantic action for non-terminal 'SwitchCondition'
Expand Down
8 changes: 4 additions & 4 deletions testcases/sv/16_case_switch.sv
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ module veryl_testcase_Module16;

always_comb begin
case (x) inside
0: a = 1;
1: a = 1;
2: begin
a = 1;
0: a = 1; // comment
1: a = 1; // comment
2: begin // comment
a = 1; // comment
a = 1;
a = 1;
end
Expand Down
8 changes: 4 additions & 4 deletions testcases/veryl/16_case_switch.veryl
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ module Module16 {

always_comb {
case x {
0: a = 1;
1: a = 1;
2: {
a = 1;
0: a = 1; // comment
1: a = 1; // comment
2: { // comment
a = 1; // comment
a = 1;
a = 1;
}
Expand Down

0 comments on commit 8b29202

Please sign in to comment.