Skip to content

Commit

Permalink
Fix a bug where indentation_context stack's size is not decreasing
Browse files Browse the repository at this point in the history
  • Loading branch information
malkoG committed Aug 11, 2023
1 parent b966b49 commit 80d42a0
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl<'tree> Traversable<'tree> for Analyzer {
let mut writer_queue = VecDeque::new();
let mut pending_queue = VecDeque::new();
let mut nodes_queue = VecDeque::from(nodes);
let mut indentation_context = VecDeque::new();
let mut indentation_context: VecDeque<Node> = VecDeque::new();
let indent_comment_pool = self.get_indent_comment_pool();

for (i, line) in self.source_code.lines().enumerate() {
Expand Down Expand Up @@ -157,19 +157,19 @@ impl<'tree> Traversable<'tree> for Analyzer {
pop_node = true;
}

if nested_traversable_symbols.contains(&node_type) {
indentation_context.push_back(*current_node);
pop_node = true;
}

if !indentation_context.is_empty() {
if let Some(current_context) = indentation_context.front() {
if cursor_position == current_context.end_position() {
indentation_context.pop_front();
if let Some(current_context) = indentation_context.back() {
if cursor_position.row >= current_context.end_position().row {
indentation_context.pop_back();
}
}
}

if nested_traversable_symbols.contains(&node_type) {
indentation_context.push_back(*current_node);
pop_node = true;
}

if cursor_position == current_node.end_position() {
pop_node = true;
}
Expand All @@ -179,6 +179,13 @@ impl<'tree> Traversable<'tree> for Analyzer {
}
}
_ => {
if !indentation_context.is_empty() {
if let Some(current_context) = indentation_context.back() {
if cursor_position.row >= current_context.end_position().row {
indentation_context.pop_back();
}
}
}
writer_queue.push_back(line.to_owned());
}
}
Expand Down
3 changes: 3 additions & 0 deletions tests/integration_test/rust_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ mod anyhow_case_test;

#[cfg(test)]
mod rustpython_case_test;

#[cfg(test)]
mod serde_case_test;
54 changes: 54 additions & 0 deletions tests/integration_test/rust_test/serde_case_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#[cfg(test)]
mod serde_case_test {
use indoc::indoc;
use crate::integration_test::assert_analyzed_source_code;

#[test]
fn test_several_impl_declaration() {
let source_code = indoc! {"
impl PartialEq<Symbol> for Ident {
fn eq(&self, word: &Symbol) -> bool {
self == word.0
}
}
impl<'a> PartialEq<Symbol> for &'a Ident {
fn eq(&self, word: &Symbol) -> bool {
*self == word.0
}
}
impl PartialEq<Symbol> for Path {
fn eq(&self, word: &Symbol) -> bool {
self.is_ident(word.0)
}
}"};

let result = indoc! {"
/// [TODO]
impl PartialEq<Symbol> for Ident {
/// [TODO]
fn eq(&self, word: &Symbol) -> bool {
self == word.0
}
}
/// [TODO]
impl<'a> PartialEq<Symbol> for &'a Ident {
/// [TODO]
fn eq(&self, word: &Symbol) -> bool {
*self == word.0
}
}
/// [TODO]
impl PartialEq<Symbol> for Path {
/// [TODO]
fn eq(&self, word: &Symbol) -> bool {
self.is_ident(word.0)
}
}"};

assert_analyzed_source_code(source_code, result, "rust")
}
}

0 comments on commit 80d42a0

Please sign in to comment.