Repository navigation
fix(align): improve usability by not writing unchanged text to buffer - #2599
Conversation
echasnovski
left a comment
There was a problem hiding this comment.
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.
| -- 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) |
There was a problem hiding this comment.
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').
| 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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Perhaps it would be more obvious if lines_were_changed was named lines_were_set
| 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) |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
I initially tried with child.o.modified, to no avail. But child.bo.modified works.
| -- Unchanged text should not be written to buffer | ||
| local start_tick = child.b.changedtick | ||
| type_keys(init_keys) | ||
| eq(child.b.changedtick, start_tick) |
There was a problem hiding this comment.
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.
Does it not work? I'd think it is the way it is to demonstrate that it is possible to modify |
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 thought it was meant to demonstrate a config where the 'ignore' option is always on... Thanks! |
echasnovski
left a comment
There was a problem hiding this comment.
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.
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>
Details:
Initially, align invokes
H.process_current_region, using the globalor buffer local config values. Unless the user changed
config.options.split_pattern, that initial call will not result inchanged 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
iand|modifiers to thesetuptest.Also, in:h MiniAlign.config, I wonder if the example for the defaultimodifier is correct. PerhapsMiniAlign.gen_step.ignore_split()should be added to theconfig.steps.pre_splittable, similar like this example.