Skip to content

Commit 17a0224

Browse files
committed
Implement ForceLineEnding option.
1 parent 9cc73da commit 17a0224

File tree

6 files changed

+28
-15
lines changed

6 files changed

+28
-15
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "shader-formatter"
3-
version = "1.2.0"
3+
version = "1.3.0"
44
edition = "2021"
55
authors = ["Alexander Tretyakov"]
66
license = "MIT"

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2024 Alexander "Flone" Tretyakov
3+
Copyright (c) 2024-2025 Alexander "Flone" Tretyakov
44

55
Permission is hereby granted, free of charge, to any
66
person obtaining a copy of this software and associated

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ Below are the rules that are not checked unless they are specified in your confi
7474
- **FloatPrefix** (string) - defines required prefix for floating-point variables, for example if this rule is set to `f` then a correct variable may look like this: `fValue`.
7575
- **GlobalVariablePrefix** (string) - defines required prefix for global variables, this rule is applied before other prefix and case rules so you can have a "mixed" global variables names like "g_iMyVariable" where global prefix is "g_", int prefix is "i" and case is "Camel".
7676

77+
- **ForceLineEnding** (string) - custom line ending, by default the formatter uses line ending according to the OS ("\r\n" for Windows and "\n" for everything else) but you can overwrite this setting by specifying the value, for example: "\n".
78+
7779
# Temporary disabling formatting or checks
7880

7981
Similar to `clang-tidy` you can use `NOLINT` comments to disable checks for certain parts of your code:

src/config.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub struct Config {
2121
pub int_prefix: Option<String>,
2222
pub float_prefix: Option<String>,
2323
pub global_variable_prefix: Option<String>,
24+
pub force_line_ending: Option<String>,
2425
pub require_docs_on_functions: bool,
2526
pub require_docs_on_structs: bool,
2627
pub require_docs_on_fields: bool,
@@ -42,6 +43,7 @@ impl Default for Config {
4243
int_prefix: None,
4344
float_prefix: None,
4445
global_variable_prefix: None,
46+
force_line_ending: None,
4547
require_docs_on_functions: false,
4648
require_docs_on_structs: false,
4749
require_docs_on_fields: false,
@@ -154,6 +156,10 @@ impl Config {
154156
config.global_variable_prefix =
155157
Some(Self::toml_value_to_string(&key, &value)?.to_string());
156158
}
159+
"ForceLineEnding" => {
160+
config.force_line_ending =
161+
Some(Self::toml_value_to_string(&key, &value)?.to_string());
162+
}
157163
"RequireDocsOnFunctions" => {
158164
config.require_docs_on_functions = Self::toml_value_to_bool(&key, &value)?;
159165
}

src/formatter.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ pub const CHANGES_REQUIRED_ERR_MSG: &str = "changes required";
1717
const NOFORMAT_BEGIN_COMMENT: &str = " NOFORMATBEGIN";
1818
const NOFORMAT_END_COMMENT: &str = " NOFORMATEND";
1919

20-
#[cfg(windows)]
21-
const LINE_ENDING: &str = "\r\n";
22-
#[cfg(not(windows))]
23-
const LINE_ENDING: &str = "\n";
24-
2520
/// Applies rules on files.
2621
pub struct Formatter {
2722
config: Config,
@@ -144,6 +139,16 @@ impl Formatter {
144139
IndentationRule::FourSpaces => " ",
145140
};
146141

142+
// Prepare line ending.
143+
let mut _line_ending = "\n";
144+
#[cfg(windows)]
145+
{
146+
_line_ending = "\r\n";
147+
}
148+
if let Some(custom_line_ending) = &self.config.force_line_ending {
149+
_line_ending = custom_line_ending;
150+
}
151+
147152
let mut output = String::with_capacity(content.len());
148153

149154
// Prepare some handy variables...
@@ -209,7 +214,7 @@ impl Formatter {
209214
}
210215

211216
// Add a new line.
212-
output += LINE_ENDING;
217+
output += _line_ending;
213218
output += &indentation_text.repeat(nesting_count);
214219

215220
consecutive_empty_new_line_count += 1;
@@ -490,7 +495,7 @@ impl Formatter {
490495
}
491496
} else {
492497
// Just put bracket to a new line.
493-
output += LINE_ENDING;
498+
output += _line_ending;
494499
output += &indentation_text.repeat(nesting_count);
495500
output.push(_char);
496501
}
@@ -502,7 +507,7 @@ impl Formatter {
502507
if !prev_line_ended_with_backslash {
503508
// Insert a new line.
504509
is_on_new_line = true;
505-
output += LINE_ENDING;
510+
output += _line_ending;
506511
output += &indentation_text.repeat(nesting_count);
507512
consecutive_empty_new_line_count += 1;
508513
}
@@ -519,7 +524,7 @@ impl Formatter {
519524

520525
// Insert a new line.
521526
is_on_new_line = true;
522-
output += LINE_ENDING;
527+
output += _line_ending;
523528
output += &indentation_text.repeat(nesting_count);
524529

525530
// Add brace.
@@ -531,7 +536,7 @@ impl Formatter {
531536

532537
// Add new line with increased nesting.
533538
nesting_count += 1;
534-
output += LINE_ENDING;
539+
output += _line_ending;
535540
output += &indentation_text.repeat(nesting_count);
536541
consecutive_empty_new_line_count += 1;
537542
}
@@ -559,7 +564,7 @@ impl Formatter {
559564
// we likelly need to keep the code on the same line.
560565
if !line_started_with_preprocessor {
561566
// Add a new line.
562-
output += LINE_ENDING;
567+
output += _line_ending;
563568
output += &indentation_text.repeat(nesting_count);
564569
} else {
565570
output.push(' '); // just add a space after text

0 commit comments

Comments
 (0)