Skip to content

Commit 493edcb

Browse files
committed
Deindent compiler directives in package files
Fixes #166. Instead of *not* formatting the directives in dpk files, we instead format them in a way consistent with what the IDE generates. In the case of #202 we decided to format with a slightly different style to the generated code for import clauses, because the difference was only in a couple of rare cases. In this case, however, we have decided to match the generated style because it affects 100% of dpk files, and it's a pretty trivial difference.
1 parent b0d4986 commit 493edcb

File tree

5 files changed

+194
-0
lines changed

5 files changed

+194
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
1313
Previously there were concerns over breaking IDE features by formatting these sections, but after some
1414
testing and reconsideration we have decided that this is a non-issue and it's better to consistently
1515
format all import clauses.
16+
- Compiler directives in `package` files are now flush with the margin, matching the style in
17+
the code generated by RAD Studio.
1618

1719
## [0.4.0-rc2] - 2025-03-12
1820

core/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Added `DeindentPackageDirectives` to match the indentation of compiler directives in dpk files
13+
with that generated by RAD Studio.
14+
1015
### Removed
1116

1217
- Removed `IgnoreNonUnitImportClauses`; all import clauses are now formatted.
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
use crate::prelude::*;
2+
3+
pub struct DeindentPackageDirectives {}
4+
5+
impl LogicalLinesConsolidator for DeindentPackageDirectives {
6+
fn consolidate(&self, (tokens, lines): (&mut [Token], &mut [LogicalLine])) {
7+
let Some(TokenType::Keyword(KeywordKind::Package)) =
8+
(tokens.iter().map(TokenData::get_token_type)).find(|tt| !tt.is_comment_or_directive())
9+
else {
10+
return;
11+
};
12+
13+
for line in lines {
14+
if let LogicalLineType::CompilerDirective | LogicalLineType::ConditionalDirective =
15+
line.get_line_type()
16+
{
17+
// must run before the call to `void_and_drain`
18+
let line_type = line.get_line_type();
19+
let tmp_tokens = line.void_and_drain().collect();
20+
let mut new_line = LogicalLine::new(line.get_parent(), 0, tmp_tokens, line_type);
21+
std::mem::swap(line, &mut new_line);
22+
}
23+
}
24+
}
25+
}
26+
27+
#[cfg(test)]
28+
mod tests {
29+
use super::*;
30+
use indoc::indoc;
31+
32+
struct IndentAsLevel;
33+
impl LogicalLineFileFormatter for IndentAsLevel {
34+
fn format(&self, formatted_tokens: &mut FormattedTokens<'_>, lines: &[LogicalLine]) {
35+
for line in lines {
36+
dbg!(line.get_line_type(), line.get_level());
37+
for (idx, &tok) in line.get_tokens().iter().enumerate() {
38+
let fmt = formatted_tokens.get_formatting_data_mut(tok).unwrap();
39+
if idx == 0 {
40+
fmt.indentations_before = line.get_level();
41+
fmt.spaces_before = 0;
42+
} else {
43+
fmt.indentations_before = 0;
44+
}
45+
fmt.continuations_before = 0;
46+
}
47+
}
48+
}
49+
}
50+
51+
fn formatter() -> Formatter {
52+
Formatter::builder()
53+
.lexer(DelphiLexer {})
54+
.parser(DelphiLogicalLineParser {})
55+
.lines_consolidator(DeindentPackageDirectives {})
56+
.file_formatter(IndentAsLevel)
57+
.reconstructor(default_test_reconstructor())
58+
.build()
59+
}
60+
61+
formatter_test_group!(
62+
deindent,
63+
implicit_build_section = {
64+
indoc! {"
65+
package Foo;
66+
{$IFDEF IMPLICITBUILDING}
67+
{$ALIGN 8}
68+
{$IMAGEBASE $400000}
69+
{$DEFINE RELEASE}
70+
{$ENDIF IMPLICITBUILDING}
71+
{$IMPLICITBUILD ON}
72+
end.
73+
"},
74+
indoc! {"
75+
package Foo;
76+
{$IFDEF IMPLICITBUILDING}
77+
{$ALIGN 8}
78+
{$IMAGEBASE $400000}
79+
{$DEFINE RELEASE}
80+
{$ENDIF IMPLICITBUILDING}
81+
{$IMPLICITBUILD ON}
82+
end.
83+
"},
84+
},
85+
outside_implicit_build_section = {
86+
indoc! {"
87+
{$IFDEF FOO}
88+
{$DEFINE OOF}
89+
{$IFDEF OOF}
90+
{$DEFINE FOO}
91+
{$ENDIF}
92+
{$ENDIF}
93+
package Foo;
94+
{$IFDEF IMPLICITBUILDING}
95+
{$ENDIF IMPLICITBUILDING}
96+
end.
97+
{$IFDEF FOO}
98+
{$DEFINE OOF}
99+
{$ENDIF}
100+
"},
101+
indoc! {"
102+
{$IFDEF FOO}
103+
{$DEFINE OOF}
104+
{$IFDEF OOF}
105+
{$DEFINE FOO}
106+
{$ENDIF}
107+
{$ENDIF}
108+
package Foo;
109+
{$IFDEF IMPLICITBUILDING}
110+
{$ENDIF IMPLICITBUILDING}
111+
end.
112+
{$IFDEF FOO}
113+
{$DEFINE OOF}
114+
{$ENDIF}
115+
"},
116+
},
117+
program_not_affected = {
118+
indoc! {"
119+
program Foo;
120+
{$IFDEF IMPLICITBUILDING}
121+
{$ALIGN 8}
122+
{$IMAGEBASE $400000}
123+
{$DEFINE RELEASE}
124+
{$ENDIF IMPLICITBUILDING}
125+
{$IMPLICITBUILD ON}
126+
end.
127+
"},
128+
indoc! {"
129+
program Foo;
130+
{$IFDEF IMPLICITBUILDING}
131+
{$ALIGN 8}
132+
{$IMAGEBASE $400000}
133+
{$DEFINE RELEASE}
134+
{$ENDIF IMPLICITBUILDING}
135+
{$IMPLICITBUILD ON}
136+
end.
137+
"},
138+
},
139+
library_not_affected = {
140+
indoc! {"
141+
library Foo;
142+
{$IFDEF IMPLICITBUILDING}
143+
{$ALIGN 8}
144+
{$IMAGEBASE $400000}
145+
{$DEFINE RELEASE}
146+
{$ENDIF IMPLICITBUILDING}
147+
{$IMPLICITBUILD ON}
148+
end.
149+
"},
150+
indoc! {"
151+
library Foo;
152+
{$IFDEF IMPLICITBUILDING}
153+
{$ALIGN 8}
154+
{$IMAGEBASE $400000}
155+
{$DEFINE RELEASE}
156+
{$ENDIF IMPLICITBUILDING}
157+
{$IMPLICITBUILD ON}
158+
end.
159+
"},
160+
},
161+
unit_not_affected = {
162+
indoc! {"
163+
unit Foo;
164+
{$IFDEF IMPLICITBUILDING}
165+
{$ALIGN 8}
166+
{$IMAGEBASE $400000}
167+
{$DEFINE RELEASE}
168+
{$ENDIF IMPLICITBUILDING}
169+
{$IMPLICITBUILD ON}
170+
end.
171+
"},
172+
indoc! {"
173+
unit Foo;
174+
{$IFDEF IMPLICITBUILDING}
175+
{$ALIGN 8}
176+
{$IMAGEBASE $400000}
177+
{$DEFINE RELEASE}
178+
{$ENDIF IMPLICITBUILDING}
179+
{$IMPLICITBUILD ON}
180+
end.
181+
"},
182+
},
183+
);
184+
}

core/src/rules/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod conditional_directive_consolidator;
2+
pub mod deindent_package_directives;
23
pub mod eof_newline;
34
pub mod formatting_toggle;
45
pub mod generics_consolidator;
@@ -7,6 +8,7 @@ pub mod optimising_line_formatter;
78
pub mod token_spacing;
89

910
pub use conditional_directive_consolidator::*;
11+
pub use deindent_package_directives::*;
1012
pub use eof_newline::*;
1113
pub use formatting_toggle::*;
1214
pub use generics_consolidator::*;

front-end/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ pub fn make_formatter(config: &FormattingConfig) -> Formatter {
304304
.parser(DelphiLogicalLineParser {})
305305
.token_consolidator(DistinguishGenericTypeParamsConsolidator {})
306306
.lines_consolidator(ConditionalDirectiveConsolidator {})
307+
.lines_consolidator(DeindentPackageDirectives {})
307308
.token_ignorer(FormattingToggler {})
308309
.token_ignorer(IgnoreAsmIstructions {})
309310
.file_formatter(TokenSpacing {})

0 commit comments

Comments
 (0)