DEVSENSE / phptools-docs

PHP Tools public content
Apache License 2.0
84 stars 10 forks source link

Cut-n-paste sometimes randomly fails to adjust indentation #637

Open php4fan opened 3 months ago

php4fan commented 3 months ago

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.

Miloslav commented 2 months ago

Hello,

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

Thanks!

php4fan commented 2 months ago

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 commented 1 month ago

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

Miloslav commented 1 month ago

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
    }
php4fan commented 1 month ago

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.

Miloslav commented 1 month ago

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.