Skip to content

fix(align): improve usability by not writing unchanged text to buffer - #2599

Merged
echasnovski merged 2 commits into
nvim-mini:backlogfrom
abeldekat:fix_align
Nov 17, 2026
Merged

echasnovski merged 2 commits into
nvim-mini:backlogfrom
abeldekat:fix_align

Conversation

@abeldekat

@abeldekat abeldekat commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Details:

  • Initially, align invokes H.process_current_region, using the global
    or buffer local config values. Unless the user changed
    config.options.split_pattern, that initial call will not result in
    changed lines.

    Currently, those lines are written to the buffer. The buffer is marked
    as changed, which confuses the user as there are no changes. Also, if
    the user stops the process, the buffer is still marked as modified.

  • Solution: Only write to buffer if lines were changed.

I included an extra commit, adding i and | modifiers to the setup test.

Also, in :h MiniAlign.config, I wonder if the example for the default i modifier is correct. Perhaps MiniAlign.gen_step.ignore_split() should be added to the config.steps.pre_split table, similar like this example.

@echasnovski echasnovski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This indeed is a good candidate to be fixed. I am a little worried that I might have quickly tried to do this in the past, stumbled on non-trivial issue, decided to postpone. I.e. it might be not as straightforward, but not use if it is.

Comment thread lua/mini/align.lua Outdated
-- Make sure that latest changes are shown
vim.cmd('redraw')
-- Set text if lines changed
local lines_changed = table.concat(strings) ~= table.concat(strings_aligned)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just realized that this is probably not entirely full proof: { 'a ', 'b' } and { 'a', ' b' } will be computed as not changed. It is better either vim.deep_equal() or use table.concat(string, '\n').

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread lua/mini/align.lua
local lines_now_changed = H.process_current_region(lines_were_changed, mode, opts, steps)
lines_were_changed = lines_were_changed or lines_now_changed
-- Process region while tracking if lines were changed
lines_were_changed = H.process_current_region(lines_were_changed, mode, opts, steps)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is an equivalent change if H.process_current_region doesn't use lines_were_changed in a similar manner. Currently lines_were_changed tracks whether the lines were set at least once, while here it tracks whether it was changed on the latest iteration. Is this necessary?

@abeldekat abeldekat Nov 16, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think so.

Variable lines_were_changed is used to call H.undo(). When only changed lines are written, it needs to reflect the latest iteration.
Otherwise, when lines_were_changed keeps being true after lines where set at least once, undo might be called even when no lines were written...

That would happen initially with the default config, but also in an interactive session when the user for example reverts the split back to ``. See also this test.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps it would be more obvious if lines_were_changed was named lines_were_set

Comment thread tests/test_align.lua Outdated
validate_keys({ 'a\3b', 'aa\3b' }, { 'ga', 'ip', stop_key }, { 'a\3b', 'aa\3b' })
eq(get_mode(), 'n')
-- Verify clean stop
eq(child.b.changedtick - start_tick, 1)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not immediately obvious that the test is for "nothing has changed", since the changedtick has changed. The main problem is that the buffer is marked as modified, so that should be tested. Would require "unwrapping" validate_keys to also include child.bo.modified = false after set_lines.

Also I'd verify this as eq(child.b.changedtick. start_tick + 1).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I initially tried with child.o.modified, to no avail. But child.bo.modified works.

Comment thread tests/test_align.lua Outdated
-- Unchanged text should not be written to buffer
local start_tick = child.b.changedtick
type_keys(init_keys)
eq(child.b.changedtick, start_tick)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same as in the previous test case: eq(child.bo.modified, false) is probably a more accurate test for what is the actual problem.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@echasnovski

Copy link
Copy Markdown
Member

Also, in :h MiniAlign.config, I wonder if the example for the default i modifier is correct. Perhaps MiniAlign.gen_step.ignore_split() should be added to the config.steps.pre_split table, similar like this example.

Does it not work? I'd think it is the way it is to demonstrate that it is possible to modify steps argument in place and it would work.

Details:
- Initially, align invokes `H.process_current_region`, using the global
  or buffer local config values. Unless the user changed
  `config.options.split_pattern`, that initial call will not result in
  changed lines.

  Currently, those lines are written to the buffer. The buffer is marked
  as changed, which confuses the user as there are no changes. Also, if
  the user stops the process, the buffer is still marked as modified.

- Solution: Only write to buffer if lines were changed.
@abeldekat

Copy link
Copy Markdown
Member Author

Does it not work? I'd think it is the way it is to demonstrate that it is possible to modify steps argument in place and it would work.

I thought it was meant to demonstrate a config where the 'ignore' option is always on... Thanks!

@echasnovski
echasnovski changed the base branch from main to backlog November 17, 2026 11:01

@echasnovski echasnovski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tested it locally and the behavior seems to be indeed the one that I'd expect: 'modified' if the original lines were actually modified. I'll rework this a bit to cover with more tests and maybe make a (previous 'mini.align') code more obvious.

@echasnovski
echasnovski merged commit dadca3c into nvim-mini:backlog Nov 17, 2026
12 checks passed
echasnovski added a commit that referenced this pull request Nov 17, 2026
Details:
- The buffer is set as 'modified' in situations where it can be avoided:
    - Right at the start without set split pattern.
    - After canceling with `<Esc>` or `<C-c>`.
    - If lines "go back" to their initial value.

- Changing buffer text only if it is different fixes the issue, as prior
  to that there is an `:undo` done if the lines from previous step were
  different.

Related to #2599

Co-authored-by: abeldekat <58370433+abeldekat@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants