madskristensen / Tweakster

A Visual Studio extension
Apache License 2.0
238 stars 23 forks source link

[Bug] CopyWithoutIndentations produce incorrect result when indentation contains a mix of tabs and spaces #115

Open phizch opened 1 year ago

phizch commented 1 year ago

Describe the bug

If the indentations of the lines of text to be copied differ, too much or too little will be copied. If the first line's indentation are 2 tabs and the second has 8 spaces, the second line will be copied with 6 spaces. Even worse, if the first line's indentation are 8 spaces and the second line has 2 tabs, 6 characters of the second line won't be copied.

I watched the YT video where this feature was created and noticed it. I then tried it myself, and saw that the bug is still present.

To Reproduce

In the screenshots whitespace is represented as · for space, and for tab:

  1. Open a text file in Visual Studio and paste the following text:

            Does this work?
            doesn't LGTM

    With visible white space and a tab size of 4 it looks like this: image

  2. Select starting with "Does" and ending after "LGTM"

  3. Press Ctrl+C to copy.

  4. Move to the start of a blank line and press Ctrl+V to paste.

Actual behavior

The pasted text look like this:

image

Expected behavior

The text should be copied correctly

Does this work?
doesn't LGTM

Additional context

The reason for the bug is that only the count of whitespace characters are taken into account, not the actual column.

https://github.com/madskristensen/Tweakster/blob/4b82c8219ca2ba13f78a570c073ee538ea9ae782/src/TweaksterShared/Tweaks/Editor/CopyWithoutIndentation.cs#L62

https://github.com/madskristensen/Tweakster/blob/4b82c8219ca2ba13f78a570c073ee538ea9ae782/src/TweaksterShared/Tweaks/Editor/CopyWithoutIndentation.cs#L84

phizch commented 1 year ago

I've mostly fixed it here.

One case that still doesn't work perfectly is when

image

With my fix, at least any text on subsequent lines are kept in the copy.

image

I haven't submitted a pull request yet, since I'm a total n00b when it comes to git.. Does it involve just the latest commit on my branch, or is it the entire history of that branch? The reason I ask is that I first did some experimentation, and then in later commits I removed the experiments again.

Tldr; the latest commit is fairly clean, but if a pull request involves the entire branch, it might be better if I squash the commits first.

phizch commented 1 year ago

I thought I checked first, but I must have been tired... This bug relates to #105 and #96.

abatishchev commented 9 months ago

I just got the same exception:

System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: length
   at Microsoft.VisualStudio.Text.Span..ctor(Int32 start, Int32 length)
   at Tweakster.Tweaks.Editor.CopyWithoutIndentation.ExecuteCommand(CopyCommandArgs args, CommandExecutionContext executionContext) in C:\projects\tweakster\src\TweaksterShared\Tweaks\Editor\CopyWithoutIndentation.cs:line 84
   at Microsoft.VisualStudio.UI.Text.Commanding.Implementation.EditorCommandHandlerService.<>c__DisplayClass15_1`1.<ExecuteStartingAtIndex>b__0()
   at Microsoft.VisualStudio.Text.Utilities.GuardedOperations.CallExtensionPoint(Object errorSource, Action call, Predicate`1 exceptionFilter)

I'm not sure what I did, but I think I created a new .http file.

Environment:

Visual Studio version 17.7.4
Tweaks 2022 version 1.1.143