codecadwallader / codemaid

CodeMaid is an open source Visual Studio extension to cleanup and simplify our C#, C++, F#, VB, PHP, PowerShell, JSON, XAML, XML, ASP, HTML, CSS, LESS, SCSS, JavaScript and TypeScript coding.
http://www.codemaid.net
GNU Lesser General Public License v3.0
1.89k stars 356 forks source link

Off-by one bug in GetSnapshotPositionForTextPoint method #902

Closed carlos-quintero closed 2 years ago

carlos-quintero commented 2 years ago

In the CodeMaidShared/Helpers/TextDocumentHelper.cs file (https://github.com/codecadwallader/codemaid/blob/dev/CodeMaidShared/Helpers/TextDocumentHelper.cs), the method GetSnapshotPositionForTextPoint has an off-by-one bug, since textPoint.LineCharOffset is 1-based (https://docs.microsoft.com/en-us/dotnet/api/envdte.textpoint.linecharoffset?view=visualstudiosdk-2022#remarks):

private static int GetSnapshotPositionForTextPoint(ITextSnapshot textSnapshot, TextPoint textPoint)
{
   ThreadHelper.ThrowIfNotOnUIThread();

   var textSnapshotLine = textSnapshot.GetLineFromLineNumber(textPoint.Line - 1);
   return textSnapshotLine.Start.Position + textPoint.LineCharOffset;  // missing to substract 1
}

The reverse GetEditPointForSnapshotPosition method is fine, though, since it adds +1 to return the textPoint.LineCharOffset value.

codecadwallader commented 2 years ago

Carlos! It is a pleasant surprise to see you here. I want you to know that all of your examples and documentation on your site were a huge help for getting CodeMaid started. Back in the VS2005 days there was very little official support and information was hard to come by so I relied heavily on information that you put out there. I can't thank you enough for all that you've done for the community and I.

Thanks for the callout. This file underwent a lot of changes with the VS2022 changes and I appreciate the catch.

carlos-quintero commented 2 years ago

Hi Steve

Incidentally I removed the MZ-Tools articles from the website last week, they were outdated and, belonging to the VS add-ins era, very DTE-biased :-) but I am glad that they very useful for so many years.

Well, now Microsoft references your pull request https://github.com/codecadwallader/codemaid/pull/847/commits/12e226a2ad6e9a4ccec4c3fda1a19db63eef6efd in their documentation (https://docs.microsoft.com/en-us/visualstudio/extensibility/migration/breaking-api-list?view=vs-2022#legacy-find-api-deprecation) to show how to replace EnvDTE.Find/Replace legacy APIs. Your code has helped me a lot! Thank you very much.

codecadwallader commented 2 years ago

Wow, thanks for pointing that documentation out. It was actually a Microsoft employee @olegtk whom did the bulk of those API deprecation updates which was super helpful for us to become compatible with VS2022. Thanks again for your off-by-1 fix and hope you're doing well.