Skip to content

Implement safe mode for reorder code#165

Merged
NathanLovato merged 2 commits intomainfrom
nathan/safe-mode-reorder
Nov 9, 2025
Merged

Implement safe mode for reorder code#165
NathanLovato merged 2 commits intomainfrom
nathan/safe-mode-reorder

Conversation

@NathanLovato
Copy link
Copy Markdown
Contributor

This branch attempts to implement #76 and allow reorder-code to have support for safe mode.

I'm opening this PR in case someone has the time and will to review it before this gets into the formatter for wider testing.

The idea is now the formatter passes work like this:

  1. formatting with topiary
  2. post-processing
  3. If safe mode is on, run the existing safe mode check on the formatted code
  4. If reorder code is on, reorder the code
  5. If safe mode is on and the code was reordered, verify that no top-level declaration has been lost or doc comments or annotations have been detached from those declarations. Code reorder only moves top level declarations, so I think this should cover the safety check for that tool.

Close #76

@fstxz
Copy link
Copy Markdown
Contributor

fstxz commented Nov 9, 2025

I tested this a bit with some of my code and seems to work fine. Code looks fine to me as well. The only issue I found is that inline comments are moved to a new line after reordering:

Before:

var test = 1 # Comment

After:

var test = 1
# Comment

Safe mode didn't catch this, I guess because the syntax tree is the same in both cases.

@NathanLovato
Copy link
Copy Markdown
Contributor Author

Thank you very much for taking the time to test and review the code! I'll try to address the issue a little later or otherwise log it

@NathanLovato NathanLovato merged commit e1b4602 into main Nov 9, 2025
1 check passed
@NathanLovato NathanLovato deleted the nathan/safe-mode-reorder branch November 9, 2025 17:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add safe mode support to the reorder code command

2 participants