Skip to content

Fix comment indentation #143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `continuation_indents` now controls the number of indentations per continuation.
- Parsing of `while`, `with`, `on`, `if`, case arm and `for` statements to use
child lines for their nested statements.
- Indentation level of comments in various edge cases.

### Added

Expand Down
49 changes: 46 additions & 3 deletions core/datatests/generators/logical_line_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,19 +292,62 @@ mod comments {
",
$comment
),
before_type_def = format!(
inside_type_def = format!(
"
_|type
_| {}
_| {0}
_| TFoo = Bar;
_| {0}
_| TFoo2 = Bar;
",
$comment
),
inside_const_def = format!(
"
_|const
_| {0}
_| A = B;
_| {0}
_| A2 = B;
",
$comment
),
inside_var_def = format!(
"
_|var
_| {0}
_| A;
_| {0}
_| A2;
",
$comment
),
after_type_def_before_routine = format!(
"
_|type
_| TFoo = Bar;
_|{}
_|{0}
_|{0}
_|procedure Foo;
",
$comment
),
after_const_def_before_routine = format!(
"
_|const
_| A = B;
_|{0}
_|{0}
_|procedure Foo;
",
$comment
),
after_var_def_before_routine = format!(
"
_|var
_| A;
_|{0}
_|{0}
_|procedure Foo;
",
$comment
Expand Down
46 changes: 28 additions & 18 deletions core/src/defaults/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ struct InternalDelphiLogicalLineParser<'a, 'b> {
pass_index: usize,
context: ParserContexts,
unfinished_comment_lines: Vec<LogicalLineRef>,
current_line_is_unfinished: bool,
paren_level: u32,
brack_level: u32,
generic_level: u32,
Expand All @@ -195,6 +196,7 @@ impl<'a, 'b> InternalDelphiLogicalLineParser<'a, 'b> {
pass_index: 0,
context: ParserContexts::default(),
unfinished_comment_lines: vec![],
current_line_is_unfinished: false,
paren_level: 0,
brack_level: 0,
generic_level: 0,
Expand Down Expand Up @@ -261,8 +263,6 @@ impl<'a, 'b> InternalDelphiLogicalLineParser<'a, 'b> {
if let TT::CompilerDirective = kind {
self.set_logical_line_type(LLT::CompilerDirective);
}
let line_ref = self.get_current_logical_line_ref();
self.finish_logical_line();
/*
The lines are considered "unfinished" if their level is determined by the
code that follows.
Expand All @@ -280,11 +280,10 @@ impl<'a, 'b> InternalDelphiLogicalLineParser<'a, 'b> {
end;
```
*/
if matches!(
self.get_last_context_type(),
Some(ContextType::SubRoutine | ContextType::TypeBlock)
) {
self.unfinished_comment_lines.push(line_ref);
if self.is_in_statement_list() {
self.finish_logical_line();
} else {
self.make_unfinished_line();
}
}
TT::Keyword(
Expand Down Expand Up @@ -320,9 +319,7 @@ impl<'a, 'b> InternalDelphiLogicalLineParser<'a, 'b> {
// If there is a `[` at the start of a line, it must be an attribute
self.skip_pair();
self.set_logical_line_type(LogicalLineType::Attribute);
let line_ref = self.get_current_logical_line_ref();
self.finish_logical_line();
self.unfinished_comment_lines.push(line_ref);
self.make_unfinished_line();
}
TT::Keyword(
keyword_kind @ (KK::Interface
Expand Down Expand Up @@ -1175,6 +1172,12 @@ impl<'a, 'b> InternalDelphiLogicalLineParser<'a, 'b> {
self.parse_statement();
self.context.pop();
}
fn is_in_statement_list(&self) -> bool {
let mut contexts = self.context.contexts.iter().rev().map(|c| c.context_type);

matches!(contexts.next(), Some(ContextType::Statement(_)))
&& matches!(contexts.next(), Some(ContextType::StatementBlock(_)))
}
fn parse_statement_list_block(&mut self, context: ParserContext) {
self.parse_statement_block_with_kind(context, StatementKind::Normal);
}
Expand Down Expand Up @@ -1733,6 +1736,13 @@ impl<'a, 'b> InternalDelphiLogicalLineParser<'a, 'b> {
self.get_current_logical_line().tokens.is_empty()
}

fn make_unfinished_line(&mut self) {
let line_ref = self.get_current_logical_line_ref();
self.current_line_is_unfinished = true;
self.unfinished_comment_lines.push(line_ref);
self.finish_logical_line();
}

fn finish_logical_line(&mut self) {
if self.is_at_start_of_line() {
self.get_current_logical_line_mut().line_type = LLT::Unknown;
Expand All @@ -1749,11 +1759,14 @@ impl<'a, 'b> InternalDelphiLogicalLineParser<'a, 'b> {
}
let (parent, context_level) = self.get_context_level();

for unfinished_line in self.unfinished_comment_lines.drain(..).collect_vec() {
if let Some(line) = self.get_logical_line_from_ref_mut(unfinished_line) {
line.level = context_level;
if !self.current_line_is_unfinished {
for unfinished_line in self.unfinished_comment_lines.drain(..).collect_vec() {
if let Some(line) = self.get_logical_line_from_ref_mut(unfinished_line) {
line.level = context_level;
}
}
}
};
self.current_line_is_unfinished = false;

let line_ref = *self.current_line.last();

Expand Down Expand Up @@ -2010,10 +2023,7 @@ impl<'a, 'b> InternalDelphiLogicalLineParser<'a, 'b> {
self.context.contexts.last()
}
fn get_last_context_type(&self) -> Option<ContextType> {
self.context
.contexts
.last()
.map(|&ParserContext { context_type, .. }| context_type)
self.context.contexts.last().map(|ctx| ctx.context_type)
}
fn get_context_level(&self) -> (Option<LineParent>, u16) {
/*
Expand Down