Skip to content

Commit

Permalink
feat: more fmt improvements (#224)
Browse files Browse the repository at this point in the history
This adds support for optional newline before curly brace in the rule declaration.

```
// This is the default
rule a {
```
vs

```
rule a
{
```

It also adds support for empty lines before and after section headers. Each one is a separate option - you can have them before section headers but not after, vice versa, both, or none.

```
rule a {

  meta:

    foo = "bar"
```

vs

```
// This is the default (no empty lines)
rule a {
  meta:
    foo = "bar"
```

This also fixes an issue where we were not inserting newlines before meta definitions, so if you had two meta definitions on the same line the formatter left them like that. They are now fixed so that each meta definition goes on a separate line.
  • Loading branch information
wxsBSD authored and plusvic committed Oct 17, 2024
1 parent cb0494f commit c889ebc
Show file tree
Hide file tree
Showing 20 changed files with 366 additions and 50 deletions.
9 changes: 8 additions & 1 deletion cli/src/commands/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,14 @@ pub fn exec_fmt(
.align_patterns(config.patterns.align_values)
.indent_section_headers(config.rule.indent_section_headers)
.indent_section_contents(config.rule.indent_section_contents)
.indent_spaces(config.rule.indent_spaces);
.indent_spaces(config.rule.indent_spaces)
.newline_before_curly_brace(config.rule.newline_before_curly_brace)
.empty_line_before_section_header(
config.rule.empty_line_before_section_header,
)
.empty_line_after_section_header(
config.rule.empty_line_after_section_header,
);
let mut changed = false;

for file in files {
Expand Down
9 changes: 9 additions & 0 deletions cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ pub struct Rule {
pub indent_section_contents: bool,
/// Number of spaces for indent. Set to 0 to use tabs.
pub indent_spaces: u8,
/// Insert a newline after the rule declaration but before the curly brace.
pub newline_before_curly_brace: bool,
/// Insert an empty line before section headers.
pub empty_line_before_section_header: bool,
/// Insert an empty line after section headers.
pub empty_line_after_section_header: bool,
}

/// Meta specific formatting information.
Expand All @@ -57,6 +63,9 @@ impl Default for Config {
indent_section_headers: true,
indent_section_contents: true,
indent_spaces: 2,
newline_before_curly_brace: false,
empty_line_before_section_header: true,
empty_line_after_section_header: false,
},
meta: Meta { align_values: true },
patterns: Patterns { align_values: true },
Expand Down
61 changes: 61 additions & 0 deletions docs/YARA-X Config Guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ The `yr` command looks in `${HOME}/.yara-x.toml` when starting up. If that file
does not exist the default values are used.

An example `.yara-x.toml` file is below, with comments that explain each option.
The values for each option are the default values that are used if omit the
option.

This is the definitive list of supported configuration options, and will be
updated as more are added.

Expand Down Expand Up @@ -60,6 +63,64 @@ rule.indent_section_contents = true
# rule.indent_section_headers and rule.indent_section_contents
rule.indent_spaces = 2

# Add a newline before the curly brace that starts the rule body.
#
# rule a {
# condition:
# true
# }
#
# Becomes:
#
# rule a
# {
# condition:
# true
# }
#
# Note: If you have multiple newlines before the curly brace and this is set to
# `true` then this will ensure exactly two newlines are left.
rule.newline_before_curly_brace = false

# Add an empty line before section headers so that:
#
# rule a {
# meta:
# date = "20240705"
# strings:
# $ = "AXSERS"
# }
#
# Becomes:
#
# rule a {
# meta:
# date = "20240705"
#
# strings:
# $ = "AXSERS"
# }
#
# Note: This does not apply to the first section header defined. All empty lines
# before the first section header are always removed.
rule.empty_line_before_section_header = true

# Add an empty line after section headers so that:
#
# rule a {
# strings:
# $ = "AXSERS"
# }
#
# Becomes:
#
# rule a {
# strings:
#
# $ = "AXSERS"
# }
rule.empty_line_after_section_header = false

# Align metadata values so that:
#
# rule a {
Expand Down
Loading

0 comments on commit c889ebc

Please sign in to comment.