phptools-docs icon indicating copy to clipboard operation
phptools-docs copied to clipboard

Cut-n-paste sometimes randomly fails to adjust indentation

Open php4fan opened this issue 1 year ago • 6 comments

Given the following code:

<?php
if (true) {
    if (true) {
        if (true) {
            echo "yes";
            echo "yes";
            
        }
        echo "yes"; // select from here
        if (true) {
            echo "yes";
        }           // to here
    }
}
  1. Select from the line that says "select from here" to where it says "to here", both included.
  2. Cut
  3. paste the copied code into the empty line above the } that precedes the copied code.

Expected: the indentation of the pasted code should automatically be adjusted.

Observed: this:

image

Only the indentation of the first pasted line has been adjusted, the rest was copied as-is.

This doesn't always happen. Often I get the expected behavior, but I don't know what makes the difference. It's not the presence of syntax errors, as you can see there are none in the example above.

php4fan avatar Aug 22 '24 15:08 php4fan

Hello,

Interesting. Unfortunately, we are unable to replicate this. Have you noticed any pattern when does this happen or is it completely random?

Thanks!

Miloslav avatar Sep 03 '24 10:09 Miloslav

It seems to depend on:

  1. whether you copy the first line in its entirety including all the trailing spaces, or without the trailing spaces, or with only some
  2. whether you paste it at the very beginning of the empty line, or at the end of the whitespace that's already in the empty line (if any), or somewhere in the middle

But not in a way that seems to make sense. Probably for each of the combinations above you'll get consistent results if you repeat consistently the same action, but not in a way that I would be able to guess. And either way I don't see a good reason for not just always adjusting indentation regardless of how much trailing space you copied or where exactly in a line only made of whitespace you paste it. Not when copying multiple lines.


Example 1

Cutting this: image

Pasting here: image

Result: (bad) image


Example 2

Cutting this: image

Pasting here: (there are white spaces after the cursor BTW) image

Result (good): image


Example 3

Cutting this: image

Pasting here: image

Result: (good) image

php4fan avatar Sep 03 '24 14:09 php4fan

Were you able to reproduce with the hints in my last comment?

php4fan avatar Oct 03 '24 16:10 php4fan

Hello,

I think format on paste is turned off. Please try to turn it on and the behavior should stay consistent across all the cases.

    "[php]": {
        "editor.formatOnPaste": true
    }

Miloslav avatar Oct 09 '24 15:10 Miloslav

It is indeed turned off, but I don't see how the current behavior makes any sense even with that option turned off.

If not formatting on paste means not touching any formatting including the indentation, then I'd expect the code to be always pasted as-is without changing the indentation, in all cases. If it means don't reformat but still do adjust indentation (which is what one would normally want in most cases), then I'd expect it to adjust indentation properly, again always.

php4fan avatar Oct 10 '24 14:10 php4fan

That's a valid point. We will investigate the factors contributing to this behavior. It's possible that Visual Studio Code has default rules aimed at maintaining proper indentation, but their effectiveness may vary.

Miloslav avatar Oct 10 '24 14:10 Miloslav