XamlStyler icon indicating copy to clipboard operation
XamlStyler copied to clipboard

VSMac carret on save refresh whole page to the top.

Open bcaceiro opened this issue 3 years ago • 4 comments

Describe the bug On Visual Studio for Mac, having the Format XAML on save option, the carret moves to the top of the current page. this is kind of annoying if working / making changes at the end of a 200 line file, and being prompted to the top of the same.

To Reproduce Steps to reproduce the behavior:

  1. When formatting '...' the carret moves to the sart of the page

Expected behavior The format should be applied and stay at the same editing position

Version Info (please complete the following information):

  • OS: MacOS 11.0.1
  • Visual Studio: VS for Mac 8.8.1
  • XAML Styler: Extension 2.0.1

Additional context Add any other context about the problem here.

bcaceiro avatar Dec 09 '20 11:12 bcaceiro

The current logic works like this:

  1. Getting all the text from document
  2. Formattimg text
  3. Replacing document text with the formatted one.

I can save the cursor position at the point 1. But the problem is that after the formatting the number of symbols before the carret can change dramatically (all the spaces and \n \t). @grochocki can you explain a little how have you fixed this problem in Windows extension?

Saratsin avatar Dec 14 '20 15:12 Saratsin

I could not find anything that handles the cursor position explicitly in the Windows extension. In comparing XamlFormattingService.cs (Mac) and StylerPackage.Formatting.cs (Windows), my best guess is that it has to do with how we are replacing the text.

We are using EditPoint to edit the text buffer.

EditPoint objects are similar to TextSelection objects, except that they operate on data in the text buffer rather than on text displayed in a code editor. The difference is that text in the buffer is not affected by global editor states, such as word wrapping and virtual spaces.

And then we use the vsEPReplaceTextKeepMarkers flag, which:

Retains existing markers (for breakpoints, named shortcuts, and so forth) in their current locations.

Neither of these mention cursor position explicitly, but if I try to set a different flag, the cursor position jumps to the end of the document on format.

grochocki avatar Dec 16 '20 07:12 grochocki

The current logic works like this:

  1. Getting all the text from document
  2. Formattimg text
  3. Replacing document text with the formatted one.

I understand that the number of lines and their length change drastically at the first save, but after that the changes are subtle.

If you were to save the first visible line and the line where the carrot is at in step 1 and then just position after step 3. That would already improve the experience a lot!

IvoKrugers avatar Jan 14 '21 10:01 IvoKrugers

If we give elements names, and you get the name and property with focus, you could find it again as you now have context.

Or, it you count the number of elements to this one before formatting, and note the property/carot and then format, you can go go to the same element number again and to its relative property / carot pis.

ajtruckle avatar Feb 22 '21 05:02 ajtruckle